Add SameSite support
This commit is contained in:
+9
-1
@@ -1075,6 +1075,8 @@ class Session(Storage):
|
|||||||
scookies['HttpOnly'] = True
|
scookies['HttpOnly'] = True
|
||||||
if self._secure:
|
if self._secure:
|
||||||
scookies['secure'] = True
|
scookies['secure'] = True
|
||||||
|
if self._same_site:
|
||||||
|
scookies['samesite'] = self._same_site
|
||||||
|
|
||||||
def clear_session_cookies(self):
|
def clear_session_cookies(self):
|
||||||
request = current.request
|
request = current.request
|
||||||
@@ -1153,6 +1155,12 @@ class Session(Storage):
|
|||||||
def secure(self):
|
def secure(self):
|
||||||
self._secure = True
|
self._secure = True
|
||||||
|
|
||||||
|
def samesite(self, mode='Lax'):
|
||||||
|
if 'samesite' not in Cookie.Morsel._reserved:
|
||||||
|
# Python version 3.7 and lower needs this
|
||||||
|
Cookie.Morsel._reserved['samesite'] = 'SameSite'
|
||||||
|
self._same_site = mode
|
||||||
|
|
||||||
def forget(self, response=None):
|
def forget(self, response=None):
|
||||||
self._close(response)
|
self._close(response)
|
||||||
self._forget = True
|
self._forget = True
|
||||||
@@ -1180,7 +1188,7 @@ class Session(Storage):
|
|||||||
|
|
||||||
def _unchanged(self, response):
|
def _unchanged(self, response):
|
||||||
if response.session_new:
|
if response.session_new:
|
||||||
internal = ['_last_timestamp', '_secure', '_start_timestamp']
|
internal = ['_last_timestamp', '_secure', '_start_timestamp', '_same_site']
|
||||||
for item in self.keys():
|
for item in self.keys():
|
||||||
if item not in internal:
|
if item not in internal:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -231,6 +231,24 @@ class testResponse(unittest.TestCase):
|
|||||||
cookie = str(current.response.cookies)
|
cookie = str(current.response.cookies)
|
||||||
self.assertTrue('httponly' not in cookie.lower())
|
self.assertTrue('httponly' not in cookie.lower())
|
||||||
|
|
||||||
|
def test_cookies_samesite(self):
|
||||||
|
current = setup_clean_session()
|
||||||
|
current.session._fixup_before_save()
|
||||||
|
cookie = str(current.response.cookies)
|
||||||
|
self.assertTrue('samesite' not in cookie.lower())
|
||||||
|
|
||||||
|
current = setup_clean_session()
|
||||||
|
current.session.samesite()
|
||||||
|
current.session._fixup_before_save()
|
||||||
|
cookie = str(current.response.cookies)
|
||||||
|
self.assertTrue('samesite=lax' in cookie.lower())
|
||||||
|
|
||||||
|
current = setup_clean_session()
|
||||||
|
current.session.samesite('Strict')
|
||||||
|
current.session._fixup_before_save()
|
||||||
|
cookie = str(current.response.cookies)
|
||||||
|
self.assertTrue('samesite=strict' in cookie.lower())
|
||||||
|
|
||||||
def test_include_meta(self):
|
def test_include_meta(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.meta[u'web2py'] = 'web2py'
|
response.meta[u'web2py'] = 'web2py'
|
||||||
|
|||||||
Reference in New Issue
Block a user