# Should we make it harder to cache responses containing CSP nonces?

**URL:** <https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065>\
**Category:** Django Internals\
**Tags:** security\
**Created:** [May 6, 2026, 10:40pm UTC](https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065 "2026-05-06T22:40:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobtylerwalls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jacobtylerwalls/32/30603_2.png) [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Post date:** [May 6, 2026, 10:40pm UTC](https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065/1 "2026-05-06T22:40:25Z")

</div>

The CSP nonce attribute on a request is lazy, so we know whether or not it was used. If it was used in a response, should we make it harder to accidentally cache that response? Perhaps by:

- adding the nonce to the cache key for Django’s per-site and per-view caches (to frustrate caching but perhaps allow strategies that use placeholders to still work)
- or, setting `Cache-Control: private`

* * *

We [link](https://docs.djangoproject.com/en/dev/ref/csp/#nonce-generation-and-caching) to MDN’s advice to avoid caching responses containing nonce values:

> However, because nonces must be unique per request, extra care is needed when using full-page caching (e.g., Django’s cache middleware, CDN caching). Serving cached responses with previously generated nonces may result in reuse across users and requests. Although such responses may still appear to work (since the nonce in the CSP header and HTML content match), reuse defeats the purpose of the nonce and weakens security.  
> [Content Security Policy | Django documentation | Django](https://docs.djangoproject.com/en/dev/ref/csp/#nonce-generation-and-caching)

This would be equivalent to the situation for CSRF ([forum post](https://forum.djangoproject.com/t/django-caching-useless-with-csrf-protection/26172)), where you simply get no caching, because we vary on cookies when setting the CSRF cookie.

Our advice continues:

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

So, having not given this all that much thought, my guess is that that would look like:

```py
MIDDLEWARE = [
    ContentSecurityPolicyMiddleware,
    RewriteNonceMiddleware,
    UpdateCacheMiddleware, # usually goes first
    FetchFromCacheMiddleware,
]

RewriteNonceMiddleware(...
    def process_request():
        """"swap out the secure nonce for a placeholder nonce"""

    def process_response():
         """parse response (!!) and rewrite body with the secure one"""

```

In which case, even under my proposal, when `UpdateCacheMiddleware` runs, it only sees the placeholder nonce and caches that in a reusable way. (Now, how realistic is this?)

* * *

This came up in [review](https://github.com/django/django/pull/21010#pullrequestreview-4235130467) with @nessita and @codingjoe, see also this [ticket](https://github.com/mdn/content/issues/44021) @codingjoe opened with MDN for clarifying guidance around whether `Cache-Control: Private` is a good defensive measure here.

/cc @robhudson

---

<div class="post-metadata">

**Author:** ![codingjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codingjoe/32/215_2.png) [@codingjoe](https://forum.djangoproject.com/u/codingjoe)\
**Post date:** [May 7, 2026, 12:08pm UTC](https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065/2 "2026-05-07T12:08:53Z")

</div>

I am known to make mistakes, so I’d definitely be curious to hear back from the openwebdocs team. I know that they are usually pretty quick.

Likewise, I believe the decision goes beyond Django. There should be an industry consensus on how to handle CSP headers. I have reached out to the CSP authors for better clarification in the current CSP spec draft; see also [Clarification RFC 7234 caches for present nonce-sources · Issue #815 · w3c/webappsec-csp · GitHub](https://github.com/w3c/webappsec-csp/issues/815)

In both cases, I believe either solution would qualify as a bug and could be applied to 6.0 and 6.1 after the freeze. Which gives us a bit more time for everyone involved to reach consensus.

---

<div class="post-metadata">

**Author:** ![jacobtylerwalls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jacobtylerwalls/32/30603_2.png) [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Post date:** [July 10, 2026, 10:36pm UTC](https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065/3 "2026-07-10T22:36:49Z")

</div>

Replying to your latest [message](https://github.com/mdn/content/issues/44021#issuecomment-4859788405) on the MDN issue:

> So I would go with a `private` policy for the cache middleware while emitting a UserWarning during runtime, or a separate check (doesn’t cache per view policies). However, the CSP middleware should have an option to disable the injected `private` CC policy for people that use a CDN that supports regeneration.

`private` by default (that is, if the nonce is materialized) with a way to opt out sounds good at first blush. @robhudson what do you think?

What would the `UserWarning` be for? (We don’t do that for CSRF. You may be interested in this [documentation ticket.](https://code.djangoproject.com/ticket/23054))

---

<div class="post-metadata">

**Author:** ![jacobtylerwalls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jacobtylerwalls/32/30603_2.png) [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Post date:** [August 15, 2026, 1:36pm UTC](https://forum.djangoproject.com/t/should-we-make-it-harder-to-cache-responses-containing-csp-nonces/45065/4 "2026-08-15T13:36:29Z")

</div>

I opened a ticket and flagged it as a candidate for upcoming sprints.

> **[\#37280 (ContentSecurityPolicyMiddleware should set Cache-Control: Private if...](https://code.djangoproject.com/ticket/37280)**
