-
Notifications
You must be signed in to change notification settings - Fork 7
feat(CheckoutWithStaticCredentials): flag actions/checkout use with static credentials
#7
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
7 commits
Select commit
Hold shift + click to select a range
023d72f
get_key for maps where the key contains dashes :)
6f6d6172 82c4640
detections for static credentials
6f6d6172 d2b1670
documentation
6f6d6172 8828bbb
stray newline
6f6d6172 abf9259
git mv spec/claws/rule/checkout_with{out,}_static_credentials_spec.rb
6f6d6172 bd071a9
more precise checks + negative test case
6f6d6172 6e92dc2
get_key should check that the input map isn't nil
6f6d6172 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module Claws | ||
| module Rule | ||
| class CheckoutWithStaticCredentials < BaseRule | ||
| description <<~DESC | ||
| Avoid using static credentials like deploy keys, SSH keys, or personal access | ||
| tokens to clone other repositories. Static credentials can be tricky to audit | ||
| and rotate, making them risky to hold onto, especially in the event of an | ||
| incident where they may be leaked. | ||
|
|
||
| Either grant your repository access directly to other repositories, or use a | ||
| Github App to generate a short lived access token. | ||
|
|
||
| For more information: | ||
| https://github.com/betterment/claws/blob/main/README.md#checkoutwithstaticcredentials | ||
| DESC | ||
|
|
||
| on_step %( | ||
| $step.meta.action.name == "actions/checkout" && | ||
| ( | ||
| get_key($step.with, "ssh-key") =~ "{{.*secrets\..*" || | ||
| get_key($step.with, "ssh-key") =~ "{{.*env\..*" || | ||
| get_key($step.with, "ssh-key") =~ "{{.*vars\..*" || | ||
| get_key($step.with, "ssh-key") =~ ".*-----BEGIN.*" | ||
| ) | ||
| ), highlight: "with.ssh-key" | ||
|
|
||
| on_step %( | ||
| $step.meta.action.name == "actions/checkout" && | ||
| ( | ||
| get_key($step.with, "token") =~ "{{.*secrets\..*" || | ||
| get_key($step.with, "token") =~ "{{.*env\..*" || | ||
| get_key($step.with, "token") =~ "{{.*vars\..*" || | ||
| get_key($step.with, "token") =~ "gh[a-z]_.*" | ||
| ) | ||
| ), highlight: "with.token" | ||
| end | ||
| end | ||
| end |
186 changes: 186 additions & 0 deletions
186
spec/claws/rule/checkout_with_static_credentials_spec.rb
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,186 @@ | ||
| RSpec.describe Claws::Rule::CheckoutWithStaticCredentials do | ||
| before do | ||
| load_detection | ||
| end | ||
|
|
||
| context "with default configuration" do | ||
| it "flags a static ssh key via secrets" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| ssh-key: ${{ secrets.deploy_key }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static ssh key via repo/org vars" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| ssh-key: ${{ vars.deploy_key }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static ssh key via env vars" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: ${{ env.deploy_key }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static ssh key via hardcoded string" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| ssh-key: | | ||
| -----BEGIN OPENSSH PRIVATE KEY----- | ||
| sike... you thought | ||
| -----END OPENSSH PRIVATE KEY----- | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static PAT stored in secrets" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: ${{ secrets.secret_pat }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static PAT stored in repo/org vars" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: ${{ vars.secret_pat }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static PAT stored in env vars" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: ${{ env.some_pat }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "flags a static PAT via hardcoded string" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: "ghp_some_bogus_string" | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(1) | ||
| expect(violations[0].line).to eq(10) | ||
| expect(violations[0].name).to eq("CheckoutWithStaticCredentials") | ||
| end | ||
|
|
||
| it "doesn't flag an access token generated using a github app" do | ||
| violations = analyze(<<~YAML) | ||
| on: push | ||
|
|
||
| jobs: | ||
| checkout: | ||
| runs-on: ubuntu | ||
| steps: | ||
| - name: Create GitHub App installation token | ||
| id: app-token | ||
| uses: actions/create-github-app-token@v2 | ||
| with: | ||
| app-id: ${{ vars.APP_ID }} | ||
| private-key: ${{ secrets.APP_PRIVATE_KEY }} | ||
|
|
||
| - uses: actions/checkout@v5 | ||
| with: | ||
| repository: foo-corp/test-action | ||
| token: ${{ steps.app-token.outputs.token }} | ||
| YAML | ||
|
|
||
| expect(violations.count).to eq(0) | ||
| end | ||
| end | ||
| end | ||
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
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.