CSP: a second line of defence against XSS and skimmer scripts
CSP is a response header with which the server tells the browser where scripts may be loaded and run from and where the page may send data. It is a second line against XSS and skimmers, not a replacement for output encoding. A strict policy relies on a nonce or hashes; 'unsafe-inline' without them removes the main protection. It is rolled out through Report-Only first, then switched to blocking.
Pentest · Client side
The XSS is already in the page, yet the browser refuses to run the script. Who stopped it?
After a test the console shows: Refused to execute inline script because it violates the following Content Security Policy directive. That was CSP (Content-Security-Policy): a response header in which the server tells the browser which sources may run scripts, load images, connect and embed the page. The lesson explains what it gives, what it does not, and how QA checks it.
Mechanism. The browser checks every load and execution against the policy. If <script>alert(1)</script> got into the page and the policy does not allow inline scripts, it will not run. A strict policy uses a nonce (a one-time random value the server generates for every response) or hashes:
Content-Security-Policy:
default-src 'self';
script-src 'nonce-r4nd0m' 'strict-dynamic';
object-src 'none';
base-uri 'self';
frame-ancestors 'none'
<script nonce="r4nd0m" src="/app.js"></script>
<!-- injected: no nonce, so it is blocked -->
The site’s script has the nonce, so it runs, while the attacker’s injected one does not know it. base-uri stops the base address from being replaced, object-src 'none' turns off legacy plugins, frame-ancestors controls who may embed the page (see the clickjacking lesson).
The same injected script
The browser runs any script from the page. An XSS flaw at once gives the attacker code in the site’s origin.
A script without a nonce is blocked, the console says "Refused to execute…". The flaw stays, but exploitation is harder.
How it is rolled out. Turning on a strict header at once is risky: inline handlers and third-party widgets break. So first Content-Security-Policy-Report-Only: the browser blocks nothing but sends violation reports to the endpoint named in report-to (defined by the Reporting-Endpoints header; for browsers without support the deprecated report-uri is added). After a few days the reports show what still has to be allowed or rewritten, and then the policy is switched to enforcing mode.
Limits.
'unsafe-inline'and'unsafe-eval'inscript-srcremove the main protection (except when the same directive has a nonce or hash: browsers then ignore'unsafe-inline', and it is kept only for old browsers);- A long allowlist of domains (
script-src https://cdn.example) is unreliable: an allowed domain may host a script that runs foreign code (JSONP, old libraries); - CSP does not cure the cause: inserted markup, a changed page and some leaks (for example through images if
img-srcis wide) remain possible; - For DOM sinks there are Trusted Types (
require-trusted-types-for 'script'), since February 2026 the current versions of all major browsers support them (Baseline 2026), but older versions do not, so they complement safe APIs rather than replace them.
A real case. In 2018 attackers modified a script on the British Airways site: when customers entered payment details, a skimmer script (attacks of this kind are called Magecart) sent them to a third-party server. More than 400,000 customers were affected. In October 2020 the UK regulator ICO announced a fine of 20 million pounds (a proposed 183 million had been named in 2019), citing insufficient protection of data. A strict CSP would not have cured the compromise of the file itself, but a policy with limited connect-src, img-src and form-action keeps a script from sending data to an unknown domain, and Subresource Integrity (integrity="sha384-…") detects a swapped third-party library.
How QA checks it. Looks at the response header of the main page and the API (DevTools → Network, or curl -I) and whether the policy is the same on the test environment and in production. On an authorized environment checks that an injected inline script is blocked (console) and that the application itself works under the policy: the e2e suite is run with the same headers as production. Report-Only reports are read as a list of defects. Third-party scripts (analytics, widgets) are inventoried: every allowed domain is a risk.
Common mistakes.
- Believing that CSP replaces output encoding. It is a second line;
- Adding
'unsafe-inline'because "otherwise everything breaks" and leaving it forever; - Using a static nonce that does not change between responses: it can be guessed or seen.
Check yourself
The policy script-src 'self' 'unsafe-inline' is set on a site with XSS in comments. Will it stop an injected <script>?
Answer
No. 'unsafe-inline' allows inline scripts, so the injected one will run too. A nonce or hashes are needed instead of 'unsafe-inline', and above all the output encoding must be fixed.
Why launch CSP in Report-Only mode first?
Answer
The browser blocks nothing but sends violation reports. So you see which legitimate scripts and domains the policy would break and fix them before blocking, without breaking the site for users.