Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding SESSION_COOKIE_PARTITIONED #5499

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Version 3.1.0

- Provide a configuration option to control automatic option
responses. :pr:`5496`
- Added support for SESSION_COOKIE_PARTITIONED. :issue`5472`


Version 3.0.3
Expand Down
9 changes: 9 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ The following configuration values are used internally by Flask:

Default: ``False``

.. py:data:: SESSION_COOKIE_PARTITIONED

Browsers are well under way in phasing out unpartitioned third-party cookies,
so Cookies Having Independent Partitioned State, the Storage Access API,
and Related Website Sets will be the only way to read and write cookies
from cross-site contexts, such as iframes, when third-party cookies are blocked.

Default: ``False``

.. py:data:: SESSION_COOKIE_SAMESITE

Restrict how cookies are sent with requests from external sites. Can
Expand Down
1 change: 1 addition & 0 deletions docs/web-security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ They can be set on other cookies too.
::

app.config.update(
SESSION_COOKIE_PARTITIONED=True,
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax',
Expand Down
1 change: 1 addition & 0 deletions src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Flask(App):
"SESSION_COOKIE_PATH": None,
"SESSION_COOKIE_HTTPONLY": True,
"SESSION_COOKIE_SECURE": False,
"SESSION_COOKIE_PARTITIONED": False,
"SESSION_COOKIE_SAMESITE": None,
"SESSION_REFRESH_EACH_REQUEST": True,
"MAX_CONTENT_LENGTH": None,
Expand Down
6 changes: 6 additions & 0 deletions src/flask/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ def get_cookie_samesite(self, app: Flask) -> str | None:
"""
return app.config["SESSION_COOKIE_SAMESITE"] # type: ignore[no-any-return]

def get_cookie_partitioned(self, app: Flask) -> bool:
"""Returns True if the cookie should be partitioned. This currently
just returns the value of the ``SESSION_COOKIE_PARTITIONED`` setting.
"""
return app.config["SESSION_COOKIE_PARTITIONED"] # type: ignore[no-any-return]

def get_expiration_time(self, app: Flask, session: SessionMixin) -> datetime | None:
"""A helper method that returns an expiration date for the session
or ``None`` if the session is linked to the browser session. The
Expand Down
3 changes: 3 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def test_session_using_session_settings(app, client):
SESSION_COOKIE_DOMAIN=".example.com",
SESSION_COOKIE_HTTPONLY=False,
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_PARTITIONED=True,
SESSION_COOKIE_SAMESITE="Lax",
SESSION_COOKIE_PATH="/",
)
Expand All @@ -315,6 +316,7 @@ def clear():
assert "secure" in cookie
assert "httponly" not in cookie
assert "samesite" in cookie
assert "partitioned" in cookie

rv = client.get("/clear", "http://www.example.com:8080/test/")
cookie = rv.headers["set-cookie"].lower()
Expand All @@ -324,6 +326,7 @@ def clear():
assert "path=/" in cookie
assert "secure" in cookie
assert "samesite" in cookie
assert "partitioned" in cookie


def test_session_using_samesite_attribute(app, client):
Expand Down
Loading