11"use strict" ;
22
33const assert = require ( "node:assert/strict" ) ;
4+ const { readFileSync } = require ( "node:fs" ) ;
5+ const path = require ( "node:path" ) ;
46const test = require ( "node:test" ) ;
57
68const {
@@ -20,8 +22,14 @@ const NEW_BASE_SHA = "e".repeat(40);
2022const MARKER = "<!-- codex-security-review -->" ;
2123const CURRENT_REVIEW_LABEL = "codex-security-review-current" ;
2224
23- function pullRequest ( { baseSha = OLD_BASE_SHA , headSha = HEAD_SHA } = { } ) {
25+ function pullRequest ( {
26+ authorAssociation = "CONTRIBUTOR" ,
27+ baseSha = OLD_BASE_SHA ,
28+ headSha = HEAD_SHA ,
29+ labels = [ ] ,
30+ } = { } ) {
2431 return {
32+ author_association : authorAssociation ,
2533 state : "open" ,
2634 base : {
2735 ref : "main" ,
@@ -33,6 +41,7 @@ function pullRequest({ baseSha = OLD_BASE_SHA, headSha = HEAD_SHA } = {}) {
3341 repo : { full_name : "outside/buzz" } ,
3442 } ,
3543 changed_files : 1 ,
44+ labels,
3645 } ;
3746}
3847
@@ -48,6 +57,7 @@ function harness({
4857 const updated = [ ] ;
4958 const addedLabels = [ ] ;
5059 const removedLabels = [ ] ;
60+ const removeLabelCalls = [ ] ;
5161 const outputs = new Map ( ) ;
5262 const failures = [ ] ;
5363 const notices = [ ] ;
@@ -85,6 +95,7 @@ function harness({
8595 addedLabels . push ( input ) ;
8696 } ,
8797 removeLabel : async ( input ) => {
98+ removeLabelCalls . push ( input ) ;
8899 if ( ! labelExists ) {
89100 throw Object . assign ( new Error ( "not found" ) , { status : 404 } ) ;
90101 }
@@ -139,6 +150,7 @@ function harness({
139150 info,
140151 notices,
141152 outputs,
153+ removeLabelCalls,
142154 removedLabels,
143155 storedComments,
144156 updated,
@@ -198,6 +210,7 @@ test("prepare binds a member command to the named head SHA", async () => {
198210 await prepare ( current ) ;
199211
200212 assert . deepEqual ( current . failures , [ ] ) ;
213+ assert . equal ( current . outputs . get ( "authorized" ) , "true" ) ;
201214 assert . equal ( current . outputs . get ( "head_sha" ) , HEAD_SHA ) ;
202215 assert . equal ( current . outputs . get ( "base_sha" ) , BASE_SHA ) ;
203216 assert . notEqual ( current . outputs . get ( "base_sha" ) , OLD_BASE_SHA ) ;
@@ -218,6 +231,62 @@ test("prepare binds a member command to the named head SHA", async () => {
218231 ) ;
219232} ) ;
220233
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 , [ ] ) ;
249+
250+ const external = harness ( {
251+ pull : pullRequest ( { authorAssociation : "CONTRIBUTOR" } ) ,
252+ } ) ;
253+ external . context . eventName = "pull_request_target" ;
254+ external . context . payload . pull_request = {
255+ number : 6816 ,
256+ head : { sha : HEAD_SHA } ,
257+ author_association : "MEMBER" ,
258+ } ;
259+
260+ await prepare ( external ) ;
261+
262+ assert . equal ( external . outputs . get ( "authorized" ) , undefined ) ;
263+ assert . deepEqual ( external . failures , [ ] ) ;
264+ assert . match ( external . info . at ( - 1 ) , / r e q u i r e s a u t h o r i z a t i o n / ) ;
265+ } ) ;
266+
267+ test ( "PR mutation jobs use pull request write permission" , ( ) => {
268+ const workflow = readFileSync (
269+ path . join ( __dirname , "../workflows/codex-security-review.yml" ) ,
270+ "utf8" ,
271+ ) ;
272+ for ( const jobName of [
273+ "reconcile-base-reviews" ,
274+ "invalidate-previous-review" ,
275+ "post-review" ,
276+ ] ) {
277+ const start = workflow . indexOf ( ` ${ jobName } :\n` ) ;
278+ assert . notEqual ( start , - 1 , `missing workflow job ${ jobName } ` ) ;
279+ const remainder = workflow . slice ( start + 2 ) ;
280+ const nextJob = remainder . search ( / ^ [ a - z ] [ a - z 0 - 9 - ] * : \n / m) ;
281+ const job =
282+ nextJob === - 1
283+ ? workflow . slice ( start )
284+ : workflow . slice ( start , start + 2 + nextJob ) ;
285+ assert . match ( job , / ^ p u l l - r e q u e s t s : w r i t e $ / m) ;
286+ assert . doesNotMatch ( job , / ^ i s s u e s : w r i t e $ / m) ;
287+ }
288+ } ) ;
289+
221290test ( "prepare rejects review commands without a full exact SHA" , async ( ) => {
222291 const state = harness ( ) ;
223292 state . context . payload . comment . body = "@buzz-security-review" ;
@@ -431,7 +500,10 @@ test("base reconciliation does not create comments on unreviewed PRs", async ()
431500
432501test ( "pull request updates invalidate member reviews without adding placeholders" , async ( ) => {
433502 const reviewed = harness ( {
434- pull : pullRequest ( { headSha : OTHER_HEAD_SHA } ) ,
503+ pull : pullRequest ( {
504+ authorAssociation : "MEMBER" ,
505+ headSha : OTHER_HEAD_SHA ,
506+ } ) ,
435507 comments : [
436508 reviewComment (
437509 `${ MARKER } \n<!-- codex-security-review-range:${ BASE_SHA } ...${ HEAD_SHA } -->\nold review` ,
@@ -441,7 +513,7 @@ test("pull request updates invalidate member reviews without adding placeholders
441513 reviewed . context . eventName = "pull_request_target" ;
442514 reviewed . context . payload . pull_request = {
443515 number : 6816 ,
444- author_association : "MEMBER " ,
516+ author_association : "CONTRIBUTOR " ,
445517 } ;
446518 await reviewed . github . rest . issues . addLabels ( {
447519 issue_number : 6816 ,
@@ -456,23 +528,28 @@ test("pull request updates invalidate member reviews without adding placeholders
456528 ) ;
457529 assert . equal ( reviewed . removedLabels . length , 1 ) ;
458530
459- const unreviewed = harness ( ) ;
531+ const unreviewed = harness ( {
532+ pull : pullRequest ( { authorAssociation : "OWNER" } ) ,
533+ } ) ;
460534 unreviewed . context . eventName = "pull_request_target" ;
461535 unreviewed . context . payload . pull_request = {
462536 number : 6816 ,
463- author_association : "OWNER " ,
537+ author_association : "CONTRIBUTOR " ,
464538 } ;
465539
466540 await invalidatePullRequestUpdate ( unreviewed ) ;
467541
468542 assert . equal ( unreviewed . created . length , 0 ) ;
469543 assert . equal ( unreviewed . updated . length , 0 ) ;
544+ assert . equal ( unreviewed . removeLabelCalls . length , 0 ) ;
470545
471- const external = harness ( ) ;
546+ const external = harness ( {
547+ pull : pullRequest ( { authorAssociation : "CONTRIBUTOR" } ) ,
548+ } ) ;
472549 external . context . eventName = "pull_request_target" ;
473550 external . context . payload . pull_request = {
474551 number : 6816 ,
475- author_association : "CONTRIBUTOR " ,
552+ author_association : "MEMBER " ,
476553 } ;
477554
478555 await invalidatePullRequestUpdate ( external ) ;
0 commit comments