From cc7ceb5f3e1bd3781eb91c08a68ceed5c2ba9ea2 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Mon, 9 Nov 2026 12:01:47 -0800 Subject: [PATCH] ci: allow the remediation publisher's credentialed checkout (#8668) * ci: allow the remediation publisher's credentialed checkout released-dependency-audit.yml's publish-remediation job checks out 1.2.x with persist-credentials: true because released_dependency_remediation.py publishes with `git push origin`. The workflow policy treats every checkout outside CHECKOUT_WRITE_WORKFLOWS as read-only, so "Workflow policy checks" fails on develop and on every pull request. List the workflow with the other workflows that push. Its read-only checkouts already set persist-credentials: false. Signed-off-by: Thomas Vincent * test(ci): cover the workflow policy's credentialed checkout list Loads check-workflow-policy.py and checks that the remediation publisher's checkout is accepted while it is listed and reported once it is not, so the changed-Python coverage gate has evidence for the script. Signed-off-by: Thomas Vincent --------- Signed-off-by: Thomas Vincent --- .github/scripts/check-workflow-policy.py | 1 + tests/tools/test_workflow_policy.py | 54 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/tools/test_workflow_policy.py diff --git a/.github/scripts/check-workflow-policy.py b/.github/scripts/check-workflow-policy.py index 9480c9c201..a7f94b10df 100644 --- a/.github/scripts/check-workflow-policy.py +++ b/.github/scripts/check-workflow-policy.py @@ -17,6 +17,7 @@ DEFAULT_WORKFLOW_GLOB = ".github/workflows/*" DEFAULT_ACTION_GLOB = ".github/actions/**/action.y*ml" ALLOWED_RUNNERS = {"ubuntu-24.04", "ubuntu-slim"} CHECKOUT_WRITE_WORKFLOWS = { + ".github/workflows/released-dependency-audit.yml", ".github/workflows/security-baseline-refresh.yml", ".github/workflows/update-contributors.yml", } diff --git a/tests/tools/test_workflow_policy.py b/tests/tools/test_workflow_policy.py new file mode 100644 index 0000000000..a798490e4f --- /dev/null +++ b/tests/tools/test_workflow_policy.py @@ -0,0 +1,54 @@ +"""Exercise the workflow policy's credentialed checkout allowance.""" + +from __future__ import annotations + +import contextlib +import importlib.util +import io +import sys +import unittest +from pathlib import Path +from unittest import mock + +ROOT = Path(__file__).resolve().parents[2] +SCRIPT = ROOT / ".github/scripts/check-workflow-policy.py" +PUBLISHER = ".github/workflows/released-dependency-audit.yml" + +sys.dont_write_bytecode = True +SPEC = importlib.util.spec_from_file_location("check_workflow_policy", SCRIPT) +assert SPEC and SPEC.loader +POLICY = importlib.util.module_from_spec(SPEC) +sys.modules[SPEC.name] = POLICY +SPEC.loader.exec_module(POLICY) + + +def run_policy(*workflows: str) -> tuple[int, str]: + output = io.StringIO() + argv = ["check-workflow-policy.py", "--workflows", *workflows] + with mock.patch.object(sys, "argv", argv), contextlib.redirect_stdout(output): + status = POLICY.main() + return status, output.getvalue() + + +class WorkflowPolicyTest(unittest.TestCase): + def test_publisher_checkout_may_keep_credentials_for_its_push(self) -> None: + status, output = run_policy(PUBLISHER) + + self.assertEqual(0, status, output) + + def test_unlisted_workflow_with_a_credentialed_checkout_is_rejected(self) -> None: + allowed = POLICY.CHECKOUT_WRITE_WORKFLOWS - {PUBLISHER} + + with mock.patch.object(POLICY, "CHECKOUT_WRITE_WORKFLOWS", allowed): + status, output = run_policy(PUBLISHER) + + self.assertEqual(1, status) + self.assertIn( + f"{PUBLISHER}:Checkout released branch for publication: " + "read-only checkout must set persist-credentials: false", + output, + ) + + +if __name__ == "__main__": + unittest.main()