Issue
Cross-site scripting (XSS) is a web security weakness that lets attacker-controlled data run as code in another person’s browser while they use a vulnerable website. It usually occurs when an application puts untrusted data into a page without handling it for the place where it appears. Browsers interpret HTML, JavaScript, URLs, and CSS differently, so a generic escaping step may not be safe in every context. OWASP XSS Prevention Cheat Sheet, CWE-79
Common forms include:
- Reflected XSS: Data from a request appears in the response, such as a search term in a results page.
- Stored XSS: The application saves attacker-controlled data and later displays it, for example in a comment or profile.
- DOM-based XSS: Client-side JavaScript reads data from a source such as the URL and passes it to a browser API that interprets it as HTML or code.
These categories can overlap. For example, data can be reflected by the server and then handled unsafely by client-side JavaScript. PortSwigger Web Security Academy
Common causes
- Inserting user-controlled data into HTML without encoding it for that specific context.
- Using HTML-parsing APIs such as
innerHTMLordocument.write()to display untrusted text. - Building JavaScript, CSS, or URL values by concatenating untrusted input.
- Allowing rich-text content without sanitizing it with a maintained, correctly configured HTML sanitizer.
- Bypassing a framework’s escaping protections or using unsafe rendering features.
- Trusting input validation or a Content Security Policy to replace safe output handling.
Examples
HTML text context
If user input is meant to appear as text, a safe page should display the characters literally rather than create an element.
Input:
<b>input cors-manipulated">xss-check-7391</b>
Expected: <b>xss-check-7391</b> appears as text.
A safe server-rendered response might encode the angle brackets:
<p><b>input cors-manipulated">xss-check-7391</b></p>
For client-side text, use a text API:
message.textContent = input cors-manipulated">userSuppliedValue;
Avoid placing untrusted strings into HTML-parsing APIs such as innerHTML; the browser parses those strings as markup. MDN: textContent, MDN: innerHTML
Authorized test request
A tester might submit a harmless marker to a test application at https://app.example.invalid/search?q=xss-check-7391. The reserved .invalid domain is illustrative. Finding the marker in the response is a lead for further investigation, not a confirmed vulnerability.
Impact
When injected code runs, the browser handles it as part of the vulnerable site’s page. Depending on the user’s privileges, the page’s features, browser controls, and the code’s capabilities, it may be able to read or change information available to that page or perform actions as the user.
Impact should be based on demonstrated behavior. A value appearing in a response, an unusual page change, or a blocked payload alone does not prove that script executed, exposed data, or enabled account takeover. For example, HttpOnly cookies cannot be read through JavaScript, though that attribute does not prevent other actions the page’s scripts may be able to perform.
Testing
1. Check whether untrusted input executes in the browser
Place a unique marker in a feature that displays user input. Inspect its output context and use a controlled browser check to determine whether script execution occurs.
Example: HTML being rendered is a clue, not proof of script execution. Check the resulting DOM and browser behavior.
GET /search?q=%3Cb%3Exss-check-7391%3C%2Fb%3E HTTP/1.1
Host: app.example.test
2. Check stored and client-side paths
Submit a test value to a record that another test account can view, and inspect URL-driven DOM updates separately. Compare the server response with the browser’s live DOM.
Example: A comment may be stored safely but inserted unsafely by client code. Check both the response and the rendered page.
POST /api/comments HTTP/1.1
Host: app.example.test
Content-Type: application/json
{"text":"xss-check-7391"}
3. Assess context and false positives
Review text, attributes, URLs, script blocks, and any HTML-parsing APIs. Record CSP blocks, browser extensions, and whether the test effect actually ran.
Example: Escaped text or inert markup is not confirmed XSS. A CSP block does not by itself show that unsafe output handling is fixed.
A marker appearing in a response proves only that the value was returned. If <b>xss-check-7391</b> appears as visible text, it was not parsed as a bold element in that location. If it becomes an element, that indicates HTML interpretation but does not by itself prove script execution. A controlled execution check provides stronger evidence for XSS in the tested context.
Common false positives include encoded text, inert markup, browser extensions that alter the page, and scanner alerts based only on reflection. A Content Security Policy (CSP) block may prevent a particular test from running; investigate the data flow and sink rather than treating the block alone as proof of exploitability or proof that the underlying issue is fixed.
Mitigation
Primary fixes
- Use a framework’s safe templating features and keep automatic output encoding enabled. Encode data for its specific output context; HTML text encoding does not make data safe for JavaScript, CSS, or URL contexts.
- For client-side text, use APIs such as
textContent. Avoid sending untrusted data to HTML or code interpreters, includinginnerHTML,document.write(), andeval(). - If users need to submit formatted HTML, sanitize it with a maintained HTML sanitizer configured for the permitted markup. Keep the sanitizer updated, and do not modify sanitized output in ways that can make it unsafe afterward. OWASP XSS Prevention Cheat Sheet, OWASP DOM Based XSS Prevention Cheat Sheet
Additional defenses
- Use a restrictive CSP to limit which scripts the browser can run. Treat it as an additional layer, not a substitute for safe output handling.
- Where supported, consider Trusted Types to restrict assignments to browser APIs that interpret strings as HTML or script.
Retest the same input and output locations after changes. Verify that ordinary text stays text, permitted rich content is sanitized as intended, and controlled execution checks no longer run. A passing test covers the locations and browser behavior exercised; review other output paths and code sinks separately.