diff --git a/src/contracts/rest-profiles.ts b/src/contracts/rest-profiles.ts index 9010708a..1c4c9b0d 100644 --- a/src/contracts/rest-profiles.ts +++ b/src/contracts/rest-profiles.ts @@ -273,6 +273,7 @@ export function createRestProviderProfile( export function resolveRestSecurityProfiles( operation: Readonly<{ + operationId?: string; method: string; auth: "none" | "external-session"; authProfileId?: string; @@ -282,27 +283,44 @@ export function resolveRestSecurityProfiles( authProfiles: RestAuthProfileSource = INSTALLED_REST_AUTH_PROFILES, csrfProfiles: Readonly> = REST_CSRF_PROFILES, ): Readonly<{ auth: RestAuthProfile; csrf: RestCsrfProfile }> { - const auth = installedAuthProfiles(authProfiles).get( - operation.authProfileId ?? "", - ); + const authProfileId = operation.authProfileId ?? ""; + const auth = installedAuthProfiles(authProfiles).get(authProfileId); const csrfProfileId = operation.csrfProfileId ?? ""; const csrf = Object.hasOwn(csrfProfiles, csrfProfileId) ? csrfProfiles[csrfProfileId] : undefined; const unsafe = !["GET", "HEAD", "OPTIONS"].includes(operation.method); - if ( - !auth || - !csrf || - !provider.allowedCredentialsModes.includes(auth.credentials) || - (operation.auth === "none" && auth.transport !== "ANONYMOUS") || - (operation.auth === "external-session" && - auth.transport === "ANONYMOUS") || - (auth.transport === "BEARER_HEADER" && csrf.mode !== "NONE") || - (unsafe && - auth.transport === "SAME_ORIGIN_COOKIE" && - csrf.mode !== "HEADER") - ) { - throw new TypeError("REST security profiles are incoherent."); + // 16-03. Every broken condition is named, with the operation and profile + // ids, so a renamed or missing profile does not have to be found by + // comparing each rule by hand. + const broken = [ + !auth && `unknown authProfileId ${authProfileId || "(none)"}`, + !csrf && `unknown csrfProfileId ${csrfProfileId || "(none)"}`, + auth && + !provider.allowedCredentialsModes.includes(auth.credentials) && + `provider ${provider.providerId} does not allow credentials "${auth.credentials}" of ${authProfileId}`, + auth && + operation.auth === "none" && + auth.transport !== "ANONYMOUS" && + `auth "none" needs an ANONYMOUS profile, ${authProfileId} is ${auth.transport}`, + auth && + operation.auth === "external-session" && + auth.transport === "ANONYMOUS" && + `auth "external-session" cannot use the ANONYMOUS profile ${authProfileId}`, + auth?.transport === "BEARER_HEADER" && + csrf && + csrf.mode !== "NONE" && + `bearer profile ${authProfileId} cannot use CSRF mode ${csrf.mode} of ${csrfProfileId}`, + unsafe && + auth?.transport === "SAME_ORIGIN_COOKIE" && + csrf && + csrf.mode !== "HEADER" && + `${operation.method} with cookie profile ${authProfileId} needs a HEADER CSRF profile, ${csrfProfileId} is ${csrf.mode}`, + ].filter((condition): condition is string => typeof condition === "string"); + if (!auth || !csrf || broken.length > 0) { + throw new TypeError( + `REST security profiles are incoherent for ${operation.operationId ?? "an operation"}: ${broken.join("; ")}.`, + ); } return Object.freeze({ auth, csrf }); } diff --git a/tests/unit/rest-profile-contract.test.ts b/tests/unit/rest-profile-contract.test.ts index fba5a25d..629f69d9 100644 --- a/tests/unit/rest-profile-contract.test.ts +++ b/tests/unit/rest-profile-contract.test.ts @@ -103,6 +103,31 @@ describe("REST provider/auth/CSRF profiles", () => { validateRestProfileBindings(SAMPLE_OPERATIONS, {}), ).toThrow("Unregistered REST provider binding"); }); + + // 16-03. A copied feature that renamed the sample's profile id stopped every + // gate with one sentence that named neither the operation nor the profile. + it("names the operation, the profile and the broken condition", () => { + const renamed = Object.freeze({ + CREATE_SAMPLE_RESOURCE: { + ...SAMPLE_COMMAND, + authProfileId: "ORDER_EXTERNAL_BEARER", + }, + }); + expect(() => + validateRestProfileBindings(renamed, { PRIMARY_API: ["omit"] }), + ).toThrow( + /incoherent.*CREATE_SAMPLE_RESOURCE.*unknown authProfileId ORDER_EXTERNAL_BEARER/, + ); + + const publicWithBearer = Object.freeze({ + CREATE_SAMPLE_RESOURCE: { ...SAMPLE_COMMAND, auth: "none" as const }, + }); + expect(() => + validateRestProfileBindings(publicWithBearer, { PRIMARY_API: ["omit"] }), + ).toThrow( + /CREATE_SAMPLE_RESOURCE.*auth "none" needs an ANONYMOUS profile.*REFERENCE_EXTERNAL_BEARER is BEARER_HEADER/, + ); + }); }); /**