Skip to content

Commit 877ef2f

Browse files
authored
Merge pull request #5282 from modelcontextprotocol/v2/chore/4795-claude-workflow-trusted-actors
fix(ci): start the Claude workflow only for trusted authors
2 parents 8ff91e0 + a34d8a9 commit 877ef2f

3 files changed

Lines changed: 173 additions & 4 deletions

File tree

‎.github/workflows/claude.yml‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ jobs:
1616
# SHA with the release it came from in a trailing comment. A moving tag
1717
# here would let whoever can move it run code next to both.
1818
# `verify:action-pins` enforces it.
19+
#
20+
# It runs only for a trusted author (#4795). A mention is not enough: the
21+
# repository is public, so anyone could otherwise start an agent that holds
22+
# a shell and the API key, and steer it with the text of their comment.
23+
# Each event checks the author of the text that carries the mention (the
24+
# comment, the review, or the issue), never someone further up the thread,
25+
# and only OWNER, MEMBER and COLLABORATOR count. An outside contributor's
26+
# issue that mentions @claude starts nothing; a maintainer's comment on it
27+
# does. `scripts/claude-workflow.test.mjs` evaluates this condition against
28+
# each event, so a disjunct that loses its check fails the script tests.
1929
claude:
2030
timeout-minutes: 30
2131
if: |
22-
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
23-
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
24-
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
25-
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
32+
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude') && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) ||
33+
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude') && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) ||
34+
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude') && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.review.author_association)) ||
35+
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')) && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.issue.author_association))
2636
runs-on: ubuntu-latest
2737
permissions:
2838
contents: read

‎AGENTS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ build jobs and `sdk-watch.yml`'s `sweep`, whose outputs `analyze` reads.
184184
downloads**, since what it builds is what gets published, and for **a job
185185
whose outputs a credentialed job reads**, since the reader acts on them.
186186
`verify:action-pins` enforces both. Other jobs keep moving major tags.
187+
- **A credentialed job that a comment, review or issue can start runs only
188+
for a trusted author**: `OWNER`, `MEMBER` or `COLLABORATOR` in the
189+
`author_association` of the text that triggered it, never of the thread
190+
around it. Today that is `claude.yml`, whose `if:` checks it on every event
191+
it listens to; `scripts/claude-workflow.test.mjs` evaluates that condition
192+
per event and fails when one loses its check.
187193
- **A job that holds a publish credential does not install dependencies or
188194
build.** `release.yml` builds, tests and verifies each package in a job
189195
with no `id-token`, and the publish job only downloads that artifact and

‎scripts/claude-workflow.test.mjs‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)