fix(backend): restrict cors allowlist to first-party hosts

Merged
LU
6mo ago
+84-5

Implements decision #2 of ADR 2026-06-05-csrf-posture.md (bundled). From the CSRF /research-and-decide on #1244.

What

Restricts the credentialed CORS allowlist to first-party hosts:

  • Drops *.replit.dev, *.repl.co, *.replit.app — multi-tenant dev platforms that should never be trusted with credentials: true.
  • Gates localhost / 127.0.0.1 to non-production only.
  • Keeps lucassantana.tech / luk-homeserver.com.br (+ subdomains) and the explicit configuredOrigins.

Why

Not a CSRF hole — the explicit SameSite=lax session cookie already blocks the cookie on cross-site requests, so a replit.dev page can't forge an authenticated mutation regardless of CORS. But allowing untrusted multi-tenant origins (and prod localhost) with credentials: true is a defense-in-depth weakness for credentialed cross-origin reads, surfaced by the CSRF review. Tightening costs nothing and shrinks the surface.

The companion decision — keep SameSite=lax, no CSRF token (the vector is already closed; strict would break Discord OAuth) — is recorded in the bundled ADR. The CodeQL js/missing-token-validation alert is being dismissed as mitigated with that ADR as evidence.

Verification

Backend type:check clean · full backend suite 994/994 pass (no CORS-origin test asserted the removed hosts).

@cubic-dev-ai please review.


Summary by cubic

Tightens backend CORS to only first-party hosts and gates localhost to non-production to reduce risk of credentialed cross-origin reads. Bundles an ADR that documents the CSRF posture (keep SameSite=lax; no CSRF token) and addresses the CodeQL alert in #1244.

  • Bug Fixes
    • Remove *.replit.dev, *.repl.co, *.replit.app from the credentialed CORS allowlist.
    • Allow localhost and 127.0.0.1 only when not in production.
    • Keep lucassantana.tech and luk-homeserver.com.br (and subdomains).
    • Add decisions/2026-06-05-csrf-posture.md to record the decision and rationale.