feat: add per-request CSP nonce generation for inline scripts#16402
Open
mtruj013 wants to merge 4 commits into
Open
feat: add per-request CSP nonce generation for inline scripts#16402mtruj013 wants to merge 4 commits into
mtruj013 wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## feat-csp-nonce #16402 +/- ##
==================================================
+ Coverage 48.30% 48.41% +0.10%
==================================================
Files 37 37
Lines 5912 5911 -1
==================================================
+ Hits 2856 2862 +6
+ Misses 3056 3049 -7
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds per-request CSP nonce generation in the Flask app, exposes the nonce to templates, and appends it to the Content-Security-Policy response header for script directives to enable nonce-based inline script execution.
Changes:
- Generate a CSP nonce on every request and store it on
flask.g. - Expose
csp_noncevia the template context processor. - Append
'nonce-…'toscript-srcandscript-src-elemin the CSP response header.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+224
to
234
| nonce = flask.g.get("csp_nonce", "") | ||
| csp = { | ||
| key: ( | ||
| values + [f"'nonce-{nonce}'"] | ||
| if key in ("script-src-elem", "script-src") | ||
| else values | ||
| ) | ||
| for key, values in CSP.items() | ||
| } | ||
| response.headers["Content-Security-Policy"] = get_csp_as_str(csp) | ||
|
|
Contributor
Author
There was a problem hiding this comment.
This work is split into multiple prs, indeed the nonce attribute will be added to all inline scripts
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.
Done
unsafe-inlinefrom script directiveQA
Issue / Card
Partial work towards completing WD-36594