fix(contracts): resolve REST auth profiles from the installed registry (Q-6.7-9)
Composition checked authProfileId against the module-global installed registry, the V3 executor used its injected registry, and the V2 client resolved against the raw REST_AUTH_PROFILES record (or an injected raw record) that was never installed. A BEARER_HEADER profile with `credentials: "same-origin"` is refused by installation but resolved fine. - resolveRestSecurityProfiles / validateRestProfileBindings take `RestAuthProfileSource` (installed registry or raw record) and default to INSTALLED_REST_AUTH_PROFILES. A raw record is installed (validated) before lookup; lookups go through the registry, and the CSRF record lookup uses Object.hasOwn. - composeContractContributions takes the registry as an optional second parameter (default INSTALLED_REST_AUTH_PROFILES). - src/adapters/http/client.ts: only the V2 resolve call site (and its import) now falls back to INSTALLED_REST_AUTH_PROFILES. Verified: 3 new cases in tests/unit/rest-profile-contract.test.ts were RED before (incoherent raw record accepted, installed registry not resolvable, composition ignored the given registry); gates green except the known removal-fixture test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
56de543d49
commit
5124c2f65f
@@ -48,7 +48,7 @@ import type { MappingResult } from "../../contracts/boundary-mapper.ts";
|
||||
import {
|
||||
createRestProviderProfile,
|
||||
resolveRestSecurityProfiles,
|
||||
REST_AUTH_PROFILES,
|
||||
INSTALLED_REST_AUTH_PROFILES,
|
||||
REST_CSRF_PROFILES,
|
||||
type RestAuthProfile,
|
||||
type RestCsrfProfile,
|
||||
@@ -561,7 +561,7 @@ export function createHttpClient(
|
||||
security = resolveRestSecurityProfiles(
|
||||
operation,
|
||||
provider,
|
||||
dependencies.authProfiles ?? REST_AUTH_PROFILES,
|
||||
dependencies.authProfiles ?? INSTALLED_REST_AUTH_PROFILES,
|
||||
dependencies.csrfProfiles ?? REST_CSRF_PROFILES,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,10 @@
|
||||
*/
|
||||
|
||||
import { isOperationId } from "./operation-id.ts";
|
||||
import { INSTALLED_REST_AUTH_PROFILES } from "./rest-profiles.ts";
|
||||
import {
|
||||
INSTALLED_REST_AUTH_PROFILES,
|
||||
type InstalledRestAuthProfiles,
|
||||
} from "./rest-profiles.ts";
|
||||
import {
|
||||
defineTrafficBudget,
|
||||
TRAFFIC_BUDGET_KEYS,
|
||||
@@ -324,6 +327,7 @@ function assertStatuses(
|
||||
function assertExecutionPolicy(
|
||||
policy: HttpExecutionPolicy,
|
||||
label: string,
|
||||
authProfiles: InstalledRestAuthProfiles,
|
||||
): void {
|
||||
const ceilings = HTTP_EXECUTION_CEILINGS;
|
||||
if (
|
||||
@@ -339,7 +343,7 @@ function assertExecutionPolicy(
|
||||
}
|
||||
// §7.7 / VD-23. A declared profile that the installed registry does not own
|
||||
// is a composition failure; the executor must never resolve it at runtime.
|
||||
if (!INSTALLED_REST_AUTH_PROFILES.has(policy.authProfileId)) {
|
||||
if (!authProfiles.has(policy.authProfileId)) {
|
||||
fail(`${label}: unknown authProfileId ${policy.authProfileId}`);
|
||||
}
|
||||
if (
|
||||
@@ -384,6 +388,7 @@ function assertExecutionPolicy(
|
||||
function assertHttpContract(
|
||||
installed: InstalledHttpContract<unknown, unknown, unknown>,
|
||||
featureId: string,
|
||||
authProfiles: InstalledRestAuthProfiles,
|
||||
): void {
|
||||
const contract = installed?.contract;
|
||||
if (!contract || typeof contract !== "object") {
|
||||
@@ -480,7 +485,7 @@ function assertHttpContract(
|
||||
fail(`${label}: command recovery descriptor`);
|
||||
}
|
||||
}
|
||||
assertExecutionPolicy(installed.frontend, label);
|
||||
assertExecutionPolicy(installed.frontend, label, authProfiles);
|
||||
if (
|
||||
installed.frontend.retryBudget > 0 &&
|
||||
contract.retrySemantics === "NEVER"
|
||||
@@ -652,6 +657,11 @@ function snapshotEventContract(
|
||||
*/
|
||||
export function composeContractContributions(
|
||||
contributions: readonly InstalledContractContribution[],
|
||||
/**
|
||||
* Q-6.7-9. The registry the executor will resolve against. Pass the same
|
||||
* one the executor receives; the default is the shared installed registry.
|
||||
*/
|
||||
authProfiles: InstalledRestAuthProfiles = INSTALLED_REST_AUTH_PROFILES,
|
||||
): ComposedContractContributions {
|
||||
const rows =
|
||||
snapshotExactArray(contributions) ?? fail("contributions: array required");
|
||||
@@ -778,7 +788,7 @@ export function composeContractContributions(
|
||||
installed as InstalledHttpContract<unknown, unknown, unknown>,
|
||||
featureId,
|
||||
);
|
||||
assertHttpContract(snapshot, featureId);
|
||||
assertHttpContract(snapshot, featureId, authProfiles);
|
||||
const operationId = snapshot.contract.operationId;
|
||||
const previous = httpByOperationId.get(operationId);
|
||||
if (previous) fail(`duplicate operation: ${operationId}`);
|
||||
|
||||
@@ -197,7 +197,35 @@ export function installRestAuthProfileRegistry(
|
||||
if (installed.size === 0) {
|
||||
throw new TypeError("REST auth profile registry cannot be empty.");
|
||||
}
|
||||
return createReadOnlyRegistry(installed);
|
||||
const registry = createReadOnlyRegistry(installed);
|
||||
INSTALLED_REGISTRIES.add(registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
/** Registries this module installed, as opposed to look-alike objects. */
|
||||
const INSTALLED_REGISTRIES = new WeakSet<object>();
|
||||
|
||||
/**
|
||||
* REST auth profiles as accepted by resolve: the installed registry, or a raw
|
||||
* record that is installed (validated) before any lookup.
|
||||
*/
|
||||
export type RestAuthProfileSource =
|
||||
| InstalledRestAuthProfiles
|
||||
| Readonly<Record<string, RestAuthProfile>>;
|
||||
|
||||
/**
|
||||
* Q-6.7-9. Composition, the V3 executor and V2 resolve answer from one
|
||||
* validated registry. A raw record never reaches a lookup unvalidated, so a
|
||||
* profile installation would refuse cannot be resolved either.
|
||||
*/
|
||||
function installedAuthProfiles(
|
||||
profiles: RestAuthProfileSource,
|
||||
): InstalledRestAuthProfiles {
|
||||
return INSTALLED_REGISTRIES.has(profiles)
|
||||
? (profiles as InstalledRestAuthProfiles)
|
||||
: installRestAuthProfileRegistry(
|
||||
profiles as Readonly<Record<string, RestAuthProfile>>,
|
||||
);
|
||||
}
|
||||
|
||||
/** The single installed registry every composition root shares. */
|
||||
@@ -250,11 +278,16 @@ export function resolveRestSecurityProfiles(
|
||||
csrfProfileId?: string;
|
||||
}>,
|
||||
provider: RestProviderProfile,
|
||||
authProfiles: Readonly<Record<string, RestAuthProfile>> = REST_AUTH_PROFILES,
|
||||
authProfiles: RestAuthProfileSource = INSTALLED_REST_AUTH_PROFILES,
|
||||
csrfProfiles: Readonly<Record<string, RestCsrfProfile>> = REST_CSRF_PROFILES,
|
||||
): Readonly<{ auth: RestAuthProfile; csrf: RestCsrfProfile }> {
|
||||
const auth = authProfiles[operation.authProfileId ?? ""];
|
||||
const csrf = csrfProfiles[operation.csrfProfileId ?? ""];
|
||||
const auth = installedAuthProfiles(authProfiles).get(
|
||||
operation.authProfileId ?? "",
|
||||
);
|
||||
const csrfProfileId = operation.csrfProfileId ?? "";
|
||||
const csrf = Object.hasOwn(csrfProfiles, csrfProfileId)
|
||||
? csrfProfiles[csrfProfileId]
|
||||
: undefined;
|
||||
const unsafe = !["GET", "HEAD", "OPTIONS"].includes(operation.method);
|
||||
if (
|
||||
!auth ||
|
||||
@@ -291,9 +324,10 @@ export function validateRestProfileBindings(
|
||||
providerCredentialModes: Readonly<
|
||||
Record<string, readonly FetchCredentialsMode[]>
|
||||
>,
|
||||
authProfiles: Readonly<Record<string, RestAuthProfile>> = REST_AUTH_PROFILES,
|
||||
authProfiles: RestAuthProfileSource = INSTALLED_REST_AUTH_PROFILES,
|
||||
csrfProfiles: Readonly<Record<string, RestCsrfProfile>> = REST_CSRF_PROFILES,
|
||||
): true {
|
||||
const installed = installedAuthProfiles(authProfiles);
|
||||
for (const operation of Object.values(operations)) {
|
||||
if (operation.contractVersion !== 2) continue;
|
||||
const allowed = providerCredentialModes[operation.providerId ?? ""];
|
||||
@@ -311,7 +345,7 @@ export function validateRestProfileBindings(
|
||||
redirect: "error",
|
||||
referrerPolicy: "no-referrer",
|
||||
}),
|
||||
authProfiles,
|
||||
installed,
|
||||
csrfProfiles,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { defineRestOperation } from "../../src/contracts/api-operations.ts";
|
||||
import {
|
||||
composeContractContributions,
|
||||
ContractContributionError,
|
||||
} from "../../src/contracts/external-contract-runtime.ts";
|
||||
import {
|
||||
createRestProviderProfile,
|
||||
INSTALLED_REST_AUTH_PROFILES,
|
||||
installRestAuthProfileRegistry,
|
||||
resolveRestSecurityProfiles,
|
||||
REST_AUTH_PROFILES,
|
||||
validateRestProfileBindings,
|
||||
} from "../../src/contracts/rest-profiles.ts";
|
||||
import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts";
|
||||
|
||||
/**
|
||||
* §24.12: the common REST profile contract must stay independently verifiable
|
||||
@@ -96,3 +104,71 @@ describe("REST provider/auth/CSRF profiles", () => {
|
||||
).toThrow("Unregistered REST provider binding");
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Q-6.7-9. Composition, the V3 executor and the V2 resolve must all answer
|
||||
* from one validated installed registry. A raw record handed to resolve used
|
||||
* to skip installation, so a bearer profile with ambient credentials passed.
|
||||
*/
|
||||
describe("one installed REST auth profile registry", () => {
|
||||
const provider = createRestProviderProfile("PRIMARY_API", "https://api.test", [
|
||||
"omit",
|
||||
"same-origin",
|
||||
]);
|
||||
|
||||
it("rejects a raw profile record that installation would refuse", () => {
|
||||
const incoherent = {
|
||||
REFERENCE_EXTERNAL_BEARER: {
|
||||
...REST_AUTH_PROFILES.REFERENCE_EXTERNAL_BEARER,
|
||||
credentials: "same-origin" as const,
|
||||
},
|
||||
};
|
||||
expect(() =>
|
||||
resolveRestSecurityProfiles(SAMPLE_COMMAND, provider, incoherent),
|
||||
).toThrow(TypeError);
|
||||
});
|
||||
|
||||
it("resolves through the installed registry", () => {
|
||||
expect(
|
||||
resolveRestSecurityProfiles(
|
||||
SAMPLE_COMMAND,
|
||||
provider,
|
||||
INSTALLED_REST_AUTH_PROFILES,
|
||||
).auth,
|
||||
).toBe(INSTALLED_REST_AUTH_PROFILES.get("REFERENCE_EXTERNAL_BEARER"));
|
||||
});
|
||||
|
||||
it("does not resolve an inherited profile id", () => {
|
||||
expect(() =>
|
||||
resolveRestSecurityProfiles(
|
||||
{ ...SAMPLE_COMMAND, authProfileId: "toString" },
|
||||
provider,
|
||||
REST_AUTH_PROFILES,
|
||||
),
|
||||
).toThrow("incoherent");
|
||||
});
|
||||
|
||||
it("composes contributions against the registry it is given", () => {
|
||||
const registry = installRestAuthProfileRegistry({
|
||||
...REST_AUTH_PROFILES,
|
||||
CUSTOM_BEARER: {
|
||||
...REST_AUTH_PROFILES.REFERENCE_EXTERNAL_BEARER,
|
||||
authProfileId: "CUSTOM_BEARER",
|
||||
},
|
||||
});
|
||||
const row = TEST_CONTRACT_CONTRIBUTION.http[0]!;
|
||||
const contribution = {
|
||||
...TEST_CONTRACT_CONTRIBUTION,
|
||||
http: [
|
||||
{ ...row, frontend: { ...row.frontend, authProfileId: "CUSTOM_BEARER" } },
|
||||
],
|
||||
};
|
||||
expect(() => composeContractContributions([contribution])).toThrow(
|
||||
ContractContributionError,
|
||||
);
|
||||
expect(
|
||||
composeContractContributions([contribution], registry).httpByOperationId
|
||||
.size,
|
||||
).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user