Skip to content

fix(csrf)!: require Origin to match Host without an allow-list and default to SameSite=Lax - #47

Open
Bccorb wants to merge 1 commit into
mainfrom
fix/csrf-host-origin-samesite-lax
Open

Bccorb wants to merge 1 commit into
mainfrom
fix/csrf-host-origin-samesite-lax

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Closes #41 (M12, part of fells-code/seamless-auth-api#603).

What changes

  • Host match without an allow-list. When allowed_origins is not set and Sec-Fetch-Site does not settle the request (same-site, or absent with an Origin), the Origin host and port must equal the request's Host, ignoring a default port (:443 for https, :80 for http). Previously such a request passed.
  • Same-site with no Origin is refused with or without an allow-list.
  • Unknown Sec-Fetch-Site values are treated as cross-site (parity item from the issue). Previously they fell through to the Origin check.
  • SameSite=Lax by default, whatever insecure_cookies says. Previously None for secure cookies.
  • The conformance reference apps set same_site="None", since verify/CONFORMANCE.md asks for SameSite=None reference cookies. Adapter-mode specs send no Origin, so the Host rule does not affect them.

Unchanged: option names (same_site, allowed_origins), the refusal (403 {"error": "cross_site_request_blocked"}), Origin: null refused first, requests with neither header still pass (native and bearer clients), and the body gate.

The check lives in the core (Adapter._check_origin), so the auth routes, RequireUser (FastAPI) and the Django guard all get it. Both bindings already pass the raw Host header through.

Breaking change (minor bump, pre-1.0)

  • A frontend on another site than the API must pass same_site="None".
  • A frontend on another origin than the API, or a deployment behind a proxy that rewrites Host, must list the frontend's origin in allowed_origins. X-Forwarded-Host is not trusted for this.

Tests

  • HOST_CASES table in tests/test_adapter.py: case, default ports, explicit ports, IPv6, sibling subdomains, scheme mismatch with default port, missing or empty Host, unparseable Origin.
  • Allow-list replaces the Host check; SameSite defaults.
  • FastAPI and Django: guard and auth-route tests through each binding.

Checks (local)

  • uv run pytest: 1117 passed
  • uv run ruff check .: all checks passed
  • uv run ruff format --check .: 45 files already formatted
  • uv run mypy: no issues in 15 source files
  • scripts/check-manifest.sh: bundled manifest matches

…fault to SameSite=Lax

Without allowed_origins, the cross-site check passed any request it could not
settle from Sec-Fetch-Site, so a sibling subdomain (same-site) or a request
with no fetch metadata got through with any Origin. It now requires the Origin
host and port to equal the request's Host, ignoring a default port. A
same-site request with no Origin is refused, and a Sec-Fetch-Site value other
than same-origin, same-site, none and cross-site is treated as cross-site.
This covers the auth routes and the FastAPI and Django guards, which share
the check. The refusal is unchanged: 403 {"error": "cross_site_request_blocked"}.

Cookies now default to SameSite=Lax, whatever insecure_cookies says. The
conformance reference apps set same_site="None", as the contract asks.

Closes #41

BREAKING CHANGE: auth cookies default to SameSite=Lax instead of None. A
frontend on another site than the API must pass same_site="None". Without
allowed_origins, a state-changing request whose Origin does not match Host now
answers 403 cross_site_request_blocked, as does a same-site request with no
Origin or an unknown Sec-Fetch-Site value. A frontend on another origin than
the API, or a proxy that rewrites Host, must list the frontend's origin in
allowed_origins.

This branch has not been deployed

No deployments
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.

fix(csrf): require Origin to match Host without an allow-list and default to SameSite=Lax

1 participant