diff --git a/signing/signing.go b/signing/signing.go index 28f70eb2..58606528 100644 --- a/signing/signing.go +++ b/signing/signing.go @@ -222,7 +222,7 @@ func lookupPayload(ctx context.Context, payload []byte) (repoPath, repoRef, repo func verifyScorecardWorkflow(workflowContent string) error { // Verify workflow contents using actionlint. workflow, lintErrs := actionlint.Parse([]byte(workflowContent)) - if lintErrs != nil { + if lintErrs != nil || workflow == nil { return fmt.Errorf("actionlint errors parsing workflow: %v", lintErrs) } @@ -231,17 +231,18 @@ func verifyScorecardWorkflow(workflowContent string) error { return errors.New("workflow contains global env vars or defaults") } - // Verify that the all scope, if set, isn't write-all. - globalPermAll := workflow.Permissions.All - if globalPermAll != nil && globalPermAll.Value == "write-all" { - return fmt.Errorf("global perm is set to write-all") - } + if workflow.Permissions != nil { + globalPerms := workflow.Permissions + // Verify that the all scope, if set, isn't write-all. + if globalPerms.All != nil && globalPerms.All.Value == "write-all" { + return fmt.Errorf("global perm is set to write-all") + } - // Verify that there are no global permissions (including id-token) set to write. - globalPerms := workflow.Permissions.Scopes - for globalPerm, val := range globalPerms { - if val.Value.Value == "write" { - return fmt.Errorf("global perm %v is set to write", globalPerm) + // Verify that there are no global permissions (including id-token) set to write. + for globalPerm, val := range globalPerms.Scopes { + if val.Value.Value == "write" { + return fmt.Errorf("global perm %v is set to write", globalPerm) + } } } diff --git a/signing/signing_test.go b/signing/signing_test.go index 36d9d6b2..5ca51c46 100644 --- a/signing/signing_test.go +++ b/signing/signing_test.go @@ -46,10 +46,17 @@ func TestVerifySignatureInvalidRepo(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, w.Code) } -func TestVerifyValidWorkflow(t *testing.T) { - workflowContent, _ := ioutil.ReadFile("../testdata/workflow-valid.yml") - err := verifyScorecardWorkflow(string(workflowContent)) - assert.Equal(t, err, nil) +func TestVerifyValidWorkflows(t *testing.T) { + workflowFiles := []string{ + "../testdata/workflow-valid.yml", + "../testdata/workflow-valid-noglobalperm.yml", + } + + for _, workflowFile := range workflowFiles { + workflowContent, _ := ioutil.ReadFile(workflowFile) + err := verifyScorecardWorkflow(string(workflowContent)) + assert.Equal(t, err, nil, workflowFile) + } } func TestVerifyInvalidWorkflows(t *testing.T) { @@ -67,6 +74,7 @@ func TestVerifyInvalidWorkflows(t *testing.T) { "../testdata/workflow-invalid-global-defaults.yml", "../testdata/workflow-invalid-otherjob.yml", "../testdata/workflow-invalid-global-idtoken.yml", + "../testdata/workflow-invalid-empty.yml", } for _, workflowFile := range workflowFiles { diff --git a/testdata/workflow-invalid-empty.yml b/testdata/workflow-invalid-empty.yml new file mode 100644 index 00000000..e69de29b diff --git a/testdata/workflow-valid-noglobalperm.yml b/testdata/workflow-valid-noglobalperm.yml new file mode 100644 index 00000000..a2b012bc --- /dev/null +++ b/testdata/workflow-valid-noglobalperm.yml @@ -0,0 +1,56 @@ +name: Scorecards supply-chain security +on: + # Only the default branch is supported. + branch_protection_rule: + schedule: + # Weekly on Saturdays. + - cron: '30 1 * * 6' + push: + branches: [ $default-branch ] + workflow_dispatch: + +jobs: + analysis: + name: Scorecards analysis + runs-on: ubuntu-latest + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + actions: read + contents: read + id-token: write # needed for keyless signing + + steps: + - name: "Checkout code" + uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2.4.0 + with: + persist-credentials: false + + - name: "Run analysis" + uses: ossf/scorecard-action@c8416b0b2bf627c349ca92fc8e3de51a64b005cf # v1.0.2 + with: + results_file: results.sarif + results_format: sarif + # Read-only PAT token. To create it, + # follow the steps in https://github.com/ossf/scorecard-action#pat-token-creation. + repo_token: ${{ secrets.SCORECARD_READ_TOKEN }} + # Publish the results for public repositories to enable scorecard badges. For more details, see + # https://github.com/ossf/scorecard-action#publishing-results. + # For private repositories, `publish_results` will automatically be set to `false`, regardless + # of the value entered here. + publish_results: true + + # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF + # format to the repository Actions tab. + - name: "Upload artifact" + uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2 # v2.3.1 + with: + name: SARIF file + path: results.sarif + retention-days: 5 + + # Upload the results to GitHub's code scanning dashboard. + - name: "Upload to code-scanning" + uses: github/codeql-action/upload-sarif@5f532563584d71fdef14ee64d17bafb34f751ce5 # v1.0.26 + with: + sarif_file: results.sarif \ No newline at end of file