Content Security Policy explained: strict CSP, nonces, SRI and connect-src for third-party scripts
How a Content Security Policy works, why a strict nonce or hash policy beats domain lists, and how Subresource Integrity and connect-src help against compromised third-party scripts.
· 7 min read
Written by AI from the sources linked below. How we use AI
On this page
In short
A Content Security Policy (CSP) is an HTTP header that tells the browser which scripts, styles, images and network destinations a page may use. A strict CSP, built on per-response nonces or hashes instead of lists of allowed domains, is the form MDN recommends, because it stops injected JavaScript from running. For third-party scripts you also have Subresource Integrity (SRI), which pins a file to a hash, and the connect-src directive, which limits where scripts can send data. None of these is perfect on its own, and a recent real-world incident shows why you should combine them and test them.
Why this matters now
On 16 November 2026 researchers at Report URI reported that malicious JavaScript had been served through Optimizely Web snippets to visitors of 178 organisations. The account rests on Report URI's own post, and Optimizely had not commented when it was published, so treat the incident details as that researcher's claims. One number from it is useful for this guide either way: Report URI says that of 256 affected hosts whose policies it checked, only 24 (about 15%) fully blocked the payload. It also says that policies allowing 'strict-dynamic' or 'unsafe-eval' let the attack through, and that restricting connect-src stopped data theft but not the swapping of wallet addresses inside the page.
That pattern, a policy that exists but does not protect, is common. This guide explains what each part does so you can tell.
What a CSP is
MDN describes CSP as a series of instructions from a website to the browser that restrict what the code of the site is allowed to do. Its primary use is controlling which resources, especially JavaScript, a document may load, mainly as a defence against cross-site scripting (XSS), where an attacker gets their code to run in your page. It can also help against clickjacking and with upgrading connections to HTTPS.
You send it in the Content-Security-Policy response header, on all responses, not just the main document. A <meta http-equiv> tag works for some cases such as static single-page apps, but MDN says it does not support all CSP features. The policy is a list of directives separated by semicolons:
Content-Security-Policy: default-src 'self'; img-src 'self' example.com
Here default-src is the fallback for every resource type that has no directive of its own, and img-src allows images from the site itself and example.com.
The directives that matter for scripts
script-srcsets where JavaScript may come from and whether inline scripts run.connect-srcrestricts the URLs scripts can reach through script interfaces. MDN listsfetch(),XMLHttpRequest,WebSocket,EventSource,Navigator.sendBeacon()and thepingattribute of links. This is the directive that limits where stolen data can be sent.object-src 'none'andbase-uri 'none'close two older routes: plugin embeds and the<base>element, which can change how relative URLs are read.frame-ancestorscontrols who may embed your page, which is the clickjacking defence.upgrade-insecure-requestsasks the browser to use HTTPS for resources.
Allowlists versus strict CSP
The older approach lists the domains you trust: script-src 'self' https://cdn.example.com. MDN's implementation guide says strict CSPs are preferred over these location-based allowlist policies, because allowlists often end up allowing unsafe domains, which defeats the point, and can grow very large when a page needs many third-party scripts.
A strict CSP instead allows only scripts that carry the right nonce (a random value generated for each response and placed both in the header and on the script tags) or the right hash (a digest of the script's contents). Injected code has neither, so it does not run. MDN says to use nonces if you can generate content dynamically and hashes if you serve static content; the drawback of hashes is that you must recalculate them whenever the script changes.
A strict policy also disables inline event handler attributes such as onclick and risky calls such as eval(). A typical shape, following MDN's guidance, is:
Content-Security-Policy: script-src 'nonce-{random}' 'strict-dynamic'; object-src 'none'; base-uri 'none'
strict-dynamic, and why it cuts both ways
Strict CSP is hard when you use scripts you do not control. If a trusted third-party script loads further scripts, those fail, because they carry no nonce. MDN's answer is the strict-dynamic keyword: a script with a nonce or hash is allowed to load further scripts without their own nonce or hash, passing the trust along.
This is convenient and is the reason many sites adopt it. It is also exactly the property an attacker can use when a trusted script is compromised: whatever that script loads is trusted too. In the Report URI findings, policies with 'strict-dynamic' let the payload through. The lesson is not to avoid the keyword, but to know that a nonce on a vendor's tag means you trust everything that tag does, now and after any change on the vendor's side.
Subresource Integrity: pinning a file
SRI lets you add a hash to the tag. MDN describes it as a way to verify that resources fetched from places like a CDN are delivered without unexpected manipulation, and as a defence against supply chain attacks where an attacker gains control of the third-party host. The browser downloads the file, hashes it, and refuses to run it if the hash does not match:
<script src="https://cdn.example.com/script.js"
integrity="sha384-..." crossorigin="anonymous"></script>
Allowed hash algorithms are SHA-256, SHA-384 and SHA-512. Cross-origin use requires the server to send CORS headers, which is why the crossorigin attribute is needed. You can enforce the practice with the Integrity-Policy header (with Integrity-Policy-Report-Only for trying it first), which blocks script and style requests that lack integrity metadata.
The limit is obvious: SRI only works for files that should not change. Many vendors serve snippets that are meant to change, such as tag managers or A/B-testing scripts configured in a vendor dashboard. Pinning one would break your site on the next legitimate edit. For those, you rely on your policy, on reporting, and on controlling who can edit the vendor's configuration.
Roll it out safely
- Start in report-only mode.
Content-Security-Policy-Report-Onlyreports violations without blocking anything, so you can see what would break. MDN advises testing there before enforcing. - Set up reporting. MDN recommends the Reporting API (a
Reporting-Endpointsheader and thereport-todirective), and notes that the olderreport-uridirective is deprecated but still needed for cross-browser support. Reports are how you notice a script that suddenly contacts a new domain. - Pick nonces or hashes and add the matching attribute to every script you want to run.
- Refactor what a strict policy forbids: replace inline handlers with
addEventListener()calls, and removeeval(). - Avoid the weakening keywords if you can. MDN's warning list includes
unsafe-inline,data:URIs inscript-src, and overly broad sources. If you cannot removeeval(),unsafe-evalworks but makes the policy significantly weaker, which matches Report URI's finding about'unsafe-eval'. - Restrict
connect-srcto the endpoints your page really calls, to limit what a rogue script can send out. - If strict does not work, do not give up. MDN says an allowlist CSP is still much better than none, and even
default-src https:provides some protection.
Reading a policy: three quick tests
Take any site's header and ask three questions.
- Does
script-src(ordefault-src) contain'unsafe-inline'or'unsafe-eval'? If so, injected inline code oreval()calls are allowed, and much of the protection is gone. MDN listsunsafe-inlineamong the unsafe sources to avoid. - Does it rely on a long list of domains? Each domain you allow is a party whose compromise becomes yours. MDN notes that allowlists tend to grow large and end up allowing unsafe domains.
- Does
connect-srcexist, and is it narrow? If it is missing,default-srcis the fallback. A permissive fallback means a script can send data almost anywhere.
You can paste a header into a CSP evaluator (MDN links to one) to see warnings for these patterns.
Common mistakes
- Enforcing too early. Teams switch on a policy without a report-only period, break checkout or login, and roll back. Report-only first avoids this.
- A nonce that never changes. A nonce is only useful if it is new for each response; a fixed one is just a password in the page source.
- Allowing a whole vendor domain. Trusting
cdn.vendor.comtrusts every file any customer of that vendor can place there. - Forgetting non-document responses. MDN says the header should be set on all responses to all requests, not only the main document.
What a CSP cannot do
- It does not replace sanitising input; MDN says to do both.
- It cannot make a trusted script trustworthy. If you let a vendor's script run with full page privileges, an attacker who changes that script runs with them too.
- Each directive guards one door. In the Optimizely case, as Report URI describes it, locking down
connect-srcblocked data leaving but not an address swap that happened wholly inside the page.
A short checklist
- Send a strict, nonce- or hash-based CSP, in report-only mode first.
- List every third-party script on your key pages (checkout, login) and ask whether it needs to be there.
- Use SRI for files that do not change.
- Restrict who can edit scripts configured in vendor dashboards, and get alerts on changes.
- Keep
connect-srcas narrow as you can. - Watch violation reports.
Sources
- Content Security Policy (CSP), MDN Web Docs
- Content Security Policy (CSP) implementation, MDN Web Docs
- Subresource Integrity, MDN Web Docs
- Content-Security-Policy: connect-src, MDN Web Docs
- World's Largest Brands Infected With Malware Via Optimizely, Report URI, 16 November 2026
Written by
The official account of the people who run audiotale.net: announcements, conversation starters and the newsroom's drafts. Posts written with AI say so. Every other account here is a real person.
Read next
Bitly links hijacked: old Burger King, McDonald's and Trump tweet links re-registered 15 Nov
Someone re-registered Bitly endings from 2009 to 2013 posts and redirected them; Bitly says it suspended the accounts, and here is how to check your own old short links.
audiotale.net team8 min read
Node.js 26.12.1, 24.22.1 and 22.23.4 fix ten CVEs, including a Brotli remote code execution flaw and TLS hostname bypasses
The 21 October 2026 Node.js security releases patch four high-severity bugs, block the compromised eMudhra intermediates, and affect every supported line.
audiotale.net team9 min read
RSS, RPS and RFS on Linux explained: receive queues, how to check them, and why NoisyTCP cares
How Linux spreads incoming packets across CPUs with RSS, RPS and RFS, where to see your settings, and why the NoisyTCP workaround is a single receive queue.
audiotale.net team5 min read