Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/authorize-gate-judges-every-client-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@objectstack/plugin-auth': patch
---

fix(auth)!: the environment-access gate on `/oauth2/authorize` judges every client the request names, from the query and the body

Clause-②: no (narrowing)
Metadata-protocol: none
Skills-docs: none — no skill under `skills/**` and no hand-written page under `content/docs/**` teaches `oidcAuthorizeGate` or the authorize endpoint's environment-access gate.

<!-- 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. -->

**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.

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.

Unchanged: a deployment with no `oidcAuthorizeGate`; a `GET` naming one client; the passes that resume an authorization (sign-in, `/oauth2/consent`, `/oauth2/continue`), which the gate already judged; and the refusal's code, status, message and delivery.
41 changes: 35 additions & 6 deletions packages/plugins/plugin-auth/src/auth-manager-before-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ import { ssoDiscoveryCompanionContext } from './sso-discovery-companion-origins.
import { snapshotRoleChangeGrants, UPDATE_MEMBER_ROLE_PATH } from './role-change-granted-set.js';
import { refuseVisitorResendWithoutLiveTransport } from './visitor-verification-resend.js';

/**
* Every client an `/oauth2/authorize` request names, for the ADR-0069 D5.1
* environment-access gate: the query's `client_id` first, then the body's,
* each a string or an array of them, de-duplicated in that order. An empty
* value names nothing; any other present value is judged by its string form
* rather than skipped, so the gate is never asked about fewer clients than
* the request names.
*/
function authorizeRequestClientIds(ctx: any): string[] {
const named = new Set<string>();
const collect = (source: unknown): void => {
if (typeof source !== 'object' || source === null || Array.isArray(source)) return;
const value = (source as Record<string, unknown>).client_id;
for (const one of Array.isArray(value) ? value : [value]) {
if (one === undefined || one === null || one === '') continue;
named.add(String(one));
}
};
collect(ctx?.query);
collect(ctx?.body);
return [...named];
}

/**
* [#33203] The self-service reset doors a cloud-managed environment refuses:
* the two that set the password and the two that send their link or code.
Expand Down Expand Up @@ -550,20 +573,26 @@ export async function globalBeforeHook(
// read as unauthenticated and this env-access check never evaluated
// at all. Two resolution sites are what let them diverge, so the fix
// is to delete one, not to correct it in place.
//
// The gate judges EVERY client the request names — the query's and the
// body's `client_id`, each a string or an array, de-duplicated
// ({@link authorizeRequestClientIds}) — and refuses if any one of them
// is refused. A request that names no client anywhere is not judged here.
if (ctx?.path === '/oauth2/authorize' && this.config.oidcAuthorizeGate) {
const clientId = ctx?.query?.client_id;
if (clientId) {
const clientIds = authorizeRequestClientIds(ctx);
if (clientIds.length > 0) {
const actor = await this.resolveActor(ctx);
// Unauthenticated → fall through, per the contract above. Only an
// AUTHENTICATED subject is gated here. `resolveActor` also returns
// `activeOrgId`; this gate deliberately does not consume it — the
// D5.1 host contract is `(userId, clientId)` and the control plane
// derives org membership / app assignment from the user itself.
if (actor?.userId) {
const allowed = await this.config.oidcAuthorizeGate({
userId: actor.userId,
clientId: String(clientId),
});
let allowed = true;
for (const clientId of clientIds) {
allowed = await this.config.oidcAuthorizeGate({ userId: actor.userId, clientId });
if (!allowed) break;
}
if (!allowed) {
// The refusal, delivered by who is asking (`oauth-authorize-refusal.ts`):
// the `403` envelope for a fetch or an API client, unchanged; for a
Expand Down
5 changes: 4 additions & 1 deletion packages/plugins/plugin-auth/src/auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,10 @@ export interface AuthManagerOptions extends Partial<AuthConfig> {
* D5.1 — OIDC OP authorization gate (cloud-as-IdP app-assignment).
* When set, it is called for an AUTHENTICATED subject on
* `/oauth2/authorize` before an authorization code is issued, with the
* subject + the requesting `clientId`. Return `false` to DENY (no code).
* subject + a `clientId` the request names. Return `false` to DENY (no code).
* It can be called once for each distinct client the request names (the
* query's and the body's `client_id`); if any one call answers `false`,
* the request is refused and no further client is asked about.
* The cloud control plane uses it to require org-membership: a cloud user
* may only obtain a code for an env client (`project_<envId>`) of an org
* they belong to. Unset (open editions / self-host, where the OP is not a
Expand Down
Loading
Loading