Skip to content

Commit 8a779de

Browse files
committed
test(auth): pin each arm of the authorize gate's client collection; state the per-client calls in the gate's contract
- one leg per arm: a query naming client_id twice is refused with each client judged by itself, in both orders; a resume pass whose body names a non-string client_id is judged on that value - the two-client form-body leg now states what it measures, in both orders: no code unless the gate judged and admitted the client - the oidcAuthorizeGate jsdoc states the gate can be called once for each distinct client the request names - the changeset says "each a string or an array" describes what the gate collects, not what every transport accepts Claude-Session: https://claude.ai/code/session_017xRBcT1w3zpekijraHJE4f Co-authored-by: Claude <noreply@anthropic.com>
1 parent a65fcc6 commit 8a779de

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

‎.changeset/authorize-gate-judges-every-client-id.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Skills-docs: none — no skill under `skills/**` and no hand-written page under
1010

1111
<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed, renamed or re-shaped: the change is a runtime refusal at one auth endpoint, judged by a host callback whose signature does not change, so there is nothing stored for a migration to rewrite and no tombstone. -->
1212

13-
**BREAKING** for a deployment that sets `oidcAuthorizeGate` (the ADR-0069 D5.1 environment-access gate; open editions and self-host leave it unset). The gate on `/oauth2/authorize` now judges every client the request names: the query's `client_id` and the body's `client_id`, each a string or an array, de-duplicated. If any one of them is refused, the request is refused with the existing refusal: `403 ENV_ACCESS_DENIED` for a fetch or an API client, and for a browser the redirect or the readable page it already gets. A request that names no client anywhere is unchanged, and so is an unauthenticated request, which still goes on to sign in.
13+
**BREAKING** for a deployment that sets `oidcAuthorizeGate` (the ADR-0069 D5.1 environment-access gate; open editions and self-host leave it unset). The gate on `/oauth2/authorize` now judges every client the request names: the query's `client_id` and the body's `client_id`, each a string or an array, de-duplicated ("each a string or an array" describes what the gate collects from the parsed request, not a promise that every transport accepts both forms). If any one of them is refused, the request is refused with the existing refusal: `403 ENV_ACCESS_DENIED` for a fetch or an API client, and for a browser the redirect or the readable page it already gets. A request that names no client anywhere is unchanged, and so is an unauthenticated request, which still goes on to sign in.
1414

1515
Who moves: only a host whose `oidcAuthorizeGate` assumes it is called at most once per request. It can now be called once for each distinct client the request names, and the first refusal ends the request. The callback's signature, `({ userId, clientId }) => boolean | Promise<boolean>`, does not change.
1616

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,10 @@ export interface AuthManagerOptions extends Partial<AuthConfig> {
960960
* D5.1 — OIDC OP authorization gate (cloud-as-IdP app-assignment).
961961
* When set, it is called for an AUTHENTICATED subject on
962962
* `/oauth2/authorize` before an authorization code is issued, with the
963-
* subject + the requesting `clientId`. Return `false` to DENY (no code).
963+
* subject + a `clientId` the request names. Return `false` to DENY (no code).
964+
* It can be called once for each distinct client the request names (the
965+
* query's and the body's `client_id`); if any one call answers `false`,
966+
* the request is refused and no further client is asked about.
964967
* The cloud control plane uses it to require org-membership: a cloud user
965968
* may only obtain a code for an env client (`project_<envId>`) of an org
966969
* they belong to. Unset (open editions / self-host, where the OP is not a

‎packages/plugins/plugin-auth/src/oauth-authorize-refusal.test.ts‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,52 @@ describe('the environment-access gate judges every client an /oauth2/authorize r
443443
});
444444
}
445445

446-
it('a body naming two clients, one of them refused, is refused', async () => {
447-
const { rig, refused, admitted } = await arrangeClients();
446+
for (const order of ['admitted first', 'refused first'] as const) {
447+
it(`a query naming client_id twice, one of them refused, is refused, each client judged by itself (${order})`, async () => {
448+
const { rig, refused, admitted } = await arrangeClients();
449+
const url = new URL(`${ISSUER}/oauth2/authorize`);
450+
const { client_id: _omit, ...params } = codeRequest(refused, ENV_CALLBACK);
451+
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
452+
for (const one of order === 'admitted first' ? [admitted, refused] : [refused, admitted]) url.searchParams.append('client_id', one);
448453

449-
const res = await rig.authorizeForm(
450-
{},
451-
{ ...codeRequest(refused, ENV_CALLBACK), client_id: [admitted, refused] },
452-
CONSOLE_FETCH,
453-
);
454+
const res = await rig.manager.handleRequest(
455+
new Request(url.toString(), { headers: { ...CONSOLE_FETCH, cookie: rig.cookie } }),
456+
);
457+
458+
expect(rig.gate.calls).toContainEqual({ userId: rig.userId, clientId: refused });
459+
await expectRefusalEnvelope(res);
460+
});
461+
462+
it(`a form body naming client_id twice is handed no code unless the gate judged and admitted the client (${order})`, async () => {
463+
const { rig, refused, admitted } = await arrangeClients();
464+
465+
const res = await rig.authorizeForm(
466+
{},
467+
{ ...codeRequest(refused, ENV_CALLBACK), client_id: order === 'admitted first' ? [admitted, refused] : [refused, admitted] },
468+
CONSOLE_FETCH,
469+
);
470+
471+
expect(rig.gate.calls.length, 'the gate judged no client').toBeGreaterThan(0);
472+
if (await codeIn(res)) {
473+
expect(rig.gate.calls.every((c) => c.clientId === admitted), JSON.stringify(rig.gate.calls)).toBe(true);
474+
} else {
475+
await expectRefusalEnvelope(res);
476+
}
477+
});
478+
}
479+
480+
it('a resume pass whose body names a client_id that is not a string is judged on that value, and refused when it is refused', async () => {
481+
const { rig, admitted } = await arrangeClients();
482+
const first = await rig.authorize(codeRequest(admitted, ENV_CALLBACK), NAVIGATION, 'anonymous');
483+
const location = first.headers.get('location') ?? '';
484+
rig.gate.calls.length = 0;
485+
486+
const res = await rig.call('/sign-in/email', {
487+
body: { email: EMAIL, password: PASSWORD, oauth_query: location.slice(location.indexOf('?')), client_id: 12345 },
488+
headers: CONSOLE_FETCH,
489+
});
454490

455-
expect(rig.gate.calls).toContainEqual({ userId: rig.userId, clientId: refused });
491+
expect(rig.gate.calls).toContainEqual({ userId: rig.userId, clientId: '12345' });
456492
await expectRefusalEnvelope(res);
457493
});
458494

0 commit comments

Comments
 (0)