Skip to content

Commit c8abb27

Browse files
committed
fix(plugin-auth): drop the Cookie from a /scim/v2 bearer request before the origin check
A SCIM bearer write carrying any Cookie and no Origin was refused 403 MISSING_OR_NULL_ORIGIN by better-auth's validateOrigin. SCIM authenticates by the bearer alone, so the Cookie carries no authority there. Claude-Session: https://claude.ai/code/session_017xRBcSZ51q2s3nMfakhdJ3 Co-authored-by: Claude <noreply@anthropic.com>
1 parent e9076fa commit c8abb27

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/plugin-auth': patch
3+
---
4+
5+
fix(plugin-auth): a SCIM bearer write that carries a Cookie and no Origin is no longer refused 403 `MISSING_OR_NULL_ORIGIN`
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
better-auth's origin check requires an `Origin` (or `Referer`) whenever a request carries any Cookie, and `AuthManager.handleRequest` used to forward the Cookie into it on `/scim/v2/*`. An identity provider that keeps a cookie jar, or sits behind an edge cookie (`__cf_bm`) or a sticky-session load-balancer cookie, was therefore answered 403 `MISSING_OR_NULL_ORIGIN` on every `POST /Users` and every `PATCH … active:false`: it could neither provision nor deprovision a user.
11+
12+
SCIM authenticates by the `Authorization: Bearer` credential alone and its writes are attributed to the verified connection, so a Cookie carries no authority on that path. `handleRequest` now drops the Cookie from a `/scim/v2/*` request that carries a bearer, before better-auth sees it. No check is relaxed: every other route, and a `/scim/v2/*` request with no bearer, still reaches the origin check with its Cookie and is still refused without an `Origin`. A deployment that strips the Cookie in front of the server for this reason can drop that strip once it runs this release.

‎packages/plugins/plugin-auth/src/auth-manager-request-routing.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ export async function handleRequest(this: Pick<RequestRoutingHost, 'config' | 'b
117117
// address the D5 allow-list judged, or none. See `client-address-vendor.ts`.
118118
request = withVendorClientAddress(request, clientAddress);
119119

120+
// [#33237] A SCIM protocol request (`/scim/v2/*`) that carries a bearer
121+
// reaches the vendor WITHOUT its Cookie. `@better-auth/scim` authenticates
122+
// that path by the `Authorization: Bearer` credential alone
123+
// (`createSCIMConnectionMiddleware`; the platform's verifier reads only the
124+
// token), and its writes are attributed to the verified connection, never
125+
// to a session (`authSystemWriteContext`), so a Cookie carries no authority
126+
// there. It did carry a refusal: the vendor's origin check
127+
// (`validateOrigin`) demands an `Origin` whenever the request HAS a cookie,
128+
// so an identity provider with a cookie jar, or behind an edge or
129+
// sticky-session cookie, was answered 403 `MISSING_OR_NULL_ORIGIN` on every
130+
// provision and deprovision. Dropping the Cookie relaxes no check: the
131+
// origin check still runs on every request that keeps one — every other
132+
// route, and a `/scim/v2/*` request with no bearer, which the SCIM
133+
// middleware refuses anyway. Pinned by `scim-bearer-cookie-origin.test.ts`.
134+
if (
135+
request.headers.has('cookie') &&
136+
/^Bearer\s+\S/i.test(request.headers.get('authorization') ?? '') &&
137+
isScimProtocolPath(this.betterAuthEndpointPath(request))
138+
) {
139+
const headers = new Headers(request.headers);
140+
headers.delete('cookie');
141+
request = new Request(request, { headers });
142+
}
143+
120144
// Dev DX: better-auth's CSRF protection rejects state-changing requests
121145
// (e.g. `/sign-in/email`) with a 403 when neither `Origin` nor `Referer`
122146
// is present. Browsers always send one; non-browser API clients (curl,

‎packages/plugins/plugin-auth/src/scim-bearer-cookie-origin.test.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
*
2727
* ## Why NODE_ENV is production here
2828
*
29-
* Outside production `handleRequest` synthesizes a same-origin `Origin` for a
30-
* request that has none (the dev-DX block), which would let every case below
31-
* pass with or without the fix. Each case runs with `NODE_ENV=production`
32-
* stubbed, and the first assertion of each case reads it back.
29+
* Two things would let every case below pass with or without the fix, and the
30+
* harness turns both off. Outside production `handleRequest` synthesizes a
31+
* same-origin `Origin` for a request that has none (the dev-DX block): each
32+
* case runs with `NODE_ENV=production` stubbed, and its first assertion reads
33+
* it back. And better-auth skips the origin check outright under a test
34+
* NODE_ENV: `boot()` puts the production value on the live context (the note
35+
* there says why it cannot come from the stub).
3336
*
3437
* Harness: a real `AuthManager` over a real `ObjectQL` on better-sqlite3 with
3538
* SCIM on and a minted connection credential — the shape of
@@ -103,6 +106,20 @@ async function boot(): Promise<Harness> {
103106
plugins: { scim: true, organization: true },
104107
} as never);
105108

109+
// better-auth reads NODE_ENV ONCE, when `@better-auth/core/env` loads, and
110+
// under vitest that read is `test`, for which it defaults the context's
111+
// `skipOriginCheck` to `true` (`create-context.mjs`: `disableOriginCheck`
112+
// unset ⇒ `isTest()`). A deployment runs with `false`. The stub above comes
113+
// too late for that module-load read, so the production value is put on the
114+
// live context here — and read back, so a harness that never reached the
115+
// origin check fails instead of passing every case below vacuously.
116+
const context = (await (await manager.getAuthInstance()).$context) as {
117+
skipOriginCheck: unknown;
118+
skipCSRFCheck: unknown;
119+
};
120+
context.skipOriginCheck = false;
121+
expect(context.skipCSRFCheck, 'the CSRF check is on, as in a deployment').toBe(false);
122+
106123
const { token } = await mintScimConnectionCredential(engine as never, SECRET, {
107124
connectionId: 'okta-33237',
108125
organizationId: 'org_scim_33237',

0 commit comments

Comments
 (0)