fix(csp): append nonce alongside existing directives instead of stripping 'unsafe-inline'#3885
Open
sanjibani wants to merge 1 commit into
Open
fix(csp): append nonce alongside existing directives instead of stripping 'unsafe-inline'#3885sanjibani wants to merge 1 commit into
sanjibani wants to merge 1 commit into
Conversation
…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.
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 #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.mdis updated to reflect the new semantics and link to the web.dev fallback guide.