How it works
XSS happens when data is treated as code. An application takes text that a user controls and places it into a page. If the text contains HTML or JavaScript and the application does not neutralize it, the browser cannot tell it apart from the site's own markup and runs it.
Browsers isolate sites from each other by origin. A script that runs on app.acme.test can read that site's pages, call its API with the user's session and change what the user sees. XSS gives the attacker that position.
There are three main forms:
- Reflected. The payload sits in the request, for example in a URL parameter, and the server includes it in the page it returns. The attacker must get the victim to follow a prepared link.
- Stored. The application saves the payload, for example in a comment or a profile field, and later shows it to other users. Simply viewing the page triggers it.
- DOM-based. The server is not involved. JavaScript in the page reads attacker-controlled data, such as the URL fragment, and writes it into the page unsafely.
What it looks like
A help desk tool lets customers open tickets. The agent dashboard builds its HTML by pasting the ticket subject into a string. A customer submits this:
POST /api/tickets HTTP/1.1
Host: support.acme.test
Content-Type: application/json
{"subject": "<svg onload=alert(document.domain)>", "body": "Please help"}When an agent opens the queue, the browser parses the subject as markup and runs the script in the agent's session. The harmless alert proves that script execution is possible.
The vulnerable code and its fix:
// Vulnerable: the subject is placed into HTML as it was received.
res.send(`<h1>${ticket.subject}</h1>`);
// Fixed: the subject is encoded for an HTML context before it is placed.
res.send(`<h1>${escapeHtml(ticket.subject)}</h1>`);After encoding, < becomes <, and the browser displays the text instead of interpreting it.
How to test for it
- Find the inputs. URL parameters, form fields, headers, file names, JSON values and anything stored and shown later.
- Find where each one lands. Send a unique marker string and search every response and rendered page for it, including pages that other roles see.
- Note the context. The marker may sit between tags, inside an attribute, inside a script block or inside a URL. Each context breaks in a different way.
- Try to leave the context. Submit
<,",'and>and see whether they come back encoded. - Prove execution. Use a harmless payload that shows the origin, in a test account.
- Check the client side. Trace what feeds
innerHTMLand similar calls in the page's JavaScript.
How to fix it
Encode output for the place it lands, and let the framework do the work.
- Rely on automatic escaping. Modern template engines and front-end frameworks escape values by default. Most XSS in such code comes from the escape hatches, such as
dangerouslySetInnerHTMLin React andv-htmlin Vue. - Encode by context. HTML body, attribute, JavaScript and URL contexts each need their own encoding.
- Prefer safe browser APIs. Use
textContentin place ofinnerHTMLwhen inserting text. - Sanitize when HTML is required. If users may write rich text, pass it through a maintained sanitizer library that only lets approved tags through.
- Validate URLs. Accept only
httpandhttpsschemes for user-supplied links, sojavascript:URLs are refused. - Add a Content Security Policy (CSP). A strict policy that blocks inline scripts limits the damage when a bug slips through. It is a second layer and not a replacement for encoding.
XSS vs CSRF and SQL injection
XSS and SQL injection are both injection flaws: data ends up interpreted as code. SQL injection targets the database on the server. XSS targets the browser of another user.
Cross-site request forgery forges a request that starts on a different site, and the attacker never sees the reply. XSS runs inside the trusted site and reads whatever the user can see. Finding these patterns before release is a core goal of secure code review.