Skip to content
Myr

New Feature: Added Support for cookie's SameSite attribute.

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

authored by Fadhel_Chaabane · committed by Fadhel_Chaabane · parent 22708b0 · Browse files · 5 files+61 added−4 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+61 added−4 removed

  • tests:+38 added−0 removed
  • other:+23 added−4 removed

5 files+61 added−4 removed

modified:docs/config.rst+8 added-1 removed
206 unmodified lines
207 sense.208209 Default: ``False``210211.. py:data:: SESSION_COOKIE_SAMESITE212213 Browser will only send cookies to the domain that created them.214 There are two possible values for the same-site attribute: "Strict" and "Lax"215 If set to "None", the samesite flag is not set.216217 Default: ``None``218219.. py:data:: PERMANENT_SESSION_LIFETIME220221 If ``session.permanent`` is true, the cookie's expiration will be set this222 number of seconds in the future. Can either be a
419 unmodified lines
642643 # or via open_instance_resource:644 with app.open_instance_resource('application.cfg') as f:645 config = f.read()638
modified:docs/security.rst+4 added-2 removed
193 unmodified lines
194- ``Secure`` limits cookies to HTTPS traffic only.195- ``HttpOnly`` protects the contents of cookies from being read with196 JavaScript.197- ``SameSite`` ensures that cookies can only be requested from the same198 domain that created them. It is not supported by Flask yet.198 domain that created them. There are two possible values for the same-site199 attribute: "Strict" and "Lax"200201::202203 app.config.update(204 SESSION_COOKIE_SECURE=True,205 SESSION_COOKIE_HTTPONLY=True,206 SESSION_COOKIE_SAMESITE='Strict'207 )208207 response.set_cookie('username', 'flask', secure=True, httponly=True)209 response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Strict')210211Specifying ``Expires`` or ``Max-Age`` options, will remove the cookie after212the given time, or the current time plus the age, respectively. If neither213option is set, the cookie will be removed when the browser is closed. ::
39 unmodified lines
modified:flask/app.py+1 added
282 unmodified lines
283 'SESSION_COOKIE_DOMAIN': None,284 'SESSION_COOKIE_PATH': None,285 'SESSION_COOKIE_HTTPONLY': True,286 'SESSION_COOKIE_SECURE': False,287 'SESSION_COOKIE_SAMESITE': None,288 'SESSION_REFRESH_EACH_REQUEST': True,289 'MAX_CONTENT_LENGTH': None,290 'SEND_FILE_MAX_AGE_DEFAULT': timedelta(hours=12),291 'TRAP_BAD_REQUEST_ERRORS': None,
1922 unmodified lines
modified:flask/sessions.py+10 added-1 removed
247 unmodified lines
248 just returns the value of the ``SESSION_COOKIE_SECURE`` setting.249 """250 return app.config['SESSION_COOKIE_SECURE']251252 def get_cookie_samesite(self, app):253 """Returns "Strict", "Lax" or None if the cookie should use254 samesite attribute. This currently just returns the value of255 the ``SESSION_COOKIE_SAMESITE`` setting.256 """257 return app.config['SESSION_COOKIE_SAMESITE']258259 def get_expiration_time(self, app, session):260 """A helper method that returns an expiration date for the session261 or ``None`` if the session is linked to the browser session. The262 default implementation returns now + the permanent session
105 unmodified lines
368 return369370 httponly = self.get_cookie_httponly(app)371 secure = self.get_cookie_secure(app)372 samesite = self.get_cookie_samesite(app)373 expires = self.get_expiration_time(app, session)374 val = self.get_signing_serializer(app).dumps(dict(session))375 response.set_cookie(376 app.session_cookie_name,
1 unmodified line
378 expires=expires,379 httponly=httponly,380 domain=domain,381 path=path,374 secure=secure382 secure=secure,383 samesite=samesite384 )
modified:tests/test_basic.py+38 added
317 unmodified lines
318 APPLICATION_ROOT='/test',319 SESSION_COOKIE_DOMAIN='.example.com',320 SESSION_COOKIE_HTTPONLY=False,321 SESSION_COOKIE_SECURE=True,322 SESSION_COOKIE_SAMESITE='Strict',323 SESSION_COOKIE_PATH='/'324 )325326 @app.route('/')
6 unmodified lines
333 assert 'domain=.example.com' in cookie334 assert 'path=/' in cookie335 assert 'secure' in cookie336 assert 'httponly' not in cookie337 assert 'samesite' in cookie338339340def test_session_using_samesite_attribute(app, client):341 app.config.update(342 SERVER_NAME='www.example.com:8080',343 APPLICATION_ROOT='/test',344 SESSION_COOKIE_DOMAIN='.example.com',345 SESSION_COOKIE_HTTPONLY=False,346 SESSION_COOKIE_SECURE=True,347 SESSION_COOKIE_SAMESITE='anyvalue',348 SESSION_COOKIE_PATH='/'349 )350351 @app.route('/')352 def index():353 flask.session['testing'] = 42354 return 'Hello World'355356 # assert excption when samesite is not set to 'Strict', 'Lax' or None357 with pytest.raises(ValueError):358 rv = client.get('/', 'http://www.example.com:8080/test/')359360 # assert the samesite flag is not set in the cookie, when set to None361 app.config.update(SESSION_COOKIE_SAMESITE=None)362 rv = client.get('/', 'http://www.example.com:8080/test/')363 cookie = rv.headers['set-cookie'].lower()364 assert 'samesite' not in cookie365366 app.config.update(SESSION_COOKIE_SAMESITE='Strict')367 rv = client.get('/', 'http://www.example.com:8080/test/')368 cookie = rv.headers['set-cookie'].lower()369 assert 'samesite=strict' in cookie370371 app.config.update(SESSION_COOKIE_SAMESITE='Lax')372 rv = client.get('/', 'http://www.example.com:8080/test/')373 cookie = rv.headers['set-cookie'].lower()374 assert 'samesite=lax' in cookie375376def test_session_localhost_warning(recwarn, app, client):377 app.config.update(378 SERVER_NAME='localhost:5000',379 )
1522 unmodified lines