// verifyEvidence keeps three verdicts apart: the signature, the issuer, and what the receipt declares. import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { fetchIndex, fingerprint, verifyEvidence, ISSUER_STATUS, OUTCOMES, VERDICT_OF } from "../src/index.js"; import { loadFixture, makeKey, signBody, FIXTURE_PUBLIC_KEY, FIXTURE_FINGERPRINT } from "./helpers.js"; const PORTABLE = new URL("../../../schemas/forget-me/v1/fixtures/", import.meta.url); const loadPortable = async (rel) => JSON.parse(await readFile(new URL(rel, PORTABLE), "utf8")); const indexFor = (keys, issuer = "Bay Parcel (reference Company)") => ({ namespace: "stellarminds.ai/forget-me", version: "1", issuer, keys: keys.map((k) => ({ public_key: k, key_fingerprint: "0".repeat(32) })), routes: {}, scopes: ["item"], evidence_format: "forget-me-receipt/1", status: "third-party proposal", limits: {}, }); describe("a portable receipt", () => { test("signature valid, issuer unconfirmed without key discovery: the embedded key alone confirms nobody", async () => { const receipt = await loadPortable("receipt/valid/inspected-and-clear.json"); const r = await verifyEvidence(receipt); assert.equal(r.signature.ok, true); assert.equal(r.signature.fingerprint, await fingerprint(receipt.public_key)); assert.equal(r.issuer.status, "unconfirmed"); assert.match(r.issuer.why, /never confirms an issuer by itself/); assert.equal(r.issuer.claimed, receipt.issuer); assert.equal(r.ok, false, "well-signed is not enough"); assert.equal(r.outcome.kind, "portable"); assert.equal(r.outcome.outcome, "inspected-and-clear"); assert.equal(r.outcome.verdict, "CLEAN"); assert.equal(r.outcome.consistent, true); assert.equal(r.outcome.settled, true); assert.equal(r.outcome.coverage.inspected, true); assert.ok(r.outcome.coverage.checks.length >= 1); assert.equal(r.files.checked, false); assert.match(r.files.note, /carries no file hashes/); assert.equal(r.summary.length, 4); assert.match(r.summary[0], /^Signature valid/); assert.match(r.summary[1], /^Issuer unconfirmed/); assert.match(r.summary[2], /^Declared by the issuer/); }); test("issuer confirmed when the key is in the index fetched from the expected origin", async () => { const receipt = await loadPortable("receipt/valid/intentionally-retained.json"); const doc = indexFor([makeKey().publicKey, receipt.public_key], receipt.issuer); const r = await verifyEvidence(receipt, { issuer: { doc, url: "https://company.example/forget-me/v1" } }); assert.equal(r.signature.ok, true); assert.equal(r.issuer.status, "confirmed"); assert.equal(r.issuer.source, "index"); assert.equal(r.issuer.url, "https://company.example/forget-me/v1"); assert.equal(r.issuer.published, receipt.issuer); assert.equal(r.issuer.nameMatches, true); assert.equal(r.ok, true); assert.equal(r.outcome.outcome, "intentionally-retained"); assert.equal(r.outcome.verdict, "RESIDUE_REMAINS"); assert.equal(r.outcome.settled, true); // a bare key list works too, and says so const r2 = await verifyEvidence(receipt, { issuer: { keys: [receipt.public_key], source: "index" } }); assert.equal(r2.issuer.status, "confirmed"); assert.equal(r2.ok, true); }); test("a name mismatch between the index and the receipt is reported, the key still decides", async () => { const receipt = await loadPortable("receipt/valid/queued.json"); const r = await verifyEvidence(receipt, { issuer: { doc: indexFor([receipt.public_key], "Someone Else") } }); assert.equal(r.issuer.status, "confirmed"); assert.equal(r.issuer.nameMatches, false); assert.equal(r.outcome.outcome, "queued"); assert.equal(r.outcome.settled, false); }); test("issuer rejected when the expected issuer publishes other keys; unconfirmed when it publishes none", async () => { const receipt = await loadPortable("receipt/valid/unsupported-api-only.json"); const rejected = await verifyEvidence(receipt, { issuer: { doc: indexFor([makeKey().publicKey]) } }); assert.equal(rejected.signature.ok, true, "the signature is still valid"); assert.equal(rejected.issuer.status, "rejected"); assert.equal(rejected.ok, false); const none = await verifyEvidence(receipt, { issuer: { keys: [] } }); assert.equal(none.issuer.status, "unconfirmed"); assert.match(none.issuer.why, /publishes no key/); assert.equal(rejected.outcome.outcome, "unsupported"); assert.equal(rejected.outcome.coverage.inspected, false); }); test("a pinned key verifies offline and is reported as pinned, never confirmed", async () => { const receipt = await loadPortable("receipt/valid/inspected-and-clear.json"); for (const pin of [receipt.public_key, await fingerprint(receipt.public_key), (await fingerprint(receipt.public_key)).toUpperCase()]) { const r = await verifyEvidence(receipt, { pinnedKey: pin }); assert.equal(r.issuer.status, "pinned", pin); assert.equal(r.ok, true); assert.match(r.summary[1], /^Key pinned/); } const wrong = await verifyEvidence(receipt, { pinnedKey: makeKey().publicKey }); assert.equal(wrong.issuer.status, "rejected"); assert.equal(wrong.ok, false); // discovery wins over a pin when both are given const both = await verifyEvidence(receipt, { pinnedKey: receipt.public_key, issuer: { keys: [receipt.public_key] } }); assert.equal(both.issuer.status, "confirmed"); }); test("tampering fails the signature while the issuer and the declaration are still reported apart", async () => { const tampered = await loadPortable("content-rules/tampered-after-count.json"); const r = await verifyEvidence(tampered, { issuer: { keys: [tampered.public_key] } }); assert.equal(r.signature.ok, false); assert.equal(r.signature.why, "the signature doesn't match this receipt"); assert.equal(r.issuer.status, "confirmed", "the key is the issuer's; the bytes are not what it signed"); assert.equal(r.ok, false); assert.match(r.summary[0], /^Signature not valid/); const inconsistent = { ...(await loadPortable("receipt/valid/queued.json")), verdict: "CLEAN" }; const r2 = await verifyEvidence(inconsistent); assert.equal(r2.signature.ok, false); assert.equal(r2.outcome.consistent, false, "queued never maps to CLEAN"); }); test("the vocabulary constants match the spec", () => { assert.deepEqual([...OUTCOMES], ["error", "bytes-still-found", "queued", "unsupported", "intentionally-retained", "inspected-and-clear"]); assert.deepEqual(VERDICT_OF, { "inspected-and-clear": "CLEAN", "intentionally-retained": "RESIDUE_REMAINS", "bytes-still-found": "RESIDUE_REMAINS", queued: "UNVERIFIABLE", unsupported: "UNVERIFIABLE", error: "UNVERIFIABLE" }); assert.deepEqual([...ISSUER_STATUS], ["confirmed", "pinned", "rejected", "unconfirmed"]); }); }); describe("an old-format receipt", () => { for (const name of ["receipt.json", "receipt-0.1.json", "receipt-unicode.json"]) { test(`${name}: still verifies; file hashes are reported as not checkable from here`, async () => { const receipt = await loadFixture(name); const r = await verifyEvidence(receipt); assert.equal(r.signature.ok, true); assert.equal(r.signature.fingerprint, FIXTURE_FINGERPRINT); assert.equal(r.issuer.status, "unconfirmed"); assert.equal(r.outcome.kind, "engine"); assert.equal(r.outcome.verdict, receipt.verdict); assert.equal(r.outcome.outcome, null); assert.equal(r.files.listed, Object.keys(receipt.files ?? {}).length); assert.equal(r.files.checked, false); if (r.files.listed) assert.match(r.files.note, /cannot inspect a remote disk/); }); } test("a 0.2 well-known document confirms the issuer of an old receipt; rotation keeps old keys valid", async () => { const receipt = await loadFixture("receipt.json"); const wellKnown = { forgetproof: "0.2", issuer: "Pim Cloud", public_key: makeKey().publicKey, keys: [{ public_key: FIXTURE_PUBLIC_KEY, key_fingerprint: FIXTURE_FINGERPRINT, valid_from: "2026-01-01T00:00:00Z" }] }; const r = await verifyEvidence(receipt, { issuer: { doc: wellKnown, url: "https://pim.example/.well-known/forgetproof.json" } }); assert.equal(r.issuer.status, "confirmed"); assert.equal(r.issuer.source, "well-known"); assert.equal(r.ok, true); }); test("files are checked when the bytes are available", async () => { const { publicKey, privateKey } = makeKey(); const bytes = new TextEncoder().encode("hello"); const digest = Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)), (b) => b.toString(16).padStart(2, "0")).join(""); const base = await loadFixture("receipt.json"); const { signature, ...body } = base; void signature; const receipt = signBody({ ...body, public_key: publicKey, files: { "notes.db": digest } }, privateKey); const good = await verifyEvidence(receipt, { pinnedKey: publicKey, files: { "notes.db": bytes } }); assert.equal(good.files.checked, true); assert.equal(good.files.ok, true); const bad = await verifyEvidence(receipt, { pinnedKey: publicKey, files: { "notes.db": new Uint8Array([1]) } }); assert.equal(bad.files.ok, false); assert.deepEqual(bad.files.mismatched, ["notes.db"]); assert.equal(bad.signature.ok, true, "a changed file is not a changed receipt"); }); }); test("verifyEvidence rejects non-objects without throwing", async () => { for (const value of [null, "x", 1, []]) { const r = await verifyEvidence(value); assert.equal(r.ok, false); assert.equal(r.signature.ok, false); assert.equal(r.issuer.status, "unconfirmed"); } }); test("fetchIndex fetches the endpoint and insists on a ForgetMe v1 index", async () => { const good = indexFor([FIXTURE_PUBLIC_KEY]); const calls = []; const impl = async (url, init) => { calls.push({ url, init }); return { ok: true, status: 200, json: async () => good }; }; const doc = await fetchIndex("https://company.example/forget-me/v1", impl); assert.deepEqual(doc, good); assert.equal(calls[0].url, "https://company.example/forget-me/v1"); assert.equal(calls[0].init.headers.accept, "application/json"); await fetchIndex("company.example/forget-me/v1", impl); assert.equal(calls[1].url, "https://company.example/forget-me/v1"); await assert.rejects(fetchIndex("https://company.example/forget-me/v1", async () => ({ ok: false, status: 404 })), /returned 404/); await assert.rejects(fetchIndex("https://company.example/forget-me/v1", async () => ({ ok: true, status: 200, json: async () => ({ hello: 1 }) })), /not a ForgetMe v1 index/); await assert.rejects(fetchIndex("https://company.example/forget-me/v1", "nope"), TypeError); });