feat: add a Content-Security-Policy to the page routes - #106
Merged
Merged
Conversation
New per-route `pageCsp` middleware on `/` and `/template` only — the JSON API and the CDN-backed Swagger UI are left untouched. script-src is 'self' + a per-request nonce; the nonce is threaded into hono's streaming Suspense hydration scripts via StreamingContext so a strict policy doesn't freeze the page on the loader. style-src keeps 'unsafe-inline' for the dynamic language-dot colour (style attributes can't carry a nonce).
Deploying petithub with
|
| Latest commit: |
89fd657
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b4b66d11.petithub.pages.dev |
| Branch Preview URL: | https://petithub-csp.petithub.pages.dev |
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.
Follow-up A from the review. Stacked on #105 — GitHub will retarget this to
mainonce #105 merges.New
pageCspmiddleware (src/utils/headers.tsx) applied only to/and/template, so the JSON API and the CDN-backed Swagger UI are untouched.stream: trueSuspense emits ~4 inline hydration<script>tags. A per-request nonce is generated inpageCsp, stashed onc.var.cspNonce, and threaded into those scripts via<StreamingContext.Provider value={{ scriptNonce }}>inrenderer.tsx. Verified locally: the header nonce and every streamed<script nonce="…">match within a request; zero un-nonced inline scripts.style-src 'unsafe-inline'stays — the language-dot colour is a dynamicstyleattribute and style attributes can't carry a nonce. Low risk (all rendered strings are hono-escaped; no HTML-injection surface)./static/script.jsissrc-based, allowed byscript-src 'self'.tscclean,buildOK (484 kB),wrangler pages dev distboots;/and/templatecarry the CSP,/apiand/api/swaggerdo not.Needs a real browser pass before merge (I can't do this offline): open the preview with the devtools console and confirm — repo renders and streams in, avatars load,
/api/swaggerstill works, and there are no CSP violation errors. Tell me what to loosen if anything breaks.