Cross-site scripting
Attacker JavaScript gets rendered by the victim browser. Session cookies, tokens, and page contents can be read from the wrong context.
Material
XSS happens when user data reaches the HTML without escaping and the browser parses it as markup. The injected script runs with the origin of the victim site, so it can read anything that page can read.
Three shapes
- Reflected: the payload sits in the URL or a form and is echoed straight into the response. The victim has to click a link.
- Stored: the payload is saved on the server, then shown to every visitor of that page. It reaches the most people.
- DOM-based: the payload never reaches the server. Page JavaScript writes it into a dangerous sink such as innerHTML.
A basic payload
html
<script>alert(document.domain)</script>
<img src=x onerror=alert(1)>The script tag is only one route. Event handlers such as onerror, onload, or javascript: URLs often pass filters that only block the word script.
Prevention
- Escape output for its context: HTML body, attributes, JavaScript, CSS, and URLs each have different rules.
- Use a framework that escapes automatically, and do not turn that escaping off with innerHTML.
- Apply a CSP that forbids inline script and eval.
- Mark session cookies HttpOnly and Secure so JavaScript cannot read them.
Phase 1 solve detection runs in your own browser: an
alert() or print() call is reported to the lab host. Stored XSS with a victim needs a victim bot, which is Phase 2.Labs in this topic
Labs run on a domain separate from this site, so a payload cannot reach your account session.
Challenges in this category
Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.