Skip to content

Fixed #36560 -- Prevented UpdateCacheMiddleware from caching responses with Cache-Control 'no-cache' or 'no-store'. #19748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2025

Conversation

mengxunQAQ
Copy link
Contributor

@mengxunQAQ mengxunQAQ commented Aug 19, 2025

Trac ticket number

ticket-36560

Branch description

When the Cache-Control header is set to private, no-cache, or no-store, the response should not be cached.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

@github-actions github-actions bot added the no ticket Based on PR title, no linked Trac ticket label Aug 19, 2025
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Hello! Thank you for your contribution 💪

As it's your first contribution be sure to check out the patch review checklist.

If you're fixing a ticket from Trac make sure to set the "Has patch" flag and include a link to this PR in the ticket!

If you have any design or process questions then you can ask in the Django forum.

Welcome aboard ⛵️!

@mengxunQAQ mengxunQAQ force-pushed the patch-1 branch 2 times, most recently from e9bf4fc to 0527f51 Compare August 19, 2025 09:03
@mengxunQAQ mengxunQAQ changed the title Fixed the issue of incorrect caching in the cache middleware. Refs #36560 -- Fixed the issue of incorrect caching in the cache middleware. Aug 19, 2025
@github-actions github-actions bot removed the no ticket Based on PR title, no linked Trac ticket label Aug 19, 2025
@sarahboyce
Copy link
Contributor

Thank you!
This needs a test, maybe something like:

--- a/tests/cache/tests.py
+++ b/tests/cache/tests.py
@@ -2731,16 +2731,17 @@ class CacheMiddlewareTest(SimpleTestCase):
                 )
             cache.clear()
 
-    def test_cached_control_private_not_cached(self):
-        """Responses with 'Cache-Control: private' are not cached."""
-        view_with_private_cache = cache_page(3)(
-            cache_control(private=True)(hello_world_view)
-        )
-        request = self.factory.get("/view/")
-        response = view_with_private_cache(request, "1")
-        self.assertEqual(response.content, b"Hello World 1")
-        response = view_with_private_cache(request, "2")
-        self.assertEqual(response.content, b"Hello World 2")
+    def test_cached_control_not_cached(self):
+        for cc in ("private", "no-cache", "no-store"):
+            with self.subTest(cache_control=cc):
+                view_with_private_cache = cache_page(3)(
+                    cache_control(**{cc: True})(hello_world_view)
+                )
+                request = self.factory.get("/view/")
+                response = view_with_private_cache(request, "1")
+                self.assertEqual(response.content, b"Hello World 1")
+                response = view_with_private_cache(request, "2")
+                self.assertEqual(response.content, b"Hello World 2")
 
     def test_sensitive_cookie_not_cached(self):

@mengxunQAQ
Copy link
Contributor Author

Hello @sarahboyce , the test cases have been updated.

…s with Cache-Control 'no-cache' or 'no-store'.
@sarahboyce sarahboyce changed the title Refs #36560 -- Fixed the issue of incorrect caching in the cache middleware. Fixed #36560 -- Prevented UpdateCacheMiddleware from caching responses with Cache-Control 'no-cache' or 'no-store'. Aug 20, 2025
Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

Thank you

@sarahboyce sarahboyce merged commit ed7c1a5 into django:main Aug 21, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants