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

gh-112713 : Add support for 'partitioned' attribute in http.cookies #112714

Merged
merged 22 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5901f2e
Add support for 'partitioned' attribute in http.cookies
Dec 4, 2023
a6fe17a
Merge branch 'main' into add-cookies-partitioned-support
giles-v Dec 4, 2023
9175076
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 4, 2023
f20ba61
Fix invalid test name function
Dec 4, 2023
20ac30d
Merge branch 'main' into add-cookies-partitioned-support
gpshead Feb 14, 2024
1482ed7
Merge branch 'main' into add-cookies-partitioned-support
gpshead Feb 14, 2024
80df6e7
Wordsmith the docs and include a link to the spec.
gpshead Feb 14, 2024
21cf60e
Style: Don't retain unusual hand spacing.
gpshead Feb 14, 2024
b1f7cbd
News ReST improvements.
gpshead Feb 14, 2024
f6391d6
Add support for 'partitioned' attribute in http.cookies
Dec 4, 2023
14d9a5c
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 4, 2023
e622cda
Fix invalid test name function
Dec 4, 2023
1949596
Wordsmith the docs and include a link to the spec.
gpshead Feb 14, 2024
9f3c9b0
Style: Don't retain unusual hand spacing.
gpshead Feb 14, 2024
f03bdd9
News ReST improvements.
gpshead Feb 14, 2024
097fc45
Remove Path=/ mandatory requirement from the docs
Sep 9, 2024
de432da
Pull changes
Sep 9, 2024
c564125
Merge branch 'main' into add-cookies-partitioned-support
giles-v Sep 9, 2024
4fdecb5
Move morsel docs version to 3.1.4, and revert spacing changes
Dec 10, 2024
7dc5110
Merge upstream/main
Dec 10, 2024
bb95c15
Merge branch 'main' into add-cookies-partitioned-support
giles-v Jan 22, 2025
e9363fb
Merge branch 'main' into add-cookies-partitioned-support
giles-v Jan 24, 2025
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
16 changes: 16 additions & 0 deletions Doc/library/http.cookies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Morsel Objects
version
httponly
samesite
partitioned

The attribute :attr:`httponly` specifies that the cookie is only transferred
in HTTP requests, and is not accessible through JavaScript. This is intended
Expand All @@ -151,6 +152,18 @@ Morsel Objects
send the cookie along with cross-site requests. This helps to mitigate CSRF
attacks. Valid values for this attribute are "Strict" and "Lax".

The attribute :attr:`partitioned` indicates to user agents that these
cross-site cookies *should* only be available in the same top-level context
that the cookie was first set in. For this to be accepted by the user agent,
you **must** also set both ``Secure`` and ``Path=/``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the wording here to clarify that Secure is required... but the CHIPS spec doesn't explicitly say anything about Path= though Path=/ appears in all of its examples. What wording should be used regarding Path, I'm not sure how I've phrased this is wholly accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per privacycg/CHIPS#49, Path=/ is not needed. I'm going to push an update to this PR shortly.

In addition, it is recommended to use the ``__Host`` prefix when setting
partitioned cookies to make them bound to the hostname and not the
registrable domain. Read
`CHIPS (Cookies Having Independent Partitioned State)`_
for full details and examples.

.. _CHIPS (Cookies Having Independent Partitioned State): https://github.com/privacycg/CHIPS/blob/main/README.md

The keys are case-insensitive and their default value is ``''``.

.. versionchanged:: 3.5
Expand All @@ -165,6 +178,9 @@ Morsel Objects
.. versionchanged:: 3.8
Added support for the :attr:`samesite` attribute.

.. versionchanged:: 3.13
Added support for the :attr:`partitioned` attribute.
giles-v marked this conversation as resolved.
Show resolved Hide resolved


.. attribute:: Morsel.value

Expand Down
21 changes: 11 additions & 10 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,19 @@ class Morsel(dict):
# variant on the left to the appropriate traditional
# formatting on the right.
_reserved = {
"expires" : "expires",
"path" : "Path",
"comment" : "Comment",
"domain" : "Domain",
"max-age" : "Max-Age",
"secure" : "Secure",
"httponly" : "HttpOnly",
"version" : "Version",
"samesite" : "SameSite",
"expires": "expires",
"path": "Path",
"comment": "Comment",
"domain": "Domain",
"max-age": "Max-Age",
"secure": "Secure",
"httponly": "HttpOnly",
"version": "Version",
"samesite": "SameSite",
gpshead marked this conversation as resolved.
Show resolved Hide resolved
"partitioned": "Partitioned",
}

_flags = {'secure', 'httponly'}
_flags = {'secure', 'httponly', 'partitioned'}

def __init__(self):
# Set defaults
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def test_set_secure_httponly_attrs(self):
self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure')

def test_set_secure_httponly_partitioned_attrs(self):
gpshead marked this conversation as resolved.
Show resolved Hide resolved
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
C['Customer']['secure'] = True
C['Customer']['httponly'] = True
C['Customer']['partitioned'] = True
self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Partitioned; Secure')

def test_samesite_attrs(self):
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
for val in samesite_values:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for the ``Partitioned`` cookie flag in :mod:`http.cookies`.
Loading