-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add GitHub workflow semgrep rules #73
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
Open
NicholasEllul
wants to merge
2
commits into
main
Choose a base branch
from
ellul/github-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/semgrep-action/rules/src/github-actions/checkout-pr-on-issue-comment.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| rules: | ||
| - id: checkout-pr-on-issue-comment | ||
| languages: | ||
| - yaml | ||
| message: >- | ||
| A workflow triggered by an `issue_comment` event is checking out a pull request. This could allow an attacker to inject malicious code by commenting on an issue in a way that causes unintended execution. Ensure proper validation is in place before checking out PRs. | ||
| severity: ERROR | ||
| patterns: | ||
| - pattern-either: | ||
| - pattern-inside: | | ||
| on: | ||
| ... | ||
| issue_comment: ... | ||
| ... | ||
| ... | ||
| - pattern-inside: | | ||
| on: [..., issue_comment, ...] | ||
| ... | ||
| - pattern-inside: | | ||
| on: issue_comment | ||
| ... | ||
| - pattern-inside: | | ||
| jobs: | ||
| ... | ||
| $JOBNAME: | ||
| ... | ||
| steps: | ||
| ... | ||
| - pattern: | | ||
| run: $CMD | ||
| - metavariable-regex: | ||
| metavariable: $CMD | ||
| regex: ".*gh pr checkout.*" | ||
| metadata: | ||
| category: security | ||
| cwe: | ||
| - "CWE-94: Improper Control of Generation of Code ('Code Injection')" | ||
| owasp: | ||
| - A08:2021 - Software and Data Integrity Failures | ||
| references: | ||
| - https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#understanding-the-risk-of-script-injections | ||
| technology: | ||
| - github-actions | ||
| - actions/checkout | ||
| cwe2022-top25: true | ||
| cwe2021-top25: true | ||
| subcategory: | ||
| - audit | ||
| likelihood: MEDIUM | ||
| impact: HIGH | ||
| confidence: MEDIUM |
33 changes: 33 additions & 0 deletions
33
packages/semgrep-action/rules/src/github-actions/curl-eval.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| rules: | ||
| - id: curl-eval | ||
| languages: | ||
| - yaml | ||
| severity: ERROR | ||
| metadata: | ||
| tags: [security] | ||
| shortDescription: Risk of code injection through curl and eval combination | ||
| confidence: LOW | ||
| help: | | ||
| ## Remediation | ||
| Avoid eval'ing data fetched from curl commands. If this operation is necessary, | ||
| verify the integrity of downloaded content by checking its SHA sum before evaluation. | ||
| See GitHub's security guidance for more details on script injection risks. | ||
| message: >- | ||
| Data is being eval'd from a `curl` command. An attacker with control of the server in the `curl` | ||
| command could inject malicious code into the `eval`, resulting in a system compromise. Avoid eval'ing | ||
| untrusted data if you can. If you must do this, consider checking the SHA sum of the content returned | ||
| by the server to verify its integrity. | ||
| patterns: | ||
| - pattern-inside: 'steps: [...]' | ||
| - pattern-inside: | | ||
| - run: ... | ||
| ... | ||
| - pattern: 'run: $SHELL' | ||
| - metavariable-pattern: | ||
| language: bash | ||
| metavariable: $SHELL | ||
| patterns: | ||
| - pattern: | | ||
| $DATA=<... curl ...> | ||
| ... | ||
| eval <... $DATA ...> |
77 changes: 77 additions & 0 deletions
77
packages/semgrep-action/rules/src/github-actions/github-script-injection.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| rules: | ||
| - id: github-script-injection | ||
| languages: | ||
| - yaml | ||
| severity: ERROR | ||
| metadata: | ||
| tags: [security] | ||
| shortDescription: Risk of code injection when using `github` context data in `actions/github-script`. | ||
| confidence: HIGH | ||
| help: | | ||
| ## Remediation | ||
| Instead of using variable interpolation with `github` context data directly in the script, | ||
| use an intermediate environment variable: | ||
|
|
||
| 1. Store the data using `env:` | ||
| 2. Reference the environment variable in the script using double-quotes: "$ENVVAR" | ||
| category: security | ||
| cwe: | ||
| - "CWE-94: Improper Control of Generation of Code ('Code Injection')" | ||
| owasp: | ||
| - A03:2021 - Injection | ||
| references: | ||
| - https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#understanding-the-risk-of-script-injections | ||
| - https://securitylab.github.com/research/github-actions-untrusted-input/ | ||
| - https://github.com/actions/github-script | ||
| technology: | ||
| - github-actions | ||
| cwe2022-top25: true | ||
| subcategory: | ||
| - vuln | ||
| likelihood: HIGH | ||
| impact: HIGH | ||
| message: >- | ||
| Using variable interpolation `${{...}}` with `github` context data in a `actions/github-script`'s | ||
| `script:` step could allow an attacker to inject their own code into the runner. This would allow them to steal secrets and code. `github` context | ||
| data can have arbitrary user input and should be treated as untrusted. Instead, use an intermediate environment | ||
| variable with `env:` to store the data and use the environment variable in the `run:` script. Be sure to use double-quotes | ||
| the environment variable, like this: "$ENVVAR". | ||
| patterns: | ||
| - pattern-inside: 'steps: [...]' | ||
| - pattern-inside: | | ||
| uses: $ACTION | ||
| ... | ||
| - pattern-inside: | | ||
| with: | ||
| ... | ||
| script: ... | ||
| ... | ||
| - pattern: 'script: $SHELL' | ||
| - metavariable-regex: | ||
| metavariable: $ACTION | ||
| regex: actions/github-script@.* | ||
| - metavariable-pattern: | ||
| language: generic | ||
| metavariable: $SHELL | ||
| patterns: | ||
| - pattern-either: | ||
| - pattern: ${{ github.event.issue.title }} | ||
| - pattern: ${{ github.event.issue.body }} | ||
| - pattern: ${{ github.event.pull_request.title }} | ||
| - pattern: ${{ github.event.pull_request.body }} | ||
| - pattern: ${{ github.event.comment.body }} | ||
| - pattern: ${{ github.event.review.body }} | ||
| - pattern: ${{ github.event.review_comment.body }} | ||
| - pattern: ${{ github.event.pages. ... .page_name}} | ||
| - pattern: ${{ github.event.head_commit.message }} | ||
| - pattern: ${{ github.event.head_commit.author.email }} | ||
| - pattern: ${{ github.event.head_commit.author.name }} | ||
| - pattern: ${{ github.event.commits ... .author.email }} | ||
| - pattern: ${{ github.event.commits ... .author.name }} | ||
| - pattern: ${{ github.event.pull_request.head.ref }} | ||
| - pattern: ${{ github.event.pull_request.head.label }} | ||
| - pattern: ${{ github.event.pull_request.head.repo.default_branch }} | ||
| - pattern: ${{ github.head_ref }} | ||
| - pattern: ${{ github.event.inputs ... }} | ||
| - pattern: ${{ github.event.discussion.title }} | ||
| - pattern: ${{ github.event.discussion.body }} |
77 changes: 77 additions & 0 deletions
77
packages/semgrep-action/rules/src/github-actions/pull-request-target-code-checkout.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| rules: | ||
| - id: pull-request-target-code-checkout | ||
| languages: | ||
| - yaml | ||
| message: >- | ||
| This GitHub Actions workflow file uses `pull_request_target` and checks out code | ||
| from the incoming pull request. When using `pull_request_target`, the Action | ||
| runs in the context of the target repository, which includes access to all repository | ||
| secrets. Normally, this is safe because the Action only runs code from the target | ||
| repository, not the incoming PR. However, by checking out the incoming PR code, you're now using | ||
| the incoming code for the rest of the action. You may be inadvertently executing arbitrary code | ||
| from the incoming PR with access to repository secrets, which would let an attacker steal repository | ||
| secrets. | ||
| This normally happens by running build scripts (e.g., `npm build` and `make`) or dependency installation | ||
| scripts (e.g., `python setup.py install`). | ||
| Audit your workflow file to make sure no code from the incoming PR is executed. | ||
| Please see https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for additional | ||
| mitigations. | ||
| metadata: | ||
| tags: [security] | ||
| shortDescription: Unsafe code checkout in pull_request_target workflow | ||
| confidence: LOW | ||
| help: | | ||
| ## Remediation | ||
| When using `pull_request_target`, avoid checking out code from the incoming PR. If you must check out PR code, | ||
| ensure no untrusted code is executed (including build scripts and dependency installation). | ||
| See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for additional mitigations. | ||
| category: security | ||
| owasp: | ||
| - A01:2021 - Broken Access Control | ||
| cwe: | ||
| - 'CWE-913: Improper Control of Dynamically-Managed Code Resources' | ||
| references: | ||
| - https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
| - https://github.com/justinsteven/advisories/blob/master/2021_github_actions_checkspelling_token_leak_via_advice_symlink.md | ||
| technology: | ||
| - github-actions | ||
| subcategory: | ||
| - audit | ||
| likelihood: LOW | ||
| impact: MEDIUM | ||
| patterns: | ||
| - pattern-either: | ||
| - pattern-inside: | | ||
| on: | ||
| ... | ||
| pull_request_target: ... | ||
| ... | ||
| ... | ||
| - pattern-inside: | | ||
| on: [..., pull_request_target, ...] | ||
| ... | ||
| - pattern-inside: | | ||
| on: pull_request_target | ||
| ... | ||
| - pattern-inside: | | ||
| jobs: | ||
| ... | ||
| $JOBNAME: | ||
| ... | ||
| steps: | ||
| ... | ||
| - pattern: | | ||
| ... | ||
| uses: "$ACTION" | ||
| with: | ||
| ... | ||
| ref: $EXPR | ||
| - metavariable-regex: | ||
| metavariable: $ACTION | ||
| regex: actions/checkout@.* | ||
| - metavariable-pattern: | ||
| language: generic | ||
| metavariable: $EXPR | ||
| patterns: | ||
| - pattern: ${{ github.event.pull_request ... }} | ||
| severity: WARNING |
71 changes: 71 additions & 0 deletions
71
packages/semgrep-action/rules/src/github-actions/run-shell-injection.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| rules: | ||
| - id: run-shell-injection | ||
| languages: | ||
| - yaml | ||
| message: 'Using variable interpolation `${{...}}` with `github` context data in a `run:` step could | ||
| allow an attacker to inject their own code into the runner. This would allow them to steal secrets | ||
| and code. `github` context data can have arbitrary user input and should be treated as untrusted. | ||
| Instead, use an intermediate environment variable with `env:` to store the data and use the environment | ||
| variable in the `run:` script. Be sure to use double-quotes the environment variable, like this: "$ENVVAR".' | ||
| metadata: | ||
| category: security | ||
| cwe: | ||
| - "CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')" | ||
| owasp: | ||
| - A01:2017 - Injection | ||
| - A03:2021 - Injection | ||
| references: | ||
| - https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#understanding-the-risk-of-script-injections | ||
| - https://securitylab.github.com/research/github-actions-untrusted-input/ | ||
| technology: | ||
| - github-actions | ||
| cwe2022-top25: true | ||
| cwe2021-top25: true | ||
| subcategory: | ||
| - vuln | ||
| likelihood: HIGH | ||
| impact: HIGH | ||
| confidence: HIGH | ||
| tags: [security] | ||
| shortDescription: Shell injection risk in GitHub Actions run steps | ||
| help: | | ||
| ## Remediation | ||
| Instead of using `github` context data directly in `run:` steps, use an intermediate environment variable: | ||
| ```yaml | ||
| steps: | ||
| - run: echo "$MY_VAR" | ||
| env: | ||
| MY_VAR: ${{ github.event.issue.title }} | ||
| ``` | ||
| patterns: | ||
| - pattern-inside: 'steps: [...]' | ||
| - pattern-inside: | | ||
| - run: ... | ||
| ... | ||
| - pattern: 'run: $SHELL' | ||
| - metavariable-pattern: | ||
| language: generic | ||
| metavariable: $SHELL | ||
| patterns: | ||
| - pattern-either: | ||
| - pattern: ${{ github.event.issue.title }} | ||
| - pattern: ${{ github.event.issue.body }} | ||
| - pattern: ${{ github.event.pull_request.title }} | ||
| - pattern: ${{ github.event.pull_request.body }} | ||
| - pattern: ${{ github.event.comment.body }} | ||
| - pattern: ${{ github.event.review.body }} | ||
| - pattern: ${{ github.event.review_comment.body }} | ||
| - pattern: ${{ github.event.pages. ... .page_name}} | ||
| - pattern: ${{ github.event.head_commit.message }} | ||
| - pattern: ${{ github.event.head_commit.author.email }} | ||
| - pattern: ${{ github.event.head_commit.author.name }} | ||
| - pattern: ${{ github.event.commits ... .author.email }} | ||
| - pattern: ${{ github.event.commits ... .author.name }} | ||
| - pattern: ${{ github.event.pull_request.head.ref }} | ||
| - pattern: ${{ github.event.pull_request.head.label }} | ||
| - pattern: ${{ github.event.pull_request.head.repo.default_branch }} | ||
| - pattern: ${{ github.head_ref }} | ||
| - pattern: ${{ github.event.inputs ... }} | ||
| - pattern: ${{ github.event.discussion.title }} | ||
| - pattern: ${{ github.event.discussion.body }} | ||
| severity: ERROR |
68 changes: 68 additions & 0 deletions
68
packages/semgrep-action/rules/src/github-actions/workflow-run-target-code-checkout.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| rules: | ||
| - id: workflow-run-target-code-checkout | ||
| languages: | ||
| - yaml | ||
| message: >- | ||
| This GitHub Actions workflow file uses `workflow_run` and checks out code | ||
| from the incoming pull request. When using `workflow_run`, the Action | ||
| runs in the context of the target repository, which includes access to all repository | ||
| secrets. Normally, this is safe because the Action only runs code from the target | ||
| repository, not the incoming PR. However, by checking out the incoming PR code, you're now using | ||
| the incoming code for the rest of the action. You may be inadvertently executing arbitrary code | ||
| from the incoming PR with access to repository secrets, which would let an attacker steal repository secrets. | ||
| This normally happens by running build scripts (e.g., `npm build` and `make`) or dependency installation | ||
| scripts (e.g., `python setup.py install`). | ||
| Audit your workflow file to make sure no code from the incoming PR is executed. | ||
| Please see https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for additional | ||
| mitigations. | ||
| metadata: | ||
| category: security | ||
| owasp: "A01:2017 - Injection" | ||
| cwe: "CWE-913: Improper Control of Dynamically-Managed Code Resources" | ||
| likelihood: MEDIUM | ||
| impact: MEDIUM | ||
| confidence: MEDIUM | ||
| subcategory: | ||
| - vuln | ||
| references: | ||
| - https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
| - https://github.com/justinsteven/advisories/blob/master/2021_github_actions_checkspelling_token_leak_via_advice_symlink.md | ||
| - https://www.legitsecurity.com/blog/github-privilege-escalation-vulnerability | ||
| technology: | ||
| - github-actions | ||
| tags: [security] | ||
| shortDescription: Unsafe code checkout in workflow_run trigger | ||
| help: | | ||
| ## Remediation | ||
| When using `workflow_run`, avoid checking out code from the triggering workflow. If you must check out external code, | ||
| ensure no untrusted code is executed (including build scripts and dependency installation). | ||
| See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for additional mitigations. | ||
| patterns: | ||
| - pattern-inside: | | ||
| on: | ||
| ... | ||
| workflow_run: ... | ||
| ... | ||
| ... | ||
| - pattern-inside: | | ||
| jobs: | ||
| ... | ||
| $JOBNAME: | ||
| ... | ||
| steps: | ||
| ... | ||
| - pattern: | | ||
| ... | ||
| uses: "$ACTION" | ||
| with: | ||
| ... | ||
| ref: $EXPR | ||
| - metavariable-regex: | ||
| metavariable: $ACTION | ||
| regex: actions/checkout@.* | ||
| - metavariable-pattern: | ||
| language: generic | ||
| metavariable: $EXPR | ||
| patterns: | ||
| - pattern: ${{ github.event.workflow_run ... }} | ||
| severity: WARNING | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rule missing alternative trigger format patterns
Medium Severity
The
workflow-run-target-code-checkoutrule only matches the object format for theon:trigger, while the similarpull-request-target-code-checkoutandcheckout-pr-on-issue-commentrules usepattern-eitherto match three formats: object format (on: ... workflow_run: ...), list format (on: [..., workflow_run, ...]), and simple format (on: workflow_run). This inconsistency means workflows using list format won't be detected by this rule.