Opened 2 months ago

Last modified 34 hours ago

#37280 assigned New feature

ContentSecurityPolicyMiddleware should set Cache-Control: Private if a nonce is rendered

Reported by: Jacob Walls Owned by: Shubham Akhilesh Singh
Component: CSP Version: dev
Severity: Normal Keywords: csp nonce
Cc: Johannes Maron, Tim Schilling, Rob Hudson Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

If a CSP nonce is rendered into a response, the response is no longer suitable for caching. See discussion ​at MDN issue tracker and ​our docs:

Avoid caching full responses that include {{ csp_nonce }} or csp_nonce_attr.

We can make it easier for pages cached with the UpdateCacheMiddleware to do the secure thing by default by having the ContentSecurityPolicyMiddleware set Cache-Control: Private if a nonce has been rendered.

Then, for this case:

If caching is necessary, use a strategy that injects a fresh nonce on each request

Johannes mused about an opt-out mechanism for folks who have done exactly that -- injected a fresh nonce somehow -- e.g. with a class attribute on the middleware to keep the Django 6.0 status quo.


Then, for the per-page cache, It's a known flaw that @cache_page runs before middlewares have had a chance to set interesting headers (#15855). So the above won't help. We could simply mention CSP nonces in the same paragraph added in 4ef959ab0c7dde0459041a795359c9bc040df2b7, unless we think it's redundant.

Change History (11)

comment:1 by Jacob Walls, 2 months ago

Type: Cleanup/optimization → New feature

Worth a release note

comment:2 by David Smith, 2 months ago

Triage Stage: Unreviewed → Accepted

comment:3 by Shubham Akhilesh Singh, 2 months ago

Owner: changed from Django Sprints to Shubham Akhilesh Singh

comment:4 by Shubham Akhilesh Singh, 2 months ago

Has patch: set

comment:5 by blighj, 8 weeks ago

Patch needs improvement: set

comment:6 by Shubham Akhilesh Singh, 2 weeks ago

Patch needs improvement: unset

comment:7 by Jacob Walls, 2 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:8 by Jacob Walls, 2 weeks ago

Patch needs improvement: set
Triage Stage: Ready for checkin → Accepted

comment:9 by Jacob Walls, 3 days ago

Component: Core (Cache system) → CSP

comment:10 by Shubham Akhilesh Singh, 2 days ago

Patch needs improvement: unset

comment:11 by Sarah Boyce, 34 hours ago

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top