2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2021 OpenSSF Scorecard Authors
|
2021-06-04 02:30:37 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-11-12 22:34:46 +03:00
|
|
|
"os"
|
2022-02-05 00:42:59 +03:00
|
|
|
"strings"
|
2021-06-04 02:30:37 +03:00
|
|
|
"testing"
|
2021-07-21 19:21:43 +03:00
|
|
|
|
2022-07-16 00:48:50 +03:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
2022-01-12 22:49:01 +03:00
|
|
|
"github.com/ossf/scorecard/v4/checker"
|
2022-07-16 00:48:50 +03:00
|
|
|
"github.com/ossf/scorecard/v4/clients"
|
|
|
|
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
|
2022-01-12 22:49:01 +03:00
|
|
|
scut "github.com/ossf/scorecard/v4/utests"
|
2021-06-04 02:30:37 +03:00
|
|
|
)
|
|
|
|
|
2023-01-31 05:41:36 +03:00
|
|
|
// nolint
|
2021-06-04 02:30:37 +03:00
|
|
|
func TestGithubTokenPermissions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-12-17 02:16:02 +03:00
|
|
|
name string
|
|
|
|
filenames []string
|
|
|
|
expected scut.TestReturn
|
2021-06-04 02:30:37 +03:00
|
|
|
}{
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run workflow codeql write test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-codeql-write.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-08-03 03:56:45 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-05-12 04:41:37 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run workflow no codeql write test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-no-codeql-write.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-08-03 03:56:45 +03:00
|
|
|
Score: checker.MaxResultScore - 1,
|
|
|
|
NumberOfWarn: 1,
|
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run workflow write test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-writes-2.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2022-10-19 11:02:17 +03:00
|
|
|
Score: checker.MaxResultScore,
|
2021-08-03 03:56:45 +03:00
|
|
|
NumberOfWarn: 3,
|
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run package workflow write test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-package-workflow-write.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-08-03 03:56:45 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run package write test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-package-write.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2022-10-19 11:02:17 +03:00
|
|
|
Score: checker.MaxResultScore,
|
2021-08-03 03:56:45 +03:00
|
|
|
NumberOfWarn: 1,
|
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "run writes test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-writes.yaml"},
|
2021-08-03 03:56:45 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-08-03 03:56:45 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-08-03 03:56:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "write all test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-writeall.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "read all test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-readall.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "no permission test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-absent.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "writes test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-writes.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 6,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "reads test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-reads.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 11,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "nones test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-nones.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 11,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "none test",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-none.yaml"},
|
2021-07-21 19:21:43 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-21 19:21:43 +03:00
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-06-04 02:30:37 +03:00
|
|
|
},
|
|
|
|
},
|
2021-07-30 03:13:01 +03:00
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "status/checks write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-status-checks.yaml"},
|
2021-07-30 03:13:01 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MaxResultScore - 1,
|
|
|
|
NumberOfWarn: 2,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 3,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 6,
|
2021-07-30 03:13:01 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "sec-events/deployments write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-secevent-deployments.yaml"},
|
2021-07-30 03:13:01 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MaxResultScore - 2,
|
|
|
|
NumberOfWarn: 2,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 4,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-07-30 03:13:01 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "contents write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-contents.yaml"},
|
2021-07-30 03:13:01 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 3,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-07-30 03:13:01 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "actions write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-actions.yaml"},
|
2021-07-30 03:13:01 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 3,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-07-30 03:13:01 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "packages write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-packages.yaml"},
|
2021-07-30 03:13:01 +03:00
|
|
|
expected: scut.TestReturn{
|
2021-08-31 00:12:57 +03:00
|
|
|
Error: nil,
|
2021-07-30 03:13:01 +03:00
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-07-30 03:13:01 +03:00
|
|
|
},
|
|
|
|
},
|
2021-11-04 06:29:06 +03:00
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "Non-yaml file",
|
|
|
|
filenames: []string{"./testdata/script.sh"},
|
2021-11-04 06:29:06 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2021-11-04 06:29:06 +03:00
|
|
|
NumberOfDebug: 0,
|
|
|
|
},
|
|
|
|
},
|
2021-11-16 06:03:54 +03:00
|
|
|
{
|
2022-02-23 03:23:07 +03:00
|
|
|
name: "package workflow contents write",
|
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-contents-writes-no-release.yaml"},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
2022-10-17 21:19:04 +03:00
|
|
|
Score: checker.MaxResultScore,
|
2022-02-23 03:23:07 +03:00
|
|
|
NumberOfWarn: 1,
|
2022-07-16 00:48:50 +03:00
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2022-02-23 03:23:07 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "release workflow contents write",
|
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-contents-writes-release.yaml"},
|
2021-11-16 06:03:54 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
2022-08-27 01:38:55 +03:00
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-27 01:38:55 +03:00
|
|
|
NumberOfDebug: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "release workflow contents write",
|
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-contents-writes-release-mvn-release.yaml"},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
2021-11-16 06:03:54 +03:00
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-11-16 06:03:54 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "package workflow write",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-packages-writes.yaml"},
|
2021-11-16 06:03:54 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-11-16 06:03:54 +03:00
|
|
|
},
|
|
|
|
},
|
2021-12-08 04:18:28 +03:00
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "workflow jobs only",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-jobs-only.yaml"},
|
2021-12-08 04:18:28 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: 9,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 4,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-12-08 04:18:28 +03:00
|
|
|
},
|
|
|
|
},
|
2021-12-14 07:14:35 +03:00
|
|
|
{
|
2021-12-17 02:16:02 +03:00
|
|
|
name: "security-events write, codeql comment",
|
2022-02-05 00:42:59 +03:00
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-run-write-codeql-comment.yaml"},
|
2021-12-14 07:14:35 +03:00
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore - 1,
|
|
|
|
NumberOfWarn: 1,
|
|
|
|
NumberOfInfo: 1,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 4,
|
2021-12-14 07:14:35 +03:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 03:12:22 +03:00
|
|
|
{
|
|
|
|
name: "security-events write, known actions",
|
|
|
|
filenames: []string{"./testdata/.github/workflows/github-workflow-permissions-secevent-known-actions.yaml"},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
|
|
|
NumberOfInfo: 2, // This is constant.
|
|
|
|
NumberOfDebug: 8, // This is 4 + (number of actions)
|
|
|
|
},
|
|
|
|
},
|
2021-12-17 02:16:02 +03:00
|
|
|
{
|
|
|
|
name: "two files mix run-level and top-level",
|
|
|
|
filenames: []string{
|
2022-02-05 00:42:59 +03:00
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-top-level-only.yaml",
|
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-run-level-only.yaml",
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore - 1,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 3,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 9,
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two files mix run-level and absent",
|
|
|
|
filenames: []string{
|
2022-02-05 00:42:59 +03:00
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-run-level-only.yaml",
|
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-absent.yaml",
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 2,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 9,
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two files mix top-level and absent",
|
|
|
|
filenames: []string{
|
2022-02-05 00:42:59 +03:00
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-top-level-only.yaml",
|
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-absent.yaml",
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MinResultScore,
|
|
|
|
NumberOfWarn: 1,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 10,
|
2022-05-12 04:41:37 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "read permission with GitHub pages write",
|
|
|
|
filenames: []string{
|
|
|
|
"./testdata/.github/workflows/github-workflow-permissions-gh-pages.yaml",
|
|
|
|
},
|
|
|
|
expected: scut.TestReturn{
|
|
|
|
Error: nil,
|
|
|
|
Score: checker.MaxResultScore,
|
|
|
|
NumberOfWarn: 0,
|
2023-01-31 05:41:36 +03:00
|
|
|
NumberOfInfo: 2,
|
2022-08-17 00:05:06 +03:00
|
|
|
NumberOfDebug: 5,
|
2021-12-17 02:16:02 +03:00
|
|
|
},
|
|
|
|
},
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt // Re-initializing variable so it is not changed while executing the closure below
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2022-07-16 00:48:50 +03:00
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockRepo := mockrepo.NewMockRepoClient(ctrl)
|
2022-08-18 00:10:49 +03:00
|
|
|
mockRepo.EXPECT().GetDefaultBranchName().Return("main", nil)
|
2022-07-16 00:48:50 +03:00
|
|
|
|
|
|
|
main := "main"
|
|
|
|
mockRepo.EXPECT().URI().Return("github.com/ossf/scorecard").AnyTimes()
|
|
|
|
mockRepo.EXPECT().GetDefaultBranch().Return(&clients.BranchRef{Name: &main}, nil).AnyTimes()
|
|
|
|
mockRepo.EXPECT().ListFiles(gomock.Any()).DoAndReturn(func(predicate func(string) (bool, error)) ([]string, error) {
|
|
|
|
files := []string{}
|
|
|
|
for _, fn := range tt.filenames {
|
|
|
|
files = append(files, strings.TrimPrefix(fn, "./testdata/"))
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
mockRepo.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(fn string) ([]byte, error) {
|
|
|
|
content, err := os.ReadFile("./testdata/" + fn)
|
2021-06-04 02:30:37 +03:00
|
|
|
if err != nil {
|
2022-07-16 00:48:50 +03:00
|
|
|
return content, fmt.Errorf("%w", err)
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2022-07-16 00:48:50 +03:00
|
|
|
return content, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
c := checker.CheckRequest{
|
|
|
|
RepoClient: mockRepo,
|
|
|
|
Dlogger: &dl,
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2021-12-17 02:16:02 +03:00
|
|
|
|
2022-07-16 00:48:50 +03:00
|
|
|
res := TokenPermissions(&c)
|
|
|
|
|
|
|
|
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &res, &dl) {
|
|
|
|
t.Errorf("test failed: log message not present: %+v\n%+v", tt.expected, dl)
|
2021-11-04 06:29:06 +03:00
|
|
|
}
|
2021-06-04 02:30:37 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 19:05:52 +03:00
|
|
|
|
|
|
|
func TestGithubTokenPermissionsLineNumber(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
expected []struct {
|
2022-01-06 03:13:53 +03:00
|
|
|
lineNumber uint
|
2021-12-06 19:05:52 +03:00
|
|
|
}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Job level write permission",
|
2022-02-05 00:42:59 +03:00
|
|
|
filename: "./testdata/.github/workflows/github-workflow-permissions-run-no-codeql-write.yaml",
|
2021-12-06 19:05:52 +03:00
|
|
|
expected: []struct {
|
2022-01-06 03:13:53 +03:00
|
|
|
lineNumber uint
|
2021-12-06 19:05:52 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
lineNumber: 22,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Workflow level write permission",
|
2022-02-05 00:42:59 +03:00
|
|
|
filename: "./testdata/.github/workflows/github-workflow-permissions-writeall.yaml",
|
2021-12-06 19:05:52 +03:00
|
|
|
expected: []struct {
|
2022-01-06 03:13:53 +03:00
|
|
|
lineNumber uint
|
2021-12-06 19:05:52 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
lineNumber: 16,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt // Re-initializing variable so it is not changed while executing the closure below
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
content, err := os.ReadFile(tt.filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot read file: %v", err)
|
|
|
|
}
|
2022-07-16 00:48:50 +03:00
|
|
|
|
2022-02-05 00:42:59 +03:00
|
|
|
p := strings.Replace(tt.filename, "./testdata/", "", 1)
|
2022-07-16 00:48:50 +03:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mockRepo := mockrepo.NewMockRepoClient(ctrl)
|
|
|
|
|
|
|
|
main := "main"
|
|
|
|
mockRepo.EXPECT().URI().Return("github.com/ossf/scorecard").AnyTimes()
|
2022-08-12 19:23:07 +03:00
|
|
|
mockRepo.EXPECT().GetDefaultBranchName().Return(main, nil).AnyTimes()
|
2022-07-16 00:48:50 +03:00
|
|
|
mockRepo.EXPECT().ListFiles(gomock.Any()).DoAndReturn(func(predicate func(string) (bool, error)) ([]string, error) {
|
|
|
|
return []string{p}, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
mockRepo.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(fn string) ([]byte, error) {
|
|
|
|
return content, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
dl := scut.TestDetailLogger{}
|
|
|
|
c := checker.CheckRequest{
|
|
|
|
RepoClient: mockRepo,
|
|
|
|
Dlogger: &dl,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = TokenPermissions(&c)
|
2022-02-05 00:42:59 +03:00
|
|
|
|
2021-12-06 19:05:52 +03:00
|
|
|
for _, expectedLog := range tt.expected {
|
|
|
|
isExpectedLog := func(logMessage checker.LogMessage, logType checker.DetailType) bool {
|
2023-01-31 05:41:36 +03:00
|
|
|
return logMessage.Finding != nil &&
|
|
|
|
logMessage.Finding.Location != nil &&
|
|
|
|
logMessage.Finding.Location.LineStart != nil &&
|
|
|
|
*logMessage.Finding.Location.LineStart == expectedLog.lineNumber &&
|
|
|
|
logMessage.Finding.Location.Value == p &&
|
2021-12-06 19:05:52 +03:00
|
|
|
logType == checker.DetailWarn
|
|
|
|
}
|
|
|
|
if !scut.ValidateLogMessage(isExpectedLog, &dl) {
|
|
|
|
t.Errorf("test failed: log message not present: %+v", tt.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|