Skip to content

Commit c87eb09

Browse files
authored
bpo-29613: Added support for SameSite cookies (GH-6413)
* bpo-29613: Added support for SameSite cookies Implemented as per draft https://tools.ietf.org/html/draft-west-first-party-cookies-07 * Documented SameSite And suggestions by members. * Missing space :( * Updated News and contributors * Added version changed details. * Fix in documentation * fix in documentation * Clubbed test cases for same attribute into single. * Updates * Style nits + expand tests * review feedback
1 parent 1d80a56 commit c87eb09

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

Doc/library/http.cookies.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,16 @@ Morsel Objects
137137
* ``secure``
138138
* ``version``
139139
* ``httponly``
140+
* ``samesite``
140141

141142
The attribute :attr:`httponly` specifies that the cookie is only transferred
142143
in HTTP requests, and is not accessible through JavaScript. This is intended
143144
to mitigate some forms of cross-site scripting.
144145

146+
The attribute :attr:`samesite` specifies that the browser is not allowed to
147+
send the cookie along with cross-site requests. This helps to mitigate CSRF
148+
attacks. Valid values for this attribute are "Strict" and "Lax".
149+
145150
The keys are case-insensitive and their default value is ``''``.
146151

147152
.. versionchanged:: 3.5
@@ -153,6 +158,9 @@ Morsel Objects
153158
:attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for
154159
setting them.
155160

161+
.. versionchanged:: 3.8
162+
Added support for the :attr:`samesite` attribute.
163+
156164

157165
.. attribute:: Morsel.value
158166

Lib/http/cookies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class Morsel(dict):
281281
"secure" : "Secure",
282282
"httponly" : "HttpOnly",
283283
"version" : "Version",
284+
"samesite" : "SameSite",
284285
}
285286

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

Lib/test/test_http_cookies.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ def test_set_secure_httponly_attrs(self):
121121
self.assertEqual(C.output(),
122122
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure')
123123

124+
def test_samesite_attrs(self):
125+
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
126+
for val in samesite_values:
127+
with self.subTest(val=val):
128+
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
129+
C['Customer']['samesite'] = val
130+
self.assertEqual(C.output(),
131+
'Set-Cookie: Customer="WILE_E_COYOTE"; SameSite=%s' % val)
132+
133+
C = cookies.SimpleCookie()
134+
C.load('Customer="WILL_E_COYOTE"; SameSite=%s' % val)
135+
self.assertEqual(C['Customer']['samesite'], val)
136+
124137
def test_secure_httponly_false_if_not_present(self):
125138
C = cookies.SimpleCookie()
126139
C.load('eggs=scrambled; Path=/bacon')

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,7 @@ Varun Sharma
14611461
Daniel Shaulov
14621462
Vlad Shcherbina
14631463
Justin Sheehy
1464+
Akash Shende
14641465
Charlie Shepherd
14651466
Bruce Sherwood
14661467
Alexander Shigin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added support for the ``SameSite`` cookie flag to the ``http.cookies``
2+
module.

0 commit comments

Comments
 (0)