Skip to content

Commit

Permalink
Merge pull request #72 from EcrinDe/2.13
Browse files Browse the repository at this point in the history
Add support for optional 'SameSite' cookie attribute
  • Loading branch information
tseaver committed Sep 15, 2016
2 parents 99a37fe + 1c65616 commit 1057219
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ http://docs.zope.org/zope2/
- Removed docstrings from some methods to avoid publishing them. From
Products.PloneHotfix20160419. [maurits]

- Add support to SameSite cookie in ``ZPublisher.HTTPResponse``:
https://tools.ietf.org/html/draft-west-first-party-cookies-07


2.13.24 (2016-02-29)
--------------------
Expand Down
6 changes: 6 additions & 0 deletions src/ZPublisher/HTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,12 @@ def _cookie_list(self):
# and block read/write access via JavaScript
elif name == 'http_only' and v:
cookie = '%s; HTTPOnly' % cookie
# Some browsers recognize the SameSite cookie attribute
# and do not send the cookie along with cross-site requests
# providing some protection against CSRF attacks
# https://tools.ietf.org/html/draft-west-first-party-cookies-07
elif name == 'same_site':
cookie = '%s; SameSite=%s' % (cookie, v)
cookie_list.append(('Set-Cookie', cookie))

# Should really check size of cookies here!
Expand Down
13 changes: 13 additions & 0 deletions src/ZPublisher/tests/testHTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ def test_setCookie_w_httponly_false_value(self):
self.assertEqual(len(cookie_list), 1)
self.assertEqual(cookie_list[0], ('Set-Cookie', 'foo="bar"'))

def test_setCookie_w_same_site(self):
response = self._makeOne()
response.setCookie('foo', 'bar', same_site='Strict')
cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('same_site'), 'Strict')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list()
self.assertEqual(len(cookies), 1)
self.assertEqual(cookies[0],
('Set-Cookie', 'foo="bar"; SameSite=Strict'))

def test_setCookie_unquoted(self):
response = self._makeOne()
response.setCookie('foo', 'bar', quoted=False)
Expand Down

0 comments on commit 1057219

Please sign in to comment.