fix(security): add per-request CSP nonce and inject into served index to fix #7456 #7953
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.
Summary 📝
Add per-request CSP nonce support and automatic nonce injection for served SPA
index.htmlso inline boot scripts are allowed without relaxing the Content Security Policy (CSP).Files Changed 📂
koa-security-headers.tscrypto.randomBytes).ctx.state.cspNonce.'nonce-<value>'into thescript-srcdirective for console and experience Helmet CSP settings.koa-serve-static.tsindex.html, injectnonce="..."into inline<script>tags that lacksrc/nonceso the markup matches the CSP header.Notes / Disclaimers⚠️
'unsafe-inline'remains in existing directives; this is expected — remove'unsafe-inline'from the CSP for a stricter policy.koa-security-headersmust run before the static-serving middleware soctx.state.cspNonceis available whenindex.htmlis patched.Testing ✅
Manual run (local container / dev):
curl -v http://localhost:3001/console(inspectContent-Security-Policyheader)<script nonce="...">for inline scripts:curl http://localhost:3001/console | grep -i 'nonce='src/nonceare modified.Checklist 📋
.changeset(add entry describing this change)index.htmlcontains matching nonce)If you want, I can prepare the
.changesetfile and a small integration test that fetches the page and asserts header/body nonce equality.