Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion .github/scripts/pr-review/action-sources-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ for (const corruption of ["duplicate", "missing-parent", "tree-hash", "symlink",
await assert.rejects(collectActionSources(f.options));
} finally { f.cleanup(); }
});
for (const key of ["repositories", "snapshots", "files", "blobBytes", "totalBytes", "depth", "actions", "edges", "dependencyEntries", "graphBytes"]) test(`reject ${key} acquisition over bound`, async () => {
for (const key of ["repositories", "snapshots", "objects", "files", "blobBytes", "totalBytes", "manifestBytes", "depth", "actions", "edges", "dependencyEntries", "graphBytes"]) test(`reject ${key} acquisition over bound`, async () => {
const f = fixture(); try { await assert.rejects(collectActionSources({ ...f.options, limits: { ...ACTION_SOURCE_LIMITS, [key]: 0 } })); } finally { f.cleanup(); }
});
for (const dependency of ["owner/script@latest", "./local", "docker://image", "${{ inputs.action }}", `owner/actions/sub@${current}`]) test(`refuse unsupported/cyclic dependency ${dependency}`, async () => {
Expand Down Expand Up @@ -308,3 +308,106 @@ test("dependency text byte budget is enforced before parsing an oversized action
const g = graphFixture(graph, ["a"], { a: "#" + "x".repeat(128) + "\n" });
try { await assert.rejects(collectActionSources({ ...g.options, limits: { ...ACTION_SOURCE_LIMITS, graphBytes: 128 } }), /byte budget/); } finally { g.cleanup(); }
});

// Logical path entries and distinct fetched objects are separate budgets. A
// complete snapshot set may repeat bytes (licenses, old/new pins) without
// another request/hash/write, but every distinct object is still counted.
function entrySource(count, { unique = 1, extra = {} } = {}) {
const files = { "sub/action.yml": "runs:\n using: node24\n main: f0.js\n", ...extra };
for (let i = 0; i < count - 1 - Object.keys(extra).length; i++) files[`sub/f${i}.js`] = `// shared ${i % unique}\n`;
return source(files);
}
test("repeated bindings consume file entries, not the distinct object budget (exact and +1)", async () => {
const run = async limits => {
const f = fixture();
try {
f.sources.set(`actions:${current}`, entrySource(7, { unique: 2 })); f.options.refs = [reference];
const file = await collectActionSources({ ...f.options, limits: { ...ACTION_SOURCE_LIMITS, ...limits } });
const data = JSON.parse(fs.readFileSync(file));
assert.equal(data.snapshots[0].files.length, 7);
assert.equal(f.calls.filter(([kind]) => kind === "blob").length, 3, "one request per distinct object");
return verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] });
} finally { f.cleanup(); }
};
assert.match(await run({ objects: 3, files: 7 }), /^[a-f0-9]{64}$/);
await assert.rejects(run({ objects: 2, files: 7 }), /object bound exceeded/);
await assert.rejects(run({ objects: 3, files: 6 }), /file\/size bound exceeded/);
});
test("default distinct-object bound: 512 accepted, 513 refused by acquisition and offline verification", async () => {
const limit = ACTION_SOURCE_LIMITS.objects;
for (const count of [limit, limit + 1]) {
const f = fixture();
try {
f.sources.set(`actions:${current}`, entrySource(count, { unique: count })); f.options.refs = [reference];
if (count === limit) {
const file = await collectActionSources(f.options);
assert.match(verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /^[a-f0-9]{64}$/);
} else {
await assert.rejects(collectActionSources(f.options), /object bound exceeded/);
assert.ok(!fs.existsSync(path.join(f.options.directory, "manifest.json")), "refusal publishes no manifest");
assert.equal(f.calls.filter(([kind]) => kind === "blob").length, limit, "refused before the extra request");
fs.rmSync(f.options.directory, { recursive: true });
const file = await collectActionSources({ ...f.options, limits: { ...ACTION_SOURCE_LIMITS, objects: count } });
assert.throws(() => verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /object bound exceeded/);
}
} finally { f.cleanup(); }
}
});
test("default file-entry bound across old/new snapshots: 2048 accepted, 2049 refused on both sides", async () => {
const limit = ACTION_SOURCE_LIMITS.files, half = limit / 2;
for (const count of [limit, limit + 1]) {
const f = fixture();
try {
// Same repository, two pins: per-snapshot trees stay under 2048 entries.
f.sources.set(`actions:${old}`, entrySource(half, { unique: 4 }));
f.sources.set(`actions:${current}`, entrySource(count - half, { unique: 4, extra: count > limit ? { "sub/new.js": "// new\n" } : {} }));
if (count === limit) {
const file = await collectActionSources(f.options), data = JSON.parse(fs.readFileSync(file));
assert.equal(data.snapshots.reduce((sum, s) => sum + s.files.length, 0), limit);
assert.match(verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /^[a-f0-9]{64}$/);
} else {
await assert.rejects(collectActionSources(f.options), /file\/size bound exceeded/);
assert.ok(!fs.existsSync(path.join(f.options.directory, "manifest.json")));
fs.rmSync(f.options.directory, { recursive: true });
const file = await collectActionSources({ ...f.options, limits: { ...ACTION_SOURCE_LIMITS, files: count } });
assert.throws(() => verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /file bound exceeded/);
}
} finally { f.cleanup(); }
}
});
test("manifest byte bound is shared: exact size accepted, +1 refused, collector refuses before writing", async () => {
const f = fixture();
try {
const file = await collectActionSources(f.options), size = fs.statSync(file).size;
const digest = verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] });
const text = fs.readFileSync(file, "utf8");
fs.chmodSync(file, 0o600);
fs.writeFileSync(file, text + " ".repeat(ACTION_SOURCE_LIMITS.manifestBytes - size));
assert.equal(verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), digest, "exact bound accepted");
fs.appendFileSync(file, " ");
assert.throws(() => verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /oversized action source manifest/);
for (const [bytes, accepted] of [[size, true], [size - 1, false]]) {
const g = fixture();
try {
const attempt = collectActionSources({ ...g.options, limits: { ...ACTION_SOURCE_LIMITS, manifestBytes: bytes } });
if (accepted) assert.equal(fs.statSync(await attempt).size, size);
else { await assert.rejects(attempt, /manifest byte bound exceeded/); assert.ok(!fs.existsSync(path.join(g.options.directory, "manifest.json"))); }
} finally { g.cleanup(); }
}
} finally { f.cleanup(); }
});
test("default bounds accept the measured four-action shape: 605 entries over 484 distinct objects", async () => {
// Mirrors the real complete checkout/download/setup-go/upload snapshot set
// (605 path entries, 484 distinct blobs); old entry-counted 512 refused it.
const f = fixture();
try {
f.sources.set(`actions:${old}`, entrySource(302, { unique: 301 }));
f.sources.set(`actions:${current}`, entrySource(303, { unique: 302, extra: Object.fromEntries(Array.from({ length: 182 }, (_, i) => [`sub/n${i}.js`, `// new ${i}\n`])) }));
const file = await collectActionSources(f.options), data = JSON.parse(fs.readFileSync(file));
const objects = new Set(data.snapshots.flatMap(s => s.files.map(item => item.blob)));
assert.equal(data.snapshots.reduce((sum, s) => sum + s.files.length, 0), 605);
assert.equal(objects.size, 484);
assert.equal(f.calls.filter(([kind]) => kind === "blob").length, 484);
assert.match(verifyActionSources(file, { base: old, head: current, requiredRefs: [reference] }), /^[a-f0-9]{64}$/);
} finally { f.cleanup(); }
});
15 changes: 11 additions & 4 deletions .github/scripts/pr-review/action-sources.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const sha256 = bytes => crypto.createHash("sha256").update(bytes).digest("hex");
const safePath = value => typeof value === "string" && value.length <= 512 && value !== ""
&& !value.startsWith("/") && !/[\\\x00-\x1f\x7f]/.test(value)
&& value.split("/").every(part => part !== "" && part !== "." && part !== "..");
export const ACTION_SOURCE_LIMITS = { repositories: 4, snapshots: 8, files: 512, blobBytes: 8 * 1024 * 1024, totalBytes: 32 * 1024 * 1024, depth: 4, actions: 32, edges: 256, dependencyEntries: 4096, graphBytes: 4 * 1024 * 1024 };
// `objects` bounds distinct blobs fetched, hash-checked and stored once (one
// API request each); `files` bounds logical path entries, which reuse those
// objects across paths and snapshots. Both sides refuse manifests over
// `manifestBytes` before the reviewer can consume them.
export const ACTION_SOURCE_LIMITS = { repositories: 4, snapshots: 8, objects: 512, files: 2048, blobBytes: 8 * 1024 * 1024, totalBytes: 32 * 1024 * 1024, manifestBytes: 4 * 1024 * 1024, depth: 4, actions: 32, edges: 256, dependencyEntries: 4096, graphBytes: 4 * 1024 * 1024 };

export function actionReference(value) {
const match = /^([A-Za-z0-9_-]+)\/([A-Za-z0-9_.-]+)(?:\/([^@]+))?@([a-f0-9]{40})$/.exec(value);
Expand Down Expand Up @@ -150,6 +154,7 @@ export async function collectActionSources({ github, directory, base, head, refs
for (const entry of entries) {
insist(++count <= limits.files && Number.isSafeInteger(entry.size) && entry.size >= 0 && entry.size <= limits.blobBytes, "Action source file/size bound exceeded");
if (!objects.has(entry.sha)) {
insist(objects.size < limits.objects, "Action source object bound exceeded");
const blob = (await github.rest.git.getBlob({ owner, repo, file_sha: entry.sha })).data;
insist(blob.sha === entry.sha && blob.encoding === "base64" && blob.size === entry.size && typeof blob.content === "string", "Malformed action source blob");
insist(blob.content.length <= 8 * Math.ceil(entry.size / 3) + 2, "Action source encoded blob bound exceeded");
Expand Down Expand Up @@ -194,15 +199,16 @@ export async function collectActionSources({ github, directory, base, head, refs
for (const ref of roots) await visit(ref);
validateActionGraph(roots, actions, ref => ({ bytes: actionBytes.get(ref), dependencies: actions.get(ref).dependencies }), limits);
const manifest = { schema: 1, base, head, refs: roots, actions: [...actions.values()].sort((a, b) => a.ref.localeCompare(b.ref, "en")), snapshots: [...snapshots.values()].sort((a, b) => `${a.repository.full_name}@${a.commit}`.localeCompare(`${b.repository.full_name}@${b.commit}`, "en")) };
const file = path.join(directory, "manifest.json");
fs.writeFileSync(file, JSON.stringify(manifest), { mode: 0o400, flag: "wx" });
const file = path.join(directory, "manifest.json"), encoded = Buffer.from(JSON.stringify(manifest));
insist(encoded.length <= limits.manifestBytes, "Action source manifest byte bound exceeded");
fs.writeFileSync(file, encoded, { mode: 0o400, flag: "wx" });
return file;
}

export function verifyActionSources(file, { base, head, requiredRefs = [] }) {
if (!file) { insist(requiredRefs.length === 0, "Pinned action source manifest is required"); return ""; }
const root = path.dirname(file);
insist(!fs.lstatSync(root).isSymbolicLink() && fs.lstatSync(file).isFile() && !fs.lstatSync(file).isSymbolicLink() && fs.statSync(file).size <= 4 * 1024 * 1024, "Unsafe or oversized action source manifest");
insist(!fs.lstatSync(root).isSymbolicLink() && fs.lstatSync(file).isFile() && !fs.lstatSync(file).isSymbolicLink() && fs.statSync(file).size <= ACTION_SOURCE_LIMITS.manifestBytes, "Unsafe or oversized action source manifest");
const manifest = JSON.parse(fs.readFileSync(file, "utf8"));
insist(manifest.schema === 1 && manifest.base === base && manifest.head === head && Array.isArray(manifest.refs) && Array.isArray(manifest.snapshots) && Array.isArray(manifest.actions), "Action source PR binding mismatch");
insist(requiredRefs.every(ref => manifest.refs.includes(ref)), "Missing changed action source reference");
Expand All @@ -227,6 +233,7 @@ export function verifyActionSources(file, { base, head, requiredRefs = [] }) {
insist(cached.blob === object.blob && cached.bytes.length === object.size, "Conflicting action source object binding");
continue;
}
insist(verifiedObjects.size < ACTION_SOURCE_LIMITS.objects, "Action source object bound exceeded");
insist((totalBytes += object.size) <= ACTION_SOURCE_LIMITS.totalBytes, "Action source byte bound exceeded");
const target = path.join(root, object.relative_path);
insist(!fs.lstatSync(root).isSymbolicLink() && !fs.lstatSync(path.join(root, "objects")).isSymbolicLink() && fs.lstatSync(target).isFile() && !fs.lstatSync(target).isSymbolicLink() && fs.statSync(target).size === object.size, "Unsafe action source evidence file");
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ rejected in every repository.
PR-supplied source/claims do not authenticate them. Native and resumed code
turns receive the manifest, and changed source evidence invalidates completed
code chunks rather than reusing a review without that evidence.
Acquisition is bounded to 4 public repositories, 8 commit snapshots, 512 file
entries, 8 MiB per blob, 32 MiB unique content and 4 action levels. Both
Acquisition is bounded to 4 public repositories, 8 commit snapshots, 512
distinct source blobs (each fetched, hash-checked and stored once), 2048
logical file entries across snapshots, 8 MiB per blob, 32 MiB unique content,
a 4 MiB manifest and 4 action levels. Repeated bytes in other paths or pins
reuse a verified blob and count only as entries; offline verification
enforces the same object, entry, byte and manifest limits. Both
acquisition and offline graph validation additionally cap 32 action nodes,
256 distinct dependency edges, 4096 dependency occurrences and 4 MiB of parsed
action-manifest text. Source blobs are hash-checked once, each graph node is
Expand Down
Loading