Skip to content

fix(csrf): mark requests as plaintext when SecureCookies=false#2

Open
syswave-dev wants to merge 1 commit intojonradoff:mainfrom
syswave-dev:fix/csrf-plaintext-http
Open

fix(csrf): mark requests as plaintext when SecureCookies=false#2
syswave-dev wants to merge 1 commit intojonradoff:mainfrom
syswave-dev:fix/csrf-plaintext-http

Conversation

@syswave-dev
Copy link
Copy Markdown

Fixes #1.

Problem

Login via /cm/login returns 403 Invalid or missing CSRF token on every POST when running locally with secure_cookies: false. Root cause is in the gorilla/csrf v1.7.x Origin validation behavior described in detail in #1.

Fix

Wrap the CSRF middleware to mark every request as plaintext via csrf.PlaintextHTTPRequest(r) before validation runs, but only when cfg.SecureCookies is false. Production deployments with SecureCookies=true are byte-for-byte unchanged.

Verification

  • Tested locally on Ubuntu 22.04, Go 1.24, MongoDB 7.0 (local replica set), LightCMS v6.0.2.
  • Before fix: every POST to /cm/login returns 403 with the CSRF error.
  • After fix: login succeeds, session cookie is set, admin UI loads normally.

Scope

  • Single-file change in cmd/server/main.go.
  • Behavior gate: if !cfg.SecureCookies { ... } — production with secure cookies is byte-for-byte unchanged.
  • Does not touch the gorilla/csrf dependency or any other middleware.

Notes

Bug analysis was assisted by Claude Code, which traced the gorilla/csrf middleware chain and identified the scheme-comparison logic. The fix itself is stock library API usage (csrf.PlaintextHTTPRequest).

gorilla/csrf v1.7.x defaults the assumed request scheme to https for
Origin validation. With secure_cookies: false (HTTP dev mode), the
browser sends Origin: http://localhost:8082 but the middleware compares
against https://localhost:8082, causing scheme mismatch and ErrBadOrigin
on every POST.

csrf.Secure(false) only controls the cookie's Secure flag, not the
Origin check. Wrapping the middleware to call csrf.PlaintextHTTPRequest(r)
before validation resolves the issue without affecting production
deployments where SecureCookies=true.

Verified locally: Ubuntu 22.04, Go 1.24, MongoDB 7.0 replica set,
LightCMS v6.0.2 dev mode. /cm/login POST now succeeds where it
previously returned 403.

Refs jonradoff#1
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.

CSRF validation fails on HTTP (localhost dev) with gorilla/csrf v1.7.3 - Origin scheme mismatch

1 participant