Skip to content

Enable CSRF protection - #219

Merged
SteRiccio merged 3 commits into
masterfrom
fix/csrf
Nov 10, 2026
Merged

SteRiccio merged 3 commits into
masterfrom
fix/csrf

Conversation

@SteRiccio

Copy link
Copy Markdown
Member

CSRF protection was disabled. It is now enabled with a cookie token repository (XSRF-TOKEN cookie, sent back as X-XSRF-TOKEN header or _csrf parameter).

  • React app: AbstractService sends the header on every non-GET request.
  • Legacy jQuery pages: Collect.AbstractService.send sends the header; OF.csrfToken() helper.
  • Login page: the form sends the token (also when auto-submitted with the default credentials).
  • Collect Earth balloon preview (Survey Designer): sends the header only when running inside Collect.
  • Not checked: /zkau/** (ZK validates requests against the session desktop id), /ws/** (SockJS transports, no client message handlers) and requests with an Authorization header (HTTP Basic clients such as Collect Mobile; browsers cannot add it to cross-site requests).
  • Dev mode CORS allows the X-XSRF-TOKEN header.

Note: a rejected request currently shows as 404, because the 403 error page (/error.html) is not served (pre-existing).

Test: CsrfProtectionMatcherTest. Checked in a running instance: login, record editing, designer, data cleansing; POSTs without token are rejected.

Spring Security CSRF protection was disabled, so a malicious site could make
a logged-in user's browser call any state-changing endpoint (e.g. create an
admin user, delete a survey).

- The token is kept in a JS-readable XSRF-TOKEN cookie (CookieCsrfTokenRepository);
  Spring Security 5.8 with the XML config loads it on every request, so the
  cookie is issued with the login page and the app pages, and the raw value is
  accepted in the X-XSRF-TOKEN header or in the _csrf parameter.
- React frontend (AbstractService), legacy jQuery pages (Collect.AbstractService),
  login form (also when auto-submitted with the default credentials) and the
  Collect Earth balloon preview send the token.
- Not checked: /zkau (ZK validates its own random desktop ids), /ws (SockJS
  transports cannot send headers; no message handlers) and requests with an
  Authorization header (HTTP Basic clients like Collect Mobile; browsers cannot
  add it to cross-site requests).
- Dev mode CORS allows the X-XSRF-TOKEN header.
@sonarqubecloud

Copy link
Copy Markdown

@SteRiccio SteRiccio self-assigned this Nov 10, 2026
@SteRiccio
SteRiccio merged commit 395883f into master Nov 10, 2026
3 checks passed
@SteRiccio
SteRiccio deleted the fix/csrf branch November 10, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant