|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The environment-membership judgment — ONE function, asked by every door that |
| 5 | + * serves a request a multi-environment host resolved into an environment: the |
| 6 | + * runtime dispatcher (`HttpDispatcher`) and the REST server |
| 7 | + * (`@objectstack/rest`). A door supplies the principal it resolved and the |
| 8 | + * engine of the request's kernel; the verdict, the exemptions, the |
| 9 | + * applicability and the refusal are decided here and nowhere else. |
| 10 | + * |
| 11 | + * ⛔ Never re-spell any part of it at a door. A second copy is how two doors on |
| 12 | + * the same host come to answer the same caller differently. |
| 13 | + * |
| 14 | + * ## The verdict, in order |
| 15 | + * |
| 16 | + * 1. No environment, or the well-known system environment ⇒ the judgment |
| 17 | + * does not apply (`null`). The system environment is reachable by any |
| 18 | + * signed-in caller. Which ids a door counts as an environment at all is |
| 19 | + * the door's own vocabulary (REST's `'platform'` names its control plane, |
| 20 | + * not an environment) and is decided before it asks. |
| 21 | + * 2. A caller acting in the platform organization ⇒ admitted (`null`). |
| 22 | + * 3. NO ENGINE ⇒ {@link AuthzStoreUnavailableError}. The read cannot be made; |
| 23 | + * an absent engine where a host resolved an environment and an auth |
| 24 | + * service signed the caller in is a fault, not a composition. |
| 25 | + * 4. `sys_environment_member` NOT REGISTERED on that engine ⇒ the judgment |
| 26 | + * does not apply (`null`). A composition that holds no environment |
| 27 | + * membership has none to judge. The question is the registry's own |
| 28 | + * `getObject`, never a caught throw; an engine whose registry cannot be |
| 29 | + * asked is asked, and a fault refuses. |
| 30 | + * 5. The membership read, as the platform (the caller's id is the `where`, |
| 31 | + * not the reader), on EVERY request — an admission is never reused, so a |
| 32 | + * removal takes effect on the caller's next request, on every replica, and |
| 33 | + * a store that cannot answer refuses that request whoever was admitted a |
| 34 | + * moment ago. ⛔ A future cache here must first meet the |
| 35 | + * authorization-cache rule in `resolve-authz-context.ts` (cache a success |
| 36 | + * only behind seams that observe the writes it depends on, TTL as the |
| 37 | + * residual bound) — and the writes that change this answer land in the |
| 38 | + * control plane, which the request's engine need not observe. A read that |
| 39 | + * throws ⇒ {@link AuthzStoreUnavailableError}: fail closed, loud and |
| 40 | + * retriable, never "allowed" and never the membership refusal. |
| 41 | + * 6. A row ⇒ admitted (`null`); none ⇒ the refusal: `403`, |
| 42 | + * `PROJECT_MEMBERSHIP_REQUIRED`, with the environment and the user as |
| 43 | + * context. |
| 44 | + * |
| 45 | + * Whether a door asks at all is the host's switch |
| 46 | + * ({@link resolveEnvironmentMembershipEnforcement}) — the same switch for every |
| 47 | + * door, so a door refuses only callers its sibling doors refuse. |
| 48 | + */ |
| 49 | + |
| 50 | +import { AuthzStoreUnavailableError } from './authz-store-unavailable.js'; |
| 51 | + |
| 52 | +/** The control-plane object that records who is a member of which environment. */ |
| 53 | +export const ENVIRONMENT_MEMBER_OBJECT = 'sys_environment_member'; |
| 54 | + |
| 55 | +/** The well-known system environment — reachable by any signed-in caller. */ |
| 56 | +const SYSTEM_ENVIRONMENT_ID = '00000000-0000-0000-0000-000000000001'; |
| 57 | + |
| 58 | +/** The well-known platform organization — its members pass every environment. */ |
| 59 | +const PLATFORM_ORGANIZATION_ID = '00000000-0000-0000-0000-000000000000'; |
| 60 | + |
| 61 | +/** The ADR-0112 code of the refusal. */ |
| 62 | +export const ENVIRONMENT_MEMBERSHIP_REQUIRED_CODE = 'PROJECT_MEMBERSHIP_REQUIRED'; |
| 63 | + |
| 64 | +/** The HTTP status of the refusal. */ |
| 65 | +export const ENVIRONMENT_MEMBERSHIP_REQUIRED_STATUS = 403; |
| 66 | + |
| 67 | +/** The refusal a door writes in its own envelope. */ |
| 68 | +export interface EnvironmentMembershipRefusal { |
| 69 | + readonly status: typeof ENVIRONMENT_MEMBERSHIP_REQUIRED_STATUS; |
| 70 | + readonly code: typeof ENVIRONMENT_MEMBERSHIP_REQUIRED_CODE; |
| 71 | + readonly message: string; |
| 72 | + readonly environmentId: string; |
| 73 | + readonly userId: string; |
| 74 | +} |
| 75 | + |
| 76 | +/** The slice of an ObjectQL engine the membership read needs. */ |
| 77 | +export interface EnvironmentMembershipEngine { |
| 78 | + registry?: { getObject?: (name: string) => unknown }; |
| 79 | + find(object: string, query: unknown): Promise<unknown>; |
| 80 | +} |
| 81 | + |
| 82 | +export interface EnvironmentMembershipInput { |
| 83 | + /** The environment the host resolved this request into. */ |
| 84 | + environmentId: string | undefined; |
| 85 | + /** The principal the door resolved. */ |
| 86 | + userId: string; |
| 87 | + /** The organization the door's resolution vetted for that principal. */ |
| 88 | + activeOrganizationId: string | undefined; |
| 89 | + /** The engine of the request's own kernel; `undefined` when none resolved. */ |
| 90 | + engine: EnvironmentMembershipEngine | null | undefined; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * The host's switch: explicit `enforceProjectMembership` wins, else on exactly |
| 95 | + * when environment scoping is on. Every door reads it from this one function, |
| 96 | + * so `api.enforceProjectMembership: false` turns the judgment off at every |
| 97 | + * door at once (ADR-0135 D9: a host whose environment-native auth IS the |
| 98 | + * membership opts out). |
| 99 | + */ |
| 100 | +export function resolveEnvironmentMembershipEnforcement(input: { |
| 101 | + enforceProjectMembership?: boolean; |
| 102 | + enableProjectScoping?: boolean; |
| 103 | +}): boolean { |
| 104 | + return input.enforceProjectMembership ?? (input.enableProjectScoping ?? false); |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Whether `environmentId` is one the judgment applies to at all — cheap and |
| 109 | + * read-free, so a door can skip resolving a principal it would not judge. |
| 110 | + */ |
| 111 | +export function isMembershipJudgedEnvironment(environmentId: string | undefined): environmentId is string { |
| 112 | + return !!environmentId && environmentId !== SYSTEM_ENVIRONMENT_ID; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * The verdict on one principal for one environment — `null` when admitted or |
| 117 | + * when the judgment does not apply, the refusal otherwise; throws |
| 118 | + * {@link AuthzStoreUnavailableError} when an input it needs cannot be read. |
| 119 | + */ |
| 120 | +export async function judgeEnvironmentMembership( |
| 121 | + input: EnvironmentMembershipInput, |
| 122 | +): Promise<EnvironmentMembershipRefusal | null> { |
| 123 | + const { environmentId, userId, activeOrganizationId, engine } = input; |
| 124 | + if (!isMembershipJudgedEnvironment(environmentId)) return null; |
| 125 | + |
| 126 | + if (activeOrganizationId === PLATFORM_ORGANIZATION_ID) return null; |
| 127 | + |
| 128 | + if (!engine) { |
| 129 | + throw new AuthzStoreUnavailableError( |
| 130 | + ENVIRONMENT_MEMBER_OBJECT, |
| 131 | + new Error('no ObjectQL engine resolved on this request\'s kernel'), |
| 132 | + ); |
| 133 | + } |
| 134 | + if (typeof engine.registry?.getObject === 'function' && !engine.registry.getObject(ENVIRONMENT_MEMBER_OBJECT)) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + let rows: unknown; |
| 139 | + try { |
| 140 | + rows = await engine.find(ENVIRONMENT_MEMBER_OBJECT, { |
| 141 | + where: { environment_id: environmentId, user_id: userId }, |
| 142 | + limit: 1, |
| 143 | + context: { isSystem: true }, |
| 144 | + }); |
| 145 | + } catch (err) { |
| 146 | + throw new AuthzStoreUnavailableError(ENVIRONMENT_MEMBER_OBJECT, err); |
| 147 | + } |
| 148 | + if (rows && (rows as { value?: unknown }).value) rows = (rows as { value?: unknown }).value; |
| 149 | + if (Array.isArray(rows) && rows.length > 0) return null; |
| 150 | + |
| 151 | + return { |
| 152 | + status: ENVIRONMENT_MEMBERSHIP_REQUIRED_STATUS, |
| 153 | + code: ENVIRONMENT_MEMBERSHIP_REQUIRED_CODE, |
| 154 | + message: `Forbidden: user ${userId} is not a member of project ${environmentId}`, |
| 155 | + environmentId, |
| 156 | + userId, |
| 157 | + }; |
| 158 | +} |
0 commit comments