2021-11-15 23:18:10 +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-12-10 00:53:55 +03:00
|
|
|
"regexp"
|
2021-11-15 23:18:10 +03:00
|
|
|
"strings"
|
|
|
|
|
2021-11-22 21:32:27 +03:00
|
|
|
"github.com/rhysd/actionlint"
|
2021-11-15 23:18:10 +03:00
|
|
|
|
2022-01-12 22:49:01 +03:00
|
|
|
"github.com/ossf/scorecard/v4/checker"
|
|
|
|
"github.com/ossf/scorecard/v4/checks/fileparser"
|
|
|
|
sce "github.com/ossf/scorecard/v4/errors"
|
2021-11-15 23:18:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// CheckDangerousWorkflow is the exported name for Dangerous-Workflow check.
|
|
|
|
const CheckDangerousWorkflow = "Dangerous-Workflow"
|
|
|
|
|
2021-12-10 00:53:55 +03:00
|
|
|
func containsUntrustedContextPattern(variable string) bool {
|
|
|
|
// GitHub event context details that may be attacker controlled.
|
|
|
|
// See https://securitylab.github.com/research/github-actions-untrusted-input/
|
|
|
|
untrustedContextPattern := regexp.MustCompile(
|
|
|
|
`.*(issue\.title|` +
|
|
|
|
`issue\.body|` +
|
|
|
|
`pull_request\.title|` +
|
|
|
|
`pull_request\.body|` +
|
|
|
|
`comment\.body|` +
|
|
|
|
`review\.body|` +
|
|
|
|
`review_comment\.body|` +
|
|
|
|
`pages.*\.page_name|` +
|
|
|
|
`commits.*\.message|` +
|
|
|
|
`head_commit\.message|` +
|
|
|
|
`head_commit\.author\.email|` +
|
|
|
|
`head_commit\.author\.name|` +
|
|
|
|
`commits.*\.author\.email|` +
|
|
|
|
`commits.*\.author\.name|` +
|
|
|
|
`pull_request\.head\.ref|` +
|
|
|
|
`pull_request\.head\.label|` +
|
|
|
|
`pull_request\.head\.repo\.default_branch).*`)
|
|
|
|
|
|
|
|
if strings.Contains(variable, "github.head_ref") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.Contains(variable, "github.event.") && untrustedContextPattern.MatchString(variable)
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
//nolint:gochecknoinits
|
|
|
|
func init() {
|
2022-02-08 03:49:49 +03:00
|
|
|
supportedRequestTypes := []checker.RequestType{
|
|
|
|
checker.FileBased,
|
2022-02-08 06:03:36 +03:00
|
|
|
checker.CommitBased,
|
2022-02-08 03:49:49 +03:00
|
|
|
}
|
|
|
|
if err := registerCheck(CheckDangerousWorkflow, DangerousWorkflow, supportedRequestTypes); err != nil {
|
2022-01-13 01:14:18 +03:00
|
|
|
// this should never happen
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
type dangerousResults int
|
|
|
|
|
|
|
|
const (
|
|
|
|
scriptInjection dangerousResults = iota
|
|
|
|
untrustedCheckout
|
|
|
|
secretsViaPullRequests
|
|
|
|
)
|
|
|
|
|
|
|
|
type triggerName string
|
|
|
|
|
|
|
|
var (
|
|
|
|
triggerPullRequestTarget = triggerName("pull_request_target")
|
|
|
|
triggerPullRequest = triggerName("pull_request")
|
2022-02-15 19:04:57 +03:00
|
|
|
checkoutUntrustedRef = "github.event.pull_request"
|
2022-02-10 21:54:44 +03:00
|
|
|
)
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// Holds stateful data to pass thru callbacks.
|
|
|
|
// Each field correpsonds to a dangerous GitHub workflow pattern, and
|
|
|
|
// will hold true if the pattern is avoided, false otherwise.
|
|
|
|
type patternCbData struct {
|
2022-02-10 21:54:44 +03:00
|
|
|
workflowPattern map[dangerousResults]bool
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DangerousWorkflow runs Dangerous-Workflow check.
|
|
|
|
func DangerousWorkflow(c *checker.CheckRequest) checker.CheckResult {
|
|
|
|
// data is shared across all GitHub workflows.
|
|
|
|
data := patternCbData{
|
2022-02-10 21:54:44 +03:00
|
|
|
workflowPattern: make(map[dangerousResults]bool),
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
2022-02-23 02:40:34 +03:00
|
|
|
err := fileparser.OnMatchingFileContentDo(c.RepoClient, fileparser.PathMatcher{
|
|
|
|
Pattern: ".github/workflows/*",
|
|
|
|
CaseSensitive: false,
|
|
|
|
},
|
|
|
|
validateGitHubActionWorkflowPatterns, c.Dlogger, &data)
|
2021-11-15 23:18:10 +03:00
|
|
|
return createResultForDangerousWorkflowPatterns(data, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check file content.
|
2022-02-23 02:40:34 +03:00
|
|
|
var validateGitHubActionWorkflowPatterns fileparser.DoWhileTrueOnFileContent = func(path string,
|
|
|
|
content []byte,
|
2022-03-23 05:23:39 +03:00
|
|
|
args ...interface{},
|
|
|
|
) (bool, error) {
|
2021-11-15 23:18:10 +03:00
|
|
|
if !fileparser.IsWorkflowFile(path) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-02-23 02:40:34 +03:00
|
|
|
if len(args) != 2 {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"validateGitHubActionWorkflowPatterns requires exactly 2 arguments: %w", errInvalidArgLength)
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// Verify the type of the data.
|
2022-02-23 02:40:34 +03:00
|
|
|
pdata, ok := args[1].(*patternCbData)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"validateGitHubActionWorkflowPatterns expects arg[0] of type *patternCbData: %w", errInvalidArgType)
|
|
|
|
}
|
|
|
|
dl, ok := args[0].(checker.DetailLogger)
|
2021-11-15 23:18:10 +03:00
|
|
|
if !ok {
|
2022-02-23 02:40:34 +03:00
|
|
|
return false, fmt.Errorf(
|
|
|
|
"validateGitHubActionWorkflowPatterns expects arg[1] of type checker.DetailLogger: %w", errInvalidArgType)
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 22:57:14 +03:00
|
|
|
if !fileparser.CheckFileContainsCommands(content, "#") {
|
2021-11-15 23:18:10 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 21:32:27 +03:00
|
|
|
workflow, errs := actionlint.Parse(content)
|
|
|
|
if len(errs) > 0 && workflow == nil {
|
|
|
|
return false, fileparser.FormatActionlintError(errs)
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Check for untrusted code checkout with pull_request_target and a ref
|
|
|
|
if err := validateUntrustedCodeCheckout(workflow, path, dl, pdata); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-12-10 00:53:55 +03:00
|
|
|
// 2. Check for script injection in workflow inline scripts.
|
|
|
|
if err := validateScriptInjection(workflow, path, dl, pdata); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// 3. Check for secrets used in workflows triggered by pull requests.
|
|
|
|
if err := validateSecretsInPullRequests(workflow, path, dl, pdata); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// TODO: Check other dangerous patterns.
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
func validateSecretsInPullRequests(workflow *actionlint.Workflow, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-15 19:04:57 +03:00
|
|
|
triggers := make(map[triggerName]bool)
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// We need pull request trigger.
|
2022-02-15 19:04:57 +03:00
|
|
|
usesPullRequest := usesEventTrigger(workflow, triggerPullRequest)
|
|
|
|
usesPullRequestTarget := usesEventTrigger(workflow, triggerPullRequestTarget)
|
|
|
|
if !usesPullRequest && !usesPullRequestTarget {
|
2022-02-10 21:54:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
// Record the triggers.
|
|
|
|
if usesPullRequest {
|
|
|
|
triggers[triggerPullRequest] = usesPullRequest
|
|
|
|
}
|
|
|
|
if usesPullRequestTarget {
|
|
|
|
triggers[triggerPullRequestTarget] = usesPullRequestTarget
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// Secrets used in env at the top of the wokflow.
|
2022-02-15 19:04:57 +03:00
|
|
|
if err := checkWorkflowSecretInEnv(workflow, triggers, path, dl, pdata); err != nil {
|
2022-02-10 21:54:44 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Secrets used on jobs.
|
|
|
|
for _, job := range workflow.Jobs {
|
2022-02-15 19:04:57 +03:00
|
|
|
if err := checkJobForUsedSecrets(job, triggers, path, dl, pdata); err != nil {
|
2022-02-10 21:54:44 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 21:32:27 +03:00
|
|
|
func validateUntrustedCodeCheckout(workflow *actionlint.Workflow, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
if !usesEventTrigger(workflow, triggerPullRequestTarget) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, job := range workflow.Jobs {
|
|
|
|
if err := checkJobForUntrustedCodeCheckout(job, path, dl, pdata); err != nil {
|
|
|
|
return err
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 21:54:44 +03:00
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
func usesEventTrigger(workflow *actionlint.Workflow, name triggerName) bool {
|
2021-11-22 21:32:27 +03:00
|
|
|
// Check if the webhook event trigger is a pull_request_target
|
|
|
|
for _, event := range workflow.On {
|
2022-02-10 21:54:44 +03:00
|
|
|
if event.EventName() == string(name) {
|
2021-11-22 21:32:27 +03:00
|
|
|
return true
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 21:32:27 +03:00
|
|
|
|
|
|
|
return false
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
func jobUsesEnvironment(job *actionlint.Job) bool {
|
|
|
|
if job.Environment == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return job.Environment.Name != nil &&
|
|
|
|
job.Environment.Name.Value != ""
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
func checkJobForUsedSecrets(job *actionlint.Job, triggers map[triggerName]bool,
|
2022-03-23 05:23:39 +03:00
|
|
|
path string, dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
if job == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the job has an environment, assume it's an env secret gated by
|
|
|
|
// some approval and don't alert.
|
|
|
|
if jobUsesEnvironment(job) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
// For pull request target, we need a ref to the pull request.
|
|
|
|
_, usesPullRequest := triggers[triggerPullRequest]
|
|
|
|
_, usesPullRequestTarget := triggers[triggerPullRequestTarget]
|
|
|
|
chk, ref := jobUsesCodeCheckout(job)
|
|
|
|
if !((chk && usesPullRequest) ||
|
|
|
|
(chk && usesPullRequestTarget && strings.Contains(ref, checkoutUntrustedRef))) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// https://docs.github.com/en/actions/security-guides/encrypted-secrets#naming-your-secrets
|
|
|
|
for _, step := range job.Steps {
|
|
|
|
if step == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkSecretInActionArgs(step, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkSecretInRun(step, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkSecretInEnv(step.Env, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
func workflowUsesCodeCheckoutAndNoEnvironment(workflow *actionlint.Workflow,
|
2022-03-23 05:23:39 +03:00
|
|
|
triggers map[triggerName]bool,
|
|
|
|
) bool {
|
2022-02-10 21:54:44 +03:00
|
|
|
if workflow == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
_, usesPullRequest := triggers[triggerPullRequest]
|
|
|
|
_, usesPullRequestTarget := triggers[triggerPullRequestTarget]
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
for _, job := range workflow.Jobs {
|
2022-02-15 19:04:57 +03:00
|
|
|
chk, ref := jobUsesCodeCheckout(job)
|
|
|
|
if ((chk && usesPullRequest) ||
|
|
|
|
(chk && usesPullRequestTarget && strings.Contains(ref, checkoutUntrustedRef))) &&
|
2022-02-10 21:54:44 +03:00
|
|
|
!jobUsesEnvironment(job) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
func jobUsesCodeCheckout(job *actionlint.Job) (bool, string) {
|
2022-02-10 21:54:44 +03:00
|
|
|
if job == nil {
|
2022-02-15 19:04:57 +03:00
|
|
|
return false, ""
|
2022-02-10 21:54:44 +03:00
|
|
|
}
|
2022-02-15 19:04:57 +03:00
|
|
|
|
|
|
|
hasCheckout := false
|
2022-02-10 21:54:44 +03:00
|
|
|
for _, step := range job.Steps {
|
|
|
|
if step == nil || step.Exec == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check for a step that uses actions/checkout
|
|
|
|
e, ok := step.Exec.(*actionlint.ExecAction)
|
|
|
|
if !ok || e.Uses == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(e.Uses.Value, "actions/checkout") {
|
2022-02-15 19:04:57 +03:00
|
|
|
hasCheckout = true
|
|
|
|
ref, ok := e.Inputs["ref"]
|
|
|
|
if !ok || ref.Value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true, ref.Value.Value
|
2022-02-10 21:54:44 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-15 19:04:57 +03:00
|
|
|
return hasCheckout, ""
|
2022-02-10 21:54:44 +03:00
|
|
|
}
|
|
|
|
|
2021-11-22 21:32:27 +03:00
|
|
|
func checkJobForUntrustedCodeCheckout(job *actionlint.Job, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2021-11-22 21:32:27 +03:00
|
|
|
if job == nil {
|
2021-11-15 23:18:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-22 21:32:27 +03:00
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// Check each step, which is a map, for checkouts with untrusted ref
|
2021-11-22 21:32:27 +03:00
|
|
|
for _, step := range job.Steps {
|
|
|
|
if step == nil || step.Exec == nil {
|
2021-11-15 23:18:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-11-22 21:32:27 +03:00
|
|
|
// Check for a step that uses actions/checkout
|
|
|
|
e, ok := step.Exec.(*actionlint.ExecAction)
|
|
|
|
if !ok || e.Uses == nil {
|
2022-02-10 21:54:44 +03:00
|
|
|
continue
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
2021-11-22 21:32:27 +03:00
|
|
|
if !strings.Contains(e.Uses.Value, "actions/checkout") {
|
2021-11-15 23:18:10 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check for reference. If not defined for a pull_request_target event, this defaults to
|
|
|
|
// the base branch of the pull request.
|
2021-11-22 21:32:27 +03:00
|
|
|
ref, ok := e.Inputs["ref"]
|
|
|
|
if !ok || ref.Value == nil {
|
2021-11-15 23:18:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-02-15 19:04:57 +03:00
|
|
|
if strings.Contains(ref.Value.Value, checkoutUntrustedRef) {
|
2022-01-06 03:13:53 +03:00
|
|
|
line := fileparser.GetLineNumber(step.Pos)
|
2022-02-12 02:48:58 +03:00
|
|
|
dl.Warn(&checker.LogMessage{
|
2022-02-15 21:26:06 +03:00
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: line,
|
|
|
|
Text: fmt.Sprintf("untrusted code checkout '%v'", ref.Value.Value),
|
2021-11-15 23:18:10 +03:00
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
|
|
|
// Detected untrusted checkout.
|
2022-02-10 21:54:44 +03:00
|
|
|
pdata.workflowPattern[untrustedCheckout] = true
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 00:53:55 +03:00
|
|
|
func validateScriptInjection(workflow *actionlint.Workflow, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2021-12-10 00:53:55 +03:00
|
|
|
for _, job := range workflow.Jobs {
|
|
|
|
if job == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, step := range job.Steps {
|
|
|
|
if step == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
run, ok := step.Exec.(*actionlint.ExecRun)
|
|
|
|
if !ok || run.Run == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check Run *String for user-controllable (untrustworthy) properties.
|
|
|
|
if err := checkVariablesInScript(run.Run.Value, run.Run.Pos, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 21:54:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-10 00:53:55 +03:00
|
|
|
|
2022-02-15 19:04:57 +03:00
|
|
|
func checkWorkflowSecretInEnv(workflow *actionlint.Workflow, triggers map[triggerName]bool,
|
2022-03-23 05:23:39 +03:00
|
|
|
path string, dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
// We need code checkout and not environment rule protection.
|
2022-02-15 19:04:57 +03:00
|
|
|
if !workflowUsesCodeCheckoutAndNoEnvironment(workflow, triggers) {
|
2022-02-10 21:54:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkSecretInEnv(workflow.Env, path, dl, pdata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSecretInEnv(env *actionlint.Env, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
if env == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range env.Vars {
|
|
|
|
if err := checkSecretInScript(v.Value.Value, v.Value.Pos, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSecretInRun(step *actionlint.Step, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
if step == nil || step.Exec == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
run, ok := step.Exec.(*actionlint.ExecRun)
|
|
|
|
if ok && run.Run != nil {
|
|
|
|
if err := checkSecretInScript(run.Run.Value, run.Run.Pos, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSecretInActionArgs(step *actionlint.Step, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
if step == nil || step.Exec == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
e, ok := step.Exec.(*actionlint.ExecAction)
|
|
|
|
if ok && e.Uses != nil {
|
|
|
|
// Check for reference. If not defined for a pull_request_target event, this defaults to
|
|
|
|
// the base branch of the pull request.
|
|
|
|
for _, v := range e.Inputs {
|
|
|
|
if v.Value != nil {
|
|
|
|
if err := checkSecretInScript(v.Value.Value, v.Value.Pos, path, dl, pdata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSecretInScript(script string, pos *actionlint.Pos, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2022-02-10 21:54:44 +03:00
|
|
|
for {
|
|
|
|
s := strings.Index(script, "${{")
|
|
|
|
if s == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
e := strings.Index(script[s:], "}}")
|
|
|
|
if e == -1 {
|
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
variable := strings.Trim(script[s:s+e+2], " ")
|
|
|
|
if strings.Contains(variable, "secrets.") {
|
|
|
|
line := fileparser.GetLineNumber(pos)
|
2022-02-12 02:48:58 +03:00
|
|
|
dl.Warn(&checker.LogMessage{
|
2022-02-15 21:26:06 +03:00
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: line,
|
|
|
|
Text: fmt.Sprintf("secret accessible to pull requests '%v'", variable),
|
2022-02-10 21:54:44 +03:00
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
|
|
|
pdata.workflowPattern[secretsViaPullRequests] = true
|
|
|
|
}
|
|
|
|
script = script[s+e:]
|
|
|
|
}
|
2021-12-10 00:53:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkVariablesInScript(script string, pos *actionlint.Pos, path string,
|
2022-03-23 05:23:39 +03:00
|
|
|
dl checker.DetailLogger, pdata *patternCbData,
|
|
|
|
) error {
|
2021-12-10 00:53:55 +03:00
|
|
|
for {
|
|
|
|
s := strings.Index(script, "${{")
|
|
|
|
if s == -1 {
|
2022-02-10 21:54:44 +03:00
|
|
|
break
|
2021-12-10 00:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
e := strings.Index(script[s:], "}}")
|
|
|
|
if e == -1 {
|
|
|
|
return sce.WithMessage(sce.ErrScorecardInternal, errInvalidGitHubWorkflow.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the variable may be untrustworthy.
|
|
|
|
variable := script[s+3 : s+e]
|
|
|
|
if containsUntrustedContextPattern(variable) {
|
2022-01-06 03:13:53 +03:00
|
|
|
line := fileparser.GetLineNumber(pos)
|
2022-02-12 02:48:58 +03:00
|
|
|
dl.Warn(&checker.LogMessage{
|
2022-02-15 21:26:06 +03:00
|
|
|
Path: path,
|
|
|
|
Type: checker.FileTypeSource,
|
|
|
|
Offset: line,
|
|
|
|
Text: fmt.Sprintf("script injection with untrusted input '%v'", variable),
|
2021-12-10 00:53:55 +03:00
|
|
|
// TODO: set Snippet.
|
|
|
|
})
|
2022-02-10 21:54:44 +03:00
|
|
|
pdata.workflowPattern[scriptInjection] = true
|
2021-12-10 00:53:55 +03:00
|
|
|
}
|
|
|
|
script = script[s+e:]
|
|
|
|
}
|
2022-02-10 21:54:44 +03:00
|
|
|
return nil
|
2021-12-10 00:53:55 +03:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// Calculate the workflow score.
|
|
|
|
func calculateWorkflowScore(result patternCbData) int {
|
|
|
|
// Start with a perfect score.
|
|
|
|
score := float32(checker.MaxResultScore)
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// Pull_request_event indicates untrusted code checkout.
|
|
|
|
if ok := result.workflowPattern[untrustedCheckout]; ok {
|
|
|
|
score -= 10
|
|
|
|
}
|
|
|
|
|
|
|
|
// Script injection with an untrusted context.
|
|
|
|
if ok := result.workflowPattern[scriptInjection]; ok {
|
2021-11-15 23:18:10 +03:00
|
|
|
score -= 10
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:54:44 +03:00
|
|
|
// Secrets available by pull requests.
|
|
|
|
if ok := result.workflowPattern[secretsViaPullRequests]; ok {
|
2021-12-10 00:53:55 +03:00
|
|
|
score -= 10
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:18:10 +03:00
|
|
|
// We're done, calculate the final score.
|
|
|
|
if score < checker.MinResultScore {
|
|
|
|
return checker.MinResultScore
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(score)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the result.
|
|
|
|
func createResultForDangerousWorkflowPatterns(result patternCbData, err error) checker.CheckResult {
|
|
|
|
if err != nil {
|
|
|
|
return checker.CreateRuntimeErrorResult(CheckDangerousWorkflow, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
score := calculateWorkflowScore(result)
|
|
|
|
|
|
|
|
if score != checker.MaxResultScore {
|
|
|
|
return checker.CreateResultWithScore(CheckDangerousWorkflow,
|
|
|
|
"dangerous workflow patterns detected", score)
|
|
|
|
}
|
|
|
|
|
|
|
|
return checker.CreateMaxScoreResult(CheckDangerousWorkflow,
|
|
|
|
"no dangerous workflow patterns detected")
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:02 +03:00
|
|
|
func testValidateGitHubActionDangerousWorkflow(pathfn string,
|
2022-03-23 05:23:39 +03:00
|
|
|
content []byte, dl checker.DetailLogger,
|
|
|
|
) checker.CheckResult {
|
2021-11-15 23:18:10 +03:00
|
|
|
data := patternCbData{
|
2022-02-10 21:54:44 +03:00
|
|
|
workflowPattern: make(map[dangerousResults]bool),
|
2021-11-15 23:18:10 +03:00
|
|
|
}
|
|
|
|
_, err := validateGitHubActionWorkflowPatterns(pathfn, content, dl, &data)
|
|
|
|
return createResultForDangerousWorkflowPatterns(data, err)
|
|
|
|
}
|