Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

untrusted_checkout_exec: consider workflow_run triggered from PRs #68

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions opa/rego/poutine/utils.rego
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ empty(xs) if {
} else if {
count(xs) == 0
}

workflow_run_parents(pkg, workflow) = parents if {
parent_names = {name |
event := workflow.events[_]
event.name == "workflow_run"
name := event.workflows[_]
}
parents := {parent |
parent := pkg.github_actions_workflows[_]
glob.match(parent_names[_], ["/"], parent.name)
}
}
34 changes: 28 additions & 6 deletions opa/rego/rules/untrusted_checkout_exec.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# METADATA
# title: Arbitrary Code Execution from Untrusted Code Changes
# description: |-
# The workflow appears to checkout untrusted code from a fork
# The workflow appears to checkout untrusted code from a fork
# and uses a command that is known to allow code execution.
# custom:
# level: error
Expand All @@ -13,6 +13,17 @@ import rego.v1

rule := poutine.rule(rego.metadata.chain())

github.events contains event if some event in {
"pull_request_target",
"issue_comment",
"workflow_call",
}

github.workflow_run.parent.events contains event if some event in {
"pull_request_target",
"pull_request",
}

build_github_actions[action] = {
"pre-commit/action": "pre-commit",
"oxsecurity/megalinter": "megalinter",
Expand Down Expand Up @@ -60,12 +71,23 @@ _steps_after_untrusted_checkout contains [pkg.purl, workflow.path, s.step] if {
pkg := input.packages[_]
workflow := pkg.github_actions_workflows[_]

utils.filter_workflow_events(workflow, {
"pull_request_target",
"issue_comment",
"workflow_call",
})
utils.filter_workflow_events(workflow, github.events)

pr_checkout := utils.find_pr_checkouts(workflow)[_]
s := utils.workflow_steps_after(pr_checkout)[_]
}

_steps_after_untrusted_checkout contains [pkg_purl, workflow.path, s.step] if {
[pkg_purl, workflow] := _workflows_runs_from_pr[_]

pr_checkout := utils.find_pr_checkouts(workflow)[_]
s := utils.workflow_steps_after(pr_checkout)[_]
}

_workflows_runs_from_pr contains [pkg.purl, workflow] if {
pkg := input.packages[_]
workflow := pkg.github_actions_workflows[_]
parent := utils.workflow_run_parents(pkg, workflow)[_]

utils.filter_workflow_events(parent, github.workflow_run.parent.events)
}
9 changes: 9 additions & 0 deletions scanner/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ func TestFindings(t *testing.T) {
Details: "Detected usage of `pre-commit`",
},
},
{
RuleId: "untrusted_checkout_exec",
Purl: purl,
Meta: opa.FindingMeta{
Path: ".github/workflows/workflow_run_valid.yml",
Line: 13,
Details: "Detected usage of `npm`",
},
},
{
RuleId: "default_permissions_on_risky_events",
Purl: purl,
Expand Down
2 changes: 2 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestGithubWorkflows(t *testing.T) {
".github/workflows/valid.yml",
".github/workflows/reusable.yml",
".github/workflows/secrets.yaml",
".github/workflows/workflow_run_valid.yml",
".github/workflows/workflow_run_reusable.yml",
})
}

Expand Down
2 changes: 1 addition & 1 deletion scanner/testdata/.github/workflows/valid.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: sample.yml
name: valid.yml
on:
push:
pull_request_target:
Expand Down
13 changes: 13 additions & 0 deletions scanner/testdata/.github/workflows/workflow_run_reusable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
on:
workflow_run:
workflows: [reusable.yml]

jobs:
pr:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- run: npm install
13 changes: 13 additions & 0 deletions scanner/testdata/.github/workflows/workflow_run_valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
on:
workflow_run:
workflows: ["v[a-z]l[i]*.yml"]

jobs:
pr:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- run: npm install
Loading