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
Provenance
- Commit
382b135- Tree
5a551d5- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
db5735c
4 files changed+32 added−29 removed
modified:docs/config.rst+8 added-4 removed
208 unmodified lines209 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.213+ Restrict how cookies are sent with requests from external sites. Can214+ be set to ``'Lax'`` (recommended) or ``'Strict'``.215+ See :ref:`security-cookie`.216 217 Default: ``None``218 219+ .. versionadded:: 1.0220+221 .. py:data:: PERMANENT_SESSION_LIFETIME222 223 If ``session.permanent`` is true, the cookie's expiration will be set this224 number of seconds in the future. Can either be a145 unmodified lines370 ``SESSION_REFRESH_EACH_REQUEST``, ``TEMPLATES_AUTO_RELOAD``,371 ``LOGGER_HANDLER_POLICY``, ``EXPLAIN_TEMPLATE_LOADING``372 373 .. versionchanged:: 1.0372-374 ``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See375 :ref:`logging` for information about configuration.376 377 Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment378 variable.379 380+ Added :data:`SESSION_COOKIE_SAMESITE` to control the session381+ cookie's ``SameSite`` option.382 383+384 Configuring from Files385 ----------------------386 387 Configuration becomes more useful if you can store it in a separate file,262 unmodified linesmodified:docs/security.rst+13 added-5 removed
182 unmodified lines183 response.headers['X-XSS-Protection'] = '1; mode=block'184 185 - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection186 187+188+.. _security-cookie:189+190 Set-Cookie options191 ~~~~~~~~~~~~~~~~~~192 193 These options can be added to a ``Set-Cookie`` header to improve their2 unmodified lines196 197 - ``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.205 206 ::207 208 app.config.update(209 SESSION_COOKIE_SECURE=True,210 SESSION_COOKIE_HTTPONLY=True,206- SESSION_COOKIE_SAMESITE='Strict'211+ SESSION_COOKIE_SAMESITE='Lax',212 )213 209- response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Strict')214+ response.set_cookie('username', 'flask', secure=True, httponly=True, samesite='Lax')215 216 Specifying ``Expires`` or ``Max-Age`` options, will remove the cookie after217 the given time, or the current time plus the age, respectively. If neither218 option is set, the cookie will be removed when the browser is closed. ::24 unmodified lines243 244 - https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies245 - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie246 247+.. _samesite_support: https://caniuse.com/#feat=same-site-cookie-attribute248+249+250 HTTP Public Key Pinning (HPKP)251 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~252 253 This tells the browser to authenticate with the server using only the specific7 unmodified linesmodified:flask/sessions.py+3 added-3 removed
248 unmodified lines249 """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.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']258 259 def get_expiration_time(self, app, session):125 unmodified linesmodified:tests/test_basic.py+8 added-17 removed
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',322+ SESSION_COOKIE_SAMESITE='Lax',323 SESSION_COOKIE_PATH='/'324 )325 326 @app.route('/')10 unmodified lines337 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-341 @app.route('/')342 def index():343 flask.session['testing'] = 42344 return 'Hello World'345 356- # assert excption when samesite is not set to 'Strict', 'Lax' or None346+ app.config.update(SESSION_COOKIE_SAMESITE='invalid')347+348 with pytest.raises(ValueError):358- rv = client.get('/', 'http://www.example.com:8080/test/')349+ client.get('/')350 360- # 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 cookie355 356 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 cookie360 361 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 cookie365 366+367 def test_session_localhost_warning(recwarn, app, client):368 app.config.update(369 SERVER_NAME='localhost:5000',370 )1522 unmodified lines