Skip to content

bpo-29613: Added support for SameSite cookies #6413

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

Merged
merged 11 commits into from
Apr 7, 2018
Merged
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
8 changes: 8 additions & 0 deletions Doc/library/http.cookies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@ Morsel Objects
* ``secure``
* ``version``
* ``httponly``
* ``samesite``

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

The attribute :attr:`samesite` specifies that the browser is not allowed to
send the cookie along with cross-site requests. This helps to mitigate CSRF
attacks. Valid values for this attribute are "Strict" and "Lax".

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

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

.. versionchanged:: 3.8
Added support for the :attr:`samesite` attribute.


.. attribute:: Morsel.value

Expand Down
1 change: 1 addition & 0 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class Morsel(dict):
"secure" : "Secure",
"httponly" : "HttpOnly",
"version" : "Version",
"samesite" : "SameSite",
}

_flags = {'secure', 'httponly'}
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ def test_set_secure_httponly_attrs(self):
self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure')

def test_samesite_attrs(self):
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
for val in samesite_values:
with self.subTest(val=val):
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
C['Customer']['samesite'] = val
self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; SameSite=%s' % val)

C = cookies.SimpleCookie()
C.load('Customer="WILL_E_COYOTE"; SameSite=%s' % val)
self.assertEqual(C['Customer']['samesite'], val)

def test_secure_httponly_false_if_not_present(self):
C = cookies.SimpleCookie()
C.load('eggs=scrambled; Path=/bacon')
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ Varun Sharma
Daniel Shaulov
Vlad Shcherbina
Justin Sheehy
Akash Shende
Charlie Shepherd
Bruce Sherwood
Alexander Shigin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added support for the ``SameSite`` cookie flag to the ``http.cookies``
module.