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
Provenance
- Commit
a1d9ebe- Tree
40a0cda- Author
Fadhel_Chaabane <fadhel.chaabane@hmhco.com>- Committer
Fadhel_Chaabane <fadhel.chaabane@hmhco.com>- Parent
22708b0
5 files changed+61 added−4 removed
modified:docs/config.rst+8 added-1 removed
206 unmodified lines207 sense.208 209 Default: ``False``210 211+.. py:data:: SESSION_COOKIE_SAMESITE212+213+ 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.216+217+ Default: ``None``218+219 .. py:data:: PERMANENT_SESSION_LIFETIME220 221 If ``session.permanent`` is true, the cookie's expiration will be set this222 number of seconds in the future. Can either be a419 unmodified lines642 643 # 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 lines194 - ``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"200 201 ::202 203 app.config.update(204 SESSION_COOKIE_SECURE=True,205 SESSION_COOKIE_HTTPONLY=True,206+ SESSION_COOKIE_SAMESITE='Strict'207 )208 207- response.set_cookie('username', 'flask', secure=True, httponly=True)209+ response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Strict')210 211 Specifying ``Expires`` or ``Max-Age`` options, will remove the cookie after212 the given time, or the current time plus the age, respectively. If neither213 option is set, the cookie will be removed when the browser is closed. ::39 unmodified linesmodified:flask/app.py+1 added
282 unmodified lines283 '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 linesmodified:flask/sessions.py+10 added-1 removed
247 unmodified lines248 just returns the value of the ``SESSION_COOKIE_SECURE`` setting.249 """250 return app.config['SESSION_COOKIE_SECURE']251 252+ 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']258+259 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 session105 unmodified lines368 return369 370 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 line378 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 lines318 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 )325 326 @app.route('/')6 unmodified lines333 assert 'domain=.example.com' in cookie334 assert 'path=/' in cookie335 assert 'secure' in cookie336 assert 'httponly' not in cookie337+ assert 'samesite' in cookie338 339 340+def 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+ )350+351+ @app.route('/')352+ def index():353+ flask.session['testing'] = 42354+ return 'Hello World'355+356+ # 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/')359+360+ # 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 cookie365+366+ 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 cookie370+371+ 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 cookie375+376 def test_session_localhost_warning(recwarn, app, client):377 app.config.update(378 SERVER_NAME='localhost:5000',379 )1522 unmodified lines