diff --git a/site/files.js b/site/files.js index ac4dab978..1b1de1331 100644 --- a/site/files.js +++ b/site/files.js @@ -170,8 +170,12 @@ export async function mirror(env, rels, open, now = Date.now, attest = null) { // the next run asks again rather than keep a list with holes in it. async function keepBundles(env, tag, rel, attest) { const lines = new Set(); + // release CI signs every file in one statement, so the first answer + // covers the rest: GitHub's API is asked once a release, not once a file + // (unsigned, it allows 60 an hour to an IP the worker shares) + const covered = new Set(); for (const [name, a] of Object.entries(rel.assets)) { - if (!a.sha256) continue; + if (!a.sha256 || covered.has(a.sha256)) continue; let got = null; try { got = await attest(a.sha256); @@ -182,7 +186,10 @@ async function keepBundles(env, tag, rel, attest) { console.log("r2 attest: none for", tag, name); return false; } - for (const b of got) lines.add(JSON.stringify(b)); + for (const b of got) { + lines.add(JSON.stringify(b)); + for (const h of subjects(b)) covered.add(h); + } } if (!lines.size) return false; try { @@ -196,6 +203,16 @@ async function keepBundles(env, tag, rel, attest) { } } +// subjects is the sha256 of each file a Sigstore bundle's statement names. +export function subjects(b) { + try { + const st = JSON.parse(atob(b.dsseEnvelope.payload)); + return (st.subject || []).map((x) => x.digest && x.digest.sha256).filter(Boolean); + } catch { + return []; + } +} + async function copy(env, tag, name, a, open) { let res = null; try { diff --git a/site/files.test.mjs b/site/files.test.mjs index c153bc27d..10ede69a4 100644 --- a/site/files.test.mjs +++ b/site/files.test.mjs @@ -203,6 +203,21 @@ test("mirror: a kept release the feeds no longer name still gets its bundles, fo assert.deepEqual(asked, []); }); +test("mirror: one statement signing every file is asked for once a release, not once a file", async () => { + const env = { RELEASES: bucket() }; + const names = ["a", "b", "c"]; + const rel = { version: "0.1.3982", assets: Object.fromEntries(names.map((n) => [n, { sha256: sha(n) }])) }; + const statement = { subject: names.map((n) => ({ name: n, digest: { sha256: sha(n) } })) }; + const bundle = { dsseEnvelope: { payload: btoa(JSON.stringify(statement)) } }; + const asked = []; + const attest = async (h) => (asked.push(h), [bundle]); + const done = await mirror(env, [rel], async (n) => new Response(n), Date.now, attest); + assert.ok(done.copied.includes("v0.1.3982/attestations.jsonl")); + assert.equal(asked.length, 1); + const res = await ask(env, "/files/v0.1.3982/attestations.jsonl"); + assert.deepEqual((await res.text()).trimEnd().split("\n").map((l) => JSON.parse(l)), [bundle]); +}); + test("mirror: a release from before the attestations is not asked for any", async () => { const env = { RELEASES: bucket() }; const rel = { version: "0.1.3890", assets: { a: { sha256: sha("A") } } };