diff --git a/.changeset/33237-scim-bearer-cookie-origin.md b/.changeset/33237-scim-bearer-cookie-origin.md new file mode 100644 index 00000000000..4bbf0682151 --- /dev/null +++ b/.changeset/33237-scim-bearer-cookie-origin.md @@ -0,0 +1,12 @@ +--- +'@objectstack/plugin-auth': patch +--- + +fix(plugin-auth): a SCIM bearer write that carries a Cookie and no Origin is no longer refused 403 `MISSING_OR_NULL_ORIGIN` + +Clause-②: no +Metadata-protocol: none + +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. + +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. diff --git a/packages/plugins/plugin-auth/src/auth-manager-request-routing.ts b/packages/plugins/plugin-auth/src/auth-manager-request-routing.ts index 915c74cbfb7..944e5a37b0b 100644 --- a/packages/plugins/plugin-auth/src/auth-manager-request-routing.ts +++ b/packages/plugins/plugin-auth/src/auth-manager-request-routing.ts @@ -117,6 +117,30 @@ export async function handleRequest(this: Pick { + vi.stubEnv('NODE_ENV', 'production'); +}); +afterEach(async () => { + vi.unstubAllEnvs(); + vi.restoreAllMocks(); + while (engines.length) { + const e = engines.pop(); + try { + await (e as unknown as { destroy?(): Promise })?.destroy?.(); + } catch { + /* noop */ + } + } +}); + +interface Harness { + engine: ObjectQL; + token: string; + send: (request: Request) => Promise; +} + +async function boot(): Promise { + const engine = new ObjectQL(); + engines.push(engine); + const driver = new SqlDriver({ + client: 'better-sqlite3', + connection: { filename: ':memory:' }, + useNullAsDefault: true, + }); + engine.registerDriver(driver, true); + await engine.init(); + for (const object of authIdentityObjects) { + engine.registry.registerObject(object as never, '@objectstack/plugin-auth'); + } + await engine.syncSchemas(); + + const manager = new AuthManager({ + secret: SECRET, + baseUrl: BASE, + dataEngine: engine as never, + getTenancy: () => createTenancyService({ requested: 'isolated', probeIsolation: () => true }), + plugins: { scim: true, organization: true }, + } as never); + + // better-auth reads NODE_ENV ONCE, when `@better-auth/core/env` loads, and + // under vitest that read is `test`, for which it defaults the context's + // `skipOriginCheck` to `true` (`create-context.mjs`: `disableOriginCheck` + // unset ⇒ `isTest()`). A deployment runs with `false`. The stub above comes + // too late for that module-load read, so the production value is put on the + // live context here — and read back, so a harness that never reached the + // origin check fails instead of passing every case below vacuously. + const context = (await (await manager.getAuthInstance()).$context) as { + skipOriginCheck: unknown; + skipCSRFCheck: unknown; + }; + context.skipOriginCheck = false; + expect(context.skipCSRFCheck, 'the CSRF check is on, as in a deployment').toBe(false); + + const { token } = await mintScimConnectionCredential(engine as never, SECRET, { + connectionId: 'okta-33237', + organizationId: 'org_scim_33237', + }); + + return { engine, token, send: (request) => manager.handleRequest(request) }; +} + +/** A SCIM request as an IdP with a cookie jar sends it: a Cookie, NO Origin, no Referer. */ +function scimRequest( + path: string, + init: { method: string; bearer?: string; cookie?: string; body?: unknown }, +): Request { + const headers: Record = {}; + if (init.bearer !== undefined) headers.authorization = `Bearer ${init.bearer}`; + if (init.cookie !== undefined) headers.cookie = init.cookie; + if (init.body !== undefined) headers['content-type'] = 'application/scim+json'; + return new Request(`${AUTH}/scim/v2${path}`, { + method: init.method, + headers, + ...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}), + }); +} + +const userBody = (localPart: string) => ({ + schemas: [USER_SCHEMA], + userName: `${localPart}@example.com`, + name: { givenName: localPart, familyName: 'Example' }, + displayName: `${localPart} Example`, + emails: [{ value: `${localPart}@example.com`, primary: true, type: 'work' }], + active: true, +}); + +async function errorCode(res: Response): Promise { + const body = (await res.clone().json().catch(() => null)) as + | { code?: unknown; error?: { code?: unknown } } + | null; + return body?.code ?? body?.error?.code; +} + +async function userRow(h: Harness, email: string): Promise | null> { + return h.engine.findOne('sys_user', { where: { email }, fields: ['id', 'email'] }, SYSTEM) as Promise< + Record | null + >; +} + +/** The dev-DX Origin synthesis must be OFF, or every case below passes without the fix. */ +function assertProductionSemantics(): void { + expect(process.env.NODE_ENV, 'NODE_ENV must read production so no Origin is synthesized').toBe('production'); +} + +describe('[#33237] a SCIM bearer request is not refused for the Cookie it carries', () => { + it('POST /Users with a SCIM bearer, a Cookie and no Origin is served (201)', async () => { + assertProductionSemantics(); + const h = await boot(); + + const res = await h.send( + scimRequest('/Users', { method: 'POST', bearer: h.token, cookie: EDGE_COOKIE, body: userBody('alice') }), + ); + + expect(res.status, `SCIM POST /Users: ${await res.clone().text()}`).toBe(201); + expect(await userRow(h, 'alice@example.com'), 'the provisioned sys_user row').not.toBeNull(); + }, 60_000); + + it('PATCH /Users/{id} active:false with a SCIM bearer, a Cookie and no Origin is served (200)', async () => { + assertProductionSemantics(); + const h = await boot(); + const created = await h.send( + scimRequest('/Users', { method: 'POST', bearer: h.token, body: userBody('bob') }), + ); + expect(created.status, `SCIM POST /Users (setup, no cookie): ${await created.clone().text()}`).toBe(201); + const { id } = (await created.json()) as { id: string }; + + const res = await h.send( + scimRequest(`/Users/${id}`, { + method: 'PATCH', + bearer: h.token, + cookie: EDGE_COOKIE, + body: { schemas: [PATCH_SCHEMA], Operations: [{ op: 'replace', path: 'active', value: false }] }, + }), + ); + + expect(res.status, `SCIM PATCH active:false: ${await res.clone().text()}`).toBe(200); + expect(((await res.json()) as { active?: unknown }).active, 'the deprovisioned resource').toBe(false); + }, 60_000); +}); + +describe('[#33237] the origin check still guards every cookie-carrying request it guarded', () => { + it('a SCIM POST with a Cookie and NO bearer is still refused: 403 MISSING_OR_NULL_ORIGIN, nothing written', async () => { + assertProductionSemantics(); + const h = await boot(); + + const res = await h.send( + scimRequest('/Users', { method: 'POST', cookie: EDGE_COOKIE, body: userBody('mallory') }), + ); + + expect(res.status, await res.clone().text()).toBe(403); + expect(await errorCode(res)).toBe('MISSING_OR_NULL_ORIGIN'); + expect(await userRow(h, 'mallory@example.com'), 'no sys_user row may be written').toBeNull(); + }, 60_000); + + it('CONTROL: a cookie-authenticated non-SCIM POST with no Origin is still refused: 403 MISSING_OR_NULL_ORIGIN', async () => { + assertProductionSemantics(); + const h = await boot(); + const up = await h.send( + new Request(`${AUTH}/sign-up/email`, { + method: 'POST', + headers: { 'content-type': 'application/json', origin: BASE }, + body: JSON.stringify({ email: 'carol@example.com', password: PASSWORD, name: 'Carol Example' }), + }), + ); + expect(up.status, `sign-up: ${await up.clone().text()}`).toBeLessThan(300); + const sessionCookie = (up.headers.getSetCookie?.() ?? []) + .map((c) => c.split(';')[0]) + .find((c) => c.includes('session_token=')); + expect(sessionCookie, 'sign-up set a session cookie').toBeTruthy(); + + // The same session, presented as a browser would — cookie only — on a + // state-changing route, with no Origin and no Referer. + const res = await h.send( + new Request(`${AUTH}/sign-out`, { + method: 'POST', + headers: { 'content-type': 'application/json', cookie: `${sessionCookie}; __cf_bm=Zm9vYmFy` }, + body: '{}', + }), + ); + + expect(res.status, await res.clone().text()).toBe(403); + expect(await errorCode(res)).toBe('MISSING_OR_NULL_ORIGIN'); + }, 60_000); +});