Skip to content

Commit 14bd27b

Browse files
authored
fix(review): verify redacted maintainer authors before proof gating (#1035)
1 parent a47ccba commit 14bd27b

2 files changed

Lines changed: 131 additions & 3 deletions

File tree

‎src/clawsweeper-review-command-workflow.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { readFileSync, writeFileSync } from "node:fs";
22
import { join } from "node:path";
33
import { ACTION_EVENT_REASON_CODES, ACTION_EVENT_STATUSES } from "./action-ledger.js";
44
import type { Args } from "./clawsweeper-args.js";
5+
import {
6+
isBulkFilerExemptRepositoryPermission as isVerifiedMaintainerRepositoryPermission,
7+
isMaintainerAuthorAssociation,
8+
} from "./clawsweeper-item-policy.js";
59
import { mediaProofRuntimeHints, prepareMediaProofArtifacts } from "./clawsweeper-media-proof.js";
610
import type {
711
AcquiredReviewStartLease,
@@ -39,6 +43,29 @@ import { reviewContentCacheHit } from "./scheduler-policy.js";
3943
import type { CreateReviewCommandWorkflowDependencies } from "./clawsweeper-review-command-dependencies.js";
4044
import { prepareReviewCommand } from "./clawsweeper-review-preparation.js";
4145

46+
export function restoreVerifiedMaintainerPullRequestAuthorAssociation(
47+
item: Pick<Item, "kind" | "author" | "authorAssociation" | "labels">,
48+
repositoryPermission: (author: string) => string | null,
49+
): boolean {
50+
if (
51+
item.kind !== "pull_request" ||
52+
!item.author.trim() ||
53+
isMaintainerAuthorAssociation(item.authorAssociation) ||
54+
!item.labels.some((label) => label.trim().toLowerCase() === "maintainer")
55+
) {
56+
return false;
57+
}
58+
let permission: string | null;
59+
try {
60+
permission = repositoryPermission(item.author);
61+
} catch {
62+
return false;
63+
}
64+
if (!isVerifiedMaintainerRepositoryPermission(permission)) return false;
65+
item.authorAssociation = "MEMBER";
66+
return true;
67+
}
68+
4269
export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWorkflowDependencies) {
4370
const {
4471
actionLedgerFailureDisposition,
@@ -238,6 +265,11 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
238265
const leaseAcquisitionFailureDetails: string[] = [];
239266
// oxfmt-ignore
240267
for (const item of candidates) {
268+
const restoredMaintainerAssociation =
269+
!localOnly &&
270+
restoreVerifiedMaintainerPullRequestAuthorAssociation(item, (author) =>
271+
bulkFilerRepositoryPermission(author, bulkFilerRepositoryPermissionCache),
272+
);
241273
activeReviewItem = item;
242274
let reviewItemFailed = false;
243275
const previousReviewMutationRunner = dependencies.activeReviewMutationRunner;
@@ -301,9 +333,16 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
301333
)
302334
? null
303335
: existingPriorReview;
304-
if (bulkFilerPolicyInvalidatesCachedReview(priorReview?.markdown ?? null, bulkFilerExemptionApplied)) {
305-
// A prior full review was made under a now-inapplicable bulk-filer policy.
306-
// Re-run it instead of refreshing its cached suppression fields.
336+
if (
337+
(restoredMaintainerAssociation &&
338+
priorReview !== null &&
339+
!isMaintainerAuthorAssociation(
340+
frontMatterValue(priorReview.markdown, "author_association"),
341+
)) ||
342+
bulkFilerPolicyInvalidatesCachedReview(priorReview?.markdown ?? null, bulkFilerExemptionApplied)
343+
) {
344+
// Ownership and bulk-filer policy changes require a fresh decision;
345+
// carrying stale front matter would preserve the wrong safeguards.
307346
priorReview = null;
308347
}
309348
const expectedPreviousReviewDigest = priorReview

‎test/pr-proof-automation.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
renderReviewCommentFromReport,
99
reviewAutomationMarkersFromReport,
1010
} from "../dist/clawsweeper.js";
11+
import { restoreVerifiedMaintainerPullRequestAuthorAssociation } from "../dist/clawsweeper-review-command-workflow.js";
1112
import {
1213
changelogReviewDecision,
1314
item,
@@ -304,6 +305,94 @@ Full review comments:
304305
assert.doesNotMatch(markers, /clawsweeper-verdict:needs-human/, scenario.author);
305306
}
306307

308+
for (const permission of ["admin", "maintain"]) {
309+
const canary = item({
310+
kind: "pull_request",
311+
number: 113345,
312+
author: "steipete",
313+
authorAssociation: "CONTRIBUTOR",
314+
labels: ["maintainer", "size: XS", "status: 📣 needs proof"],
315+
});
316+
const redactedReport = reportFor({
317+
author: canary.author,
318+
association: canary.authorAssociation,
319+
status: "mock_only",
320+
});
321+
assert.match(
322+
renderReviewCommentFromReport(redactedReport, "none"),
323+
/blocked until real behavior proof from a real setup is added/i,
324+
);
325+
let lookups = 0;
326+
assert.equal(
327+
restoreVerifiedMaintainerPullRequestAuthorAssociation(canary, (author) => {
328+
lookups += 1;
329+
assert.equal(author, "steipete");
330+
return permission;
331+
}),
332+
true,
333+
);
334+
assert.equal(lookups, 1);
335+
assert.equal(canary.authorAssociation, "MEMBER");
336+
337+
const correctedReport = reportFor({
338+
author: canary.author,
339+
association: canary.authorAssociation,
340+
status: "mock_only",
341+
});
342+
assert.match(
343+
renderReviewCommentFromReport(correctedReport, "none"),
344+
/✅ \*\*Ready for maintainer review\*\*/,
345+
);
346+
assert.match(reviewAutomationMarkersFromReport(correctedReport), /clawsweeper-verdict:pass/);
347+
}
348+
349+
for (const permission of ["write", "read", null]) {
350+
const unverified = item({
351+
kind: "pull_request",
352+
author: "external",
353+
authorAssociation: "CONTRIBUTOR",
354+
labels: ["maintainer"],
355+
});
356+
assert.equal(
357+
restoreVerifiedMaintainerPullRequestAuthorAssociation(unverified, () => permission),
358+
false,
359+
String(permission),
360+
);
361+
assert.equal(unverified.authorAssociation, "CONTRIBUTOR");
362+
}
363+
364+
const unavailable = item({
365+
kind: "pull_request",
366+
authorAssociation: "CONTRIBUTOR",
367+
labels: ["maintainer"],
368+
});
369+
assert.equal(
370+
restoreVerifiedMaintainerPullRequestAuthorAssociation(unavailable, () => {
371+
throw new Error("GitHub permission lookup failed");
372+
}),
373+
false,
374+
);
375+
assert.equal(unavailable.authorAssociation, "CONTRIBUTOR");
376+
377+
for (const ineligible of [
378+
item({ kind: "issue", authorAssociation: "CONTRIBUTOR", labels: ["maintainer"] }),
379+
item({ kind: "pull_request", authorAssociation: "CONTRIBUTOR", labels: [] }),
380+
item({ kind: "pull_request", authorAssociation: "OWNER", labels: ["maintainer"] }),
381+
item({ kind: "pull_request", authorAssociation: "MEMBER", labels: ["maintainer"] }),
382+
item({ kind: "pull_request", authorAssociation: "COLLABORATOR", labels: ["maintainer"] }),
383+
item({ kind: "pull_request", author: "", labels: ["maintainer"] }),
384+
]) {
385+
let lookups = 0;
386+
assert.equal(
387+
restoreVerifiedMaintainerPullRequestAuthorAssociation(ineligible, () => {
388+
lookups += 1;
389+
return "admin";
390+
}),
391+
false,
392+
);
393+
assert.equal(lookups, 0);
394+
}
395+
307396
const suppliedComment = renderReviewCommentFromReport(
308397
reportFor({ author: "maintainer", association: "MEMBER", status: "sufficient" }),
309398
"none",

0 commit comments

Comments
 (0)