spike(security): ship Trusted Types (report-only) for the search UI#52
Merged
Conversation
…search Worked-example spike for /spec/security/trusted-types/. Pagefind's search UI (pagefind-ui.js) builds its results list with innerHTML — 11 assignments, the only TrustedHTML sink on the site (our own scripts use none). So enforcing require-trusted-types-for would break search unless a default policy covers it; a default policy is the only option because Pagefind's bundled code assigns raw strings and can't opt into a named policy. - Vendor DOMPurify 3.4.11 as a self-hosted first-party asset (script-src 'self'), copied from node_modules by scripts/generate-assets.mjs at prebuild. - public/trusted-types-policy.js registers the `default` policy backed by DOMPurify (strips scripts/handlers, keeps Pagefind's a/p/mark/list markup). Loaded first in <head> so it exists before Pagefind mounts; fails closed and loud if DOMPurify is missing rather than registering a pass-through. - _headers: add Content-Security-Policy-Report-Only with require-trusted-types-for 'script'; trusted-types default, reporting to the existing /reports collector. Report-only is non-blocking — safe in prod, and measures real violations. - Ignore the vendored minified lib in eslint/prettier. To enforce: drop the `-Report-Only` suffix once reports are clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…policy Report-only testing surfaced two sinks the static innerHTML grep missed: - pagefind.js loads its own JS/WASM chunks via a TrustedScriptURL sink, not just innerHTML. The default policy now implements createScriptURL (same-origin allowlist; script-src 'self' is the backstop) alongside createHTML. - DOMPurify registers its own Trusted Types policy named "dompurify"; add it to the trusted-types allowlist next to default. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Deploying specification-website with
|
| Latest commit: |
51623da
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://43dc4e91.specification-website.pages.dev |
| Branch Preview URL: | https://spike-trusted-types-search.specification-website.pages.dev |
Owner
Author
|
Report-only testing (thanks!) surfaced two sinks my static
The |
… into spike/trusted-types-search # Conflicts: # package-lock.json # package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes the site a worked example of
/spec/security/trusted-types/(documented in #51) by actually wiring Trusted Types — starting in report-only so nothing breaks in production.The finding
Pagefind's search UI is the only thing on the site that would trip Trusted Types. The built
pagefind-ui.jsassignsinnerHTML11 times and uses no other sink (noeval,document.write,srcdoc); our own scripts use zero DOM-injection sinks. Because Pagefind's bundled code does rawel.innerHTML = …, it can't opt into a named policy — so the auto-applieddefaultpolicy is the only thing that can cover it, which means a general sanitiser (DOMPurify), not a<mark>-only one.What this PR does
/vendor/purify.min.js(stays underscript-src 'self'), copied fromnode_modulesbyscripts/generate-assets.mjsat prebuild.public/trusted-types-policy.jsregistering thedefaultTrusted Types policy backed byDOMPurify.sanitize(strips scripts/handlers, keeps Pagefind'sa/p/mark/list markup). Loaded first in<head>so it exists before Pagefind mounts. Fails closed and loud if DOMPurify is absent rather than registering an insecure pass-through.Content-Security-Policy-Report-Only: require-trusted-types-for 'script'; trusted-types default; report-to csp-endpointto_headers, reusing the existing/reportscollector. Report-only = non-blocking.Build, lint, and format all pass; both files land in
dist.npm run previewdoesn't apply_headers, so I couldn't drive a live search with Trusted Types enforced. To confirm DOMPurify preserves Pagefind's markup and results still render:npx wrangler pages dev dist, drop the-Report-Onlysuffix locally, and run a search.To enforce once verified: change
Content-Security-Policy-Report-Only→ fold the two directives into the mainContent-Security-Policy. If this lands, the Trusted Types page should get a "this site ships it" callout (as the reporting-endpoints page has).🤖 Generated with Claude Code