@@ -3,21 +3,30 @@ package workflowcheck
33import (
44 "fmt"
55 "regexp"
6+ "slices"
67 "strings"
78)
89
910const (
1011 toolchainAssertCommand = "go run ./cmd/slsa-builder-internal npm-profile-toolchain-assert"
1112 provisionNPMCommand = "go run ./cmd/slsa-builder-internal npm-profile-provision-npm"
13+ outcomeReportPath = "${{ runner.temp }}/windlass-npm-outcome-report.json"
1214)
1315
1416// Exclude npm-profile subcommands, npm-executable flags, and diagnostic labels (npm:).
1517var npmReferencePattern = regexp .MustCompile (`(^|[^a-zA-Z0-9_.-])npm($|[^a-zA-Z0-9_.:-])` )
1618
19+ // Capture the whole shell word, including concatenated quoted/unquoted fragments.
20+ var npmExecutableArgumentPattern = regexp .MustCompile (`(?:^|[\s;|&])--npm-executable(?:[ \t]+|=)((?:"[^"\r\n]*"|'[^'\r\n]*'|[^\s"';|&])+)` )
21+
1722func checkPairAssertion (steps []workflowStep , job string ) error {
23+ if err := checkToolchainConditions (steps ); err != nil {
24+ return fmt .Errorf ("%s: %w" , job , err )
25+ }
1826 asserted := false
1927 nodeReady := false
2028 for _ , current := range steps {
29+ current .Run = toolchainShellText (current .Run )
2130 if strings .HasPrefix (current .Uses , "actions/setup-node@" ) {
2231 nodeReady = true
2332 asserted = false
@@ -40,7 +49,11 @@ func checkPairAssertion(steps []workflowStep, job string) error {
4049 return nil
4150}
4251
43- func checkPublishToolchain (steps []workflowStep ) error {
52+ func checkPublishToolchain (job workflowJob ) error {
53+ steps := job .Steps
54+ if err := checkToolchainConditions (steps ); err != nil {
55+ return err
56+ }
4457 nodeReady := false
4558 asserted := false
4659 provisionID := ""
@@ -50,35 +63,55 @@ func checkPublishToolchain(steps []workflowStep) error {
5063 asserted = false
5164 provisionID = ""
5265 }
53- run := strings . ReplaceAll (current .Run , " \\ \n " , " " )
66+ run := toolchainShellText (current .Run )
5467 if npmReferencePattern .MatchString (run ) {
5568 return fmt .Errorf ("publish job must use the provisioning output instead of ambient npm" )
5669 }
5770 if strings .Contains (run , "npm-profile-toolchain-assert" ) {
5871 if ! nodeReady || current .WorkingDirectory != ".slsa-builder" ||
59- strings .TrimSpace (run ) != toolchainAssertCommand + " --node-only" {
60- return fmt .Errorf ("publish job must invoke npm-profile-toolchain-assert --node-only after Node.js setup" )
72+ strings .TrimSpace (run ) != toolchainAssertCommand + ` --node-only --report-path "$REPORT_PATH"` ||
73+ current .Env ["REPORT_PATH" ] != outcomeReportPath {
74+ return fmt .Errorf ("publish job must invoke npm-profile-toolchain-assert --node-only --report-path after Node.js setup" )
6175 }
6276 asserted = true
6377 }
6478 if strings .Contains (run , "npm-profile-provision-npm" ) {
6579 if ! asserted || current .ID == "" || current .WorkingDirectory != ".slsa-builder" ||
6680 ! strings .HasPrefix (strings .TrimSpace (run ), provisionNPMCommand + " " ) ||
67- ! containsAll (run , `--output-directory "${{ runner.temp }}/windlass-npm-cli"` , `--github-output "$GITHUB_OUTPUT"` ) {
68- return fmt .Errorf ("publish npm provisioning must have an id and emit GitHub outputs after the node-only assertion" )
81+ ! containsAll (run , `--output-directory "${{ runner.temp }}/windlass-npm-cli"` , `--github-output "$GITHUB_OUTPUT"` , `--report-path "$REPORT_PATH"` ) ||
82+ current .Env ["REPORT_PATH" ] != outcomeReportPath {
83+ return fmt .Errorf ("publish npm provisioning must have an id, emit GitHub outputs, and persist a failure report after the node-only assertion" )
6984 }
7085 provisionID = current .ID
7186 }
72- if strings .Contains (run , "npm-executable" ) || strings .Contains (run , "npm-profile-publish" ) {
87+ if strings .Contains (run , "npm-executable" ) || strings .Contains (run , "npm-profile-publish" ) || strings . Contains ( run , "NPM_EXECUTABLE" ) {
7388 if provisionID == "" {
7489 return fmt .Errorf ("npm provisioning must precede publish diagnostics and npm-profile-publish" )
7590 }
7691 output := "${{ steps." + provisionID + ".outputs.npm-executable }}"
92+ argumentRun := run
93+ if strings .Contains (run , "NPM_EXECUTABLE" ) {
94+ binding , present := current .Env ["NPM_EXECUTABLE" ]
95+ if ! present {
96+ binding = job .Env ["NPM_EXECUTABLE" ]
97+ }
98+ if binding != output {
99+ return fmt .Errorf ("NPM_EXECUTABLE must bind to %s" , output )
100+ }
101+ run = strings .NewReplacer ("${{ env.NPM_EXECUTABLE }}" , output ,
102+ "$NPM_EXECUTABLE" , output , "${NPM_EXECUTABLE}" , output ).Replace (run )
103+ }
77104 if ! strings .Contains (run , output ) || strings .Contains (strings .ReplaceAll (run , output , "" ), ".outputs.npm-executable" ) {
78105 return fmt .Errorf ("publish npm references must use %s" , output )
79106 }
80- if strings .Contains (run , "npm-profile-publish" ) && ! strings .Contains (run , `--npm-executable "` + output + `"` ) {
81- return fmt .Errorf ("npm-profile-publish --npm-executable must use %s" , output )
107+ if strings .Contains (run , "npm-profile-publish" ) {
108+ arguments := npmExecutableArgumentPattern .FindAllStringSubmatch (argumentRun , - 1 )
109+ if len (arguments ) != 1 || strings .Count (argumentRun , "--npm-executable" ) != 1 ||
110+ ! slices .Contains ([]string {`"` + output + `"` , "'" + output + "'" ,
111+ `"${{ env.NPM_EXECUTABLE }}"` , `'${{ env.NPM_EXECUTABLE }}'` ,
112+ `"$NPM_EXECUTABLE"` , `"${NPM_EXECUTABLE}"` }, arguments [0 ][1 ]) {
113+ return fmt .Errorf ("npm-profile-publish --npm-executable must use exactly one pinned output argument: %s" , output )
114+ }
82115 }
83116 }
84117 }
@@ -87,3 +120,42 @@ func checkPublishToolchain(steps []workflowStep) error {
87120 }
88121 return nil
89122}
123+
124+ // Conditions are compared as whitespace-normalized strings, not evaluated.
125+ // Missing gates must also agree so npm cannot run while its assertion is skipped.
126+ func checkToolchainConditions (steps []workflowStep ) error {
127+ gate := ""
128+ seen := false
129+ for _ , step := range steps {
130+ step .Run = toolchainShellText (step .Run )
131+ if ! strings .HasPrefix (step .Uses , "actions/setup-node@" ) &&
132+ ! npmReferencePattern .MatchString (step .Run ) &&
133+ ! strings .Contains (step .Run , "npm-profile-toolchain-assert" ) &&
134+ ! strings .Contains (step .Run , "npm-profile-provision-npm" ) &&
135+ ! strings .Contains (step .Run , "npm-profile-build" ) &&
136+ ! strings .Contains (step .Run , "npm-profile-sign" ) &&
137+ ! strings .Contains (step .Run , "npm-profile-publish" ) &&
138+ ! strings .Contains (step .Run , "npm-executable" ) &&
139+ ! strings .Contains (step .Run , "NPM_EXECUTABLE" ) {
140+ continue
141+ }
142+ condition := strings .Join (strings .Fields (step .If ), " " )
143+ if normalizeExpression (condition ) == "false" || (seen && condition != gate ) {
144+ return fmt .Errorf ("toolchain step conditions must be identical and not constant false" )
145+ }
146+ gate = condition
147+ seen = true
148+ }
149+ return nil
150+ }
151+
152+ // This conservative line-based strip treats every # as a comment delimiter,
153+ // including inside quotes. It is not a shell evaluator; ambiguous syntax fails
154+ // closed. Strip before joining continuations so comments cannot supply evidence.
155+ func toolchainShellText (run string ) string {
156+ lines := strings .Split (run , "\n " )
157+ for index , line := range lines {
158+ lines [index ], _ , _ = strings .Cut (line , "#" )
159+ }
160+ return strings .ReplaceAll (strings .Join (lines , "\n " ), "\\ \n " , " " )
161+ }
0 commit comments