[Bug 43782] New: GenerateCSRF caches token with global key causing cross-session CSRF 403 errors on Plack workers
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Bug ID: 43782 Summary: GenerateCSRF caches token with global key causing cross-session CSRF 403 errors on Plack workers Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: major Priority: P5 - low Component: Templates Assignee: oleonard@myacpl.org Reporter: johan@imcode.com QA Contact: testopia@bugs.koha-community.org Target Milestone: --- Bug 35955 (commit 108c955ea) introduced caching of the CSRF token in Koha::Template::Plugin::Koha::GenerateCSRF using Koha::Cache::Memory::Lite. The cache uses a static key "CSRF-TOKEN" shared across all sessions served by a Plack worker. Because Koha::Cache::Memory::Lite is a package-level in-process hash that is NOT flushed between requests (despite what its documentation says), the cached token from the first session persists and is returned to all subsequent sessions on the same worker process. When session B receives session A's token and submits a form, CSRF validation compares the token against session B's session_id — it was generated for session A — and returns 403. Symptoms: - Intermittent "Invalid CSRF token" 403 errors - Reproducible as "first request after idle period" (e.g. every morning) - Affects both intranet login and OPAC self-checkout (sco-main.pl) - Works on the second attempt (the second request gets the correct session's token because the old cached value is still returned — but a reload generates a new session and fresh token) The 8-hour CSRF_EXPIRY_HOURS (Bug 17110) made the window smaller. After Bug 42924 increased it to 168 hours, a worker running more than ~7 days could additionally serve a near-expired token with less than 1 hour remaining. Note: Bug 18124 correctly made CSRF tokens cryptographically session-specific. The cache layer introduced by Bug 35955 bypasses that protection. The issue can be hard to reproduce, so I have been using reports from customers to piece this together. -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Johan Larsson <johan@imcode.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Assignee|oleonard@myacpl.org |johan@imcode.com Patch complexity|--- |Small patch Status|NEW |Needs Signoff -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #1 from Johan Larsson <johan@imcode.com> --- Created attachment 207786 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=207786&action=edit Bug 43782: Fix GenerateCSRF cache key to be session-specific The GenerateCSRF template plugin (added by Bug 35955) cached the CSRF token under the global key 'CSRF-TOKEN', shared across all sessions served by the same Plack worker. Because Koha::Cache::Memory::Lite is a package-level in-process hash that persists across requests, the first session's token was returned to all subsequent sessions on the same worker, causing CSRF validation to fail with 403 errors. This patch: - Changes the cache key to 'CSRF-TOKEN-{session_id}' so each session gets its own cached token - Stores the token with a timestamp and rejects cached tokens with less than one hour of remaining lifetime (CSRF_EXPIRY_HOURS * 3600 - 3600 seconds) to prevent serving near-expired tokens Test plan: 1. Apply patch 2. prove t/db_dependent/Koha/Template/Plugin/Koha.t Signed-off-by: Johan Larsson <johlar@imcode.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 David Nind <david@davidnind.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Needs Signoff |Signed Off -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 David Nind <david@davidnind.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #207786|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #2 from David Nind <david@davidnind.com> --- Created attachment 207788 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=207788&action=edit Bug 43782: Fix GenerateCSRF cache key to be session-specific The GenerateCSRF template plugin (added by Bug 35955) cached the CSRF token under the global key 'CSRF-TOKEN', shared across all sessions served by the same Plack worker. Because Koha::Cache::Memory::Lite is a package-level in-process hash that persists across requests, the first session's token was returned to all subsequent sessions on the same worker, causing CSRF validation to fail with 403 errors. This patch: - Changes the cache key to 'CSRF-TOKEN-{session_id}' so each session gets its own cached token - Stores the token with a timestamp and rejects cached tokens with less than one hour of remaining lifetime (CSRF_EXPIRY_HOURS * 3600 - 3600 seconds) to prevent serving near-expired tokens Test plan: 1. Apply patch 2. prove t/db_dependent/Koha/Template/Plugin/Koha.t Signed-off-by: Johan Larsson <johlar@imcode.com> Signed-off-by: David Nind <david@davidnind.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Lisette Scheer <lisette@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- QA Contact|testopia@bugs.koha-communit |andrew@bywatersolutions.com |y.org | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Andrew Fuerste-Henry <andrew@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Signed Off |Passed QA -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Andrew Fuerste-Henry <andrew@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #207788|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #3 from Andrew Fuerste-Henry <andrew@bywatersolutions.com> --- Created attachment 207995 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=207995&action=edit Bug 43782: Fix GenerateCSRF cache key to be session-specific The GenerateCSRF template plugin (added by Bug 35955) cached the CSRF token under the global key 'CSRF-TOKEN', shared across all sessions served by the same Plack worker. Because Koha::Cache::Memory::Lite is a package-level in-process hash that persists across requests, the first session's token was returned to all subsequent sessions on the same worker, causing CSRF validation to fail with 403 errors. This patch: - Changes the cache key to 'CSRF-TOKEN-{session_id}' so each session gets its own cached token - Stores the token with a timestamp and rejects cached tokens with less than one hour of remaining lifetime (CSRF_EXPIRY_HOURS * 3600 - 3600 seconds) to prevent serving near-expired tokens Test plan: 1. Apply patch 2. prove t/db_dependent/Koha/Template/Plugin/Koha.t Signed-off-by: Johan Larsson <johlar@imcode.com> Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Version(s)| |26.11.00 released in| | --- Comment #4 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Pushed to main for 26.11 with consent from Pedro whilst he's on annual leave -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Passed QA |Pushed to main -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #5 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 208603 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=208603&action=edit Bug 43782: (RM follow-up) Return nothing from GenerateCSRF when there is no session Bug 43782 builds the cache key for the CSRF token from the session id, but GenerateCSRF is also rendered, through csrf-token.inc, in templates that have no sessionID in their stash. It then warned twice: Use of uninitialized value $session_id in concatenation (.) or string Use of uninitialized value $csrf_token in concatenation (.) or string which fails t/db_dependent/Koha/Plugins/TemplateIncludePathHook.t on its "no warnings" check, and cached a token under a session-less key. A CSRF token is bound to a session, and Koha::Token->generate_csrf already returns nothing when it is not given one, as GenerateCSRF did before bug 43782. Return early in the same way, before the cache key is built. Test plan: 1. prove t/db_dependent/Koha/Plugins/TemplateIncludePathHook.t => it passes (it failed with "not ok 6 - no warnings") 2. prove t/db_dependent/Koha/Template/Plugin/Koha.t => it passes, including the new 'GenerateCSRF - No session' subtest Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #6 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- RM follow-up pushed to fix a CI failure caused by this bug (attachment 208603, commit e386b282b60). t/db_dependent/Koha/Plugins/TemplateIncludePathHook.t failed on main with "not ok 6 - no warnings". GenerateCSRF builds its cache key from the session id, but it is also rendered, through csrf-token.inc, in templates that have no sessionID in their stash (that test renders such a template). It then warned twice ("Use of uninitialized value $session_id in concatenation" and the same for $csrf_token), which fails Test::NoWarnings, and it cached a token under a session-less key. A CSRF token is bound to a session, and Koha::Token->generate_csrf already returns nothing when it is not given one, as GenerateCSRF did before this bug. GenerateCSRF now returns early in the same way, before the cache key is built. The new subtest 'GenerateCSRF - No session' in t/db_dependent/Koha/Template/Plugin/Koha.t covers it; the tests added by this bug always set a session, so they did not cover this case. Test plan: 1. prove t/db_dependent/Koha/Plugins/TemplateIncludePathHook.t (it passes again) 2. prove t/db_dependent/Koha/Template/Plugin/Koha.t (it passes, including the new subtest) -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 Lucas Gass (lukeg) <lucas@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Version(s)|26.11.00 |26.11.00,26.05.06 released in| | Status|Pushed to main |Pushed to stable -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43782 --- Comment #7 from Lucas Gass (lukeg) <lucas@bywatersolutions.com> --- Nice work everyone! Pushed to 26.05.x for the upcoming 26.05.06 release. -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org