Skip to content
Myr

reorder session cookie checks to deleted, accessed, modified

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent e2f4c0a · Browse files · 1 file+37 added−38 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+37 added−38 removed

  • other:+37 added−38 removed

1 file+37 added−38 removed

modified:flask/sessions.py+37 added-38 removed
123 unmodified lines
124 def __init__(self, initial=None):125 def on_update(self):126 self.modified = True127 self.accessed = True128 CallbackDict.__init__(self, initial, on_update)128129 super(SecureCookieSession, self).__init__(initial, on_update)130 self.modified = False131 self.accessed = False132133 def __getitem__(self, key):
172 unmodified lines
306 if session.permanent:307 return datetime.utcnow() + app.permanent_session_lifetime308309 def should_set_cookie(self, app, session):309 """Indicates whether a cookie should be set now or not. This is310 used by session backends to figure out if they should emit a311 set-cookie header or not. The default behavior is controlled by312 the ``SESSION_REFRESH_EACH_REQUEST`` config variable. If313 it's set to ``False`` then a cookie is only set if the session is314 modified, if set to ``True`` it's always set if the session is315 permanent.310 """Used by session backends to determine if a ``Set-Cookie`` header311 should be set for this session cookie for this response. If the session312 has been modified, the cookie is set. If the session is permanent and313 the ``SESSION_REFRESH_EACH_REQUEST`` config is true, the cookie is314 always set.315 316 This check is usually skipped if the session was deleted.317317 This check is usually skipped if sessions get deleted.318318 .. versionadded:: 0.11319 """321 if session.modified:322 return True323 save_each = app.config['SESSION_REFRESH_EACH_REQUEST']324 return save_each and session.permanent320321 return session.modified or (322 session.permanent and app.config['SESSION_REFRESH_EACH_REQUEST']323 )324325 def open_session(self, app, request):326 """This method has to be implemented and must either return ``None``327 in case the loading failed because of a configuration error or an328 instance of a session object which implements a dictionary like
56 unmodified lines
385 def save_session(self, app, session, response):386 domain = self.get_cookie_domain(app)387 path = self.get_cookie_path(app)388389 # If the session is modified to be empty, remove the cookie.390 # If the session is empty, return without setting the cookie.391 if not session:390 if session.accessed:392 if session.modified:393 response.delete_cookie(394 app.session_cookie_name,395 domain=domain,396 path=path397 )398399 return400401 # Add a "Vary: Cookie" header if the session was accessed at all.402 if session.accessed:403 response.headers.add('Vary', 'Cookie')404394 else:405 if not self.should_set_cookie(app, session):406 return407396 # Delete case. If there is no session we bail early.397 # If the session was modified to be empty we remove the398 # whole cookie.399 if not session:400 if session.modified:401 response.delete_cookie(app.session_cookie_name,402 domain=domain, path=path)403 return404405 # Modification case. There are upsides and downsides to406 # emitting a set-cookie header each request. The behavior407 # is controlled by the :meth:`should_set_cookie` method408 # which performs a quick check to figure out if the cookie409 # should be set or not. This is controlled by the410 # SESSION_REFRESH_EACH_REQUEST config flag as well as411 # the permanent flag on the session itself.412 if not self.should_set_cookie(app, session):413 return414408 httponly = self.get_cookie_httponly(app)409 secure = self.get_cookie_secure(app)410 expires = self.get_expiration_time(app, session)411 val = self.get_signing_serializer(app).dumps(dict(session))419 response.set_cookie(app.session_cookie_name, val,420 expires=expires, httponly=httponly,421 domain=domain, path=path, secure=secure)412 response.set_cookie(413 app.session_cookie_name,414 val,415 expires=expires,416 httponly=httponly,417 domain=domain,418 path=path,419 secure=secure420 )