UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 15.7 kB
{"version":3,"file":"chain.mjs","names":[],"sources":["../../../src/services/verify/chain.ts"],"sourcesContent":["import {\n signaturePayloadOf,\n type WorkspaceVersionManifest,\n WorkspaceVersionManifestSchema,\n} from \"every-plugin/version-manifest\";\nimport { ZodError } from \"zod\";\nimport type { BosConfigInput } from \"../../types\";\nimport { verifyManifestSignature } from \"../deploy/manifest-signature\";\nimport { manifestDocumentUrl } from \"../deploy/manifest-verify\";\nimport { walkRemotes } from \"../remotes\";\nimport { asDigest, fetchVerified, isVerifyError, verifyDocument } from \"./engine\";\n\nexport type ChainDepth = \"pointer\" | \"entries\" | \"files\";\n\ninterface PinnedRemoteRef {\n production?: unknown;\n digest?: { manifest?: unknown; integrity?: unknown };\n}\n\nexport function collectPinnedRemotes(\n config: BosConfigInput,\n): Array<{ remote: string; ref: PinnedRemoteRef }> {\n const remotes: Array<{ remote: string; ref: PinnedRemoteRef }> = [];\n walkRemotes(config as unknown as Record<string, unknown>, ({ remote, entry }) => {\n if (entry && typeof entry === \"object\") remotes.push({ remote, ref: entry as PinnedRemoteRef });\n });\n return remotes;\n}\n\nexport function digestOf(ref: PinnedRemoteRef): { manifest: string; integrity: string } | null {\n const manifest = typeof ref.digest?.manifest === \"string\" ? ref.digest.manifest : undefined;\n const integrity = typeof ref.digest?.integrity === \"string\" ? ref.digest.integrity : undefined;\n return manifest && integrity ? { manifest, integrity } : null;\n}\n\nexport interface RemoteDescriptor {\n remote: string;\n /** the remote's production base URL (…/bundles/<account>/<gateway>/<workspace>/) */\n base: string;\n digest: { manifest: string; integrity: string };\n}\n\n/** Every pinned remote with both a production base and a verifiable pin. */\nexport function collectRemoteDescriptors(config: BosConfigInput): RemoteDescriptor[] {\n return collectPinnedRemotes(config)\n .map(({ remote, ref }) => {\n const digest = digestOf(ref);\n const base = typeof ref.production === \"string\" ? ref.production : undefined;\n return digest && base ? { remote, base, digest } : null;\n })\n .filter((entry): entry is RemoteDescriptor => entry !== null);\n}\n\nexport interface VerifyFileMismatch {\n file: string;\n expected: string;\n actual?: string;\n error?: string;\n}\n\nexport interface VerifyRemoteResult {\n remote: string;\n base: string;\n manifest: string;\n manifestOk: boolean;\n manifestReason?: string;\n /** attribution leg (ADR 0046 ticket 05) — present only when the walked\n * config demands attribution (`publish.verification`); a false `ok` is a\n * hard failure for the chain (adopt/smoke/`bos verify`), independent of\n * the integrity legs. */\n signature?: { ok: boolean; reason?: string };\n /** the immutable version directory the pin resolves into (absent on legacy pins) */\n dir?: string;\n legacy: boolean;\n filesChecked: number;\n mismatches: VerifyFileMismatch[];\n}\n\nexport interface VerifyReport {\n remotes: VerifyRemoteResult[];\n}\n\nexport interface ChainEntryResult {\n remote: string;\n url: string;\n ok: boolean;\n reason?: string;\n}\n\nexport interface ChainVerifyResult {\n depth: ChainDepth;\n remotes: VerifyRemoteResult[];\n entries: ChainEntryResult[];\n ok: boolean;\n}\n\nconst VERIFY_CONCURRENCY = 8;\n\nconst messageOf = (cause: unknown): string =>\n cause instanceof Error ? cause.message : String(cause);\n\nconst manifestLegReason = (error: unknown): string => {\n if (!isVerifyError(error)) return messageOf(error);\n if (error._tag === \"IntegrityMismatch\") {\n return `manifest SRI mismatch — pin ${error.expected}, served ${error.actual}`;\n }\n if (error._tag === \"ProvenanceUnknown\") {\n if (error.cause instanceof ZodError) return `manifest schema: ${error.cause.message}`;\n if (error.status !== undefined) return `manifest GET ${error.status}`;\n return `manifest fetch failed: ${messageOf(error.cause)}`;\n }\n return `manifest signature: ${error.reason}`;\n};\n\nconst entryLegReason = (error: unknown): string => {\n if (!isVerifyError(error)) return messageOf(error);\n if (error._tag === \"IntegrityMismatch\") {\n return `entry SRI mismatch — pin ${error.expected}, served ${error.actual}`;\n }\n if (error._tag === \"ProvenanceUnknown\") {\n if (error.status !== undefined) return `entry GET ${error.status}`;\n return `entry fetch failed: ${messageOf(error.cause)}`;\n }\n return `entry signature: ${error.reason}`;\n};\n\nconst fileBaseOf = (base: string, dir: string | undefined): string =>\n dir ? `${base.replace(/\\/$/, \"\")}/${dir}` : base.replace(/\\/$/, \"\");\n\n/** The attribution demand the walked config carries — `undefined`/`\"off\"`\n * means the signature leg is skipped (attribution-off rollouts verify as\n * today). */\ntype VerificationDemand = { publicKey: string } | \"off\" | undefined;\n\nasync function walkRemotePin(\n descriptor: { remote: string; base: string; digest: { manifest: string; integrity: string } },\n depth: ChainDepth,\n fetchImpl: typeof fetch,\n verification: VerificationDemand,\n): Promise<{ remote: VerifyRemoteResult; entryResults: ChainEntryResult[] }> {\n const manifestUrl = manifestDocumentUrl(descriptor.base, descriptor.digest.manifest);\n const remote: VerifyRemoteResult = {\n remote: descriptor.remote,\n base: descriptor.base,\n manifest: descriptor.digest.manifest,\n manifestOk: false,\n legacy: true,\n filesChecked: 0,\n mismatches: [],\n };\n const entryResults: ChainEntryResult[] = [];\n\n let manifest: WorkspaceVersionManifest;\n try {\n manifest = await verifyDocument({\n url: manifestUrl,\n expected: asDigest(descriptor.digest.integrity),\n schema: WorkspaceVersionManifestSchema,\n fetchImpl,\n cache: false,\n });\n } catch (error) {\n remote.manifestReason = manifestLegReason(error);\n return { remote, entryResults };\n }\n\n remote.manifestOk = true;\n if (manifest.dir) {\n remote.dir = manifest.dir;\n remote.legacy = false;\n }\n\n // Attribution before any integrity check is reported (ADR 0046 ticket 05):\n // the signature leg is distinct from the SRI legs, and a failure here is a\n // hard chain failure — but the integrity legs still run and report, so a\n // rotated key record shows exactly what stayed sound.\n if (verification && verification !== \"off\") {\n try {\n verifyManifestSignature(\n signaturePayloadOf(manifest),\n manifest.signature,\n verification.publicKey,\n );\n remote.signature = { ok: true };\n } catch (error) {\n remote.signature = { ok: false, reason: manifestLegReason(error) };\n }\n }\n const fileBase = fileBaseOf(descriptor.base, manifest.dir);\n\n if (depth === \"entries\") {\n const targets = [\n { url: `${fileBase}/${manifest.entry}`, integrity: manifest.entryIntegrity },\n ...(manifest.ssr\n ? [{ url: `${fileBase}/${manifest.ssr.entry}`, integrity: manifest.ssr.integrity }]\n : []),\n ];\n for (const target of targets) {\n try {\n await fetchVerified({\n url: target.url,\n expected: asDigest(target.integrity),\n fetchImpl,\n cache: false,\n });\n entryResults.push({ remote: descriptor.remote, url: target.url, ok: true });\n } catch (error) {\n entryResults.push({\n remote: descriptor.remote,\n url: target.url,\n ok: false,\n reason: entryLegReason(error),\n });\n }\n }\n }\n\n if (depth === \"files\") {\n const entries = Object.entries(manifest.files ?? {});\n remote.filesChecked = entries.length;\n\n const check = async ([path, expected]: [\n string,\n string,\n ]): Promise<VerifyFileMismatch | null> => {\n try {\n await fetchVerified({\n url: `${fileBase}/${path}`,\n expected: asDigest(expected),\n fetchImpl,\n cache: false,\n });\n return null;\n } catch (error) {\n if (isVerifyError(error)) {\n if (error._tag === \"IntegrityMismatch\") {\n return { file: path, expected, actual: error.actual };\n }\n if (error._tag === \"ProvenanceUnknown\") {\n return {\n file: path,\n expected,\n error: error.status !== undefined ? `GET ${error.status}` : messageOf(error.cause),\n };\n }\n return { file: path, expected, error: error.reason };\n }\n return { file: path, expected, error: messageOf(error) };\n }\n };\n\n const mismatches: VerifyFileMismatch[] = [];\n let cursor = 0;\n const workers = Array.from(\n { length: Math.min(VERIFY_CONCURRENCY, entries.length) },\n async () => {\n while (cursor < entries.length) {\n const entry = entries[cursor++];\n if (!entry) continue;\n const mismatch = await check(entry);\n if (mismatch) mismatches.push(mismatch);\n }\n },\n );\n await Promise.all(workers);\n remote.mismatches = mismatches.sort((a, b) => a.file.localeCompare(b.file));\n }\n\n return { remote, entryResults };\n}\n\nexport async function verifyChain(input: {\n config: BosConfigInput;\n depth: ChainDepth;\n fetchImpl?: typeof fetch;\n}): Promise<ChainVerifyResult> {\n const descriptors = collectRemoteDescriptors(input.config);\n const remotes: VerifyRemoteResult[] = [];\n const entries: ChainEntryResult[] = [];\n const fetchImpl = input.fetchImpl ?? fetch;\n const verification = input.config.publish?.verification;\n for (const descriptor of descriptors) {\n const walk = await walkRemotePin(descriptor, input.depth, fetchImpl, verification);\n remotes.push(walk.remote);\n entries.push(...walk.entryResults);\n }\n const ok =\n remotes.every(\n (remote) =>\n remote.manifestOk && remote.mismatches.length === 0 && remote.signature?.ok !== false,\n ) && entries.every((entry) => entry.ok);\n return { depth: input.depth, remotes, entries, ok };\n}\n\nexport function formatChainFailures(result: ChainVerifyResult): string {\n const lines: string[] = [];\n for (const remote of result.remotes) {\n if (!remote.manifestOk) {\n lines.push(`${remote.remote}: ${remote.manifestReason}`);\n }\n if (remote.signature && !remote.signature.ok) {\n lines.push(`${remote.remote}: signature — ${remote.signature.reason}`);\n }\n for (const mismatch of remote.mismatches) {\n const detail = mismatch.actual\n ? `expected ${mismatch.expected}, served ${mismatch.actual}`\n : (mismatch.error ?? \"mismatch\");\n lines.push(`${remote.remote}/${mismatch.file}: ${detail}`);\n }\n }\n for (const entry of result.entries) {\n if (!entry.ok) lines.push(`${entry.remote} entry: ${entry.url} — ${entry.reason}`);\n }\n return lines.join(\"\\n\");\n}\n"],"mappings":";;;;;;;;AAmBA,SAAgB,qBACd,QACiD;CACjD,MAAM,UAA2D,CAAC;CAClE,YAAY,SAA+C,EAAE,QAAQ,YAAY;EAC/E,IAAI,SAAS,OAAO,UAAU,UAAU,QAAQ,KAAK;GAAE;GAAQ,KAAK;EAAyB,CAAC;CAChG,CAAC;CACD,OAAO;AACT;AAEA,SAAgB,SAAS,KAAsE;CAC7F,MAAM,WAAW,OAAO,IAAI,QAAQ,aAAa,WAAW,IAAI,OAAO,WAAW;CAClF,MAAM,YAAY,OAAO,IAAI,QAAQ,cAAc,WAAW,IAAI,OAAO,YAAY;CACrF,OAAO,YAAY,YAAY;EAAE;EAAU;CAAU,IAAI;AAC3D;;AAUA,SAAgB,yBAAyB,QAA4C;CACnF,OAAO,qBAAqB,MAAM,CAAC,CAChC,KAAK,EAAE,QAAQ,UAAU;EACxB,MAAM,SAAS,SAAS,GAAG;EAC3B,MAAM,OAAO,OAAO,IAA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