Skip to content

fix(csp): append nonce alongside existing directives instead of stripping 'unsafe-inline'#3885

Open
sanjibani wants to merge 1 commit into
freshframework:mainfrom
sanjibani:fix/csp-use-nonce-append
Open

fix(csp): append nonce alongside existing directives instead of stripping 'unsafe-inline'#3885
sanjibani wants to merge 1 commit into
freshframework:mainfrom
sanjibani:fix/csp-use-nonce-append

Conversation

@sanjibani

Copy link
Copy Markdown

Closes #3813

The csp middleware used replaceAll() to substitute any 'unsafe-inline' with the per-request nonce, which forced the strict-CSP pattern that strips the fallback. This is the wrong direction: web.dev's recommended strict-CSP fallback is to keep 'unsafe-inline' alongside the nonce so older browsers that ignore nonces still work, while modern browsers prefer the nonce.

This changes the middleware to simply append the nonce to each inline directive (script-src, style-src, default-src, script-src-elem, style-src-elem, style-src-attr), preserving whatever the user wrote — including their 'unsafe-inline' fallback. The default 'unsafe-inline' entries remain in the rendered header alongside the nonce, matching the production pattern used by e.g. YouTube.

Tests updated to assert the append behaviour; two new tests cover the explicit-fallback case (web.dev strict-CSP pattern) and the strict-no-unsafe-inline case. The doc page at docs/latest/plugins/csp.md is updated to reflect the new semantics and link to the web.dev fallback guide.

…ping 'unsafe-inline'

Closes freshframework#3813

The csp middleware used replaceAll() to substitute any "'unsafe-inline'" with
the per-request nonce, which forced the strict-CSP pattern that strips the
fallback. This is exactly the wrong direction: web.dev's recommended strict-CSP
fallback is to keep "'unsafe-inline'" alongside the nonce so older browsers
that ignore nonces still work, while modern browsers prefer the nonce.

Now the middleware simply appends the nonce to each inline directive
(script-src, style-src, default-src, script-src-elem, style-src-elem,
style-src-attr), preserving whatever the user wrote (including their
"'unsafe-inline'" fallback). The default "'unsafe-inline'" entries remain
in the rendered header alongside the nonce, matching the production pattern
used by e.g. YouTube.

Tests updated to assert the append behaviour; two new tests cover the
explicit-fallback case and the strict-no-unsafe-inline case.
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.

Feature Request: adding insecureUnsafeInline property to CSP middleware

1 participant