2021-06-04 02:30:37 +03:00
|
|
|
// Copyright 2021 Security Scorecard Authors
|
|
|
|
//
|
|
|
|
// 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-06-04 03:12:56 +03:00
|
|
|
"strings"
|
2021-06-04 02:30:37 +03:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2021-10-08 02:16:01 +03:00
|
|
|
"github.com/ossf/scorecard/v3/checker"
|
2021-11-08 21:26:59 +03:00
|
|
|
"github.com/ossf/scorecard/v3/checks/fileparser"
|
2021-10-08 02:16:01 +03:00
|
|
|
sce "github.com/ossf/scorecard/v3/errors"
|
2021-06-04 02:30:37 +03:00
|
|
|
)
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// CheckTokenPermissions is the exported name for Token-Permissions check.
|
2021-07-30 03:13:01 +03:00
|
|
|
const CheckTokenPermissions = "Token-Permissions"
|
2021-06-04 02:30:37 +03:00
|
|
|
|
|
|
|
//nolint:gochecknoinits
|
|
|
|
func init() {
|
2021-07-30 03:13:01 +03:00
|
|
|
registerCheck(CheckTokenPermissions, TokenPermissions)
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-07-30 03:13:01 +03:00
|
|
|
// Holds stateful data to pass thru callbacks.
|
|
|
|
// Each field correpsonds to a GitHub permission type, and
|
|
|
|
// will hold true if declared non-write, false otherwise.
|
|
|
|
type permissionCbData struct {
|
2021-08-03 03:56:45 +03:00
|
|
|
topLevelWritePermissions map[string]bool
|
|
|
|
runLevelWritePermissions map[string]bool
|
2021-07-30 03:13:01 +03:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:31:34 +03:00
|
|
|
// TokenPermissions runs Token-Permissions check.
|
2021-07-30 03:13:01 +03:00
|
|
|
func TokenPermissions(c *checker.CheckRequest) checker.CheckResult {
|
2021-08-03 03:56:45 +03:00
|
|
|
// data is shared across all GitHub workflows.
|
|
|
|
data := permissionCbData{
|
|
|
|
topLevelWritePermissions: make(map[string]bool),
|
|
|
|
runLevelWritePermissions: make(map[string]bool),
|
|
|
|
}
|
2021-07-30 18:09:52 +03:00
|
|
|
err := CheckFilesContent(".github/workflows/*", false,
|
2021-07-30 03:13:01 +03:00
|
|
|
c, validateGitHubActionTokenPermissions, &data)
|
|
|
|
return createResultForLeastPrivilegeTokens(data, err)
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func validatePermission(key string, value interface{}, path string,
|
2021-08-03 03:56:45 +03:00
|
|
|
dl checker.DetailLogger, pPermissions map[string]bool,
|
|
|
|
ignoredPermissions map[string]bool) error {
|
2021-06-04 03:12:56 +03:00
|
|
|
val, ok := value.(string)
|
|
|
|
if !ok {
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2021-06-04 03:12:56 +03:00
|
|
|
|
|
|
|
if strings.EqualFold(val, "write") {
|
2021-08-03 03:56:45 +03:00
|
|
|
if isPermissionOfInterest(key, ignoredPermissions) {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Warn3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line.
|
|
|
|
Offset: 1,
|
|
|
|
Text: fmt.Sprintf("'%v' permission set to '%v'", key, val),
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
recordPermissionWrite(key, pPermissions)
|
2021-07-30 03:13:01 +03:00
|
|
|
} else {
|
|
|
|
// Only log for debugging, otherwise
|
|
|
|
// it may confuse users.
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line.
|
|
|
|
Offset: 1,
|
|
|
|
Text: fmt.Sprintf("'%v' permission set to '%v'", key, val),
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-07-30 03:13:01 +03:00
|
|
|
}
|
|
|
|
return nil
|
2021-06-04 03:12:56 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Info3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line correctly.
|
|
|
|
Offset: 1,
|
|
|
|
Text: fmt.Sprintf("'%v' permission set to '%v'", key, val),
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-07-30 03:13:01 +03:00
|
|
|
return nil
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateMapPermissions(values map[interface{}]interface{}, path string,
|
2021-08-03 03:56:45 +03:00
|
|
|
dl checker.DetailLogger, pPermissions map[string]bool,
|
|
|
|
ignoredPermissions map[string]bool) error {
|
2021-06-04 02:30:37 +03:00
|
|
|
// Iterate over the permission, verify keys and values are strings.
|
|
|
|
for k, v := range values {
|
2021-06-04 03:12:56 +03:00
|
|
|
key, ok := k.(string)
|
|
|
|
if !ok {
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2021-06-04 03:12:56 +03:00
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
if err := validatePermission(key, v, path, dl, pPermissions, ignoredPermissions); err != nil {
|
2021-07-30 03:13:01 +03:00
|
|
|
return err
|
2021-06-04 03:12:56 +03:00
|
|
|
}
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2021-07-30 03:13:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
func recordPermissionWrite(name string, pPermissions map[string]bool) {
|
|
|
|
pPermissions[name] = true
|
2021-07-30 03:13:01 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
func recordAllPermissionsWrite(pPermissions map[string]bool) {
|
2021-07-30 03:13:01 +03:00
|
|
|
// Special case: `all` does not correspond
|
|
|
|
// to a GitHub permission.
|
2021-08-03 03:56:45 +03:00
|
|
|
pPermissions["all"] = true
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
func validatePermissions(permissions interface{}, path string,
|
|
|
|
dl checker.DetailLogger, pPermissions map[string]bool,
|
|
|
|
ignoredPermissions map[string]bool) error {
|
2021-06-07 21:01:18 +03:00
|
|
|
// Check the type of our values.
|
|
|
|
switch val := permissions.(type) {
|
|
|
|
// Empty string is nil type.
|
|
|
|
// It defaults to 'none'
|
|
|
|
case nil:
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Info3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line correctly.
|
|
|
|
Offset: 1,
|
|
|
|
Text: "permissions set to 'none'",
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-06-07 21:01:18 +03:00
|
|
|
// String type.
|
|
|
|
case string:
|
|
|
|
if !strings.EqualFold(val, "read-all") && val != "" {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Warn3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line correctly.
|
|
|
|
Offset: 1,
|
|
|
|
Text: fmt.Sprintf("permissions set to '%v'", val),
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
recordAllPermissionsWrite(pPermissions)
|
2021-07-30 03:13:01 +03:00
|
|
|
return nil
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
2021-08-24 03:54:22 +03:00
|
|
|
|
|
|
|
dl.Info3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line correctly.
|
|
|
|
Offset: 1,
|
|
|
|
Text: fmt.Sprintf("permissions set to '%v'", val),
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-06-04 02:30:37 +03:00
|
|
|
|
2021-06-07 21:01:18 +03:00
|
|
|
// Map type.
|
|
|
|
case map[interface{}]interface{}:
|
2021-08-03 03:56:45 +03:00
|
|
|
if err := validateMapPermissions(val, path, dl, pPermissions, ignoredPermissions); err != nil {
|
2021-07-30 03:13:01 +03:00
|
|
|
return err
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-07 21:01:18 +03:00
|
|
|
// Invalid type.
|
|
|
|
default:
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
2021-08-03 03:56:45 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTopLevelPermissions(config map[interface{}]interface{}, path string,
|
|
|
|
dl checker.DetailLogger, pdata *permissionCbData) error {
|
|
|
|
// Check if permissions are set explicitly.
|
|
|
|
permissions, ok := config["permissions"]
|
|
|
|
if !ok {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Warn3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: 1,
|
|
|
|
Text: "no permission defined",
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
recordAllPermissionsWrite(pdata.topLevelWritePermissions)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return validatePermissions(permissions, path, dl,
|
|
|
|
pdata.topLevelWritePermissions, map[string]bool{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateRunLevelPermissions(config map[interface{}]interface{}, path string,
|
|
|
|
dl checker.DetailLogger, pdata *permissionCbData,
|
|
|
|
ignoredPermissions map[string]bool) error {
|
|
|
|
var jobs interface{}
|
|
|
|
|
|
|
|
jobs, ok := config["jobs"]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mjobs, ok := jobs.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
for _, value := range mjobs {
|
|
|
|
job, ok := value.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
2021-09-10 18:50:33 +03:00
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
2021-08-03 03:56:45 +03:00
|
|
|
}
|
|
|
|
// Run-level permissions may be left undefined.
|
|
|
|
// For most workflows, no write permissions are needed,
|
|
|
|
// so only top-level read-only permissions need to be declared.
|
|
|
|
permissions, ok := job["permissions"]
|
|
|
|
if !ok {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: 1,
|
|
|
|
Text: "no permission defined",
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := validatePermissions(permissions, path, dl,
|
|
|
|
pdata.runLevelWritePermissions, ignoredPermissions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 03:13:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
func isPermissionOfInterest(name string, ignoredPermissions map[string]bool) bool {
|
|
|
|
permissions := []string{
|
|
|
|
"statuses", "checks", "security-events",
|
|
|
|
"deployments", "contents", "packages", "actions",
|
|
|
|
}
|
|
|
|
for _, p := range permissions {
|
|
|
|
_, present := ignoredPermissions[p]
|
|
|
|
if strings.EqualFold(name, p) && !present {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func permissionIsPresent(result permissionCbData, name string) bool {
|
|
|
|
_, ok1 := result.topLevelWritePermissions[name]
|
|
|
|
_, ok2 := result.runLevelWritePermissions[name]
|
|
|
|
return ok1 || ok2
|
2021-07-30 03:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the score.
|
|
|
|
func calculateScore(result permissionCbData) int {
|
|
|
|
// See list https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/.
|
|
|
|
// Note: there are legitimate reasons to use some of the permissions like checks, deployments, etc.
|
|
|
|
// in CI/CD systems https://docs.travis-ci.com/user/github-oauth-scopes/.
|
2021-08-03 03:56:45 +03:00
|
|
|
|
|
|
|
if permissionIsPresent(result, "all") {
|
2021-07-30 03:13:01 +03:00
|
|
|
return checker.MinResultScore
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
// Start with a perfect score.
|
2021-07-30 03:13:01 +03:00
|
|
|
score := float32(checker.MaxResultScore)
|
2021-08-03 03:56:45 +03:00
|
|
|
|
2021-07-30 03:13:01 +03:00
|
|
|
// status: https://docs.github.com/en/rest/reference/repos#statuses.
|
|
|
|
// May allow an attacker to change the result of pre-submit and get a PR merged.
|
|
|
|
// Low risk: -0.5.
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "statuses") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score -= 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks.
|
|
|
|
// May allow an attacker to edit checks to remove pre-submit and introduce a bug.
|
|
|
|
// Low risk: -0.5.
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "checks") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score -= 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
// secEvents.
|
|
|
|
// May allow attacker to read vuln reports before patch available.
|
|
|
|
// Low risk: -1
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "security-events") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score--
|
|
|
|
}
|
|
|
|
|
|
|
|
// deployments: https://docs.github.com/en/rest/reference/repos#deployments.
|
|
|
|
// May allow attacker to charge repo owner by triggering VM runs,
|
|
|
|
// and tiny chance an attacker can trigger a remote
|
|
|
|
// service with code they own if server accepts code/location var unsanitized.
|
|
|
|
// Low risk: -1
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "deployments") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score--
|
|
|
|
}
|
|
|
|
|
|
|
|
// contents.
|
|
|
|
// Allows attacker to commit unreviewed code.
|
|
|
|
// High risk: -10
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "contents") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score -= checker.MaxResultScore
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
// packages: https://docs.github.com/en/packages/learn-github-packages/about-permissions-for-github-packages.
|
2021-07-30 03:13:01 +03:00
|
|
|
// Allows attacker to publish packages.
|
|
|
|
// High risk: -10
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "packages") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score -= checker.MaxResultScore
|
|
|
|
}
|
|
|
|
|
|
|
|
// actions.
|
|
|
|
// May allow an attacker to steal GitHub secrets by adding a malicious workflow/action.
|
|
|
|
// High risk: -10
|
2021-08-03 03:56:45 +03:00
|
|
|
if permissionIsPresent(result, "actions") {
|
2021-07-30 03:13:01 +03:00
|
|
|
score -= checker.MaxResultScore
|
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
// We're done, calculate the final score.
|
2021-07-30 03:13:01 +03:00
|
|
|
if score < checker.MinResultScore {
|
|
|
|
return checker.MinResultScore
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(score)
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:21:43 +03:00
|
|
|
// Create the result.
|
2021-07-30 03:13:01 +03:00
|
|
|
func createResultForLeastPrivilegeTokens(result permissionCbData, err error) checker.CheckResult {
|
2021-07-21 19:21:43 +03:00
|
|
|
if err != nil {
|
2021-07-30 03:13:01 +03:00
|
|
|
return checker.CreateRuntimeErrorResult(CheckTokenPermissions, err)
|
2021-07-21 19:21:43 +03:00
|
|
|
}
|
2021-07-30 03:13:01 +03:00
|
|
|
|
|
|
|
score := calculateScore(result)
|
|
|
|
|
|
|
|
if score != checker.MaxResultScore {
|
|
|
|
return checker.CreateResultWithScore(CheckTokenPermissions,
|
|
|
|
"non read-only tokens detected in GitHub workflows", score)
|
2021-07-21 19:21:43 +03:00
|
|
|
}
|
|
|
|
|
2021-07-30 03:13:01 +03:00
|
|
|
return checker.CreateMaxScoreResult(CheckTokenPermissions,
|
2021-07-21 19:21:43 +03:00
|
|
|
"tokens are read-only in GitHub workflows")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testValidateGitHubActionTokenPermissions(pathfn string,
|
|
|
|
content []byte, dl checker.DetailLogger) checker.CheckResult {
|
2021-08-03 03:56:45 +03:00
|
|
|
data := permissionCbData{
|
|
|
|
topLevelWritePermissions: make(map[string]bool),
|
|
|
|
runLevelWritePermissions: make(map[string]bool),
|
|
|
|
}
|
2021-07-30 03:13:01 +03:00
|
|
|
_, err := validateGitHubActionTokenPermissions(pathfn, content, dl, &data)
|
|
|
|
return createResultForLeastPrivilegeTokens(data, err)
|
2021-07-21 19:21:43 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 02:30:37 +03:00
|
|
|
// Check file content.
|
|
|
|
func validateGitHubActionTokenPermissions(path string, content []byte,
|
2021-07-30 03:13:01 +03:00
|
|
|
dl checker.DetailLogger, data FileCbData) (bool, error) {
|
2021-11-08 21:26:59 +03:00
|
|
|
if !fileparser.IsWorkflowFile(path) {
|
2021-11-04 06:29:06 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
2021-07-30 03:13:01 +03:00
|
|
|
// Verify the type of the data.
|
|
|
|
pdata, ok := data.(*permissionCbData)
|
|
|
|
if !ok {
|
|
|
|
// This never happens.
|
|
|
|
panic("invalid type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !CheckFileContainsCommands(content, "#") {
|
|
|
|
return true, nil
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var workflow map[interface{}]interface{}
|
2021-07-30 03:13:01 +03:00
|
|
|
err := yaml.Unmarshal(content, &workflow)
|
2021-06-04 02:30:37 +03:00
|
|
|
if err != nil {
|
2021-07-30 03:13:01 +03:00
|
|
|
return false,
|
2021-09-10 18:50:33 +03:00
|
|
|
sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("yaml.Unmarshal: %v", err))
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
// 1. Top-level permission definitions.
|
2021-06-04 02:30:37 +03:00
|
|
|
//nolint
|
|
|
|
// https://docs.github.com/en/actions/reference/authentication-in-a-workflow#example-1-passing-the-github_token-as-an-input,
|
|
|
|
// https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/,
|
|
|
|
// https://docs.github.com/en/actions/reference/authentication-in-a-workflow#modifying-the-permissions-for-the-github_token.
|
2021-08-03 03:56:45 +03:00
|
|
|
if err := validateTopLevelPermissions(workflow, path, dl, pdata); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Run-level permission definitions,
|
|
|
|
// see https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idpermissions.
|
|
|
|
ignoredPermissions := createIgnoredPermissions(string(content), path, dl)
|
|
|
|
if err := validateRunLevelPermissions(workflow, path, dl, pdata, ignoredPermissions); err != nil {
|
2021-07-30 03:13:01 +03:00
|
|
|
return false, err
|
2021-06-04 02:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(laurent): 2. Identify github actions that require write and add checks.
|
|
|
|
|
|
|
|
// TODO(laurent): 3. Read a few runs and ensures they have the same permissions.
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-08-03 03:56:45 +03:00
|
|
|
|
|
|
|
func createIgnoredPermissions(s, fp string, dl checker.DetailLogger) map[string]bool {
|
|
|
|
ignoredPermissions := make(map[string]bool)
|
|
|
|
if requiresPackagesPermissions(s, fp, dl) {
|
|
|
|
ignoredPermissions["packages"] = true
|
|
|
|
}
|
2021-08-05 20:10:34 +03:00
|
|
|
if isSARIFUploadWorkflow(s, fp, dl) {
|
2021-08-03 03:56:45 +03:00
|
|
|
ignoredPermissions["security-events"] = true
|
|
|
|
}
|
2021-08-05 20:10:34 +03:00
|
|
|
|
2021-08-03 03:56:45 +03:00
|
|
|
return ignoredPermissions
|
|
|
|
}
|
|
|
|
|
2021-08-05 20:10:34 +03:00
|
|
|
// Scanning tool run externally and SARIF file uploaded.
|
|
|
|
func isSARIFUploadWorkflow(s, fp string, dl checker.DetailLogger) bool {
|
|
|
|
//nolint
|
|
|
|
// CodeQl analysis workflow automatically sends sarif file to GitHub.
|
|
|
|
// https://docs.github.com/en/code-security/secure-coding/integrating-with-code-scanning/uploading-a-sarif-file-to-github#about-sarif-file-uploads-for-code-scanning.
|
|
|
|
// `The CodeQL action uploads the SARIF file automatically when it completes analysis`.
|
|
|
|
if isCodeQlAnalysisWorkflow(s, fp, dl) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
// Third-party scanning tools use the SARIF-upload action from code-ql.
|
|
|
|
// https://docs.github.com/en/code-security/secure-coding/integrating-with-code-scanning/uploading-a-sarif-file-to-github#uploading-a-code-scanning-analysis-with-github-actions
|
|
|
|
// We only support CodeQl today.
|
|
|
|
if isSARIFUploadAction(s, fp, dl) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: some third party tools may upload directly thru their actions.
|
|
|
|
// Very unlikely.
|
|
|
|
// See https://github.com/marketplace for tools.
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// CodeQl run externally and SARIF file uploaded.
|
|
|
|
func isSARIFUploadAction(s, fp string, dl checker.DetailLogger) bool {
|
|
|
|
if strings.Contains(s, "github/codeql-action/upload-sarif@") {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: fp,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line.
|
|
|
|
Offset: 1,
|
|
|
|
Text: "codeql SARIF upload workflow detected",
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-08-05 20:10:34 +03:00
|
|
|
return true
|
|
|
|
}
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: fp,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: 1,
|
|
|
|
Text: "not a codeql upload SARIF workflow",
|
|
|
|
})
|
2021-08-05 20:10:34 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
// CodeQl run within GitHub worklow automatically bubbled up to
|
|
|
|
// security events, see
|
|
|
|
// https://docs.github.com/en/code-security/secure-coding/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning.
|
2021-08-03 03:56:45 +03:00
|
|
|
func isCodeQlAnalysisWorkflow(s, fp string, dl checker.DetailLogger) bool {
|
|
|
|
if strings.Contains(s, "github/codeql-action/analyze@") {
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: fp,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
// TODO: set line.
|
|
|
|
Offset: 1,
|
|
|
|
Text: "codeql workflow detected",
|
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
return true
|
|
|
|
}
|
2021-08-24 03:54:22 +03:00
|
|
|
dl.Debug3(&checker.LogMessage{
|
|
|
|
Path: fp,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: 1,
|
|
|
|
Text: "not a codeql workflow",
|
|
|
|
})
|
2021-08-03 03:56:45 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// A packaging workflow using GitHub's supported packages:
|
|
|
|
// https://docs.github.com/en/packages.
|
|
|
|
func requiresPackagesPermissions(s, fp string, dl checker.DetailLogger) bool {
|
|
|
|
// TODO: add support for GitHub registries.
|
|
|
|
// Example: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry.
|
|
|
|
// This feature requires parsing actions properly.
|
|
|
|
// For now, we just re-use the Packaging check to verify that the
|
|
|
|
// workflow is a packaging workflow.
|
|
|
|
return isPackagingWorkflow(s, fp, dl)
|
|
|
|
}
|