Skip to content

Commit

Permalink
Merge PR Pylons#3298 of Pylons/pyramid into feature/session-samesite-lax
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalresistor committed Jun 6, 2018
2 parents 967a06c + 2c5e199 commit 0a998e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,6 @@ Contributors

- Hunter Senft-Grupp, 2018/05/14

- Junhak Lee, 2018/05/14
- Junhak Lee, 2018/05/14

- Alex Gaynor, 2018/05/24
16 changes: 16 additions & 0 deletions pyramid/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def BaseCookieSessionFactory(
domain=None,
secure=False,
httponly=False,
samesite=b'Lax',
timeout=1200,
reissue_time=0,
set_on_exception=True,
Expand Down Expand Up @@ -187,6 +188,9 @@ def BaseCookieSessionFactory(
Hide the cookie from Javascript by setting the 'HttpOnly' flag of the
session cookie. Default: ``False``.
``samesite``
The 'samesite' option of the session cookie. Default ``b'Lax'``.
``timeout``
A number of seconds of inactivity before a session times out. If
``None`` then the cookie never expires. This lifetime only applies
Expand Down Expand Up @@ -229,6 +233,7 @@ class CookieSession(dict):
_cookie_domain = domain
_cookie_secure = secure
_cookie_httponly = httponly
_cookie_samesite = samesite
_cookie_on_exception = set_on_exception
_timeout = timeout if timeout is None else int(timeout)
_reissue_time = reissue_time if reissue_time is None else int(reissue_time)
Expand Down Expand Up @@ -367,6 +372,7 @@ def _set_cookie(self, response):
domain=self._cookie_domain,
secure=self._cookie_secure,
httponly=self._cookie_httponly,
samesite=self._cookie_samesite,
)
return True

Expand All @@ -382,6 +388,7 @@ def UnencryptedCookieSessionFactoryConfig(
cookie_domain=None,
cookie_secure=False,
cookie_httponly=False,
cookie_samesite=b'Lax',
cookie_on_exception=True,
signed_serialize=signed_serialize,
signed_deserialize=signed_deserialize,
Expand Down Expand Up @@ -434,6 +441,9 @@ def UnencryptedCookieSessionFactoryConfig(
``cookie_httponly``
The 'httpOnly' flag of the session cookie.
``cookie_samesite``
The 'samesite' option of the session cookie. Default: ``b'Lax'``.
``cookie_on_exception``
If ``True``, set a session cookie even if an exception occurs
while rendering a view.
Expand Down Expand Up @@ -469,6 +479,7 @@ def dumps(self, appstruct):
domain=cookie_domain,
secure=cookie_secure,
httponly=cookie_httponly,
samesite=cookie_samesite,
timeout=timeout,
reissue_time=0, # to keep session.accessed == session.renewed
set_on_exception=cookie_on_exception,
Expand All @@ -491,6 +502,7 @@ def SignedCookieSessionFactory(
domain=None,
secure=False,
httponly=False,
samesite=b'Lax',
set_on_exception=True,
timeout=1200,
reissue_time=0,
Expand Down Expand Up @@ -553,6 +565,9 @@ def SignedCookieSessionFactory(
Hide the cookie from Javascript by setting the 'HttpOnly' flag of the
session cookie. Default: ``False``.
``samesite``
The 'samesite' option of the session cookie. Default: ``b'Lax'``.
``timeout``
A number of seconds of inactivity before a session times out. If
``None`` then the cookie never expires. This lifetime only applies
Expand Down Expand Up @@ -608,6 +623,7 @@ def SignedCookieSessionFactory(
domain=domain,
secure=secure,
httponly=httponly,
samesite=samesite,
timeout=timeout,
reissue_time=reissue_time,
set_on_exception=set_on_exception,
Expand Down
7 changes: 4 additions & 3 deletions pyramid/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ def test__set_cookie_options(self):
response = Response()
self.assertEqual(session._set_cookie(response), True)
cookieval = response.headerlist[-1][1]
val, domain, path, secure, httponly = [x.strip() for x in
cookieval.split(';')]
val, domain, path, secure, httponly, samesite = [x.strip() for x in
cookieval.split(';')]
self.assertTrue(val.startswith('abc='))
self.assertEqual(domain, 'Domain=localhost')
self.assertEqual(path, 'Path=/foo')
self.assertEqual(secure, 'secure')
self.assertEqual(httponly, 'HttpOnly')
self.assertEqual(samesite, 'SameSite=Lax')

def test_flash_default(self):
request = testing.DummyRequest()
Expand Down Expand Up @@ -503,7 +504,7 @@ def test_serialize_option(self):
expected_cookieval = dummy_signed_serialize(
(session.accessed, session.created, {'key': 'value'}), secret)
response = Response()
response.set_cookie('session', expected_cookieval)
response.set_cookie('session', expected_cookieval, samesite=b'Lax')
expected_cookie = response.headerlist[-1][1]
self.assertEqual(cookie, expected_cookie)

Expand Down

0 comments on commit 0a998e4

Please sign in to comment.