-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-29613: Added support for SameSite cookies #214
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
Changes from all commits
ef83909
c8bc135
c064da4
364ea46
c3890a2
6ed3f3f
aa54e50
a6cbc20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,11 +137,17 @@ 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 browser is not allowed to send the | ||
cookie along with cross-site requests. This help to mitigate CSRF attacks. Valid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helps |
||
values for this attribute are "Strict" and "Lax". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the meaning of these values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explaining the values would be out of scope of the Python documentation. I think invalid values should be accepted, after all its browser's job to discard invalid values. Suppose, in future they proposed or added another value for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tim is correct that we need to add a test for invalid values. However, we need to decide on what we should do with invalid values first. I don't have time to do a research at the moment, but just a note that Firefox doesn't implement SameSite support yet: https://bugzilla.mozilla.org/show_bug.cgi?id=795346 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. However, chrome implemented SamSite. Right now only Chrome implemented this. https://bugs.chromium.org/p/chromium/issues/detail?id=459154 I messed with this branch :( Should I open new PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to fix the branch by rebasing and force pushing. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add below this line:
Thanks @alex for the clarification about this :) |
||
|
||
The keys are case-insensitive and their default value is ``''``. | ||
|
||
.. versionchanged:: 3.5 | ||
|
@@ -153,6 +159,9 @@ Morsel Objects | |
:attr:`~Morsel.coded_value` are read-only. Use :meth:`~Morsel.set` for | ||
setting them. | ||
|
||
.. versionchanged:: 3.7 | ||
Added support for :attr:`samesite` attribute. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the |
||
|
||
|
||
.. attribute:: Morsel.value | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,15 @@ 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use single quotes for consistency? |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Negative. It just an illusion i guess. Perfectly showing in my vim. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What Tim meant was that the second argument needs to be indented, like: self.assertEqual(C.output(),
'Set-Cookie: Customer="WILE_E_COYOTE"; SameSite=%s' % val) or even better: expected = f'Set-Cookie: Customer="WILE_E_COYOTE"; SameSite={val}'
self.assertEqual(C.output(), expected) |
||
|
||
def test_secure_httponly_false_if_not_present(self): | ||
C = cookies.SimpleCookie() | ||
C.load('eggs=scrambled; Path=/bacon') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "The samesite attribute" is better English (not sure if the pattern from the previous paragraph is commonly used).
"the browser" (add "the")