How this site protects you
A list of what may run
Every page that shows someone else’s data — a comment, a file name, a URL parameter — risks someone smuggling code into it. The browser cannot tell the author’s script from an injected one when both sit in the same HTML. So this site gives the browser a list: a Content Security Policy says where scripts, styles, images and connections may come from, and the browser itself blocks everything else.
Scripts from files come from this domain (script-src 'self'; section 2 describes one exception). Scripts written
straight into the HTML are allowed only when their SHA-384 hash is on the list. Astro
computes those hashes when it builds the site and writes the policy into a <meta> tag. The browser hashes every
inline script again and compares — a script changed by a single space has a different hash and does not run. This
kind of CSP works with static files from a CDN; the alternative, a one-time nonce, would need the HTML generated per request.
The server adds the second half: response headers. Some of them have to arrive as headers, because <meta>
cannot carry them — the CSP spec ignores frame-ancestors, report-uri and sandbox there
(CSP3 §3.3). And since everything travels over
TLS 1.3, nobody on the way can add or strip a header or a hash. The toy reads all of it from
the very page you are on.
This page under the magnifier
The <meta> CSP of this page
This page has no <meta> CSP — that is how the Astro dev server works. The policy only exists in the production build.
Trusted Types not enabled yet: no policy has require-trusted-types-forResponse headers of this page
Fetching headers…
API response headers (/api/health)
Fetching headers…
Trusted Types, a third layer against injection, are turned on by the same policy (require-trusted-types-for 'script') — the row in the toy shows the live state. The browser refuses plain strings in places that interpret
them as HTML or script (innerHTML, createContextualFragment, eval) unless they pass through an approved
policy.
Inject a script, swap a file
The most common attack on a website is XSS: the attacker gets their HTML into someone else’s page, where it
runs in the victim’s browser with access to their session, cookies and everything the page can see. The toy
below does it for real: it inserts a payload into this page’s document through createContextualFragment — the
way an unfiltered comment ends up in the HTML. The three classic payloads are an inline script, code in an event
handler attribute and a script from a foreign domain.
Inject a script
- Inline script
<script>window.stolen = document.cookie</script> - Event handler attribute
<img src="" alt="" onerror="window.stolen = document.cookie"> - Script from a foreign domain
<script src="https://evil.example/steal.js"></script>
Pick a payload and inject it — the result shows what the browser did.
Trusted Types stop every attempt first: createContextualFragment gets a plain string, so it throws a TypeError
before a single element exists. The bypass switch passes the payload through a policy that accepts any HTML — this
is what careless code looks like — and then the second layer, CSP, stops it: the inline script has no listed hash,
event handler attributes are forbidden because the list has no 'unsafe-hashes', and the foreign domain is not
'self'. The browser does not even send a request to evil.example — it blocks it before it exists. That is why
script-injection is “resistant” in the graph. Use the word with care: CSP is the last line of defence, not the
first. Injected HTML without scripts can still rewrite the page. The first line is inserting data as text
(textContent, not innerHTML).
The second attack skips the site and goes for its suppliers. A page that loads a library from someone else’s CDN
trusts whoever controls that server — today and in the future. Subresource Integrity turns that trust into a
check: the page’s author records the hash of the file they reviewed in the integrity attribute, and the browser
rejects any other (SRI). On files from the site’s own server SRI has nothing to guard:
they could only be swapped by someone who could also change the HTML holding their hash. The toy therefore shows the
mechanism on a pair of files posing as a library on a CDN — the original and a copy with one added line.
Swap the library on the CDN
integrity="sha384-EHEC7rYDuxs1scSCaX0ou+PTnRG6vShCkmfEP6Ob4FNlh30htCkOMQrba0nKIovM" The author computed it from the original they reviewed. It ships with the page’s HTML, so an attacker who controls only the CDN cannot change it.
One added line changes the hash as completely as one flipped bit — the avalanche
effect. The attacker would need another file with the same SHA-384, a second
preimage, and no way faster than guessing is known. Hence “resistant” for cdn-tampering — with one exception.
The Passkeys station page and the use case pages load one file from a
foreign domain: the Cloudflare Turnstile script from challenges.cloudflare.com, which keeps bots from creating
accounts and rooms. Their CSP allows that domain in script-src and frame-src (every other page has a policy
without it), and the script comes without SRI — so there we do what section 4 warns against. A hash cannot be pinned:
Cloudflare changes api.js under the same URL, so a recorded hash would stop matching at the first update and block
registration and room creation. The extra trust is small: Cloudflare
already carries all of this site’s traffic and terminates the TLS connection with your browser, so it could swap any
of its files today.
CSP, SRI and HSTS in the world
- polyfill.ioIn February 2024 a Chinese company bought the domain and GitHub account of a popular project whose script from cdn.polyfill.io was embedded by more than 100,000 sites. In June Sansec reported that the served code redirected some phone users to sports betting sites — only on certain devices and at certain hours, skipping administrators.Source (polyfill.io)
- Strict CSP at GoogleGoogle recommends a CSP with nonces or hashes and ‘strict-dynamic’ instead of a domain allowlist. In “CSP Is Dead, Long Live CSP!” (ACM CCS 2016) a Google team showed that allowlists could be bypassed in 94.72% of the distinct policies found on the web.Source (Strict CSP at Google)
- Trusted TypesChrome and Edge have had them since version 83, Firefox since 148, Safari since 26. Since February 2026 MDN marks them Baseline — they work in every major browser.Source (Trusted Types)
- cdnjsThe public JavaScript library CDN provides SRI hashes for all the script and link tags it offers to copy, and its domain is HSTS preloaded.Source (cdnjs)
- .appThe first top-level domain entirely on the HSTS preload list: browsers reach every .app site — this one included — over HTTPS only, from the very first visit.Source (.app)
A policy of hashes, resources with integrity
Set a CSP without 'unsafe-inline': hash-based for static sites, nonce-based for pages rendered per request.
Send frame-ancestors, reporting and HSTS as server headers. Give every resource from a foreign domain
integrity and crossorigin, and insert user data as text. Then turn on Trusted Types.
Content-Security-Policy: default-src 'none'; script-src 'self' 'sha384-…'; style-src 'self';
img-src 'self'; connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload<script src="https://cdnjs.cloudflare.com/ajax/libs/…/lib.min.js"
integrity="sha384-…" crossorigin="anonymous"></script>Do not add 'unsafe-inline' “for now” — it switches off all protection against inline scripts. Do not rely on a
domain allowlist in script-src: one JSONP endpoint or old library on an allowed domain is enough. Do not put
frame-ancestors in <meta>, where the browser ignores it. And do not treat CSP as a substitute for inserting
data correctly.
<!-- ✕ the browser ignores frame-ancestors in <meta> -->
<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'none'">// ✕ HTML from the user lands in the page
comment.innerHTML = userInput;
// ✓ text stays text
comment.textContent = userInput;