XSS: reflected, stored and DOM
Stage 3 · Lesson 3 · 25 minutes · Requires Lesson 1
The one-line answer
Cross-site scripting (XSS) happens when an application puts user input into a page without making it safe, so an attacker's JavaScript runs in another user's browser — as if the website itself sent it.
The boundary: XSS attacks other people's browsers. Never test it on real sites or real users. PortSwigger labs simulate the victim for you.
Why you should care
XSS is one of the most commonly reported web vulnerabilities, year after year, and a staple of bug bounty programmes. It's also the bug that teaches you how the browser decides what to trust.
Lesson 1 was data treated as code by a database. XSS is the same idea, but the "interpreter" is the victim's browser.
How it actually works
The core problem
A search page shows what you searched for:
/search?q=laptop → <p>Results for: laptop</p>If the application inserts q into the HTML as-is, then whatever you send becomes part of the page. Send some HTML, and the browser renders it as HTML. Send a script tag, and the browser runs it:
/search?q=<script>alert(1)</script>
→ <p>Results for: <script>alert(1)</script></p>alert(1) just pops up a box — testers use it as harmless proof that their JavaScript ran. A real attacker would run something else.
Why does this matter? Recall Stage 0 Lesson 8 and Stage 1 Lesson 5: the browser trusts code from a site to act as that site — read the page, make requests with the user's cookies, change what the user sees. Injected code gets all of that trust.
The three types
1. Reflected XSS — the payload comes in the request and is reflected straight back in the response.
- Delivery: the attacker sends the victim a crafted link (by email, chat, a QR code). The victim clicks it; the script runs on the real site.
- Example: the search page above.
2. Stored XSS — the payload is saved by the application and shown to everyone who views it later.
- Delivery: the attacker posts it once — a comment, a profile name, a product review, a support ticket. Every visitor who loads that page runs it. No link-clicking needed.
- Usually the most serious type, because it hits many users automatically — sometimes including admins.
3. DOM-based XSS — the vulnerability is entirely in the page's own JavaScript.
- The server may never see the payload. Instead, front-end JavaScript reads something attacker-controlled (often part of the URL, like
location.hash) and writes it into the page unsafely, e.g. usinginnerHTML. - Recall Stage 0 Lesson 7: you can watch this in devtools.
| Type | Where the payload lives | How the victim gets it |
|---|---|---|
| Reflected | In the request | Clicks a crafted link |
| Stored | In the app's database | Just visits the page |
| DOM-based | Processed by page JavaScript | Usually a crafted link |
Impact
Once attacker JavaScript runs in a victim's session, it can often:
- Steal the session cookie — unless it's marked
HttpOnly(Stage 1 Lesson 5) — and take over the account. - Act as the user — change their email, make purchases, send messages — even when the cookie is protected, because the script runs inside the logged-in page.
- Show fake content — a fake login form on the real domain to capture passwords.
- Capture keystrokes on that page.
Context matters
Where input lands decides what's dangerous:
| Input lands in | Example | What breaks it out |
|---|---|---|
| HTML body | <p>INPUT</p> | < and > (new tags) |
| An attribute | <input value="INPUT"> | " (closing the attribute) |
| Inside JavaScript | var x = 'INPUT'; | ' (closing the string) |
| A URL | <a href="INPUT"> | javascript: URLs |
This is why the fix is called context-aware encoding: safe output depends on where it goes.
See it yourself
Only on PortSwigger labs:
- Read the XSS learning path introduction (link below).
- Do the apprentice lab for reflected XSS into HTML context with nothing encoded. Use a harmless
alert()as proof. - Do the apprentice lab for stored XSS into HTML context. Notice the payload runs every time the page loads.
- Do one DOM XSS apprentice lab. Open devtools and find the line of JavaScript that writes your input into the page.
- After each, view the page source and find exactly where your input landed.
How attackers abuse this
Attackers look for anywhere user input is displayed: search results, error messages that repeat your input, comments, profile fields, file names, support tickets viewed by staff. Stored XSS in an admin-only view (e.g. a support dashboard) is especially valuable to them — one ticket can compromise an administrator.
The defensive fix:
- Context-aware output encoding. Before placing user data into a page, convert special characters so the browser shows them as text instead of treating them as code — e.g.
<becomes<. Encode for the right context (HTML body, attribute, JavaScript, URL). - Use your framework's auto-escaping. Modern templating (React, Angular, Django, Jinja2 and others) escapes output by default. Bugs appear when developers bypass it —
dangerouslySetInnerHTML,|safe,innerHTML. Treat those as red flags in code review. - Use safe DOM APIs in front-end code:
textContentinstead ofinnerHTMLfor untrusted data. - Content-Security-Policy (CSP) — recall Stage 1 Lesson 4. A strong CSP tells the browser which scripts are allowed to run, blocking most injected ones. It's a powerful second layer, not a replacement for encoding.
HttpOnlycookies — so injected scripts can't read the session cookie directly.- Validate input where it has a known format (e.g. a username only allowing letters and numbers).
Common mistakes
- Filtering the word
<script>and calling it fixed. There are many other ways to run JavaScript (event handlers,javascript:URLs). Encode output instead. - Encoding for the wrong context. HTML-encoding data placed inside a JavaScript string doesn't make it safe.
- Relying on CSP alone. CSP limits damage; encoding prevents the bug.
- Assuming
HttpOnlystops XSS. It protects the cookie; the script can still act as the user. - Testing with harmful payloads. Use
alert()orprint()as proof. A finding needs proof the code runs, not damage.
Check yourself
- What's the key difference between reflected and stored XSS from the victim's point of view?
- A session cookie is marked
HttpOnly. Is an XSS bug on that site now harmless? Why? - Name the primary fix for XSS and one strong second layer.
Answers
- With reflected XSS, the victim must open a crafted link. With stored XSS, the victim only has to visit a normal page that contains the saved payload.
- No.
HttpOnlystops scripts reading the cookie, but the injected script still runs inside the logged-in page and can perform actions as the user or show fake content. - Primary: context-aware output encoding (including framework auto-escaping and safe DOM APIs). Second layer: a strong Content-Security-Policy (also
HttpOnlycookies).
Your lab
- PortSwigger Cross-site scripting learning path: https://portswigger.net/web-security/cross-site-scripting
Writeup: one lab of each type — where the input landed, which context, proof (alert), and the specific fix for that context.
Summary in your language
தமிழில் சுருக்கம்
XSS என்பது ஒரு application பயனர் input-ஐ பாதுகாப்பாக்காமல் page-இல் வைக்கும்போது, attacker-இன் JavaScript மற்றொரு பயனரின் browser-இல் அந்த website அனுப்பியது போலவே இயங்குவது. மூன்று வகைகள்: Reflected (crafted link மூலம்), Stored (சேமிக்கப்பட்டு எல்லோருக்கும் காட்டப்படும்), DOM-based (page-இன் JavaScript-இலேயே பிழை). Session திருட்டு, பயனர் போல் செயல்படுதல், போலி login form ஆகியவை பாதிப்புகள். தீர்வு: context-aware output encoding, framework auto-escaping, textContent பயன்பாடு; கூடுதலாக CSP, HttpOnly cookies.
हिंदी में सारांश
XSS तब होता है जब application user input को सुरक्षित किए बिना page में डाल देता है, और attacker का JavaScript दूसरे user के browser में ऐसे चलता है जैसे website ने ही भेजा हो। तीन प्रकार: Reflected (crafted link से), Stored (save होकर सबको दिखता है), DOM-based (page के अपने JavaScript में गलती)। नुकसान: session चोरी, user बनकर काम करना, नकली login form। इलाज: context-aware output encoding, framework auto-escaping, textContent का इस्तेमाल; साथ में CSP और HttpOnly cookies।
Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.
What's next
The simplest serious bug of all — just change a number: IDOR: the simplest serious bug