Skip to content

feat: add per-request CSP nonce generation for inline scripts#16402

Open
mtruj013 wants to merge 4 commits into
feat-csp-noncefrom
csp-nonce-infra
Open

feat: add per-request CSP nonce generation for inline scripts#16402
mtruj013 wants to merge 4 commits into
feat-csp-noncefrom
csp-nonce-infra

Conversation

@mtruj013
Copy link
Copy Markdown
Contributor

@mtruj013 mtruj013 commented Jun 1, 2026

Done

  • Added per-request CSP nonce generation to the Flask app as groundwork for removing unsafe-inline from script directive
  • Added related unit tests

QA

  • No QA yet, QA will come in subsequent PRs

Issue / Card

Partial work towards completing WD-36594

@webteam-app
Copy link
Copy Markdown

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.41%. Comparing base (889e319) to head (a6e2cdd).
⚠️ Report is 39 commits behind head on feat-csp-nonce.

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     
Files with missing lines Coverage Δ
webapp/handlers.py 75.51% <100.00%> (+1.59%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_nonce via the template context processor.
  • Append 'nonce-…' to script-src and script-src-elem in the CSP response header.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread webapp/handlers.py
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This work is split into multiple prs, indeed the nonce attribute will be added to all inline scripts

Comment thread webapp/handlers.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.

Comment thread webapp/handlers.py
Comment thread tests/test_routes.py
Comment thread tests/test_routes.py
Comment thread tests/test_routes.py
Comment thread webapp/handlers.py
Comment thread tests/test_routes.py
Comment thread webapp/handlers.py
Comment thread tests/test_routes.py
Comment thread tests/test_routes.py
Comment thread tests/test_routes.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants