Testing for Cross-Site Scripting, and What Content Security Policy Changes

7 min read188 courses on OnlySecurity

Cross-site scripting (XSS) is a flaw where an application puts attacker-influenced data into a page in a way the browser runs as script. The script then runs with the origin and privileges of the vulnerable site. This article shows how to test for it by output context, not payload lists, and what a strict Content Security Policy changes. Test only networks and systems you own or are authorised to assess.

Where XSS sits in the OWASP Top 10:2025

In the 2025 list, XSS is part of A05:2025 Injection. OWASP maps CWE-79 to that category and lists cross-site scripting as high frequency and low impact, with more than 30,000 CVEs. The OWASP Web Security Testing Guide covers it in three tests: WSTG-INPV-01 for reflected, WSTG-INPV-02 for stored and WSTG-CLNT-01 for DOM-based.

Reflected, stored and DOM-based

Reflected XSS returns attacker data in a single response, so it needs a victim to open a crafted link or third-party page. Stored XSS saves the data first, in a comment, profile field or log viewer, and runs it for every user who later views it. DOM-based XSS happens in the browser: JavaScript takes data from a source such as the URL and passes it to a sink that can run code. OWASP’s DOM XSS cheat sheet notes that stored and reflected describe how a payload reaches the victim, while server-side and client-side describe where it is handled unsafely, so DOM XSS can also be stored or reflected.

Test by output context

The same input is harmless in one place and executable in another because the browser parses each context differently. OWASP’s prevention cheat sheet lists HTML body, HTML attribute, JavaScript, CSS and URL contexts, each needing its own protection. So first find where input lands.

Send a unique, inert marker such as zq7391 in each parameter, then find every place it appears in the response, in stored pages and, for DOM issues, in the live DOM in browser developer tools, because View Source does not show changes JavaScript has made. Note the context of each hit, then test one thing: which characters survive unencoded for that context. Use the harmless probes in the commands block, one per context, and stop at the first proof. In a lab, proof is an alert(document.domain) or a visible marker element. Do not capture real session cookies or tokens; that adds nothing to the finding. Proxying the browser through Burp makes this faster, and the Burp Suite Community Edition: Manual Web Application Testing course covers that workflow. Scanners such as those in Nikto and sqlmap find only obvious cases, but context reasoning finds the rest.

DOM sources and sinks

A source is attacker-controllable input in the browser, most often the URL through location.search or location.hash. A sink is a function that runs or renders it. PortSwigger’s Web Security Academy names eval() and innerHTML as typical sinks, and OWASP’s DOM cheat sheet also lists outerHTML, document.write and document.writeln, plus methods that implicitly run strings as code. Read the page’s JavaScript for each source and trace where it flows. Fixing it means replacing unsafe sinks with safe ones such as textContent on ordinary elements, never on a <script> element.

What breaks and what it means

When a marker comes back with < and " intact in a context that needs them, you have a finding. When the application encodes for the context, a probe fails and that is a pass. Report the context, the encoding that was missing and the one-line fix. A stored finding on a page an administrator views comes first: WSTG-INPV-02 calls stored XSS particularly dangerous where users with high privileges have access.

Commands and snippets

Checked on 27 November 2026 against the OWASP XSS and DOM XSS Prevention cheat sheets, OWASP WSTG v4.2, MDN with browser-compat-data 8.1.11, and the W3C Content Security Policy Level 3 (27 October 2026) and Trusted Types (7 October 2026) Working Drafts.

# Step 1: find reflection and context (lab host)
curl -s 'https://lab.example/search?q=zq7391' | grep -n 'zq7391'

# Step 2: one harmless probe per context
HTML body:      <i>zq7391</i>
Attribute:      "zq7391
JavaScript str: '</zq7391
URL attribute:  javascript:void(7391)
Proof, lab only: <img src=x onerror=alert(document.domain)>

# DOM sources and sinks to search for in the page's scripts
location.search  location.hash  innerHTML  document.write  eval(
# A strict CSP: nonce plus strict-dynamic, reporting, Trusted Types
Content-Security-Policy: script-src 'nonce-{RANDOM}' 'strict-dynamic'; object-src 'none'; base-uri 'none'; require-trusted-types-for 'script'; trusted-types app-policy; report-uri https://lab.example/csp-reports; report-to csp-endpoint
Reporting-Endpoints: csp-endpoint="https://lab.example/csp-reports"
  1. Generate a new nonce of at least 128 bits from a secure random generator for every response (CSP3), and template it onto each legitimate <script> so it never lands on an injected one (MDN).
  2. The probe javascript:void(7391) only shows whether the scheme is accepted in a URL context; run nothing more than that.
  3. Test the policy first as Content-Security-Policy-Report-Only, as MDN documents, and read the reports.
  4. require-trusted-types-for 'script' makes sinks such as the innerHTML setter reject plain strings, so the code needs a trustedTypes.createPolicy() policy named in trusted-types. MDN lists the directive as Baseline 2026 newly available (Chrome and Edge 83, Firefox 148, Safari 26), so check your users’ browsers.
  5. report-uri stays beside report-to because Firefox added report-to only in version 149; browsers that support report-to ignore report-uri. The two send different formats, application/csp-report and application/reports+json, so the endpoint must accept both.

Detection and defence

Fix the cause first. OWASP’s prevention cheat sheet puts framework protections, output encoding and HTML sanitisation ahead of every other control. Use your framework’s automatic escaping, encode for the exact context where it does not, and sanitise with a maintained library such as DOMPurify when users must author HTML. Avoid the framework escape hatches the same sheet names, such as React’s dangerouslySetInnerHTML without sanitising, and keep variables out of its listed dangerous contexts: directly inside a <script> block, inside an HTML comment, in a style block or as an attribute or tag name.

OWASP states that cookie attributes try to limit the impact of an XSS attack but do not prevent malicious content from running. An HttpOnly cookie cannot be read by the injected script, but the script can still act as the user in the page.

CSP is defence in depth. OWASP says it should not be the primary defence, because mistakes are easy. MDN recommends nonce- or hash-based policies over host allowlists, which often admit unsafe domains. Google’s Lukas Weichselbaum, Michele Spagnuolo, Sebastian Lekies and Artur Janc showed this in CSP Is Dead, Long Live CSP!, finding bypasses in 94.72% of the distinct policies they measured, and proposed 'strict-dynamic'. It lets a trusted script load others, and MDN notes it makes the policy less secure: if those scripts create <script> elements from attacker-influenced data, the policy will not stop them. Trusted Types, per MDN, narrows DOM XSS to small, isolated policy code that is easy to review. MDN and CSP3 deprecate report-uri in favour of report-to with a Reporting-Endpoints header, but suggest sending both for now (note 5).

In logs, look for request parameters containing <, " or script-related strings, and for stored records containing markup that the field should never hold. A payload in the URL fragment never reaches the server, as WSTG-CLNT-01 notes, so hash-based DOM XSS leaves no access log line; browser violation reports are the signal there. CSP3 reports blocked inline script as inline, which on a nonce-based page points to injection, a missed nonce or a browser extension. MDN warns that reports are attacker-controlled data, so sanitise them before display. A tester’s zq7391 marker is easy to filter out of that noise. The Web Security Fundamentals: OWASP Top 10:2025 and ASVS 5.0 course covers how these controls map to ASVS requirements.

Where to go next

Work through the Web Security Fundamentals: OWASP Top 10:2025 and ASVS 5.0 course to see injection, output encoding and ASVS verification together. A related upload attack is in IIS web.config upload attacks, and the workflow from first scan to report is in Penetration Testing Field Notes.

This article replaces the KSEC ARK notes first published in 2015.

More from this author