Skip to content

Commit 5bbde12

Browse files
committed
Added support for SameSite cookie option for CSRF prevention
This was an easy addon compared to the complexity of the CSRF nonce support. It only works in chromium browsers (Chrome, Opera...) at the moment. But there is recent activity on implementing it in firefox. Who know when edge/ie will adopt it. So csrf nonce and header analysis will be needed for a while.
1 parent 9fd22d5 commit 5bbde12

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ Features:
188188
Requiring enforcement will need some changes to
189189
templates. Support for protecting xmlrpc endpoint not well
190190
tested. See ``upgrading.txt``. (John Rouillard)
191+
- Added support for using the SameSite cookie option on the
192+
session cookie. Default is lax, but there is a settable
193+
option in config.ini file to change to strict or
194+
suppress it entirely. See ``upgrading.txt``. (John Rouillard)
191195

192196
Fixed:
193197

doc/upgrading.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ It is suggested that you change your templates so every form
101101
has an @csrf field and change the setting to 'required' for
102102
the csrf_enforce_token.
103103

104+
Support for SameSite cookie option for session cookie
105+
-----------------------------------------------------
106+
107+
Support for serving the session cookie using the SameSite cookie option
108+
has been added. By default it is set to lax to provide a better user
109+
experience. But this can be changes to strict or the option can be
110+
removed entirely.
111+
112+
Using the process for merging config.ini changes described in
113+
`Cross Site Request Forgery Detection Added`_ you can add the
114+
``samesite_cookie_setting`` to the ``[web]`` section of the config
115+
file.
116+
104117
Fix for path traversal changes template resolution
105118
--------------------------------------------------
106119

roundup/cgi/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,9 @@ def header(self, headers=None, response=None):
19811981
# mark as secure if https, see issue2550689
19821982
if self.secure:
19831983
cookie += " secure;"
1984+
ssc = self.db.config['WEB_SAMESITE_COOKIE_SETTING']
1985+
if ssc != "None":
1986+
cookie += " SameSite=%s;"%ssc
19841987
# prevent theft of session cookie, see issue2550689
19851988
cookie += " HttpOnly;"
19861989
headers.append(('Set-Cookie', cookie))

roundup/configuration.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ def str2value(self, value):
306306
else:
307307
raise OptionValueError(self, value, self.class_description)
308308

309+
class SameSiteSettingOption(Option):
310+
311+
"""How should the SameSite cookie setting be set: strict, lax
312+
or should it not be added (none)"""
313+
314+
class_description = "Allowed values: Strict, Lax, None"
315+
316+
def str2value(self, value):
317+
_val = value.lower()
318+
if _val in ("strict", "lax", "none"):
319+
return _val.capitalize()
320+
else:
321+
raise OptionValueError(self, value, self.class_description)
322+
309323
class EmailBodyOption(Option):
310324

311325
"""When to replace message body or strip quoting: always, never or for new items only"""
@@ -646,6 +660,15 @@ def str2value(self, value):
646660
"variables supplied by your web server (in that order).\n"
647661
"Set this option to 'no' if you do not wish to use HTTP Basic\n"
648662
"Authentication in your web interface."),
663+
(SameSiteSettingOption, 'samesite_cookie_setting', "Lax",
664+
"""Set the mode of the SameSite cookie option for
665+
the session cookie. Choices are 'Lax' or
666+
'Strict'. 'None' can be used to suppress the
667+
option. Strict mode provides additional security
668+
against CSRF attacks, but may confuse users who
669+
are logged into roundup and open a roundup link
670+
from a source other than roundup (e.g. link in
671+
email)."""),
649672
(CsrfSettingOption, 'csrf_enforce_token', "yes",
650673
"""How do we deal with @csrf fields in posted forms.
651674
Set this to 'required' to block the post and notify

0 commit comments

Comments
 (0)