Skip to content

Commit b030328

Browse files
committed
fix: authorize team security reviews automatically
Signed-off-by: Jordan Mecom <[email protected]>
1 parent 0808ab4 commit b030328

3 files changed

Lines changed: 80 additions & 32 deletions

File tree

‎.github/scripts/codex-security-review.js‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const completedMarker = (baseSha, headSha) =>
2828

2929
const reviewCommand = (headSha) => `${REVIEW_COMMAND} ${headSha}`;
3030

31-
const isOrganizationMember = (association) =>
32-
association === "MEMBER" || association === "OWNER";
31+
const hasWritePermission = (permission) =>
32+
permission === "write" || permission === "admin";
3333

3434
const hasCurrentReviewLabel = (pullRequest) =>
3535
pullRequest.labels?.some(
@@ -234,6 +234,28 @@ async function getPullRequest({ github, context, prNumber }) {
234234
return pullRequest;
235235
}
236236

237+
async function pullRequestAuthorCanWrite({ github, context, pullRequest }) {
238+
const username = pullRequest.user?.login;
239+
if (typeof username !== "string" || username.length === 0) {
240+
return false;
241+
}
242+
243+
try {
244+
const { data: access } =
245+
await github.rest.repos.getCollaboratorPermissionLevel({
246+
owner: context.repo.owner,
247+
repo: context.repo.repo,
248+
username,
249+
});
250+
return hasWritePermission(access.permission);
251+
} catch (error) {
252+
if (error?.status === 404) {
253+
return false;
254+
}
255+
throw error;
256+
}
257+
}
258+
237259
async function getLiveMainSha({ github, context }) {
238260
const { data: mainRef } = await github.rest.git.getRef({
239261
owner: context.repo.owner,
@@ -364,10 +386,12 @@ async function prepare({ github, context, core }) {
364386
}
365387
if (
366388
context.eventName === "pull_request_target" &&
367-
!isOrganizationMember(pullRequest.author_association)
389+
!(await pullRequestAuthorCanWrite({ github, context, pullRequest }))
368390
) {
391+
const author = pullRequest.user?.login || "unknown author";
369392
core.info(
370-
`Pull request #${prNumber} requires authorization from a Block organization member.`,
393+
`Pull request #${prNumber} author ${author} does not have write access ` +
394+
`to ${context.repo.owner}/${context.repo.repo}; manual authorization is required.`,
371395
);
372396
return;
373397
}
@@ -479,7 +503,7 @@ async function invalidatePullRequestUpdate({ github, context, core }) {
479503
github,
480504
context,
481505
core,
482-
existingOnlyForOrganizationMembers: true,
506+
existingOnlyForTrustedAuthors: true,
483507
});
484508
}
485509

@@ -489,7 +513,7 @@ async function invalidate({
489513
core,
490514
prNumber: requestedPrNumber,
491515
existingOnly = false,
492-
existingOnlyForOrganizationMembers = false,
516+
existingOnlyForTrustedAuthors = false,
493517
}) {
494518
const prNumber = Number(
495519
requestedPrNumber ?? context.payload.pull_request?.number,
@@ -506,10 +530,12 @@ async function invalidate({
506530
}
507531

508532
const existing = await findReviewComment({ github, context, prNumber });
533+
const trustedUnreviewedAuthor =
534+
!existing &&
535+
existingOnlyForTrustedAuthors &&
536+
(await pullRequestAuthorCanWrite({ github, context, pullRequest }));
509537
const shouldOnlyUpdateExisting =
510-
existingOnly ||
511-
(existingOnlyForOrganizationMembers &&
512-
isOrganizationMember(pullRequest.author_association));
538+
existingOnly || trustedUnreviewedAuthor;
513539
if (!existing && shouldOnlyUpdateExisting) {
514540
if (existingOnly || hasCurrentReviewLabel(pullRequest)) {
515541
await clearCurrentReview({ github, context, prNumber });

‎.github/scripts/codex-security-review.test.js‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const CURRENT_REVIEW_LABEL = "codex-security-review-current";
2424

2525
function pullRequest({
2626
authorAssociation = "CONTRIBUTOR",
27+
authorLogin = "pr-author",
2728
baseSha = OLD_BASE_SHA,
2829
headSha = HEAD_SHA,
2930
labels = [],
3031
} = {}) {
3132
return {
3233
author_association: authorAssociation,
34+
user: { login: authorLogin },
3335
state: "open",
3436
base: {
3537
ref: "main",
@@ -46,6 +48,7 @@ function pullRequest({
4648
}
4749

4850
function harness({
51+
authorPermission = "read",
4952
pull = pullRequest(),
5053
comments = [],
5154
files = [],
@@ -63,6 +66,7 @@ function harness({
6366
const notices = [];
6467
const info = [];
6568
const warnings = [];
69+
const permissionChecks = [];
6670
const listComments = async () => storedComments;
6771
const listFiles = async () => files;
6872
let labelExists = false;
@@ -122,6 +126,12 @@ function harness({
122126
get: async () => ({ data: pull }),
123127
listFiles,
124128
},
129+
repos: {
130+
getCollaboratorPermissionLevel: async (input) => {
131+
permissionChecks.push(input);
132+
return { data: { permission: authorPermission } };
133+
},
134+
},
125135
},
126136
};
127137
const core = {
@@ -150,6 +160,7 @@ function harness({
150160
info,
151161
notices,
152162
outputs,
163+
permissionChecks,
153164
removeLabelCalls,
154165
removedLabels,
155166
storedComments,
@@ -231,24 +242,31 @@ test("prepare binds a member command to the named head SHA", async () => {
231242
);
232243
});
233244

234-
test("pull request authorization uses the live author association", async () => {
235-
const member = harness({
236-
pull: pullRequest({ authorAssociation: "MEMBER" }),
237-
});
238-
member.context.eventName = "pull_request_target";
239-
member.context.payload.pull_request = {
240-
number: 6816,
241-
head: { sha: HEAD_SHA },
242-
author_association: "CONTRIBUTOR",
243-
};
244-
245-
await prepare(member);
246-
247-
assert.equal(member.outputs.get("authorized"), "true");
248-
assert.deepEqual(member.failures, []);
245+
test("pull request authorization uses live repository write access", async () => {
246+
for (const authorPermission of ["write", "admin"]) {
247+
const trusted = harness({
248+
authorPermission,
249+
pull: pullRequest({ authorAssociation: "CONTRIBUTOR" }),
250+
});
251+
trusted.context.eventName = "pull_request_target";
252+
trusted.context.payload.pull_request = {
253+
number: 6816,
254+
head: { sha: HEAD_SHA },
255+
author_association: "CONTRIBUTOR",
256+
};
257+
258+
await prepare(trusted);
259+
260+
assert.equal(trusted.outputs.get("authorized"), "true");
261+
assert.deepEqual(trusted.failures, []);
262+
assert.deepEqual(trusted.permissionChecks, [
263+
{ owner: "block", repo: "buzz", username: "pr-author" },
264+
]);
265+
}
249266

250267
const external = harness({
251-
pull: pullRequest({ authorAssociation: "CONTRIBUTOR" }),
268+
authorPermission: "read",
269+
pull: pullRequest({ authorAssociation: "MEMBER" }),
252270
});
253271
external.context.eventName = "pull_request_target";
254272
external.context.payload.pull_request = {
@@ -261,7 +279,7 @@ test("pull request authorization uses the live author association", async () =>
261279

262280
assert.equal(external.outputs.get("authorized"), undefined);
263281
assert.deepEqual(external.failures, []);
264-
assert.match(external.info.at(-1), /requires authorization/);
282+
assert.match(external.info.at(-1), /does not have write access/);
265283
});
266284

267285
test("PR mutation jobs use pull request write permission", () => {
@@ -498,8 +516,9 @@ test("base reconciliation does not create comments on unreviewed PRs", async ()
498516
assert.equal(state.updated.length, 0);
499517
});
500518

501-
test("pull request updates invalidate member reviews without adding placeholders", async () => {
519+
test("pull request updates invalidate trusted reviews without adding placeholders", async () => {
502520
const reviewed = harness({
521+
authorPermission: "write",
503522
pull: pullRequest({
504523
authorAssociation: "MEMBER",
505524
headSha: OTHER_HEAD_SHA,
@@ -529,6 +548,7 @@ test("pull request updates invalidate member reviews without adding placeholders
529548
assert.equal(reviewed.removedLabels.length, 1);
530549

531550
const unreviewed = harness({
551+
authorPermission: "write",
532552
pull: pullRequest({ authorAssociation: "OWNER" }),
533553
});
534554
unreviewed.context.eventName = "pull_request_target";
@@ -544,6 +564,7 @@ test("pull request updates invalidate member reviews without adding placeholders
544564
assert.equal(unreviewed.removeLabelCalls.length, 0);
545565

546566
const external = harness({
567+
authorPermission: "read",
547568
pull: pullRequest({ authorAssociation: "CONTRIBUTOR" }),
548569
});
549570
external.context.eventName = "pull_request_target";

‎.github/workflows/codex-security-review.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ jobs:
1616
name: Authorize Security Review
1717
# This workflow posts an advisory review; its skipped jobs are not a merge
1818
# gate and must not be configured as required status checks.
19-
# MEMBER and OWNER are GitHub's associations for members of the `block`
20-
# organization. The trusted prepare step checks the live PR instead of the
21-
# event snapshot. Outside contributors require this exact command from one
22-
# of those members: @buzz-security-review <full-head-sha>
19+
# Authors with live write or admin access to `block/buzz` are reviewed
20+
# automatically. This calculated repository permission includes direct,
21+
# team, organization, and enterprise grants. Outside contributors require
22+
# this exact command from a Block organization member:
23+
# @buzz-security-review <full-head-sha>
2324
if: >-
2425
github.repository == 'block/buzz' && (
2526
(
@@ -301,7 +302,7 @@ jobs:
301302
codex-version: '0.149.0'
302303
model: ${{ env.CODEX_MODEL }}
303304
codex-args: '["-c","model_reasoning_effort=${{ env.CODEX_REASONING_EFFORT }}"]'
304-
# The trusted authorization job already enforced repository membership.
305+
# The trusted authorization job already enforced repository write access.
305306
allow-users: '*'
306307
safety-strategy: drop-sudo
307308
permission-profile: ':read-only'

0 commit comments

Comments
 (0)