Append a 'Vary: Cookie' header to the response when the session has been accessed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mihir Singh · committed by Mihir Singh · parent 5c3b161 · Browse files · 1 file·+39 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+39 added−17 removed
- other:+39 added−17 removed
Provenance
- Commit
671c67a- Tree
673b68f- Author
Mihir Singh <me@mihirsingh.com>- Committer
Mihir Singh <me@mihirsingh.com>- Parent
5c3b161
1 file changed+39 added−17 removed
modified:flask/sessions.py+39 added-17 removed
49 unmodified lines50 #: long as changes do not happen on mutable structures in the session.51 #: The default mixin implementation just hardcodes `True` in.52 modified = True53 54+ #: the accessed variable indicates whether or not the session object has55+ #: been accessed in that request. This allows flask to append a `Vary:56+ #: Cookie` header to the response if the session is being accessed. This57+ #: allows caching proxy servers, like Varnish, to use both the URL and the58+ #: session cookie as keys when caching pages, preventing multiple users59+ #: from being served the same cache.60+ accessed = True61 62 class TaggedJSONSerializer(object):63 """A customized JSON serializer that supports a few extra types that64 we take for granted when serializing (tuples, markup objects, datetime).53 unmodified lines118 119 def __init__(self, initial=None):120 def on_update(self):121 self.modified = True122+ self.accessed = True123 CallbackDict.__init__(self, initial, on_update)124 self.modified = False125+ self.accessed = False126 127+ def __getitem__(self, key):128+ self.accessed = True129+ return super(SecureCookieSession, self).__getitem__(key)130 131+ def get(self, key, default=None):132+ self.accessed = True133+ return super(SecureCookieSession, self).get(key, default)134+135 class NullSession(SecureCookieSession):136 """Class used to generate nicer error messages if sessions are not137 available. Will still allow read-only access to the empty session138 but fail on setting.210 unmodified lines349 def save_session(self, app, session, response):350 domain = self.get_cookie_domain(app)351 path = self.get_cookie_path(app)352 337- # Delete case. If there is no session we bail early.338- # If the session was modified to be empty we remove the339- # whole cookie.340- if not session:341- if session.modified:353+ if session.accessed:342- response.delete_cookie(app.session_cookie_name,343- domain=domain, path=path)344- return354 346- # Modification case. There are upsides and downsides to347- # emitting a set-cookie header each request. The behavior348- # is controlled by the :meth:`should_set_cookie` method349- # which performs a quick check to figure out if the cookie350- # should be set or not. This is controlled by the351- # SESSION_REFRESH_EACH_REQUEST config flag as well as352- # the permanent flag on the session itself.353- if not self.should_set_cookie(app, session):354- return355+ response.headers.add('Vary', 'Cookie')356 357+ else:358+359+ # Delete case. If there is no session we bail early.360+ # If the session was modified to be empty we remove the361+ # whole cookie.362+ if not session:363+ if session.modified:364+ response.delete_cookie(app.session_cookie_name,365+ domain=domain, path=path)366+ return367+368+ # Modification case. There are upsides and downsides to369+ # emitting a set-cookie header each request. The behavior370+ # is controlled by the :meth:`should_set_cookie` method371+ # which performs a quick check to figure out if the cookie372+ # should be set or not. This is controlled by the373+ # SESSION_REFRESH_EACH_REQUEST config flag as well as374+ # the permanent flag on the session itself.375+ if not self.should_set_cookie(app, session):376+ return377+378 httponly = self.get_cookie_httponly(app)379 secure = self.get_cookie_secure(app)380 expires = self.get_expiration_time(app, session)381 val = self.get_signing_serializer(app).dumps(dict(session))6 unmodified lines