Repository navigation
Add an org-wide check for locked, exact action refs #47
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
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
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,24 @@ | ||
| name: Actions Lock Check Self-Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4.4.0 | ||
| with: | ||
| persist-credentials: false | ||
| - run: | | ||
| python3 -m venv /tmp/action-pins-tests | ||
| /tmp/action-pins-tests/bin/pip install PyYAML==6.0.3 | ||
| /tmp/action-pins-tests/bin/python -m unittest discover -s scripts -p test_action_pins.py | ||
|
|
||
| check: | ||
| uses: ./.github/workflows/actions-lock-check.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,51 @@ | ||
| name: Actions lock check | ||
|
|
||
| # Checks the caller's workflows against the org convention: every step-level action is | ||
| # an exact vX.Y.Z release (or a full SHA for a commit with no release tag), and | ||
| # actions.lock pins it. The job holds no secrets because it runs a third-party binary. | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| allow-sha-pins: | ||
| description: Accept SHA pins of tagged releases, for workflows a generator owns | ||
| type: boolean | ||
| required: false | ||
| default: false | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - uses: actions/checkout@v4.4.0 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| # --verify-local makes no API calls. Online --verify reads every action's repository | ||
| # with this token, and orgs with an IP allow list (aquasecurity) reject runner IPs. | ||
| # The runner itself rejects a locked commit that isn't reachable from its tag. | ||
| - name: Verify actions.lock coverage | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: | | ||
| gh extension install github/gh-actions-lock --pin v0.4.1 | ||
| gh actions-lock --verify-local | ||
|
|
||
| - name: Check action refs | ||
| env: | ||
| ALLOW_SHA_PINS: ${{ inputs.allow-sha-pins }} | ||
| run: | | ||
| if [ "$GITHUB_REPOSITORY" = kernel/security-workflows ]; then | ||
| cp scripts/action_pins.py "$RUNNER_TEMP/action_pins.py" # self-test runs the PR's copy | ||
| else | ||
| curl -fsSL https://raw.githubusercontent.com/kernel/security-workflows/main/scripts/action_pins.py \ | ||
| -o "$RUNNER_TEMP/action_pins.py" | ||
| fi | ||
| python3 -m venv "$RUNNER_TEMP/venv" | ||
| "$RUNNER_TEMP/venv/bin/pip" install -q PyYAML==6.0.3 | ||
| flags=() | ||
| if [ "$ALLOW_SHA_PINS" = true ]; then flags+=(--allow-sha-pins); fi | ||
| "$RUNNER_TEMP/venv/bin/python" -I "$RUNNER_TEMP/action_pins.py" "${flags[@]}" . |
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,89 @@ | ||
| import tempfile | ||
| import textwrap | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| from action_pins import repo_problems | ||
|
|
||
| TAGGED = "11d5960a326750d5838078e36cf38b85af677262" | ||
| UNTAGGED = "af490855f90e83f89dcaf4ea18da962ec294350e" | ||
| LOCK = f""" | ||
| version: 'v0.0.3' | ||
| workflows: | ||
| '.github/workflows/ci.yml': | ||
| - 'actions/checkout@v4.4.0' | ||
| - 'actions/checkout@{TAGGED}' | ||
| - 'kernel/security-workflows@{UNTAGGED}' | ||
| '.github/workflows/caller.yml': [] | ||
| dependencies: | ||
| 'actions/checkout@v4.4.0': | ||
| ref: 'v4.4.0' | ||
| commit: 'sha1-{TAGGED}' | ||
| 'actions/checkout@{TAGGED}': | ||
| ref: 'v4.4.0' | ||
| commit: 'sha1-{TAGGED}' | ||
| 'kernel/security-workflows@{UNTAGGED}': | ||
| ref: 'main' | ||
| commit: 'sha1-{UNTAGGED}' | ||
| """ | ||
|
|
||
|
|
||
| class RepoProblemsTest(unittest.TestCase): | ||
| def check(self, uses, lock=LOCK, allow_sha_pins=False, path=".github/workflows/ci.yml"): | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = Path(tmp) | ||
| (root / ".github/workflows").mkdir(parents=True) | ||
| if lock is not None: | ||
| (root / ".github/workflows/actions.lock").write_text(lock) | ||
| (root / ".github/workflows/caller.yml").write_text( | ||
| "on: push\njobs:\n x:\n uses: kernel/security-workflows/.github/workflows/semgrep.yml@main\n" | ||
| ) | ||
| target = root / path | ||
| target.parent.mkdir(parents=True, exist_ok=True) | ||
| steps = "".join(f"\n - uses: {u}" for u in uses) | ||
| if path.endswith("action.yml"): | ||
| target.write_text(f"runs:\n using: composite\n steps:{steps.replace(chr(10) + ' ', chr(10) + ' ')}\n") | ||
| else: | ||
| target.write_text(f"on: push\njobs:\n x:\n runs-on: ubuntu-latest\n steps:{steps}\n") | ||
| return repo_problems(root, allow_sha_pins) | ||
|
|
||
| def test_accepts_exact_tags_untagged_shas_and_local_actions(self): | ||
| uses = [ | ||
| "actions/checkout@v4.4.0", | ||
| f"kernel/security-workflows/.github/actions/llm-credentials@{UNTAGGED}", | ||
| "$/.github/actions/build", | ||
| "./.github/actions/build", | ||
| "docker://alpine:3", | ||
| ] | ||
| self.assertEqual(self.check(uses), []) | ||
|
|
||
| def test_rejects_floating_branch_unlocked_and_tagged_sha_refs(self): | ||
| for uses in ( | ||
| "actions/checkout@v4", | ||
| "actions/checkout@main", | ||
| "actions/checkout@v4.5.0", | ||
| f"actions/checkout@{TAGGED}", | ||
| ): | ||
| self.assertEqual(len(self.check([uses])), 1, uses) | ||
|
|
||
| def test_allow_sha_pins_accepts_locked_tagged_shas_only(self): | ||
| self.assertEqual(self.check([f"actions/checkout@{TAGGED}"], allow_sha_pins=True), []) | ||
| self.assertEqual(len(self.check(["actions/checkout@v4"], allow_sha_pins=True)), 1) | ||
|
|
||
| def test_workflows_must_be_onboarded(self): | ||
| (problem,) = self.check(["actions/checkout@v4.4.0"], path=".github/workflows/new.yml") | ||
| self.assertIn("not onboarded", problem) | ||
| (problem,) = self.check(["actions/checkout@v4.4.0"], lock=None) | ||
| self.assertIn("not onboarded", problem) | ||
|
|
||
| def test_in_repo_actions_need_a_locked_exact_ref(self): | ||
| path = ".github/actions/build/action.yml" | ||
| self.assertEqual(self.check(["actions/checkout@v4.4.0"], path=path), []) | ||
| self.assertEqual(len(self.check(["actions/checkout@v4"], path=path)), 1) | ||
|
|
||
| def test_reusable_workflow_calls_are_ignored(self): | ||
| self.assertEqual(self.check([]), []) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
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.
Local composites outside .github/actions skipped
Medium Severity
repo_problemsonly opens composite files under.github/actions/**, whileref_problemtreats every./uses:as local and returns immediately. A workflow canuses: ./some-actionwhoseaction.ymlpinsevil@main, and neither the Python check nor onboarding will see that remote ref.Additional Locations (1)
scripts/action_pins.py#L27-L29Triggered by learned rule: Keep actions.lock; do not rewrite SHA pins
Reviewed by Cursor Bugbot for commit 54ed6cf. Configure here.