Files
clean-architecture-frontend…/src/contracts/contract-set.ts
T
donghyeon-kaandClaude Opus 5.5 9dc95ca066 fix(contracts): set zod jitless before any schema is built so the production csp sees no eval (F-7, 13-08)
zod probed Function("") on its first object schema, which the production CSP
(no unsafe-eval) reported on every boot. Schema code now imports z from
src/contracts/zod.ts, enforced by a dependency-cruiser rule, and the CSP smoke
drops its init-script workaround.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 12:58:57 +09:00

141 lines
4.2 KiB
TypeScript

import { z } from "./zod.ts";
import {
CONTRACT_SET_ALGORITHM,
computeContractSetDigest,
type ContractSetPackage,
} from "./contract-set-canonical.ts";
/**
* §5. Contract set and release coherence.
*
* The frontend verifies that the packages compiled into this build match the
* packages the release manifest declares. It never negotiates ranges, resolves
* `latest`, or infers a provider runtime version.
*/
export type ContractSetFailureCode =
| "CONTRACT_SET_SCHEMA_INVALID"
| "CONTRACT_SET_ENTRY_INVALID"
| "CONTRACT_SET_DUPLICATE_PACKAGE"
| "CONTRACT_SET_DIGEST_INVALID"
| "CONTRACT_SET_DIGEST_MISMATCH"
| "CONTRACT_SET_PACKAGE_MISSING"
| "CONTRACT_SET_PACKAGE_UNEXPECTED"
| "CONTRACT_SET_VERSION_MISMATCH"
| "CONTRACT_RUNTIME_PROTOCOL_UNSUPPORTED";
const digestSchema = z.string().regex(/^sha256:[0-9a-f]{64}$/);
export const contractSetPackageSchema = z
.object({
packageId: z
.string()
.regex(/^@[a-z0-9][a-z0-9._-]{0,62}\/[a-z0-9][a-z0-9._-]{0,62}$/),
version: z
.string()
.regex(
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/,
),
digest: digestSchema,
runtimeProtocolVersion: z.literal(1),
sourceRevision: z.string().regex(/^[0-9a-f]{7,64}$/),
})
.strict();
export const contractSetSchema = z
.object({
setAlgorithm: z.literal(CONTRACT_SET_ALGORITHM),
setDigest: digestSchema,
packages: z.array(contractSetPackageSchema).max(256),
})
.strict();
export type ContractSet = z.output<typeof contractSetSchema>;
export type ContractSetVerification =
| Readonly<{ ok: true }>
| Readonly<{ ok: false; code: ContractSetFailureCode }>;
/**
* §5.5. Every comparison below must hold. There is no precedence rule between
* the embedded expectation and the manifest: a disagreement fails the boot.
*/
export async function verifyContractSet(
input: Readonly<{
expected: readonly ContractSetPackage[];
manifest: ContractSet;
expectedSetDigest?: `sha256:${string}`;
}>,
): Promise<ContractSetVerification> {
const manifestIds = new Set<string>();
for (const entry of input.manifest.packages) {
if (manifestIds.has(entry.packageId)) {
return failure("CONTRACT_SET_DUPLICATE_PACKAGE");
}
manifestIds.add(entry.packageId);
if (entry.runtimeProtocolVersion !== 1) {
return failure("CONTRACT_RUNTIME_PROTOCOL_UNSUPPORTED");
}
}
const expectedById = new Map(
input.expected.map((entry) => [entry.packageId, entry] as const),
);
// Q-6.7-12. A duplicated expectation is a failure code, not a rejection
// from the digest computation further down.
if (expectedById.size !== input.expected.length) {
return failure("CONTRACT_SET_DUPLICATE_PACKAGE");
}
for (const entry of input.manifest.packages) {
if (!expectedById.has(entry.packageId)) {
return failure("CONTRACT_SET_PACKAGE_UNEXPECTED");
}
}
for (const entry of input.expected) {
const found = input.manifest.packages.find(
(candidate) => candidate.packageId === entry.packageId,
);
if (!found) return failure("CONTRACT_SET_PACKAGE_MISSING");
if (
found.version !== entry.version ||
found.sourceRevision !== entry.sourceRevision
) {
return failure("CONTRACT_SET_VERSION_MISMATCH");
}
if (found.digest !== entry.digest) {
return failure("CONTRACT_SET_DIGEST_MISMATCH");
}
}
let recomputed: `sha256:${string}`;
try {
recomputed = await computeContractSetDigest(
input.manifest.packages as readonly ContractSetPackage[],
);
} catch {
return failure("CONTRACT_SET_DIGEST_INVALID");
}
if (recomputed !== input.manifest.setDigest) {
return failure("CONTRACT_SET_DIGEST_MISMATCH");
}
let expectedDigest: `sha256:${string}`;
try {
expectedDigest =
input.expectedSetDigest ??
(await computeContractSetDigest(input.expected));
} catch {
return failure("CONTRACT_SET_DIGEST_INVALID");
}
if (expectedDigest !== input.manifest.setDigest) {
return failure("CONTRACT_SET_DIGEST_MISMATCH");
}
return Object.freeze({ ok: true as const });
}
function failure(code: ContractSetFailureCode): ContractSetVerification {
return Object.freeze({ ok: false as const, code });
}