Framework Reference
Cross-Site Login Protection
A page on another site can make a visitor's browser submit a form to a sign-in route. Without a
check, the response would set session cookies and sign that browser into whichever account the
form named (login CSRF). SameSite=Lax cookies do not prevent this: they control when a cookie is
sent, not whether a top-level response may set one.
The routes that issue a session from a request body therefore reject cross-site browser requests
with 403 { ok: false, error } before they read the body:
| Route | Session cookies |
|---|---|
POST /api/auth/login | auth_token, session_token |
POST /api/auth/session/refresh | auth_token |
POST /api/customer_accounts/login | customer_auth_token, customer_session_token |
POST /api/customer_accounts/magic-link/verify | customer_auth_token, customer_session_token |
POST /api/customer_accounts/invitations/accept | customer_auth_token, customer_session_token |
Policy
The decision is made by isCrossSiteBrowserRequest(req) from @open-mercato/shared/lib/url:
- Fetch Metadata.
Sec-Fetch-Site: cross-siteis rejected.same-originandnoneare accepted. A browser computes this header itself and a page cannot override it. - No browser headers. A request with neither
Sec-Fetch-SitenorOriginis accepted. CLI tools, scripts, mobile apps, server-to-server calls and the integration-test helpers sign in this way. - Origin. For any other request (
same-site, an unknownSec-Fetch-Sitevalue, or a browser that sendsOriginwithout Fetch Metadata):- a missing
Origin(whenSec-Fetch-Siteis present),Origin: null, or an unparsableOriginis rejected; - an
Originequal toAPP_URL,NEXT_PUBLIC_APP_URLor an entry ofAPP_ALLOWED_ORIGINSis accepted; - outside production, a loopback
Origin(localhost,127.0.0.1) is accepted when one of the configured origins is loopback too, on any port; - an
Originequal to the request's ownX-Forwarded-HostorHostis accepted. The scheme comes fromX-Forwarded-Protowhen the proxy sends it, so it must match; without that header only the host and port are compared; - anything else is rejected and logged as a
Cross-site request rejectedwarning with theorigin,hostandforwardedHostvalues.
- a missing
The first-party sign-in pages call these routes with same-origin fetch, so they pass at step 1
(over plain HTTP on a non-loopback host, browsers send no Fetch Metadata and the pages pass
through the Host match in step 3).
Portal custom domains pass because the browser's Origin equals the custom domain's Host.
Configuration
Configuration only matters for requests that reach step 3: browsers that send no Fetch Metadata,
and same-site requests from a sibling subdomain. For those, make sure the origin is recognised:
APP_URL=https://admin.example.com
APP_ALLOWED_ORIGINS=https://ops.example.com
Alternatively, let the reverse proxy forward X-Forwarded-Host (and X-Forwarded-Proto).
A sign-in form on a sibling subdomain (for example www.example.com posting to
app.example.com) works once its origin is listed in APP_ALLOWED_ORIGINS.
APP_ALLOWED_ORIGINS cannot re-enable a sign-in form hosted on a different site. The browser marks
that request Sec-Fetch-Site: cross-site, which is always rejected. Sign in on the application's
own origin, or call the API from a server.