The browser cannot tell which bytes you meant as data and which bytes the server assembled into code. Ask where the value crosses into HTML, an attribute, a URL or a JavaScript context.
XSS is a context failure. The server or client inserts untrusted data into a document without encoding for the exact sink, so the browser parses it as markup or script. The payload does not need to be clever; it only needs to land in the wrong grammar.
Developers test the page they built, not the parser the browser will use. The gap between 'it looks like text' and 'the browser sees a tag' is where XSS lives.
What is actually at stake?
Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.
Assets
- Victim session and same-origin data
- Administrator actions
- CSRF tokens and private page content
- User trust in the application
Actors
- Attacker who controls a reflected parameter
- Malicious user storing content for others
- Attacker hosting a crafted link
- Compromised third-party script
Trust boundaries
- Server data to HTML body
- Data to attribute, URL or script context
- DOM source to DOM sink
- Origin boundary to same-origin fetch
Abuse cases
- Reflect a script in a search result
- Store a payload in a comment read by an admin
- Use an image onerror handler in a DOM sink
- Read a same-origin admin page and exfiltrate it
- Bypass a weak sanitizer with an alternate context
Follow the decision, not the payload
Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.
- 01
Source
The attacker controls a query parameter, stored field, fragment, postMessage or third-party response.
- 02
Transform
The application concatenates, escapes for the wrong context, sanitizes with a regex or writes to a DOM property.
- 03
Parse
The browser parses the result as HTML, an attribute, a URL or JavaScript and creates an execution context.
- 04
Execute
Script runs with the origin of the vulnerable page, not the attacker's origin.
- 05
Impact
The script can read same-origin data, perform actions, steal non-HttpOnly values or pivot to the user.
The Samy worm on MySpace, 2005
MySpace Samy worm, 2005, public reportingMySpace allowed users to add HTML and CSS to their profiles. A stored XSS payload was enough to make a profile execute script for every visitor.
Samy Kamkar crafted a profile payload that added the viewer as a friend and copied itself into the viewer's profile. The payload spread through normal browsing.
The worm spread to over a million profiles in hours and forced MySpace to take the site offline while it cleaned the payload.
User-controlled profile content reached the page without effective HTML sanitization, so browser markup became executable code in every visitor's session.
Use a context-aware sanitizer with a strict allowlist, block event handlers and dangerous URLs, and add CSP as defense in depth.
Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.
A repeatable testing sequence
- Map every place your input appears: HTML body, attribute, URL, script, CSS or DOM sink.
- Identify the encoding and the parser that will read the value.
- Use a harmless probe to learn the context before trying to execute code.
- Prove execution in a real browser, not only in the HTTP response.
- Show the impact: whose session, what data, which action?
Where smart people get fooled
- Encoding for HTML body does not protect an attribute or a JavaScript string.
- A payload that works in the browser console may be blocked by CSP or Trusted Types.
- The response may be filtered on the server while the DOM sink remains client-side.
- A blocked alert does not prove safety; execution can happen without visible output.
What would the logs show?
A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.
- Requests containing HTML or JavaScript syntax in reflected parameters
- Stored content with event handlers, script tags or dangerous URL schemes
- A browser request to a same-origin admin page followed by an external callback
- CSP violation reports for inline or eval execution
- A user session performing actions without a matching UI navigation
index=web sourcetype=app_logs | eval xss=if(match(request_body,"(?i)(<script|onerror=|onload=|javascript:|<svg|<iframe)"),1,0) | where xss=1 | stats count values(path) by clientip, user_id | where count > 0Fix the decision, then prove it stays fixed
- Encode for the exact output context, preferably with a context-aware templating engine.
- Use a strict Content Security Policy and Trusted Types where the application supports them.
- Sanitize rich HTML with a maintained allowlist library, never with a regex.
- Keep session cookies HttpOnly and test the actual browser execution path.
const html = `<p>No results for ${req.query.q}</p>`;
res.send(html);const escaped = escapeHtml(String(req.query.q));
res.send(`<p>No results for ${escaped}</p>`);
// Prefer a context-aware template engine over manual string building.Encode for the exact sink. HTML escaping is not enough inside an attribute, URL or script. Use a maintained sanitizer for rich HTML and add CSP as a second layer.
Regression tests a reviewer would ask for
Reflected body
GivenA search page reflects q in HTML body
Whenq contains a script tag
ThenThe response shows literal text and the browser executes no script
Attribute context
GivenA value is placed inside a quoted attribute
WhenThe value contains quotes and an event handler
ThenThe attribute stays intact and no handler executes
Stored content
GivenA comment is saved and rendered to an admin
WhenThe comment contains an image onerror payload
ThenThe sanitizer removes the handler or the browser refuses execution
DOM sink
GivenA client-side preview reads location.hash
WhenThe hash contains HTML
ThenThe sink uses textContent or a sanitizer and does not execute
What this finding is not
Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.
- The payload appears in the response but is HTML-encoded: reflection is not execution; verify in a browser.
- An alert is blocked by CSP: try a different sink or check whether the policy can be bypassed; a blocked alert alone does not prove safety.
- The server strips script tags but leaves event handlers: test attributes, URLs and DOM sinks separately.
- HttpOnly blocks document.cookie, but XSS can still fetch same-origin pages and perform actions.
Terms you should be able to use precisely
- Reflected XSS
- A payload in the current request is echoed into the response and executes immediately.
- Stored XSS
- A payload is persisted and executes when another user views the stored content.
- DOM XSS
- Client-side JavaScript moves attacker-controlled data into an execution sink without server involvement.
- Sink
- A browser API or parsing context that can turn data into code, such as innerHTML or eval.
- CSP
- Content Security Policy: a browser policy that restricts script sources and inline execution.
- Trusted Types
- A browser enforcement that requires sanitized values for dangerous DOM sinks.
Answer these without reciting a payload
Why is HTML-escaping not a universal XSS fix?
The correct encoding depends on the sink. HTML body, attribute, URL and JavaScript string contexts have different grammars; escaping for one can leave another exploitable.
Does HttpOnly stop XSS?
It prevents JavaScript from reading document.cookie, but the script still runs in the page origin. It can fetch same-origin pages, perform actions and read non-HttpOnly data.
How do you prove XSS impact to a reviewer?
Show browser execution, the origin of the execution context, and a concrete same-origin read or action. A screenshot of an alert is weaker than an exfiltrated protected value.
Can you separate the mechanism from the payload?
Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.
XSS هي فشل سياق وليس فشل اقتباس. نفس القيمة قد تكون آمنة في نص HTML وخطيرة داخل attribute أو URL أو JavaScript. لذلك الترميز يجب أن يطابق مكان الظهور، والاختبار يجب أن يجري في متصفح حقيقي. تذكّر أن HttpOnly يحمي الكوكي فقط، لا الصفحة: السكربت يستطيع قراءة صفحات same-origin وتنفيذ إجراءات. استخدم قالبًا واعيًا بالسياق، ومنقّي HTML بصيانة، وCSP كطبقة إضافية.
If the same value is rendered in a different context tomorrow, does your control still hold?