#13448 closed (fixed)
CsrfResponseMiddleware breaks the Etag in CommonMiddleware
Reported by: | defaultwombat | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | 1.1 |
Severity: | Keywords: | csrf etag middleware never_cache | |
Cc: | kanu@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If the CsrfResponseMiddleware.process_response adds the csrf_token field to the content the etag of the response doesn't get updated.
The CommonMiddleware.process_response however only generates a new etag if it doesn't already exist.
So the comparison between HTTP_IF_NONE_MATCH and Etag is based on a wrong etag and might lead to a wrong HttpResponseNotModified.
My problem was that I sometimes got a 403 from the csrf-middleware when i added or changed things in the admin site.
Although I haven't resolved the whole puzzle yet, the main pieces are:
- When you use a admin form the etag is generated by the never_cache decorator.
- The csrf middleware adds the csrf_token field to the form
- If your browser doesn't have the page cached everything is fine
- When you logout and login again and you get a new session_id
- If you now use the same admin form again the common middleware doesn't notice any changes beween the HTTP_IF_NONE_MATCH and the etag.
- So it sends a HttpResponseNotModified which let the browser use the cached version of the page.
- As the csrf_token in the cached page is based on a different session_id you end up with a 403 when you try to submit the form.
Change History (3)
comment:1 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I can recreate the problem on 1.1.1, but not on either current trunk nor current 1.1.X SVN branch. In current trunk the CSRF token is session-independent, so the logout/login does not cause a problem. In current 1.1.X, something seems to prevent the not-modified response after the logout/login. The sequence I see is:
On 1.1.1, the same sequence did produce a problem:
So while I have not tracked down what exactly was changed to fix this in the 1.1.X branch, it does appear to already be fixed there.