-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(release-service): isolate verification egress #1988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
c494fc0
67ba3c9
5559194
97afb8c
638f9df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@emdash-cms/registry-verification": minor | ||
| --- | ||
|
|
||
| Adds a Worker-safe safe-fetch entry point for isolated resource verification. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| type VerifierMethod = "fetchArtifact" | "fetchProvenance"; | ||
|
|
||
| export type ReleaseVerifierResult = | ||
| | { success: true; value: Uint8Array } | ||
| | { success: false; error: { code: string; message: string } }; | ||
|
|
||
| export interface ReleaseVerifierBinding { | ||
| fetchArtifact(url: string): Promise<unknown>; | ||
| fetchProvenance(url: string): Promise<unknown>; | ||
| } | ||
|
|
||
| export class VerifierUnavailableError extends Error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] The catch in Consider stashing the original error as export class VerifierUnavailableError extends Error {
readonly retryable = true;
constructor(cause?: unknown) {
super("Release verifier is unavailable", { cause });
this.name = "VerifierUnavailableError";
}
}and then rethrowing |
||
| readonly retryable = true; | ||
|
|
||
| constructor(options?: ErrorOptions) { | ||
| super("Release verifier is unavailable", options); | ||
| this.name = "VerifierUnavailableError"; | ||
| } | ||
| } | ||
|
|
||
| export async function fetchArtifact( | ||
| binding: ReleaseVerifierBinding, | ||
| url: string, | ||
| ): Promise<ReleaseVerifierResult> { | ||
| return callVerifier(binding, "fetchArtifact", url); | ||
| } | ||
|
|
||
| export async function fetchProvenance( | ||
| binding: ReleaseVerifierBinding, | ||
| url: string, | ||
| ): Promise<ReleaseVerifierResult> { | ||
| return callVerifier(binding, "fetchProvenance", url); | ||
| } | ||
|
|
||
| async function callVerifier( | ||
| binding: ReleaseVerifierBinding, | ||
| method: VerifierMethod, | ||
| url: string, | ||
| ): Promise<ReleaseVerifierResult> { | ||
| try { | ||
| const result = await binding[method](url); | ||
| if (!isVerificationResult(result)) { | ||
| throw new TypeError("Release verifier returned an invalid response"); | ||
| } | ||
| return result; | ||
| } catch (error) { | ||
| if (error instanceof VerifierUnavailableError) throw error; | ||
| throw new VerifierUnavailableError({ cause: error }); | ||
| } | ||
| } | ||
|
|
||
| function isVerificationResult(value: unknown): value is ReleaseVerifierResult { | ||
| if (!isRecord(value) || typeof value["success"] !== "boolean") return false; | ||
| if (value["success"] === true) return value["value"] instanceof Uint8Array; | ||
| return isVerificationError(value["error"]); | ||
| } | ||
|
|
||
| function isVerificationError(value: unknown): value is { code: string; message: string } { | ||
| return ( | ||
| isRecord(value) && typeof value["code"] === "string" && typeof value["message"] === "string" | ||
| ); | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return value !== null && typeof value === "object" && !Array.isArray(value); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { WorkerEntrypoint } from "cloudflare:workers"; | ||
|
|
||
| export default class TestReleaseVerifier extends WorkerEntrypoint { | ||
| fetchArtifact(url) { | ||
| if (url.endsWith("/unavailable")) throw new Error("internal service address"); | ||
| return { success: true, value: new Uint8Array([7]) }; | ||
| } | ||
|
|
||
| fetchProvenance() { | ||
| return { success: true, value: "not bytes" }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { env } from "cloudflare:workers"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { | ||
| fetchArtifact, | ||
| fetchProvenance, | ||
| type ReleaseVerifierBinding, | ||
| VerifierUnavailableError, | ||
| } from "../src/verifier.js"; | ||
|
|
||
| function verifierBinding(overrides: Partial<ReleaseVerifierBinding> = {}): ReleaseVerifierBinding { | ||
| return { | ||
| fetchArtifact: async () => ({ success: true, value: new Uint8Array([1]) }), | ||
| fetchProvenance: async () => ({ success: true, value: new Uint8Array([2]) }), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("release verifier binding adapter", () => { | ||
| it("validates results across the configured RPC service binding", async () => { | ||
| await expect( | ||
| fetchArtifact(env.RELEASE_VERIFIER, "https://example.test/plugin.tgz"), | ||
| ).resolves.toEqual({ success: true, value: new Uint8Array([7]) }); | ||
| await expect( | ||
| fetchProvenance(env.RELEASE_VERIFIER, "https://example.test/provenance.json"), | ||
| ).rejects.toBeInstanceOf(VerifierUnavailableError); | ||
| }); | ||
|
|
||
| it("normalizes configured service-binding failures", async () => { | ||
| await expect( | ||
| fetchArtifact(env.RELEASE_VERIFIER, "https://example.test/unavailable"), | ||
| ).rejects.toMatchObject({ | ||
| message: "Release verifier is unavailable", | ||
| retryable: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns successful bytes and stable verification failures", async () => { | ||
| await expect( | ||
| fetchArtifact(verifierBinding(), "https://example.test/plugin.tgz"), | ||
| ).resolves.toEqual({ | ||
| success: true, | ||
| value: new Uint8Array([1]), | ||
| }); | ||
| const failure = { | ||
| success: false, | ||
| error: { code: "RESOURCE_STATUS_ERROR", message: "The resource returned HTTP 404." }, | ||
| }; | ||
| await expect( | ||
| fetchProvenance( | ||
| verifierBinding({ fetchProvenance: async () => failure }), | ||
| "https://example.test/provenance.json", | ||
| ), | ||
| ).resolves.toEqual(failure); | ||
| }); | ||
|
|
||
| it("preserves definitive failures from a newer verifier", async () => { | ||
| const failure = { | ||
| success: false as const, | ||
| error: { code: "NEW_VERSION_CODE", message: "new definitive failure" }, | ||
| }; | ||
|
|
||
| await expect( | ||
| fetchArtifact( | ||
| verifierBinding({ fetchArtifact: async () => failure }), | ||
| "https://example.test/plugin.tgz", | ||
| ), | ||
| ).resolves.toEqual(failure); | ||
| }); | ||
|
|
||
| it("fails closed with a retryable generic error when the binding rejects", async () => { | ||
| const cause = new Error("internal service address"); | ||
| const binding = verifierBinding({ | ||
| fetchArtifact: vi.fn().mockRejectedValue(cause), | ||
| }); | ||
| const result = fetchArtifact(binding, "https://example.test/plugin.tgz"); | ||
| await expect(result).rejects.toBeInstanceOf(VerifierUnavailableError); | ||
| await expect(result).rejects.toMatchObject({ | ||
| message: "Release verifier is unavailable", | ||
| retryable: true, | ||
| cause, | ||
| }); | ||
| }); | ||
|
|
||
| it("preserves malformed response details as a diagnostic cause", async () => { | ||
| const binding = verifierBinding({ fetchArtifact: async () => null }); | ||
|
|
||
| await expect(fetchArtifact(binding, "https://example.test/plugin.tgz")).rejects.toMatchObject({ | ||
| name: "VerifierUnavailableError", | ||
| cause: expect.objectContaining({ | ||
| name: "TypeError", | ||
| message: "Release verifier returned an invalid response", | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| null, | ||
| { success: true, value: "not bytes" }, | ||
| { success: false, error: { code: "FETCH_FAILED" } }, | ||
| ])("rejects malformed RPC result %#", async (rpcResult) => { | ||
| const binding = verifierBinding({ fetchArtifact: async () => rpcResult }); | ||
| await expect(fetchArtifact(binding, "https://example.test/plugin.tgz")).rejects.toMatchObject({ | ||
| name: "VerifierUnavailableError", | ||
| retryable: true, | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "@emdash-cms/release-verifier", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "description": "Isolated egress Worker for delegated release verification.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "wrangler deploy --dry-run --outdir dist", | ||
| "deploy": "wrangler deploy", | ||
| "typecheck": "tsgo --noEmit", | ||
| "test": "vitest run", | ||
| "types": "wrangler types" | ||
| }, | ||
| "dependencies": { | ||
| "@emdash-cms/registry-verification": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/vitest-pool-workers": "catalog:", | ||
| "@types/node": "catalog:", | ||
| "typescript": "catalog:", | ||
| "vite": "catalog:", | ||
| "vitest": "catalog:", | ||
| "wrangler": "catalog:" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] The added
@emdash-cms/registry-verificationdependency is not imported anywhere inapps/release-service(the verifier app imports it, and the service-binding types are generated from../release-verifier/wrangler.jsonc). It adds unused weight to the lockfile and is out of scope for this package.