Skip to content

fix(csp): warn in development when a response has no nonce - #3900

Open
Kjubikstronk wants to merge 1 commit into
freshframework:mainfrom
Kjubikstronk:csp-warn-missing-nonce
Open

fix(csp): warn in development when a response has no nonce#3900
Kjubikstronk wants to merge 1 commit into
freshframework:mainfrom
Kjubikstronk:csp-warn-missing-nonce

Conversation

@Kjubikstronk

Copy link
Copy Markdown

Closes #3859.

csp({ useNonce: true }) swaps 'unsafe-inline' for a per-request nonce, but only ctx.render() ever sets one. NONCE_SYMBOL has a single writer, in Context.render(), so a route returning ctx.html(), ctx.json(), ctx.text() or ctx.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: true and has a few ctx.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 Set lives inside the csp() 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 --check and deno lint clean. Full deno task test gives 627 passed / 2 failed, and both failures (init - fmt, lint, and type check project and Builder - compile entry) reproduce on unmodified main on my machine, so they are unrelated to this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSP nonce silently degrades to unsafe-inline without warning

1 participant