4545// name and version an artifact must match before it is published, so a
4646// moved tag there could approve a tampered artifact.
4747//
48+ // GitHub reads expression contexts, their property names and job ids without
49+ // regard to case, so `SECRETS.X`, `NEEDS.detect.outputs` and
50+ // `needs.DETECT.outputs` are matched here as their lowercase spellings are, and
51+ // a job id named anywhere (`needs:` or an expression) resolves to the job in
52+ // `jobs:` it names in any case (#4934).
53+ //
4854// `GITHUB_TOKEN` alone does not count: every job holds one, so counting it
4955// would turn this into "pin everything", which #4873 does not ask. That is why
5056// `version-packages.yml` and `prepare-python-release.yml` are out of scope
@@ -81,11 +87,11 @@ const repoRoot = path.resolve(
8187 ".." ,
8288) ;
8389
84- // `GITHUB_TOKEN` in either accessor spelling; removed before looking for any
85- // other `secrets` reference, so every spelling of every other name, a dynamic
86- // index included, still reads as a secret.
90+ // `GITHUB_TOKEN` in either accessor spelling, in any case; removed before
91+ // looking for any other `secrets` reference, so every spelling of every other
92+ // name, a dynamic index included, still reads as a secret.
8793const DEFAULT_TOKEN =
88- / \b s e c r e t s \s * (?: \. \s * G I T H U B _ T O K E N \b | \[ \s * ( [ ' " ] ) G I T H U B _ T O K E N \1\s * \] ) / g ;
94+ / \b s e c r e t s \s * (?: \. \s * G I T H U B _ T O K E N \b | \[ \s * ( [ ' " ] ) G I T H U B _ T O K E N \1\s * \] ) / gi ;
8995// Where the first expression in a string opens. Everything from there on is
9096// searched, rather than each `${{ … }}` body: finding where an expression ENDS
9197// needs a parser, since a quoted string inside one may itself contain ` }}`
@@ -124,7 +130,7 @@ function handedSecret(job, inherited) {
124130 const open = text . indexOf ( EXPRESSION_OPEN ) ;
125131 return (
126132 open !== - 1 &&
127- / \b s e c r e t s \b / . test ( text . slice ( open ) . replace ( DEFAULT_TOKEN , "" ) )
133+ / \b s e c r e t s \b / i . test ( text . slice ( open ) . replace ( DEFAULT_TOKEN , "" ) )
128134 ) ;
129135 } ) ;
130136}
@@ -182,6 +188,11 @@ function parseWorkflow(yaml, file = "<workflow>") {
182188export function credentialedJobs ( yaml , file ) {
183189 const workflow = parseWorkflow ( yaml , file ) . toJS ( ) ?? { } ;
184190 const jobs = Object . entries ( workflow . jobs ?? { } ) ;
191+ // A job id as written anywhere (a `needs:` entry, `needs.<job>`) → its key
192+ // in `jobs:`, or undefined when no job has that id in any case.
193+ const byId = new Map ( jobs . map ( ( [ name ] ) => [ name . toLowerCase ( ) , name ] ) ) ;
194+ const resolve = ( id ) => byId . get ( id . toLowerCase ( ) ) ;
195+ const needed = ( job ) => needsOf ( job ) . map ( resolve ) . filter ( Boolean ) ;
185196 const held = new Set ( ) ;
186197 for ( const [ name , job ] of jobs ) {
187198 const permissions =
@@ -197,12 +208,12 @@ export function credentialedJobs(yaml, file) {
197208 // about the chain.
198209 const upstreamOf = ( name ) => {
199210 const seen = new Set ( ) ;
200- const queue = [ ... needsOf ( workflow . jobs [ name ] ) ] ;
211+ const queue = needed ( workflow . jobs [ name ] ) ;
201212 while ( queue . length > 0 ) {
202213 const next = queue . pop ( ) ;
203- if ( seen . has ( next ) || ! workflow . jobs [ next ] ) continue ;
214+ if ( seen . has ( next ) ) continue ;
204215 seen . add ( next ) ;
205- queue . push ( ...needsOf ( workflow . jobs [ next ] ) ) ;
216+ queue . push ( ...needed ( workflow . jobs [ next ] ) ) ;
206217 }
207218 return seen ;
208219 } ;
@@ -213,7 +224,7 @@ export function credentialedJobs(yaml, file) {
213224 // reference that names no job (bare `needs`, `needs.*`, a dynamic index)
214225 // counts every job the reader needs.
215226 const NEEDS_USE =
216- / \b n e e d s \b \s * ( \. \s * ( [ \w - ] + | \* ) | \[ \s * (?: ' ( [ ^ ' ] * ) ' | " ( [ ^ " ] * ) " | [ ^ \] ] * ) \s * \] ) ? \s * (?: \. \s * ( \w + ) | \[ \s * [ ' " ] ( \w + ) [ ' " ] \s * \] ) ? / g ;
227+ / \b n e e d s \b \s * ( \. \s * ( [ \w - ] + | \* ) | \[ \s * (?: ' ( [ ^ ' ] * ) ' | " ( [ ^ " ] * ) " | [ ^ \] ] * ) \s * \] ) ? \s * (?: \. \s * ( \w + ) | \[ \s * [ ' " ] ( \w + ) [ ' " ] \s * \] ) ? / gi ;
217228 const outputsReadBy = ( job ) => {
218229 const read = new Set ( ) ;
219230 for ( const text of stringsIn ( job ) ) {
@@ -222,10 +233,11 @@ export function credentialedJobs(yaml, file) {
222233 for ( const m of text . slice ( open ) . matchAll ( NEEDS_USE ) ) {
223234 const named = m [ 2 ] ?? m [ 3 ] ?? m [ 4 ] ;
224235 const property = m [ 5 ] ?? m [ 6 ] ;
225- if ( m [ 1 ] !== undefined && property === "result" ) continue ;
236+ if ( m [ 1 ] !== undefined && property ?. toLowerCase ( ) === "result" )
237+ continue ;
226238 if ( named === undefined || named === "*" )
227- for ( const n of needsOf ( job ) ) read . add ( n ) ;
228- else read . add ( named ) ;
239+ for ( const n of needed ( job ) ) read . add ( n ) ;
240+ else if ( resolve ( named ) ) read . add ( resolve ( named ) ) ;
229241 }
230242 }
231243 return read ;
@@ -235,7 +247,7 @@ export function credentialedJobs(yaml, file) {
235247 for ( const [ name , job ] of jobs ) {
236248 if ( ! held . has ( name ) ) continue ;
237249 for ( const source of outputsReadBy ( job ) ) {
238- if ( workflow . jobs [ source ] && ! held . has ( source ) ) {
250+ if ( ! held . has ( source ) ) {
239251 held . add ( source ) ;
240252 grew = true ;
241253 }
0 commit comments