generated from google/new-project
-
Notifications
You must be signed in to change notification settings - Fork 2
Add semgrep checks for pinned actions. #26
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4080b5
Add semgrep tests for pinned actions.
billnapier 2145ea1
Fix mdformat.
billnapier e1be33a
Use ratchet to pin all our action usage references.
billnapier 74634de
Fix message to also include link to docs.
billnapier 185ddcf
Add additional test case.
billnapier 5cf2956
Fix yaml format.
billnapier 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
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
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
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
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
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,31 @@ | ||
| # actions-need-pinned-commits | ||
|
|
||
| For actions that look like: | ||
|
|
||
| ``` | ||
| uses: actions/checkout@v4 | ||
| ``` | ||
|
|
||
| GitHub uses the underlying git label v4 to fetch the action to run. As seen in the | ||
| [tj-actions/changed-files](https://semgrep.dev/blog/2025/popular-github-action-tj-actionschanged-files-is-compromised/) | ||
| vulnerabilty, these lables are not immutable and trivially changeable. So what you think is a nice | ||
| stable safe version, an attacker has changed behind your back to something nefarious. | ||
|
|
||
| We are strongly encouraging use to use the full git commit hash instead to prevent this type of | ||
| attacks. | ||
|
|
||
| ## Ratchet | ||
|
|
||
| [Ratchet](https://github.com/sethvargo/ratchet) provides an easy way to do this. | ||
|
|
||
| You can pin all your workflow files like this: | ||
|
|
||
| ``` | ||
| ratchet pin .github/workflows/* | ||
| ``` | ||
|
|
||
| And upgrade them (as needed, under your control and not someone elses): | ||
|
|
||
| ``` | ||
| ratchet upgrade .github/workflows/action_to_upgrade.yml | ||
| ``` |
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,18 @@ | ||
| rules: | ||
| - id: actions-need-pinned-commits | ||
| languages: | ||
| - yaml | ||
| severity: WARNING | ||
| message: 'Referencing an action to run by git tag is risky, due to the mutability of git tags. Prefer | ||
| to use full git SHAs instead. More information: https://google.github.io/github-team/semgrep-rules/actions-need-pinned-commits.html' | ||
| metadata: | ||
| category: best-practice | ||
| technology: | ||
| - github-actions | ||
| patterns: | ||
| - pattern-either: | ||
| - patterns: | ||
| - pattern-inside: "{steps: ...}" | ||
| # Match all uses patterns that don't contain the full SHA1 hash. Yes, short hashes exist but suffer from a similar (but slightly harder) attack vector by purposely crafting a colliding SHA. | ||
| - pattern: "uses: ..." | ||
| - pattern-not-regex: ".*@[0-9A-Fa-f]{40}.*" |
19 changes: 19 additions & 0 deletions
19
semgrep-tests/actions/actions_need_pinned_commits.test.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,19 @@ | ||
| name: 'Test Actions Needing Pinned Commits' | ||
| on: | ||
| pull_request: | ||
| jobs: | ||
| do-stuff: | ||
| steps: | ||
| - name: 'Step 1' | ||
| # ruleid: actions-need-pinned-commits | ||
| uses: actions/checkout@v4 | ||
|
||
| - name: 'Step 2' | ||
| uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' | ||
| - name: 'Step 2.5' | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # ratchet:actions/checkout@v4 | ||
| - name: 'Step 3' | ||
| # ruleid: actions-need-pinned-commits | ||
| uses: 'actions/checkout@11bd719' | ||
|
||
| - name: 'Step 4' | ||
| # ruleid: actions-need-pinned-commits | ||
| uses: 'actions/checkout@my_git_label' | ||
|
||
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,2 @@ | ||
| #!/bin/sh | ||
| docker run -it --rm -v "${PWD}:${PWD}" -w "${PWD}" ghcr.io/sethvargo/ratchet:latest $* |
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.
Uh oh!
There was an error while loading. Please reload this page.