-
Notifications
You must be signed in to change notification settings - Fork 37
feat(zizmor): add Zizmor config validation #1877
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
mikefat
wants to merge
8
commits into
main
Choose a base branch
from
mikefat/AddZizmorConfigValidation
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
8 commits
Select commit
Hold shift + click to select a range
5c021e5
feat: Add Zizmor config validation before execution
mikefat 21f0e03
Add extra audits that will fail validation if present
mikefat c2b99bc
Move zizmor validation code to standalone function
mikefat d519872
Pin composite action
mikefat 672e03b
Satisfy linter gods
mikefat 0259c48
Run prettier on branch
mikefat 35386a4
Update docs and tests based on PR comments
mikefat 6f4f884
Fix dependency naming for renovate
mikefat 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| name: Test validate-zizmor-config action | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - actions/validate-zizmor-config/** | ||
| - .github/workflows/test-validate-zizmor-config.yml | ||
| - .github/workflows/reusable-zizmor.yml | ||
|
|
||
| pull_request: | ||
| paths: | ||
| - actions/validate-zizmor-config/** | ||
| - .github/workflows/test-validate-zizmor-config.yml | ||
| - .github/workflows/reusable-zizmor.yml | ||
| types: | ||
| - edited | ||
| - opened | ||
| - ready_for_review | ||
| - synchronize | ||
|
|
||
| merge_group: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
|
|
||
| steps: | ||
| - name: Harden the runner (Audit all outbound calls) | ||
| uses: step-security/harden-runner@f808768d1510423e83855289c910610ca9b43176 # v2.17.0 | ||
| with: | ||
| egress-policy: audit | ||
|
|
||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Setup UV | ||
| uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0 | ||
| with: | ||
| enable-cache: true | ||
|
|
||
| - name: Unit tests | ||
| env: | ||
| # renovate: datasource=pypi depName=pyyaml | ||
| PYYAML_VERSION: "6.0.2" | ||
| run: | | ||
| cd actions/validate-zizmor-config | ||
| uv run --with "pyyaml==${PYYAML_VERSION}" python3 -m unittest discover -v | ||
|
|
||
| - name: Integration smoke (action on repo default zizmor) | ||
| uses: ./actions/validate-zizmor-config | ||
| with: | ||
| config_path: .github/zizmor.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,25 @@ | ||
| # validate-zizmor-config | ||
|
|
||
| Composite action that enforces Grafana policy on a **repo-local** `zizmor.yml` / `.github/zizmor.yml` before running zizmor. | ||
|
|
||
| Intended to be called from [`.github/workflows/reusable-zizmor.yml`](../../.github/workflows/reusable-zizmor.yml). | ||
|
|
||
| ## Inputs | ||
|
|
||
| | Name | Required | Description | | ||
| | ------------- | -------- | ------------------------------------------------- | | ||
| | `config_path` | yes | Path to the config file relative to the workspace | | ||
|
|
||
| ## Requirements | ||
|
|
||
| The calling job must run **`setup-uv`** (or otherwise provide `uv`) before this action, and the workspace must contain the file at `config_path`. | ||
|
|
||
| ## Tests | ||
|
|
||
| From the repository root: | ||
|
|
||
| ```bash | ||
| cd actions/validate-zizmor-config && uv run --with pyyaml==6.0.2 python3 -m unittest discover -v | ||
| ``` | ||
|
|
||
| CI: [`.github/workflows/test-validate-zizmor-config.yml`](../../.github/workflows/test-validate-zizmor-config.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,22 @@ | ||
| name: Validate repo-local zizmor config | ||
| description: >- | ||
| Enforces Grafana policy on a repo-local zizmor.yml (used from reusable-zizmor before running zizmor). | ||
| inputs: | ||
| config_path: | ||
| description: Path to the zizmor config file relative to the workspace (e.g. zizmor.yml). | ||
| required: true | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Run policy validator | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| uv run --with "pyyaml==${PYYAML_VERSION}" python3 \ | ||
| "${GITHUB_ACTION_PATH}/validate_zizmor_config.py" \ | ||
| "${CONFIG_PATH}" | ||
| env: | ||
| CONFIG_PATH: ${{ inputs.config_path }} | ||
| # renovate: datasource=pypi depName=pyyaml | ||
| PYYAML_VERSION: "6.0.2" |
101 changes: 101 additions & 0 deletions
101
actions/validate-zizmor-config/test_validate_zizmor_config.py
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,101 @@ | ||
| """Unit tests for validate_zizmor_config policy logic.""" | ||
|
|
||
| import unittest | ||
|
|
||
| import yaml | ||
|
|
||
| from validate_zizmor_config import UniqueKeyFullLoader, collect_violations | ||
|
|
||
|
|
||
| class CollectViolationsTests(unittest.TestCase): | ||
| def test_parsed_none_ok(self) -> None: | ||
| self.assertEqual(collect_violations(None), []) | ||
|
|
||
| def test_no_rules_key_ok(self) -> None: | ||
| data = yaml.safe_load("other: true\n") | ||
| self.assertEqual(collect_violations(data), []) | ||
|
|
||
| def test_empty_rules_ok(self) -> None: | ||
| data = yaml.safe_load("rules: {}\n") | ||
| self.assertEqual(collect_violations(data), []) | ||
|
|
||
| def test_allows_grafana_style_unpinned(self) -> None: | ||
| text = """ | ||
| rules: | ||
| unpinned-uses: | ||
| config: | ||
| policies: | ||
| actions/*: any | ||
| grafana/*: any | ||
| """ | ||
| data = yaml.safe_load(text) | ||
| self.assertEqual(collect_violations(data), []) | ||
|
|
||
| def test_rejects_insecure_commands(self) -> None: | ||
| data = yaml.safe_load( | ||
| "rules:\n insecure-commands:\n ignore: [x.yml]\n", | ||
| ) | ||
| v = collect_violations(data) | ||
| self.assertEqual(len(v), 1) | ||
| self.assertIn("insecure-commands", v[0]) | ||
|
|
||
| def test_rejects_template_injection(self) -> None: | ||
| data = yaml.safe_load("rules:\n template-injection:\n disable: true\n") | ||
| v = collect_violations(data) | ||
| self.assertEqual(len(v), 1) | ||
| self.assertIn("template-injection", v[0]) | ||
|
|
||
| def test_rejects_impostor_commit(self) -> None: | ||
| data = yaml.safe_load("rules:\n impostor-commit: {}\n") | ||
| self.assertTrue(any("impostor-commit" in m for m in collect_violations(data))) | ||
|
|
||
| def test_rejects_known_vulnerable_actions(self) -> None: | ||
| data = yaml.safe_load("rules:\n known-vulnerable-actions:\n ignore: []\n") | ||
| v = collect_violations(data) | ||
| self.assertEqual(len(v), 1) | ||
| self.assertIn("known-vulnerable-actions", v[0]) | ||
|
|
||
| def test_rejects_ref_confusion(self) -> None: | ||
| data = yaml.safe_load("rules:\n ref-confusion:\n disable: true\n") | ||
| v = collect_violations(data) | ||
| self.assertEqual(len(v), 1) | ||
| self.assertIn("ref-confusion", v[0]) | ||
|
|
||
| def test_multiple_violations_in_one_config(self) -> None: | ||
| text = """ | ||
| rules: | ||
| insecure-commands: | ||
| ignore: [a.yml] | ||
| template-injection: | ||
| ignore: [b.yml] | ||
| unpinned-uses: | ||
| disable: true | ||
| """ | ||
| data = yaml.safe_load(text) | ||
| v = collect_violations(data) | ||
| self.assertGreaterEqual(len(v), 3, msg=v) | ||
| joined = " ".join(v) | ||
| self.assertIn("insecure-commands", joined) | ||
| self.assertIn("template-injection", joined) | ||
| self.assertIn("unpinned-uses.disable", joined) | ||
|
|
||
| def test_rejects_unpinned_disable(self) -> None: | ||
| data = yaml.safe_load("rules:\n unpinned-uses:\n disable: true\n") | ||
| v = collect_violations(data) | ||
| self.assertTrue(any("disable" in m for m in v)) | ||
|
|
||
| def test_rejects_star_any_policy(self) -> None: | ||
| data = yaml.safe_load( | ||
| 'rules:\n unpinned-uses:\n config:\n policies:\n "*": any\n', | ||
| ) | ||
| v = collect_violations(data) | ||
| self.assertTrue(any("*" in m or "any" in m for m in v)) | ||
|
|
||
| def test_duplicate_mapping_keys_rejected_by_loader(self) -> None: | ||
| text = "rules:\n insecure-commands:\n x: 1\n insecure-commands:\n y: 2\n" | ||
| with self.assertRaises(yaml.YAMLError): | ||
| yaml.load(text, Loader=UniqueKeyFullLoader) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
Uh oh!
There was an error while loading. Please reload this page.