Reflected XSS
Reflected XSS is when the payload lives in the request and is reflected back in the response. The victim has to click a crafted link — but if they do, their browser runs the attacker's script on the site they trust.
Why this lab is theory, not live. A live reflected XSS lab would execute untrusted JavaScript on the same origin as your real account. So this lab teaches through explanation, code, and screenshots. A sandboxed live version on an isolated subdomain is coming.
The problem
A payload that lives in a link.
Reflected XSS happens when a web app takes input from the request — a query parameter, a form field, an HTTP header — and puts it into the response without escaping. The response looks normal to the victim, but the payload inside it executes.
Where it happens
Search pages that echo the query back ("You searched for: …"), error pages that say "Invalid input: …", login pages that display the attempted username, any page that reflects a value from the request into the HTML.
The crafted link
The attacker constructs a URL that contains the payload. They then send it to the victim — by email, message, or a link on another site. The victim clicks. The payload is reflected into the page and runs.
https://shop.example/search?q=<script>alert(document.cookie)</script>The victim sees a URL that starts with https://shop.example/ — the legitimate domain. They click. The page loads. The script runs.
session=... — the attacker's script read the victim's session cookie the moment the page loaded.How it works
Why the domain does not save the victim.
The victim's browser trusts the domain. It sees shop.example, sends the victim's cookies, and runs any JavaScript the page provides. It has no way to know that the script came from the URL, not from the site's developers.
The payload does not have to look like a script tag
Filters that look for <script> are useless. There are dozens of other ways to execute JavaScript in a page.
<img src=x onerror=alert(1)>
<svg onload=alert(1)>
<body onload=alert(1)>
<iframe src="javascript:alert(1)">
<input autofocus onfocus=alert(1)>
<details open ontoggle=alert(1)>Every one of those runs JavaScript without using <script>. That is why blacklist filters always fail. The correct fix is not to filter payloads — it is to escape them.
The impact
Less severe than stored — but still severe.
Reflected XSS requires the victim to click. That reduces its reach compared to stored XSS. But it is still serious. A well-crafted link can be sent to a targeted admin, a support agent, or a specific user — and the click is the only step.
Once the script runs, the impact is the same as stored XSS: session theft, account takeover, acting as the user, data exfiltration.
How to fix it
Encode at the sink. That is the whole fix.
Output encode. Any time a value from the request is placed into an HTML response, encode it: & → &, < → <, > → >, " → ", ' → '.
Use the framework. Flask/Jinja escapes by default. So does React, Vue, and Angular. The bug happens when a developer reaches for the "render as raw" escape hatch — in Flask it's |safe, in React it's dangerouslySetInnerHTML. Every use of those should be reviewed.
Encode per context. HTML is one context. URL parameters, JSON strings, CSS, and JavaScript are others. Use a context-aware encoder. Never reuse the same encoding across contexts.
Content Security Policy. A strict CSP that forbids inline scripts and only allows scripts from your own origin means even an unescaped payload cannot execute. This is defense in depth — encoding is the primary defense.
This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons
Lesson 14 — Reflected XSS
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 →