Skip to content

Commit b60a46d

Browse files
alexamol-
authored andcommitted
Fixed #158 -- set SameSite=Lax on session cookies by default
1 parent d35cf7e commit b60a46d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

beaker/cookie.py

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
sys.version_info >= (3, 4, 3)
1212
)
1313

14+
# Add support for the SameSite attribute (obsolete when PY37 is unsupported).
15+
http_cookies.Morsel._reserved.setdefault('samesite', 'SameSite')
16+
1417

1518
# Adapted from Django.http.cookies and always enabled the bad_cookies
1619
# behaviour to cope with any invalid cookie key while keeping around

beaker/session.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class Session(dict):
127127
to keep backward compatibility with sessions generated before 1.8.0
128128
set this to 48.
129129
:param crypto_type: encryption module to use
130+
:param samesite: SameSite value for the cookie -- should be either 'Lax',
131+
'Strict', or None.
130132
"""
131133
def __init__(self, request, id=None, invalidate_corrupt=False,
132134
use_cookies=True, type=None, data_dir=None,
@@ -135,7 +137,7 @@ def __init__(self, request, id=None, invalidate_corrupt=False,
135137
data_serializer='pickle', secret=None,
136138
secure=False, namespace_class=None, httponly=False,
137139
encrypt_key=None, validate_key=None, encrypt_nonce_bits=DEFAULT_NONCE_BITS,
138-
crypto_type='default',
140+
crypto_type='default', samesite='Lax',
139141
**namespace_args):
140142
if not type:
141143
if data_dir:
@@ -178,6 +180,7 @@ def __init__(self, request, id=None, invalidate_corrupt=False,
178180
self.secret = secret
179181
self.secure = secure
180182
self.httponly = httponly
183+
self.samesite = samesite
181184
self.encrypt_key = encrypt_key
182185
self.validate_key = validate_key
183186
self.encrypt_nonce_size = get_nonce_size(encrypt_nonce_bits)
@@ -246,6 +249,8 @@ def _set_cookie_values(self, expires=None):
246249
self.cookie[self.key]['domain'] = self._domain
247250
if self.secure:
248251
self.cookie[self.key]['secure'] = True
252+
if self.samesite:
253+
self.cookie[self.key]['samesite'] = self.samesite
249254
self._set_cookie_http_only()
250255
self.cookie[self.key]['path'] = self._path
251256

@@ -556,13 +561,15 @@ class CookieSession(Session):
556561
otherwise invalid data will cause an exception.
557562
:type invalidate_corrupt: bool
558563
:param crypto_type: The crypto module to use.
564+
:param samesite: SameSite value for the cookie -- should be either 'Lax',
565+
'Strict', or None.
559566
"""
560567
def __init__(self, request, key='beaker.session.id', timeout=None,
561568
save_accessed_time=True, cookie_expires=True, cookie_domain=None,
562569
cookie_path='/', encrypt_key=None, validate_key=None, secure=False,
563570
httponly=False, data_serializer='pickle',
564571
encrypt_nonce_bits=DEFAULT_NONCE_BITS, invalidate_corrupt=False,
565-
crypto_type='default',
572+
crypto_type='default', samesite='Lax',
566573
**kwargs):
567574

568575
self.crypto_module = get_crypto_module(crypto_type)
@@ -582,6 +589,7 @@ def __init__(self, request, key='beaker.session.id', timeout=None,
582589
self.request['set_cookie'] = False
583590
self.secure = secure
584591
self.httponly = httponly
592+
self.samesite = samesite
585593
self._domain = cookie_domain
586594
self._path = cookie_path
587595
self.invalidate_corrupt = invalidate_corrupt

0 commit comments

Comments
 (0)