Unverified Kaydet (Commit) c87eb09d authored tarafından Alex Gaynor's avatar Alex Gaynor Kaydeden (comit) GitHub

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
üst 1d80a561
...@@ -137,11 +137,16 @@ Morsel Objects ...@@ -137,11 +137,16 @@ Morsel Objects
* ``secure`` * ``secure``
* ``version`` * ``version``
* ``httponly`` * ``httponly``
* ``samesite``
The attribute :attr:`httponly` specifies that the cookie is only transferred The attribute :attr:`httponly` specifies that the cookie is only transferred
in HTTP requests, and is not accessible through JavaScript. This is intended in HTTP requests, and is not accessible through JavaScript. This is intended
to mitigate some forms of cross-site scripting. 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 ``''``. The keys are case-insensitive and their default value is ``''``.
.. versionchanged:: 3.5 .. versionchanged:: 3.5
...@@ -153,6 +158,9 @@ Morsel Objects ...@@ -153,6 +158,9 @@ Morsel Objects
:attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for :attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for
setting them. setting them.
.. versionchanged:: 3.8
Added support for the :attr:`samesite` attribute.
.. attribute:: Morsel.value .. attribute:: Morsel.value
......
...@@ -281,6 +281,7 @@ class Morsel(dict): ...@@ -281,6 +281,7 @@ class Morsel(dict):
"secure" : "Secure", "secure" : "Secure",
"httponly" : "HttpOnly", "httponly" : "HttpOnly",
"version" : "Version", "version" : "Version",
"samesite" : "SameSite",
} }
_flags = {'secure', 'httponly'} _flags = {'secure', 'httponly'}
......
...@@ -121,6 +121,19 @@ class CookieTests(unittest.TestCase): ...@@ -121,6 +121,19 @@ class CookieTests(unittest.TestCase):
self.assertEqual(C.output(), self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure') '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): def test_secure_httponly_false_if_not_present(self):
C = cookies.SimpleCookie() C = cookies.SimpleCookie()
C.load('eggs=scrambled; Path=/bacon') C.load('eggs=scrambled; Path=/bacon')
......
...@@ -1461,6 +1461,7 @@ Varun Sharma ...@@ -1461,6 +1461,7 @@ Varun Sharma
Daniel Shaulov Daniel Shaulov
Vlad Shcherbina Vlad Shcherbina
Justin Sheehy Justin Sheehy
Akash Shende
Charlie Shepherd Charlie Shepherd
Bruce Sherwood Bruce Sherwood
Alexander Shigin Alexander Shigin
......
Added support for the ``SameSite`` cookie flag to the ``http.cookies``
module.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment