From a16a80b0fcfa2965c0c3ce7fae963c89d2d58410 Mon Sep 17 00:00:00 2001 From: Dmitriy Vasyura Date: Thu, 26 Nov 2026 04:37:19 -0800 Subject: [PATCH] ci: tighten workflow token permissions Limit checkout tokens to repository read access, remove unused Actions scopes, and avoid persisting checkout credentials across build steps. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/build.yml | 5 ++++ .github/workflows/coverage.yml | 1 - .github/workflows/lint.yml | 4 ++++ .github/workflows/perf-baseline.yml | 2 ++ .github/workflows/perf-tests.yml | 1 - .github/workflows/pr-check.yml | 11 +++++++++ scripts/tests/test_quality_workflows.py | 31 +++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 90ff4970..5eb52ae9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,6 +8,9 @@ on: - release/* - release-* +permissions: + contents: read + jobs: build: name: Build @@ -37,6 +40,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Rust Tool Chain setup uses: dtolnay/rust-toolchain@7e38f4b43b4db5c8dd498af069a4f6196df1d067 # v1 diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index e4d7cba2..828789e0 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -9,7 +9,6 @@ on: - release-* permissions: - actions: read contents: read pull-requests: write diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b7bdf0a5..1f546724 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,6 +17,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Rust Tool Chain setup uses: dtolnay/rust-toolchain@7e38f4b43b4db5c8dd498af069a4f6196df1d067 # v1 @@ -40,6 +42,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Rust Tool Chain setup uses: dtolnay/rust-toolchain@7e38f4b43b4db5c8dd498af069a4f6196df1d067 # v1 diff --git a/.github/workflows/perf-baseline.yml b/.github/workflows/perf-baseline.yml index b91bf870..1a6693ba 100644 --- a/.github/workflows/perf-baseline.yml +++ b/.github/workflows/perf-baseline.yml @@ -33,6 +33,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Set Python to PATH uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 diff --git a/.github/workflows/perf-tests.yml b/.github/workflows/perf-tests.yml index ec0cccbe..e3eb8c5d 100644 --- a/.github/workflows/perf-tests.yml +++ b/.github/workflows/perf-tests.yml @@ -10,7 +10,6 @@ on: workflow_dispatch: permissions: - actions: read contents: read pull-requests: write diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index ef421cc3..0e851b0b 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -15,6 +15,9 @@ on: - release-* workflow_dispatch: +permissions: + contents: read + jobs: tests: # Very generic tests, we don't verify whether the envs are discovered correctly or not. @@ -43,6 +46,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Set Python to PATH uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 @@ -256,6 +261,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false # region Setup Poetry - name: Set Python 3.x to PATH @@ -422,6 +429,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false # Homebrew - name: Homebrew Python @@ -485,6 +494,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Rust Tool Chain setup uses: dtolnay/rust-toolchain@7e38f4b43b4db5c8dd498af069a4f6196df1d067 # v1 diff --git a/scripts/tests/test_quality_workflows.py b/scripts/tests/test_quality_workflows.py index 84f8a6dd..d7150d3e 100644 --- a/scripts/tests/test_quality_workflows.py +++ b/scripts/tests/test_quality_workflows.py @@ -229,6 +229,37 @@ def test_comment_steps_are_fork_safe_and_non_gating(self) -> None: ), ) + def test_workflow_tokens_have_only_required_permissions(self) -> None: + for workflow, expected in ( + ("build.yml", {"contents": "read"}), + ("pr-check.yml", {"contents": "read"}), + ("coverage.yml", {"contents": "read", "pull-requests": "write"}), + ("perf-tests.yml", {"contents": "read", "pull-requests": "write"}), + ): + with self.subTest(workflow=workflow): + text = (ROOT / ".github" / "workflows" / workflow).read_text(encoding="utf-8") + match = re.search( + r"(?m)^permissions:\n((?: [a-z-]+: [a-z]+\n)+)", text + ) + self.assertIsNotNone(match) + assert match is not None + self.assertEqual( + dict(re.findall(r"(?m)^ ([a-z-]+): ([a-z]+)$", match.group(1))), + expected, + ) + + def test_checkout_tokens_are_not_persisted_after_fetch(self) -> None: + for workflow in ("build.yml", "lint.yml", "perf-baseline.yml", "pr-check.yml"): + text = (ROOT / ".github" / "workflows" / workflow).read_text(encoding="utf-8") + checkouts = [ + step for step in re.split(r"(?m)^ - name: ", text)[1:] + if re.search(r"(?m)^ uses: actions/checkout@", step) + ] + with self.subTest(workflow=workflow): + self.assertTrue(checkouts) + for checkout in checkouts: + self.assertRegex(checkout, r"(?m)^ persist-credentials: false$") + def test_coverage_proof_and_source_reports_are_required_and_uploaded(self) -> None: for workflow in ("coverage.yml", "coverage-baseline.yml", "coverage-macos.yml"): with self.subTest(workflow=workflow):