|
| 1 | +// Tests for the trusted-author gate on `.github/workflows/claude.yml` (#4795). |
| 2 | +// The `claude` job holds a shell, the API key and `id-token: write`, and it |
| 3 | +// reads the text that mentions it, so who may start it is a security |
| 4 | +// property. These tests evaluate the job's `if:` against a synthetic payload |
| 5 | +// for every event the workflow listens to, rather than matching its text, so |
| 6 | +// a disjunct that loses its author check, or checks the wrong author, fails |
| 7 | +// here. Run via `npm run test:scripts`. |
| 8 | +// |
| 9 | +// The evaluator covers only the expression subset the condition uses |
| 10 | +// (`==`, `&&`, `||`, `!`, parentheses, string literals, `github.*` paths, |
| 11 | +// `contains` and `fromJSON`), with `contains` case-insensitive as in Actions. |
| 12 | +// It refuses any other token, so widening the condition past that subset |
| 13 | +// fails loudly instead of being evaluated wrongly. |
| 14 | + |
| 15 | +import { test } from "node:test"; |
| 16 | +import assert from "node:assert/strict"; |
| 17 | +import { readFileSync } from "node:fs"; |
| 18 | +import path from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | +import { parse } from "yaml"; |
| 21 | + |
| 22 | +const repoRoot = path.resolve( |
| 23 | + path.dirname(fileURLToPath(import.meta.url)), |
| 24 | + "..", |
| 25 | +); |
| 26 | +const workflow = parse( |
| 27 | + readFileSync(path.join(repoRoot, ".github/workflows/claude.yml"), "utf8"), |
| 28 | +); |
| 29 | +const condition = workflow.jobs.claude.if; |
| 30 | + |
| 31 | +const TOKEN = |
| 32 | + /\s+|'(?:[^']|'')*'|==|&&|\|\||!|\(|\)|,|contains|fromJSON|github(?:\.[A-Za-z_][A-Za-z0-9_-]*)+/y; |
| 33 | + |
| 34 | +/** Compiles the expression subset above into a function of `github`. */ |
| 35 | +function compile(expr) { |
| 36 | + let js = ""; |
| 37 | + TOKEN.lastIndex = 0; |
| 38 | + while (TOKEN.lastIndex < expr.length) { |
| 39 | + const at = TOKEN.lastIndex; |
| 40 | + const match = TOKEN.exec(expr); |
| 41 | + if (!match) { |
| 42 | + throw new Error(`unsupported expression syntax at: ${expr.slice(at)}`); |
| 43 | + } |
| 44 | + const tok = match[0]; |
| 45 | + if (tok.startsWith("'")) { |
| 46 | + js += JSON.stringify(tok.slice(1, -1).replaceAll("''", "'")); |
| 47 | + } else if (tok.startsWith("github.")) { |
| 48 | + // A missing property reads as null, as it does in Actions. |
| 49 | + js += `(${tok.replaceAll(".", "?.")} ?? null)`; |
| 50 | + } else if (tok === "==") { |
| 51 | + // Only `event_name` is compared, against exact event names. |
| 52 | + js += "==="; |
| 53 | + } else { |
| 54 | + js += tok; |
| 55 | + } |
| 56 | + } |
| 57 | + return new Function("github", "contains", "fromJSON", `return (${js});`); |
| 58 | +} |
| 59 | + |
| 60 | +const fold = (v) => (typeof v === "string" ? v.toLowerCase() : v); |
| 61 | +const eq = (a, b) => fold(a) === fold(b); |
| 62 | +const contains = (haystack, needle) => |
| 63 | + Array.isArray(haystack) |
| 64 | + ? haystack.some((item) => eq(item, needle)) |
| 65 | + : haystack != null && |
| 66 | + String(haystack).toLowerCase().includes(String(needle).toLowerCase()); |
| 67 | + |
| 68 | +const evaluate = (github) => |
| 69 | + Boolean(compile(condition)(github, contains, JSON.parse)); |
| 70 | + |
| 71 | +// One payload per event the workflow listens to, with the mention placed in |
| 72 | +// the text that event carries, and `who` as that text's author association. |
| 73 | +// The surrounding issue or pull request is always opened by a maintainer, so |
| 74 | +// a gate that read the wrong author would let an outsider through. |
| 75 | +const EVENTS = { |
| 76 | + issue_comment: (who) => ({ |
| 77 | + event: { |
| 78 | + issue: { body: "", title: "", author_association: "OWNER" }, |
| 79 | + comment: { body: "@claude review this", author_association: who }, |
| 80 | + }, |
| 81 | + }), |
| 82 | + pull_request_review_comment: (who) => ({ |
| 83 | + event: { |
| 84 | + pull_request: { author_association: "OWNER" }, |
| 85 | + comment: { body: "@claude fix this line", author_association: who }, |
| 86 | + }, |
| 87 | + }), |
| 88 | + pull_request_review: (who) => ({ |
| 89 | + event: { |
| 90 | + pull_request: { author_association: "OWNER" }, |
| 91 | + review: { body: "@claude summarize", author_association: who }, |
| 92 | + }, |
| 93 | + }), |
| 94 | + issues: (who) => ({ |
| 95 | + event: { |
| 96 | + issue: { |
| 97 | + body: "@claude look at this", |
| 98 | + title: "", |
| 99 | + author_association: who, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }), |
| 103 | +}; |
| 104 | + |
| 105 | +const TRUSTED = ["OWNER", "MEMBER", "COLLABORATOR"]; |
| 106 | +const UNTRUSTED = [ |
| 107 | + "CONTRIBUTOR", |
| 108 | + "FIRST_TIME_CONTRIBUTOR", |
| 109 | + "FIRST_TIMER", |
| 110 | + "MANNEQUIN", |
| 111 | + "NONE", |
| 112 | +]; |
| 113 | + |
| 114 | +test("every event the workflow listens to has a payload here", () => { |
| 115 | + assert.deepEqual(Object.keys(workflow.on).sort(), Object.keys(EVENTS).sort()); |
| 116 | +}); |
| 117 | + |
| 118 | +for (const [name, payload] of Object.entries(EVENTS)) { |
| 119 | + const run = (who) => evaluate({ event_name: name, ...payload(who) }); |
| 120 | + |
| 121 | + test(`${name}: a trusted author's mention starts the job`, () => { |
| 122 | + for (const who of TRUSTED) assert.equal(run(who), true, who); |
| 123 | + }); |
| 124 | + |
| 125 | + test(`${name}: an untrusted author's mention starts nothing`, () => { |
| 126 | + for (const who of UNTRUSTED) assert.equal(run(who), false, who); |
| 127 | + }); |
| 128 | + |
| 129 | + test(`${name}: a trusted author without a mention starts nothing`, () => { |
| 130 | + const github = { event_name: name, ...payload("OWNER") }; |
| 131 | + const text = |
| 132 | + github.event.comment ?? github.event.review ?? github.event.issue; |
| 133 | + text.body = "no mention here"; |
| 134 | + assert.equal(evaluate(github), false); |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +test("an outsider's issue title mention starts nothing", () => { |
| 139 | + const github = { |
| 140 | + event_name: "issues", |
| 141 | + event: { |
| 142 | + issue: { body: "", title: "@claude do it", author_association: "NONE" }, |
| 143 | + }, |
| 144 | + }; |
| 145 | + assert.equal(evaluate(github), false); |
| 146 | + github.event.issue.author_association = "MEMBER"; |
| 147 | + assert.equal(evaluate(github), true); |
| 148 | +}); |
| 149 | + |
| 150 | +test("the evaluator refuses syntax it does not model", () => { |
| 151 | + assert.throws(() => compile("github.event_name != 'x'"), /unsupported/); |
| 152 | + assert.throws(() => compile("startsWith(github.ref, 'x')"), /unsupported/); |
| 153 | +}); |
0 commit comments