@@ -3,6 +3,7 @@ import { dirname, join, relative } from 'node:path'
33import { describe , expect , it } from 'vitest'
44import { expr } from './lib/expressions.mjs'
55import { REPO_ROOT } from './lib/repo-root.mjs'
6+ import { isRunsOnJob } from './lib/runs-on.mjs'
67import { readWorkflow , workflowFiles } from './lib/workflows.mjs'
78
89/**
@@ -33,8 +34,14 @@ import { readWorkflow, workflowFiles } from './lib/workflows.mjs'
3334 * task that reaches either through its body, its `depends`, or a nested
3435 * `mise run`. A login a job did not need costs one step and nothing else.
3536 *
36- * Only a literal `runs-on=` label counts as RunsOn. GitHub-hosted runners are
37- * not limited this way, and Blacksmith jobs are leaving.
37+ * A job counts as RunsOn when its label starts with `runs-on=`. It also
38+ * counts when `MATRIX_RUNNERS` in `lib/runs-on.mjs` says its matrix gives it
39+ * RunsOn machines, because a matrix runner is an expression this file cannot
40+ * read. GitHub-hosted runners are not limited this way.
41+ *
42+ * In a called workflow, `secrets.*` is empty unless the caller passes it. A
43+ * login there would skip, and the job would pull anonymously with no error.
44+ * So every caller of a workflow whose RunsOn jobs log in passes both secrets.
3845 */
3946
4047// ---------------------------------------------------------------------------
@@ -269,11 +276,9 @@ export function credentialsFinding(credentials) {
269276 return null
270277}
271278
272- const isRunsOn = ( job ) => String ( job ?. [ 'runs-on' ] ?? '' ) . startsWith ( 'runs-on=' )
273-
274279/** Every finding for one job, or [] when it is correct. */
275- export function jobFindings ( where , job ) {
276- if ( ! isRunsOn ( job ) ) return [ ]
280+ export function jobFindings ( where , job , ref ) {
281+ if ( ! isRunsOnJob ( job , ref ) ) return [ ]
277282 const findings = [ ]
278283 const containers = [
279284 ...Object . entries ( job . services ?? { } ) . map ( ( [ name , svc ] ) => [
@@ -310,8 +315,41 @@ export function jobFindings(where, job) {
310315function runsOnJobs ( ) {
311316 return workflowFiles ( ) . flatMap ( ( file ) =>
312317 Object . entries ( readWorkflow ( file ) ?. jobs ?? { } )
313- . filter ( ( [ , job ] ) => isRunsOn ( job ) )
314- . map ( ( [ name , job ] ) => ( { where : `${ file } ${ name } ` , job } ) ) ,
318+ . map ( ( [ name , job ] ) => ( { where : `${ file } ${ name } ` , ref : `${ file } #${ name } ` , job } ) )
319+ . filter ( ( { job, ref } ) => isRunsOnJob ( job , ref ) ) ,
320+ )
321+ }
322+
323+ /** Whether a calling job passes both Docker Hub secrets to the workflow it calls. */
324+ export function callerFinding ( job ) {
325+ if ( job ?. secrets === 'inherit' ) return null
326+ const passed = job ?. secrets ?? { }
327+ const missing = [
328+ [ 'DOCKER_HUB_USERNAME' , USERNAME ] ,
329+ [ 'DOCKER_HUB_PASSWORD' , PASSWORD ] ,
330+ ]
331+ . filter ( ( [ name , value ] ) => passed [ name ] !== value )
332+ . map ( ( [ name ] ) => name )
333+ return missing . length
334+ ? `does not pass ${ missing . join ( ' and ' ) } to the workflow it calls, so its login would skip`
335+ : null
336+ }
337+
338+ /** Each local workflow call whose called RunsOn jobs log in, and what it lacks. */
339+ function callerFindings ( ) {
340+ return workflowFiles ( ) . flatMap ( ( file ) =>
341+ Object . entries ( readWorkflow ( file ) ?. jobs ?? { } )
342+ . filter ( ( [ , job ] ) => String ( job ?. uses ?? '' ) . startsWith ( './.github/workflows/' ) )
343+ . flatMap ( ( [ name , job ] ) => {
344+ const called = job . uses . replace ( / ^ \. \/ / , '' ) . replace ( / @ .* $ / , '' )
345+ const logsIn = Object . entries ( readWorkflow ( called ) ?. jobs ?? { } ) . some (
346+ ( [ id , calledJob ] ) =>
347+ isRunsOnJob ( calledJob , `${ called } #${ id } ` ) &&
348+ ( calledJob . steps ?? [ ] ) . some ( ( step ) => loginFinding ( step ) === null ) ,
349+ )
350+ const finding = logsIn ? callerFinding ( job ) : null
351+ return finding ? [ `${ file } #${ name } : ${ finding } ` ] : [ ]
352+ } ) ,
315353 )
316354}
317355
@@ -322,7 +360,7 @@ describe('Docker Hub login on RunsOn', () => {
322360
323361 it ( 'finds the jobs it checks' , ( ) => {
324362 // A discovery test that finds nothing passes having checked nothing.
325- expect ( jobs . length ) . toBeGreaterThan ( 20 )
363+ expect ( jobs . length ) . toBeGreaterThan ( 50 )
326364 expect (
327365 jobs . filter ( ( { job } ) => ( job . steps ?? [ ] ) . some ( ( s ) => stepPulls ( s ) ) )
328366 . length ,
@@ -332,9 +370,13 @@ describe('Docker Hub login on RunsOn', () => {
332370 } )
333371
334372 it ( 'logs in before every pull' , ( ) => {
335- expect ( jobs . flatMap ( ( { where, job } ) => jobFindings ( where , job ) ) ) . toEqual (
336- [ ] ,
337- )
373+ expect (
374+ jobs . flatMap ( ( { where, job, ref } ) => jobFindings ( where , job , ref ) ) ,
375+ ) . toEqual ( [ ] )
376+ } )
377+
378+ it ( 'passes the Docker Hub secrets to every called workflow whose RunsOn jobs log in' , ( ) => {
379+ expect ( callerFindings ( ) ) . toEqual ( [ ] )
338380 } )
339381
340382 // Every job in the tree is correct, so the test above runs none of the
@@ -367,6 +409,30 @@ describe('Docker Hub login on RunsOn', () => {
367409 expect (
368410 jobFindings ( 'w j' , { 'runs-on' : 'ubuntu-latest' , steps : [ up ] } ) ,
369411 ) . toEqual ( [ ] )
412+ // A matrix runner that MATRIX_RUNNERS marks as RunsOn is in scope.
413+ expect (
414+ jobFindings (
415+ 'w j' ,
416+ // biome-ignore lint/suspicious/noTemplateCurlyInString: a GitHub Actions expression, the literal `runs-on` value under test.
417+ { 'runs-on' : '${{ matrix.cfg.os }}' , steps : [ up ] } ,
418+ '.github/workflows/_build-ffi-artifacts.yml#binaries' ,
419+ ) ,
420+ ) . toEqual ( [ expect . stringMatching ( / n o D o c k e r H u b l o g i n c o m e s b e f o r e i t / ) ] )
421+ } )
422+
423+ it ( 'reports a call that does not pass both secrets' , ( ) => {
424+ const uses = './.github/workflows/x.yml'
425+ expect ( callerFinding ( { uses } ) ) . toMatch ( / D O C K E R _ H U B _ U S E R N A M E / )
426+ expect ( callerFinding ( { uses, secrets : 'inherit' } ) ) . toBeNull ( )
427+ expect (
428+ callerFinding ( {
429+ uses,
430+ secrets : { DOCKER_HUB_USERNAME : USERNAME , DOCKER_HUB_PASSWORD : PASSWORD } ,
431+ } ) ,
432+ ) . toBeNull ( )
433+ expect (
434+ callerFinding ( { uses, secrets : { DOCKER_HUB_USERNAME : USERNAME } } ) ,
435+ ) . toMatch ( / D O C K E R _ H U B _ P A S S W O R D / )
370436 } )
371437
372438 it ( 'reports a login that would fail on a fork pull request' , ( ) => {
0 commit comments