-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
Append Vary: Cookies
header to response when session is accessed
#1026
Conversation
I didn't know exactly how Please correct me, if anything about that is wrong. Anyway based on this I don't see why the cookie has to be set, if we respond with |
Note: Sorry, github made it look like I double posted this so I deleted one of them. Looks like it actually wasn't a double post so I'm reposting now. :-/ Vary: Cookie allows caching the response but the cache has to use the values in the cookie as part of its key. That way the cache can save separate entries for a request for /user/profile/ when there's a cookie for username=abadger and a request for /user/profile/ when the cookie is for username=citruspi |
As per the spec the header fields listed in Vary should be used by the cache to determine whether a cached response is valid or not and thus two requests with different cookies but otherwise identical headers would not match. It should be noted that not all caches (including varnish and Google's page speed service) implement what is described by @abadger and instead just drop caching for that request altogether with this header. Despite this, I've got a great big 👍 for this PR, this is a very clean and nice way of ensuring that whenever a request relies upon the data stored in the session then the cache will not serve a cached response if the session data has changed. |
I'm not so sure myself yet. I would love to get more input on this. For me this sounds like a neat idea but I need to think more about it. |
As of Nginx 1.7.7, the Nginx proxy cache (commonly used alongside uWSGI) also supports using the I didn't dig into the implementation much, but I'm +1 on the general idea here. |
# whole cookie. | ||
if not session: | ||
if session.modified: | ||
response.delete_cookie(app.session_cookie_name, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This appears to be the only way for Flask itself to do this automatically, but I'm not sure whose responsibility we want caching config to be. Perhaps this should be part of something like Flask-Login (e.g.: everything decorated with I'd much rather see an issue opened about this before we discuss a concrete implementation. |
Django adds |
Then I suppose I am fine with it conceptually. |
Continued in #2288. |
To prevent caching proxies like Varnish from providing multiple users with the same cache, you can set a
Vary: Cookie
header.This pull request appends the header to each response when the session is accessed.
I came across this issue while working on Warehouse, PyPi 2.0. @dstufft wanted something that added the header to the response anytime the session was being used.
— @citruspi + @abadger