Mercurial > p > roundup > code
diff roundup/cgi/templating.py @ 8575:b1024bf0d9f7
feature: add nonceless/tokenless CSRF protection
Add tokenless CSRF protection following:
https://words.filippo.io/csrf/
Must be enabled using use_tokenless_csrf_protection in config.ini. By
default it's off. If enabled the older csrf_* settings are ignored.
The allowed_api_origins setting is still used for Origin comparisons.
This should also improve performance as a nonce isn't required so
generating random nonce and saving it to the otks database is
eliminated.
doc/admin_guide.txt, doc/reference.txt doc/upgrading.txt
doc updates.
roundup/configuration.py
add use_tokenless_csrf_protection setting.
move allowed_api_origins directly after
use_tokenless_csrf_protection and before the older csrf_* settings.
It's used by both of them.
Rewrite description of allowed_api_origins as its applied to all
URLs with tokenless protection, not just API URLs.
roundup/anypy/urllib_.py
import urlsplit, it is used in new code.
urlparse() is less efficient and splits params out of the path
component.
Since Roundup doesn't require that params be split from the path. I
expect future patch will replace urlparse() with urlsplit() globally
and not need urlparse().
roundup/cgi/client.py
add handle_csrf_tokenless() and call from handle_csrf() if
use_tokenless_csrf_protection is enabled.
refactor code that expires csrf tokens when used with the wrong
methods (i.e. GET) into expire_exposed_keys(). Call same from
handle_csrf and handle_csrf_tokenless. Also improve logging if this
happens including both Referer and Origin headers if available.
Arguably we dont care about CSRF tokens exposed via
GET/HEAD/OPTIONS in the tokenless case, but this cleans them up in
case the admin has to switch back. At some future date we can
delete all the nonce based CSRF from 2018.
Update handle_csrf() docstring about calling/returning
handle_csrf_tokenless() when enabled. Call
expire_exposed_keys(method) if token is supplied with wrong method.
roundup/cgi/templating.py
disable nonce generation/save and always return "0" when
use_tokenless_csrf_protection enabled.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 19 Apr 2026 20:50:07 -0400 |
| parents | 6783a7f2b5e1 |
| children | 6d2b5132739e |
line wrap: on
line diff
--- a/roundup/cgi/templating.py Tue Apr 14 19:40:15 2026 -0400 +++ b/roundup/cgi/templating.py Sun Apr 19 20:50:07 2026 -0400 @@ -241,11 +241,19 @@ def anti_csrf_nonce(client, lifetime=None): ''' Create a nonce for defending against CSRF attack. - Then it stores the nonce, the session id for the user - and the user id in the one time key database for use - by the csrf validator that runs in the client::inner_main + If tokenless/nonceless CSRF is activated, return 0 and + continue. 0 should never be a value in the otks db, but even + if it is, it will never be checked if tokenless csrf is used. + + If the nonce is used, store the nonce, the session id for the + user and the user id in the one time key database for use by + the csrf validator that runs in the client::inner_main module/function. + ''' + if self.db.config['WEB_USE_TOKENLESS_CSRF_PROTECTION']: + return "0" + otks = client.db.getOTKManager() key = otks.getUniqueKey() # lifetime is in minutes.
