Skip to content
Myr

Added SESSION_REFRESH_EACH_REQUEST config option.

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

This also changes how sessions are being refreshed. With the new behavior set-cookie is only emitted if the session is modified or if the session is permanent. Permanent sessions can be set to not refresh automatically through the SESSION_REFRESH_EACH_REQUEST config key. This fixes #798.

authored by Armin Ronacher · committed by Armin Ronacher · parent 1a66a7e · Browse files · 5 files+96 added−0 removed
This commit

State

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

Diff stats

5 files+96 added−0 removed

  • other:+96 added−0 removed

5 files+96 added−0 removed

modified:docs/config.rst+12 added
86 unmodified lines
87``PERMANENT_SESSION_LIFETIME`` the lifetime of a permanent session as88 :class:`datetime.timedelta` object.89 Starting with Flask 0.8 this can also be90 an integer representing seconds.91``SESSION_REFRESH_EACH_REQUEST`` this flag controls how permanent92 sessions are refresh. If set to `True`93 (which is the default) then the cookie94 is refreshed each request which95 automatically bumps the lifetime. If96 set to `False` a `set-cookie` header is97 only sent if the session is modified.98 Non permanent sessions are not affected99 by this.100``USE_X_SENDFILE`` enable/disable x-sendfile101``LOGGER_NAME`` the name of the logger102``SERVER_NAME`` the name and port number of the server.103 Required for subdomain support (e.g.:
114 unmodified lines
218219.. versionadded:: 0.10220 ``JSON_AS_ASCII``, ``JSON_SORT_KEYS``, ``JSONIFY_PRETTYPRINT_REGULAR``221222.. versionadded:: 1.0223 ``SESSION_REFRESH_EACH_REQUEST``224225Configuring from Files226----------------------227228Configuration becomes more useful if you can store it in a separate file,
213 unmodified lines
modified:flask/testsuite/basic.py+43 added
343 unmodified lines
344 self.assert_equal(rv['b'], b'\xff')345 self.assert_equal(type(rv['b']), bytes)346 self.assert_equal(rv['t'], (1, 2, 3))347348 def test_session_cookie_setting(self):349 app = flask.Flask(__name__)350 app.testing = True351 app.secret_key = 'dev key'352 is_permanent = True353354 @app.route('/bump')355 def bump():356 rv = flask.session['foo'] = flask.session.get('foo', 0) + 1357 flask.session.permanent = is_permanent358 return str(rv)359360 @app.route('/read')361 def read():362 return str(flask.session.get('foo', 0))363364 def run_test(expect_header):365 with app.test_client() as c:366 self.assert_equal(c.get('/bump').data, '1')367 self.assert_equal(c.get('/bump').data, '2')368 self.assert_equal(c.get('/bump').data, '3')369370 rv = c.get('/read')371 set_cookie = rv.headers.get('set-cookie')372 self.assert_equal(set_cookie is not None, expect_header)373 self.assert_equal(rv.data, '3')374375 is_permanent = True376 app.config['SESSION_REFRESH_EACH_REQUEST'] = True377 run_test(expect_header=True)378379 is_permanent = True380 app.config['SESSION_REFRESH_EACH_REQUEST'] = False381 run_test(expect_header=False)382383 is_permanent = False384 app.config['SESSION_REFRESH_EACH_REQUEST'] = True385 run_test(expect_header=False)386387 is_permanent = False388 app.config['SESSION_REFRESH_EACH_REQUEST'] = False389 run_test(expect_header=False)390391 def test_flashes(self):392 app = flask.Flask(__name__)393 app.secret_key = 'testkey'394
903 unmodified lines
modified:flask/app.py+1 added
283 unmodified lines
284 'SESSION_COOKIE_DOMAIN': None,285 'SESSION_COOKIE_PATH': None,286 'SESSION_COOKIE_HTTPONLY': True,287 'SESSION_COOKIE_SECURE': False,288 'SESSION_REFRESH_EACH_REQUEST': True,289 'MAX_CONTENT_LENGTH': None,290 'SEND_FILE_MAX_AGE_DEFAULT': 12 * 60 * 60, # 12 hours291 'TRAP_BAD_REQUEST_ERRORS': False,292 'TRAP_HTTP_EXCEPTIONS': False,
1551 unmodified lines
modified:flask/sessions.py+33 added
250 unmodified lines
251 """252 if session.permanent:253 return datetime.utcnow() + app.permanent_session_lifetime254255 def should_set_cookie(self, app, session):256 """Indicates weather a cookie should be set now or not. This is257 used by session backends to figure out if they should emit a258 set-cookie header or not. The default behavior is controlled by259 the ``SESSION_REFRESH_EACH_REQUEST`` config variable. If260 it's set to `False` then a cookie is only set if the session is261 modified, if set to `True` it's always set if the session is262 permanent.263264 This check is usually skipped if sessions get deleted.265266 .. versionadded:: 1.0267 """268 if session.modified:269 return True270 save_each = app.config['SESSION_REFRESH_EACH_REQUEST']271 return save_each and session.permanent272273 def open_session(self, app, request):274 """This method has to be implemented and must either return `None`275 in case the loading failed because of a configuration error or an276 instance of a session object which implements a dictionary like
55 unmodified lines
332333 def save_session(self, app, session, response):334 domain = self.get_cookie_domain(app)335 path = self.get_cookie_path(app)336337 # 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:342 response.delete_cookie(app.session_cookie_name,343 domain=domain, path=path)344 return345346 # 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 return355356 httponly = self.get_cookie_httponly(app)357 secure = self.get_cookie_secure(app)358 expires = self.get_expiration_time(app, session)359 val = self.get_signing_serializer(app).dumps(dict(session))
6 unmodified lines
modified:CHANGES+7 added
6 unmodified lines
7-----------89(release date to be announced, codename to be selected)1011- Added ``SESSION_REFRESH_EACH_REQUEST`` config key that controls the12 set-cookie behavior. If set to `True` a permanent session will be13 refreshed each request and get their lifetime extended, if set to14 `False` it will only be modified if the session actually modifies.15 Non permanent sessions are not affected by this and will always16 expire if the browser window closes.1718Version 0.10.219--------------2021(bugfix release, release date to be announced)
463 unmodified lines