-
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 1 commit
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,70 @@ | ||||||||||||||||||
| import type { | ||||||||||||||||||
| VerificationError, | ||||||||||||||||||
| VerificationResult, | ||||||||||||||||||
| } from "@emdash-cms/registry-verification/fetch"; | ||||||||||||||||||
| import { VERIFICATION_ERROR_CODES } from "@emdash-cms/registry-verification/fetch"; | ||||||||||||||||||
|
|
||||||||||||||||||
| type VerifierMethod = "fetchArtifact" | "fetchProvenance"; | ||||||||||||||||||
| const VERIFICATION_ERROR_CODE_SET = new Set<string>(VERIFICATION_ERROR_CODES); | ||||||||||||||||||
|
|
||||||||||||||||||
| 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() { | ||||||||||||||||||
| super("Release verifier is unavailable"); | ||||||||||||||||||
| this.name = "VerifierUnavailableError"; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export async function fetchArtifact( | ||||||||||||||||||
| binding: ReleaseVerifierBinding, | ||||||||||||||||||
| url: string, | ||||||||||||||||||
| ): Promise<VerificationResult<Uint8Array>> { | ||||||||||||||||||
| return callVerifier(binding, "fetchArtifact", url); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export async function fetchProvenance( | ||||||||||||||||||
| binding: ReleaseVerifierBinding, | ||||||||||||||||||
| url: string, | ||||||||||||||||||
| ): Promise<VerificationResult<Uint8Array>> { | ||||||||||||||||||
| return callVerifier(binding, "fetchProvenance", url); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| async function callVerifier( | ||||||||||||||||||
| binding: ReleaseVerifierBinding, | ||||||||||||||||||
| method: VerifierMethod, | ||||||||||||||||||
| url: string, | ||||||||||||||||||
| ): Promise<VerificationResult<Uint8Array>> { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const result = await binding[method](url); | ||||||||||||||||||
| if (!isVerificationResult(result)) throw new VerifierUnavailableError(); | ||||||||||||||||||
| return result; | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
| if (error instanceof VerifierUnavailableError) throw error; | ||||||||||||||||||
| throw new VerifierUnavailableError(); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| function isVerificationResult(value: unknown): value is VerificationResult<Uint8Array> { | ||||||||||||||||||
| 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 VerificationError { | ||||||||||||||||||
| return ( | ||||||||||||||||||
| isRecord(value) && | ||||||||||||||||||
| typeof value["code"] === "string" && | ||||||||||||||||||
| VERIFICATION_ERROR_CODE_SET.has(value["code"]) && | ||||||||||||||||||
|
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. [needs fixing] The adapter treats a well-formed The typed service binding already guarantees the result shape, so validate only the shape here, not the specific code.
Suggested change
Then remove the now-dead |
||||||||||||||||||
| 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,81 @@ | ||
| 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("fails closed with a retryable generic error when the binding rejects", async () => { | ||
| const binding = verifierBinding({ | ||
| fetchArtifact: vi.fn().mockRejectedValue(new Error("internal service address")), | ||
| }); | ||
| 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, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| null, | ||
| { success: true, value: "not bytes" }, | ||
| { success: false, error: { code: "NEW_VERSION_CODE", message: "version skew" } }, | ||
|
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. [needs fixing] This test case labels a valid version-skew error result as a “malformed RPC result.” A Move it out of the malformed list and assert that the adapter returns the error result unchanged, instead of normalizing it to a retryable verifier-unavailable error. |
||
| { 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.