Make SameSite=Lax the default for all web2py apps
This commit is contained in:
1 parent
928fd364cf
commit
11b441b777
2 files changed
+16
-10
No files matched your search
+8
-3
@@ -1075,7 +1075,15 @@ 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 is None:
|
||||||
|
# Using SameSite Lax Mode is the default
|
||||||
|
# You actually have to call session.samesite(False) if you really
|
||||||
|
# dont want the extra protection provided by the SameSite header
|
||||||
|
self._same_site = 'Lax'
|
||||||
if self._same_site:
|
if self._same_site:
|
||||||
|
if 'samesite' not in Cookie.Morsel._reserved:
|
||||||
|
# Python version 3.7 and lower needs this
|
||||||
|
Cookie.Morsel._reserved['samesite'] = 'SameSite'
|
||||||
scookies['samesite'] = self._same_site
|
scookies['samesite'] = self._same_site
|
||||||
|
|
||||||
def clear_session_cookies(self):
|
def clear_session_cookies(self):
|
||||||
@@ -1156,9 +1164,6 @@ class Session(Storage):
|
|||||||
self._secure = True
|
self._secure = True
|
||||||
|
|
||||||
def samesite(self, mode='Lax'):
|
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
|
self._same_site = mode
|
||||||
|
|
||||||
def forget(self, response=None):
|
def forget(self, response=None):
|
||||||
|
|||||||
@@ -232,17 +232,18 @@ class testResponse(unittest.TestCase):
|
|||||||
self.assertTrue('httponly' not in cookie.lower())
|
self.assertTrue('httponly' not in cookie.lower())
|
||||||
|
|
||||||
def test_cookies_samesite(self):
|
def test_cookies_samesite(self):
|
||||||
|
# Test Lax is the default mode
|
||||||
current = setup_clean_session()
|
current = setup_clean_session()
|
||||||
current.session._fixup_before_save()
|
current.session._fixup_before_save()
|
||||||
cookie = str(current.response.cookies)
|
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())
|
self.assertTrue('samesite=lax' in cookie.lower())
|
||||||
|
# Test you can disable samesite
|
||||||
|
current = setup_clean_session()
|
||||||
|
current.session.samesite(False)
|
||||||
|
current.session._fixup_before_save()
|
||||||
|
cookie = str(current.response.cookies)
|
||||||
|
self.assertTrue('samesite' not in cookie.lower())
|
||||||
|
# Test you can change mode
|
||||||
current = setup_clean_session()
|
current = setup_clean_session()
|
||||||
current.session.samesite('Strict')
|
current.session.samesite('Strict')
|
||||||
current.session._fixup_before_save()
|
current.session._fixup_before_save()
|
||||||
|
|||||||
Reference in new issue
Block a user