Skip to content

CORS: never allow credentials for the wildcard origin - #53

Merged
thorwhalen merged 2 commits into
mainfrom
cors-no-credentials-by-default
Sep 22, 2026
Merged

thorwhalen merged 2 commits into
mainfrom
cors-no-credentials-by-default

Conversation

@thorwhalen

@thorwhalen thorwhalen commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

The platform-level CORS middleware was allow_origins=["*"], allow_credentials=True. With that pair Starlette reflects the request's Origin and adds Access-Control-Allow-Credentials: true, so a page on another origin that gets the browser to attach the session cookie can read a signed-in user's responses (confirmed locally with a protected app behind enlace_auth). SameSite=Lax session cookies limit which origins can do that in practice, but the header pair should never be sent for arbitrary origins.

  • New PlatformConfig.cors_origins ([platform] cors_origins in platform.toml):
    • ["*"] (default) — any origin, without credentials (public responses stay readable cross-origin, as before);
    • explicit list — only those origins, with credentials;
    • [] — no CORS headers.
  • cors_origins is validated: "*" must stand alone, "null" is refused, named origins must be scheme://host[:port].
  • README config example updated.

Independent refute-review done: no blockers; its should-fixes ("null" re-opening exposure, "*" mixed with named origins, malformed origins silently never matching) are fixed in the second commit. Noted, not changed: auth middleware runs before CORS, so 401s carry no CORS headers (pre-existing); enlace_auth's /api/ CSRF exemption relies on SameSite=Lax, which does not stop same-site sibling hosts.

Behaviour change: cross-origin credentialed calls from arbitrary origins stop working unless the origin is listed. In-fleet frontends call the API same-origin (relative URLs), so nothing known depends on it.

Tests: enlace/tests/test_cors.py (fail on main). Full suite 308 passed. Dependents against this branch: enlace_auth 377 passed, enlace_docker 58 passed / 2 skipped.

🤖 Generated with Claude Code

thorwhalen and others added 2 commits September 22, 2026 15:32
The parent app used allow_origins=['*'] with allow_credentials=True, which
makes Starlette reflect any request Origin and add
Access-Control-Allow-Credentials: true, so any page that got the browser
to send the session cookie could read a signed-in user's responses.

New PlatformConfig.cors_origins (platform.toml [platform]): ['*'] (default)
allows any origin without credentials; an explicit list allows those
origins with credentials; [] disables CORS.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Refuse "*" mixed with named origins (would silently drop credentials),
"null" (any sandboxed page sends it), and values that are not
scheme://host[:port] (never match, fail silently).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@thorwhalen
thorwhalen merged commit 41d530b into main Sep 22, 2026
12 checks passed
@thorwhalen
thorwhalen deleted the cors-no-credentials-by-default branch September 22, 2026 15:44
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