Skip to content

Commit 9abbb58

Browse files
giles-vGiles Coppblurb-it[bot]gpshead
authored
gh-112713 : Add support for 'partitioned' attribute in http.cookies (GH-112714)
* Add support for 'partitioned' attribute in http.cookies Co-authored-by: Giles Copp <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Gregory P. Smith [Google LLC] <[email protected]>
1 parent 3a3a6b8 commit 9abbb58

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

Doc/library/http.cookies.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Morsel Objects
142142
version
143143
httponly
144144
samesite
145+
partitioned
145146

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

155+
The attribute :attr:`partitioned` indicates to user agents that these
156+
cross-site cookies *should* only be available in the same top-level context
157+
that the cookie was first set in. For this to be accepted by the user agent,
158+
you **must** also set ``Secure``.
159+
160+
In addition, it is recommended to use the ``__Host`` prefix when setting
161+
partitioned cookies to make them bound to the hostname and not the
162+
registrable domain. Read
163+
`CHIPS (Cookies Having Independent Partitioned State)`_
164+
for full details and examples.
165+
166+
.. _CHIPS (Cookies Having Independent Partitioned State): https://github.com/privacycg/CHIPS/blob/main/README.md
167+
154168
The keys are case-insensitive and their default value is ``''``.
155169

156170
.. versionchanged:: 3.5
@@ -165,6 +179,9 @@ Morsel Objects
165179
.. versionchanged:: 3.8
166180
Added support for the :attr:`samesite` attribute.
167181

182+
.. versionchanged:: 3.14
183+
Added support for the :attr:`partitioned` attribute.
184+
168185

169186
.. attribute:: Morsel.value
170187

Lib/http/cookies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ class Morsel(dict):
264264
"httponly" : "HttpOnly",
265265
"version" : "Version",
266266
"samesite" : "SameSite",
267+
"partitioned": "Partitioned",
267268
}
268269

269270
_reserved_defaults = dict.fromkeys(_reserved, "")
270271

271-
_flags = {'secure', 'httponly'}
272+
_flags = {'secure', 'httponly', 'partitioned'}
272273

273274
def __init__(self):
274275
# Set defaults

Lib/test/test_http_cookies.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ def test_set_secure_httponly_attrs(self):
205205
self.assertEqual(C.output(),
206206
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure')
207207

208+
def test_set_secure_httponly_partitioned_attrs(self):
209+
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
210+
C['Customer']['secure'] = True
211+
C['Customer']['httponly'] = True
212+
C['Customer']['partitioned'] = True
213+
self.assertEqual(C.output(),
214+
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Partitioned; Secure')
215+
208216
def test_samesite_attrs(self):
209217
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
210218
for val in samesite_values:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for the ``Partitioned`` cookie flag in :mod:`http.cookies`.

0 commit comments

Comments
 (0)