@@ -143,14 +143,87 @@ describe("enforce-pr-target workflow", () => {
143143 . split ( "- name: Checkout trusted PR-quality scripts" ) [ 1 ]
144144 . split ( / \n { 6 } - n a m e : / ) [ 0 ] ;
145145 assert . match ( checkoutStep , / a c t i o n s \/ c h e c k o u t @ [ 0 - 9 a - f ] { 40 } / ) ;
146- // `pull_request_target` pins the PR base SHA. Trusted `status`
147- // revalidation has no pull_request payload, so it sources scripts from the
148- // repository default branch that supplied the privileged workflow itself.
149- assert . match (
150- checkoutStep ,
151- / r e f : \s * \$ \{ \{ \s * g i t h u b \. e v e n t _ n a m e \s * = = \s * ' s t a t u s ' \s * & & \s * g i t h u b \. e v e n t \. r e p o s i t o r y \. d e f a u l t _ b r a n c h \s * \| \| \s * g i t h u b \. e v e n t \. p u l l _ r e q u e s t \. b a s e \. s h a \s * \} \} / ,
146+ // The trusted ref comes from a fixed set of integration branches, never
147+ // from the PR's own base commit: a stacked child's base is another open
148+ // PR's head, and `base.sha` would let that unpromoted commit choose the
149+ // code that runs with this job's write-capable token.
150+ //
151+ // `status` has no pull_request payload and sources from the default branch
152+ // that supplied the privileged workflow. A `main`-targeting PR sources
153+ // from `main` so the workflow definition and the scripts match. Everything
154+ // else resolves to `dev`.
155+ //
156+ // Exact equality, not fragment matching: separate checks for `status`,
157+ // `main`, and `dev` would all pass with the operator grouping wrong or a
158+ // surviving `base.sha` fallback.
159+ const ref = checkoutStep . match ( / ^ \s * r e f : \s * ( .+ ) $ / m) ?. [ 1 ] ;
160+ assert . ok ( ref , "trusted checkout must declare ref" ) ;
161+ assert . equal (
162+ ref . replace ( / \s + / g, " " ) . trim ( ) ,
163+ "${{ github.event_name == 'status' && github.event.repository.default_branch || (github.event.pull_request.base.ref == 'main' && 'main' || 'dev') }}" ,
164+ ) ;
165+ assert . doesNotMatch ( ref , / b a s e \. s h a | h e a d \. (?: s h a | r e f ) / ) ;
166+ // Pinning the checkout ref only gates one step. A later `run:` or
167+ // `github-script` step interpolating a head ref would execute
168+ // PR-controlled content with this workflow's write-capable token, so the
169+ // whole file is gated. (`pr.head.sha` read back from the API is an
170+ // identity for comparison, not an interpolated ref, and is unaffected.)
171+ assert . doesNotMatch (
172+ workflow ,
173+ / g i t h u b \. e v e n t \. p u l l _ r e q u e s t \. h e a d \. (?: s h a | r e f | r e p o ) / ,
174+ "no step in a pull_request_target workflow may interpolate a PR head ref" ,
175+ ) ;
176+ // `refs/pull/<n>/head` reaches the same PR-controlled tree without ever
177+ // naming `head`, so ban the merge-ref form too.
178+ assert . doesNotMatch (
179+ workflow ,
180+ / r e f s \/ p u l l \/ / ,
181+ "no step may check out a refs/pull/* ref" ,
182+ ) ;
183+ // Banning literal text is not enough: `format('refs/{0}/{1}/{2}', ...)`
184+ // builds the same PR-controlled ref without ever spelling it. Every
185+ // checkout in a pull_request_target workflow must therefore declare a ref
186+ // drawn from the trusted allowlist, and no other step may name the PR
187+ // number in a ref-shaped expression.
188+ // An ALLOWLIST, not a denylist: every checkout in this workflow must use
189+ // exactly the trusted expression. Banning known-bad shapes lost twice —
190+ // first to `refs/pull/<n>/head`, then to `format('refs/{0}/...')` — and a
191+ // `repository:` override pointing at the fork head is a third shape no
192+ // denylist would have caught.
193+ const checkouts = workflow . match ( / u s e s : \s * a c t i o n s \/ c h e c k o u t @ [ \s \S ] * ?(? = \n { 6 } - n a m e : | $ ) / g) ?? [ ] ;
194+ for ( const step of checkouts ) {
195+ const stepRef = ( step . match ( / ^ \s * r e f : \s * ( .+ ) $ / m) ?. [ 1 ] ?? "" ) . replace ( / \s + / g, " " ) . trim ( ) ;
196+ assert . equal ( stepRef , "${{ github.event_name == 'status' && github.event.repository.default_branch || (github.event.pull_request.base.ref == 'main' && 'main' || 'dev') }}" , "every checkout must use the trusted ref" ) ;
197+ assert . doesNotMatch ( step , / r e p o s i t o r y : / , "a checkout must not retarget its repository" ) ;
198+ }
199+ // Checkout is not the only way to obtain PR-controlled code. A `run:` step
200+ // can fetch it directly, and that is a realistic future edit rather than a
201+ // synthetic one, so executable steps are gated on the acquisition verbs
202+ // themselves.
203+ // Stop enumerating command shapes. A denylist lost four times here
204+ // (`refs/pull`, `format()`, `repository:`, `gh pr checkout`), and
205+ // `git clone https://github.com/<fork>` would have been the fifth. The
206+ // invariant is simpler than the attack surface: under
207+ // `pull_request_target`, nothing executable may name the PR head or the
208+ // fork repository at all.
209+ // Comments may discuss the head ref; only executable content may not use
210+ // it, so YAML comment lines are stripped before this check.
211+ const executable = workflow
212+ . split ( "\n" )
213+ . filter ( line => ! / ^ \s * # / . test ( line ) && ! / ^ \s * \/ \/ / . test ( line . replace ( / ^ \s * / , "" ) ) )
214+ . join ( "\n" ) ;
215+ assert . doesNotMatch (
216+ executable . replace ( / ^ \s * \/ \/ .* $ / gm, "" ) ,
217+ / g i t h u b \. h e a d _ r e f | p u l l _ r e q u e s t (?: \[ [ ' " ] h e a d [ ' " ] \] | \. h e a d ) \s * (?: \[ | \. ) ? \s * [ ' " ] ? r e p o / ,
218+ "no executable step may reference the PR head repository" ,
219+ ) ;
220+ // Belt and braces for the acquisition verbs, which have no legitimate use
221+ // in either gate: both only read PR metadata through the API.
222+ assert . doesNotMatch (
223+ workflow ,
224+ / g h \s + p r \s + c h e c k o u t | g i t \s + (?: f e t c h | c h e c k o u t | c l o n e | s w i t c h ) | r e f s \/ p u l l / ,
225+ "no step may acquire pull-request code" ,
152226 ) ;
153- assert . doesNotMatch ( checkoutStep , / \| \| \s * ' d e v ' / ) ;
154227 // The readiness ping reads MAINTAINERS.md from the same trusted checkout.
155228 assert . match ( checkoutStep , / s p a r s e - c h e c k o u t : \s * \| \s * \n \s * \. g i t h u b \/ s c r i p t s \n \s * M A I N T A I N E R S \. m d / ) ;
156229 assert . match ( checkoutStep , / p e r s i s t - c r e d e n t i a l s : \s * f a l s e / ) ;
@@ -197,6 +270,9 @@ describe("enforce-pr-target workflow", () => {
197270 ) ;
198271 assert . ok ( qualityCall , "must call collectPrQualityFailures" ) ;
199272 assert . match ( qualityCall [ 1 ] , / s t a c k e d B a s e / ) ;
273+ assert . match ( qualityCall [ 1 ] , / c h a n g e d F i l e P a t h s / ) ;
274+ assert . match ( qualityCall [ 1 ] , / f i l e s T r u n c a t e d / ) ;
275+ assert . match ( workflow , / i s C h a n g e d F i l e L i s t T r u n c a t e d / ) ;
200276 } ) ;
201277
202278 it ( "strips stale WRONG BRANCH prefix on failure when base is corrected" , ( ) => {
0 commit comments