Skip to content
Myr

clean up samesite docs

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 db5735c · Browse files · 4 files+32 added−29 removed
This commit

State

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

Diff stats

4 files+32 added−29 removed

  • tests:+8 added−17 removed
  • other:+24 added−12 removed

4 files+32 added−29 removed

modified:docs/config.rst+8 added-4 removed
208 unmodified lines
209 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.213 Restrict how cookies are sent with requests from external sites. Can214 be set to ``'Lax'`` (recommended) or ``'Strict'``.215 See :ref:`security-cookie`.216217 Default: ``None``218219 .. versionadded:: 1.0220221.. py:data:: PERMANENT_SESSION_LIFETIME222223 If ``session.permanent`` is true, the cookie's expiration will be set this224 number of seconds in the future. Can either be a
145 unmodified lines
370 ``SESSION_REFRESH_EACH_REQUEST``, ``TEMPLATES_AUTO_RELOAD``,371 ``LOGGER_HANDLER_POLICY``, ``EXPLAIN_TEMPLATE_LOADING``372373.. versionchanged:: 1.0372374 ``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See375 :ref:`logging` for information about configuration.376377 Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment378 variable.379380 Added :data:`SESSION_COOKIE_SAMESITE` to control the session381 cookie's ``SameSite`` option.382383384Configuring from Files385----------------------386387Configuration becomes more useful if you can store it in a separate file,
262 unmodified lines
modified:docs/security.rst+13 added-5 removed
182 unmodified lines
183 response.headers['X-XSS-Protection'] = '1; mode=block'184185- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection186187188.. _security-cookie:189190Set-Cookie options191~~~~~~~~~~~~~~~~~~192193These options can be added to a ``Set-Cookie`` header to improve their
2 unmodified lines
196197- ``Secure`` limits cookies to HTTPS traffic only.198- ``HttpOnly`` protects the contents of cookies from being read with199 JavaScript.197- ``SameSite`` ensures that cookies can only be requested from the same198 domain that created them. There are two possible values for the same-site199 attribute: "Strict" and "Lax"200- ``SameSite`` restricts how cookies are sent with requests from201 external sites. Can be set to ``'Lax'`` (recommended) or ``'Strict'``.202 ``Lax`` prevents sending cookies with CSRF-prone requests from203 external sites, such as submitting a form. ``Strict`` prevents sending204 cookies with all external requests, including following regular links.205206::207208 app.config.update(209 SESSION_COOKIE_SECURE=True,210 SESSION_COOKIE_HTTPONLY=True,206 SESSION_COOKIE_SAMESITE='Strict'211 SESSION_COOKIE_SAMESITE='Lax',212 )213209 response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Strict')214 response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Lax')215216Specifying ``Expires`` or ``Max-Age`` options, will remove the cookie after217the given time, or the current time plus the age, respectively. If neither218option is set, the cookie will be removed when the browser is closed. ::
24 unmodified lines
243244- https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies245- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie246247.. _samesite_support: https://caniuse.com/#feat=same-site-cookie-attribute248249250HTTP Public Key Pinning (HPKP)251~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~252253This tells the browser to authenticate with the server using only the specific
7 unmodified lines
modified:flask/sessions.py+3 added-3 removed
248 unmodified lines
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.253 """Return ``'Strict'`` or ``'Lax'`` if the cookie should use the254 ``SameSite`` attribute. This currently just returns the value of255 the :data:`SESSION_COOKIE_SAMESITE` setting.256 """257 return app.config['SESSION_COOKIE_SAMESITE']258259 def get_expiration_time(self, app, session):
125 unmodified lines
modified:tests/test_basic.py+8 added-17 removed
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',322 SESSION_COOKIE_SAMESITE='Lax',323 SESSION_COOKIE_PATH='/'324 )325326 @app.route('/')
10 unmodified lines
337 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 )350341 @app.route('/')342 def index():343 flask.session['testing'] = 42344 return 'Hello World'345356 # assert excption when samesite is not set to 'Strict', 'Lax' or None346 app.config.update(SESSION_COOKIE_SAMESITE='invalid')347348 with pytest.raises(ValueError):358 rv = client.get('/', 'http://www.example.com:8080/test/')349 client.get('/')350360 # assert the samesite flag is not set in the cookie, when set to None351 app.config.update(SESSION_COOKIE_SAMESITE=None)362 rv = client.get('/', 'http://www.example.com:8080/test/')352 rv = client.get('/')353 cookie = rv.headers['set-cookie'].lower()354 assert 'samesite' not in cookie355356 app.config.update(SESSION_COOKIE_SAMESITE='Strict')367 rv = client.get('/', 'http://www.example.com:8080/test/')357 rv = client.get('/')358 cookie = rv.headers['set-cookie'].lower()359 assert 'samesite=strict' in cookie360361 app.config.update(SESSION_COOKIE_SAMESITE='Lax')372 rv = client.get('/', 'http://www.example.com:8080/test/')362 rv = client.get('/')363 cookie = rv.headers['set-cookie'].lower()364 assert 'samesite=lax' in cookie365366367def test_session_localhost_warning(recwarn, app, client):368 app.config.update(369 SERVER_NAME='localhost:5000',370 )
1522 unmodified lines