fix(csp): warn in development when a response has no nonce - #3900
Open
Kjubikstronk wants to merge 1 commit into
Open
fix(csp): warn in development when a response has no nonce#3900Kjubikstronk wants to merge 1 commit into
Kjubikstronk wants to merge 1 commit into
Conversation
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.
Closes #3859.
csp({ useNonce: true })swaps'unsafe-inline'for a per-request nonce, but onlyctx.render()ever sets one.NONCE_SYMBOLhas a single writer, inContext.render(), so a route returningctx.html(),ctx.json(),ctx.text()orctx.redirect()produces a response with no nonce and the header keeps'unsafe-inline'.That fallback is deliberate and already covered by "CSP - useNonce with non-rendered response falls back to unsafe-inline", so I have not changed it. The gap is that it happens silently: someone who sets
useNonce: trueand has a fewctx.html()routes gets a weaker CSP on exactly those routes with nothing to tell them.So this only adds a development-mode warning on that path. Production is untouched and the header is byte for byte the same either way.
The warning is deduplicated per pathname, since otherwise it fires on every request while you are developing. The
Setlives inside thecsp()call rather than at module scope, so each middleware instance keeps its own and nothing leaks between apps or between tests.Three tests cover it: the warning fires in development, it fires once across repeated requests to the same path, and it stays quiet in production. With the change reverted the first two fail and the third still passes, so they are not vacuous.
Local runs: 58/58 middleware tests,
deno fmt --checkanddeno lintclean. Fulldeno task testgives 627 passed / 2 failed, and both failures (init - fmt, lint, and type check projectandBuilder - compile entry) reproduce on unmodified main on my machine, so they are unrelated to this change.