Skip to content

Commit 055cf59

Browse files
committed
fix(github): reserve app quota for privileged mutations
1 parent 4dc8d1d commit 055cf59

7 files changed

Lines changed: 170 additions & 4 deletions

File tree

‎.github/workflows/repair-comment-router.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ jobs:
195195
id: route-comments
196196
env:
197197
GH_TOKEN: ${{ steps.app_token.outputs.token }}
198+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ github.token }}
198199
CLAWSWEEPER_DISPATCH_TOKEN: ${{ steps.dispatch-token.outputs.token }}
199200
CLAWSWEEPER_MUTATION_TOKEN_SOURCE: clawsweeper-app
200201
CLAWSWEEPER_ALLOW_MERGE: ${{ vars.CLAWSWEEPER_ALLOW_MERGE || '0' }}
@@ -351,6 +352,7 @@ jobs:
351352
if: ${{ steps.waiting-repair-dispatches.outputs.count != '' && steps.waiting-repair-dispatches.outputs.count != '0' }}
352353
env:
353354
GH_TOKEN: ${{ steps.app_token.outputs.token }}
355+
CLAWSWEEPER_PUBLIC_GH_TOKEN: ${{ github.token }}
354356
CLAWSWEEPER_DISPATCH_TOKEN: ${{ steps.dispatch-token.outputs.token }}
355357
CLAWSWEEPER_MUTATION_TOKEN_SOURCE: clawsweeper-app
356358
CLAWSWEEPER_ALLOW_MERGE: ${{ vars.CLAWSWEEPER_ALLOW_MERGE || '0' }}

‎.github/workflows/sweep.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ jobs:
723723
id: live-item
724724
env:
725725
CLAIM_DECISION: ${{ steps.claim-exact-review-queue.outputs.decision }}
726-
GH_TOKEN: ${{ steps.target-read-token.outputs.token }}
726+
GH_TOKEN: ${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || steps.target-read-token.outputs.token }}
727727
TARGET_REPO: ${{ steps.target.outputs.target_repo }}
728728
ITEM_NUMBER: ${{ steps.target.outputs.item_number }}
729729
CLAIM_TARGET_BRANCH: ${{ fromJSON(steps.claim-exact-review-queue.outputs.decision).targetBranch }}

‎src/repair/github-cli.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export function ghPagedLimitWithRetry<T = JsonValue>(
151151
}
152152

153153
export function ghText(ghArgs: string[], options: GhRunOptions = {}): string {
154-
const env = ghEnv(options.env);
154+
const env = ghCommandEnv(ghArgs, options);
155155
const command = ghCommand(ghArgs, env);
156156
const text = execFileSync(command.command, command.args, {
157157
cwd: options.cwd ?? repoRoot(),
@@ -203,7 +203,7 @@ export async function ghTextWithRetryAsync(
203203

204204
export async function ghTextAsync(ghArgs: string[], options: GhRunOptions = {}): Promise<string> {
205205
if (options.input !== undefined) return ghText(ghArgs, options);
206-
const env = ghEnv(options.env);
206+
const env = ghCommandEnv(ghArgs, options);
207207
const command = ghCommand(ghArgs, env);
208208
const { stdout } = await execFileAsync(command.command, command.args, {
209209
cwd: options.cwd ?? repoRoot(),
@@ -249,6 +249,50 @@ export function ghEnv(overrides: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv {
249249
return ghCliEnv(overrides);
250250
}
251251

252+
function ghCommandEnv(ghArgs: readonly string[], options: GhRunOptions): NodeJS.ProcessEnv {
253+
const overrides = options.env ?? {};
254+
const env = ghEnv(overrides);
255+
const publicToken = process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN?.trim();
256+
if (
257+
!publicToken ||
258+
options.input !== undefined ||
259+
Object.hasOwn(overrides, "GH_TOKEN") ||
260+
Object.hasOwn(overrides, "GITHUB_TOKEN") ||
261+
(env.GH_HOST && env.GH_HOST.toLowerCase() !== "github.com") ||
262+
!isPublicOpenClawReadOnlyRequest(ghArgs)
263+
) {
264+
return env;
265+
}
266+
return ghEnv({ ...overrides, GH_TOKEN: publicToken });
267+
}
268+
269+
function isPublicOpenClawReadOnlyRequest(ghArgs: readonly string[]): boolean {
270+
if (ghArgs[0] !== "api") return false;
271+
const endpoint = ghArgs[1] ?? "";
272+
const endpointPath = endpoint.split("?", 1)[0] ?? "";
273+
if (!/^repos\/openclaw\/openclaw\/(?:issues|pulls)(?:\/|$)/.test(endpointPath)) {
274+
return false;
275+
}
276+
if (
277+
endpointPath.includes("\\") ||
278+
endpointPath.split("/").some((segment) => segment === "." || segment === "..") ||
279+
/%(?:2e|2f|5c)/i.test(endpointPath)
280+
) {
281+
return false;
282+
}
283+
284+
for (let index = 2; index < ghArgs.length; index += 1) {
285+
const flag = ghArgs[index];
286+
if (flag === "--paginate" || flag === "--slurp") continue;
287+
if ((flag === "--jq" || flag === "-q") && index + 1 < ghArgs.length) {
288+
index += 1;
289+
continue;
290+
}
291+
return false;
292+
}
293+
return true;
294+
}
295+
252296
export function ghErrorText(error: unknown): string {
253297
if (!error || typeof error !== "object") return String(error ?? "");
254298
const commandError = error as {

‎test/repair/comment-router-action-ledger.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ test("comment router wraps every GitHub mutation at the request boundary", () =>
5454
}
5555
});
5656

57+
test("comment router isolates public target reads from its GitHub App mutation identity", () => {
58+
const source = readText("src/repair/github-cli.ts");
59+
const workflow = readText(".github/workflows/repair-comment-router.yml");
60+
61+
assert.equal(
62+
workflow.match(/CLAWSWEEPER_PUBLIC_GH_TOKEN: \$\{\{ github\.token \}\}/g)?.length,
63+
2,
64+
);
65+
assert.equal(
66+
workflow.match(/GH_TOKEN: \$\{\{ steps\.app_token\.outputs\.token \}\}/g)?.length,
67+
2,
68+
);
69+
assert.match(source, /process\.env\.CLAWSWEEPER_PUBLIC_GH_TOKEN\?\.trim\(\)/);
70+
assert.match(source, /Object\.hasOwn\(overrides, "GH_TOKEN"\)/);
71+
assert.match(source, /Object\.hasOwn\(overrides, "GITHUB_TOKEN"\)/);
72+
assert.match(source, /function isPublicOpenClawReadOnlyRequest/);
73+
assert.match(source, /repos\\\/openclaw\\\/openclaw\\\/\(\?:issues\|pulls\)/);
74+
});
75+
5776
test("exact comment convergence classifies a missing comment as no mutation", () => {
5877
const source = readText("src/repair/comment-router.ts");
5978
const fastPath = source.slice(source.indexOf("function convergeExactCommentVersionFastPathAck"));

‎test/repair/github-cli.test.ts‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { githubLimitedPagePath, githubPaginatedPath } from "../../dist/repair/github-cli.js";
4+
import {
5+
ghJson,
6+
ghJsonWithRetryAsync,
7+
ghSpawn,
8+
githubLimitedPagePath,
9+
githubPaginatedPath,
10+
} from "../../dist/repair/github-cli.js";
511

612
test("githubPaginatedPath requests maximum REST page size by default", () => {
713
assert.equal(
@@ -36,3 +42,92 @@ test("githubLimitedPagePath caps one REST page and preserves existing filters",
3642
"repos/openclaw/openclaw/pulls/123/files?per_page=1&page=1",
3743
);
3844
});
45+
46+
test("public target reads preserve GitHub App mutations and every explicit credential", async () => {
47+
const fixtureEnv = {
48+
GH_BIN: process.execPath,
49+
GH_BIN_ARGS: JSON.stringify([
50+
"--eval",
51+
"process.stdout.write(JSON.stringify({ token: process.env.GH_TOKEN, args: process.argv.slice(1) }))",
52+
"--",
53+
]),
54+
GH_TOKEN: "app-mutation-token",
55+
CLAWSWEEPER_PUBLIC_GH_TOKEN: "public-read-token",
56+
};
57+
const previous = Object.fromEntries(
58+
Object.keys(fixtureEnv).map((key) => [key, process.env[key]]),
59+
);
60+
Object.assign(process.env, fixtureEnv);
61+
62+
const observed = (args: string[], options?: { env?: NodeJS.ProcessEnv; input?: string }) =>
63+
ghJson<{ token: string; args: string[] }>(args, options);
64+
65+
try {
66+
for (const args of [
67+
["api", "repos/openclaw/openclaw/issues/comments/123"],
68+
["api", "repos/openclaw/openclaw/issues/123"],
69+
["api", "repos/openclaw/openclaw/issues?state=open&per_page=100"],
70+
["api", "repos/openclaw/openclaw/issues/123/comments?per_page=100", "--paginate", "--slurp"],
71+
["api", "repos/openclaw/openclaw/pulls/123/reviews?per_page=100"],
72+
["api", "repos/openclaw/openclaw/pulls/123", "--jq", ".requested_reviewers"],
73+
]) {
74+
assert.equal(observed(args).token, "public-read-token", args.join(" "));
75+
}
76+
77+
assert.equal(
78+
(await ghJsonWithRetryAsync<{ token: string }>(["api", "repos/openclaw/openclaw/issues/123"]))
79+
.token,
80+
"public-read-token",
81+
);
82+
83+
for (const args of [
84+
["api", "user"],
85+
["api", "repos/openclaw/openclaw/collaborators/person/permission"],
86+
["api", "repos/openclaw/clawsweeper/issues/123"],
87+
["api", "repos/private/secret/issues/123"],
88+
["api", "https://api.github.com/repos/openclaw/openclaw/issues/123"],
89+
["api", "repos/openclaw/openclaw/issues/../../clawsweeper/issues/123"],
90+
["api", "repos/openclaw/openclaw/issues/%2e%2e/clawsweeper"],
91+
["api", "repos/openclaw/openclaw/issues%2f123"],
92+
["api", "repos/openclaw/openclaw/issues\\123"],
93+
["api", "repos/openclaw/openclaw/issues/123", "--method", "GET"],
94+
["api", "repos/openclaw/openclaw/issues/123", "--method", "POST"],
95+
["api", "repos/openclaw/openclaw/issues/123", "--method=DELETE"],
96+
["api", "repos/openclaw/openclaw/issues/123", "-X", "PATCH"],
97+
["api", "repos/openclaw/openclaw/issues/123", "-XDELETE"],
98+
["api", "repos/openclaw/openclaw/issues/123", "-f", "body=mutated"],
99+
["api", "repos/openclaw/openclaw/issues/123", "-F", "body=mutated"],
100+
["api", "repos/openclaw/openclaw/issues/123", "--field=body=mutated"],
101+
["api", "repos/openclaw/openclaw/issues/123", "--raw-field", "body=mutated"],
102+
["api", "repos/openclaw/openclaw/issues/123", "--input", "payload.json"],
103+
["api", "repos/openclaw/openclaw/issues/123", "--header", "X-HTTP-Method-Override: DELETE"],
104+
["api", "repos/openclaw/openclaw/issues/123", "--hostname", "example.invalid"],
105+
["api", "repos/openclaw/openclaw/issues/123", "--jq"],
106+
["pr", "view", "123", "--repo", "openclaw/openclaw"],
107+
]) {
108+
assert.equal(observed(args).token, "app-mutation-token", args.join(" "));
109+
}
110+
111+
const publicArgs = ["api", "repos/openclaw/openclaw/issues/comments/123"];
112+
assert.equal(
113+
observed(publicArgs, { env: { GH_TOKEN: "explicit-dispatch-token" } }).token,
114+
"explicit-dispatch-token",
115+
);
116+
assert.equal(
117+
observed(publicArgs, { env: { GITHUB_TOKEN: "explicit-token" } }).token,
118+
"app-mutation-token",
119+
);
120+
assert.equal(observed(publicArgs, { input: "request body" }).token, "app-mutation-token");
121+
122+
const spawned = ghSpawn(publicArgs);
123+
assert.equal(JSON.parse(spawned.stdout).token, "app-mutation-token");
124+
125+
delete process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN;
126+
assert.equal(observed(publicArgs).token, "app-mutation-token");
127+
} finally {
128+
for (const [key, value] of Object.entries(previous)) {
129+
if (value === undefined) delete process.env[key];
130+
else process.env[key] = value;
131+
}
132+
}
133+
});

‎test/repair/process-env.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test("codexSubprocessEnv forces ClawSweeper git identity and strips tokens", ()
2121
CLAWSWEEPER_GIT_USER_NAME: "clawsweeper-repair",
2222
CLAWSWEEPER_GIT_USER_EMAIL: "[email protected]",
2323
CLAWSWEEPER_TARGET_GH_TOKEN: "secret",
24+
CLAWSWEEPER_PUBLIC_GH_TOKEN: "public-read-secret",
2425
GH_TOKEN: "secret",
2526
GITHUB_TOKEN: "secret",
2627
GITHUB_ACTIONS: "true",
@@ -45,6 +46,7 @@ test("codexSubprocessEnv forces ClawSweeper git identity and strips tokens", ()
4546
assert.equal(env.GH_TOKEN, undefined);
4647
assert.equal(env.GITHUB_TOKEN, undefined);
4748
assert.equal(env.CLAWSWEEPER_TARGET_GH_TOKEN, undefined);
49+
assert.equal(env.CLAWSWEEPER_PUBLIC_GH_TOKEN, undefined);
4850
assert.equal(env.OPENAI_API_KEY, undefined);
4951
assert.equal(env.CODEX_API_KEY, undefined);
5052
assert.equal(env.CLAWSWEEPER_INTERNAL_MODEL, undefined);

‎test/sweep-workflow.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ test("exact event review publishes directly with a queue-bounded canonical fallb
649649
liveItem.env?.CLAIM_DECISION,
650650
"${{ steps.claim-exact-review-queue.outputs.decision }}",
651651
);
652+
assert.equal(
653+
liveItem.env?.GH_TOKEN,
654+
"${{ steps.target.outputs.target_repo == 'openclaw/openclaw' && github.token || steps.target-read-token.outputs.token }}",
655+
);
652656
assert.match(liveItem.run ?? "", /grep -Eq '\^\[0-9\]\+\$'/);
653657
assert.match(
654658
liveItem.run ?? "",

0 commit comments

Comments
 (0)