From 4bdd389999cb5693be63f190560959402451e9b2 Mon Sep 17 00:00:00 2001 From: abrichr Date: Thu, 19 Nov 2026 09:32:33 -0500 Subject: [PATCH] fix(ci): bind pull_request_target safety runs to the PR head, not GITHUB_SHA The protected safety review compared GITHUB_SHA with the head_sha GitHub records for the workflow run. On pull_request_target GITHUB_SHA is a base-branch commit and the run's head_sha is the pull request head, so the comparison could not hold and every pull_request_target run since #1992 failed with 'workflow run SHA does not match the triggering event SHA'. For pull_request_target the run is now bound to the event's pull request head. issue_comment and merge_group keep the GITHUB_SHA binding. A missing or malformed SHA is an error. --- scripts/ci/protected_safety_review.py | 21 +++++- tests/test_protected_safety_review.py | 101 ++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/scripts/ci/protected_safety_review.py b/scripts/ci/protected_safety_review.py index f031288806..5d199d70c0 100644 --- a/scripts/ci/protected_safety_review.py +++ b/scripts/ci/protected_safety_review.py @@ -619,7 +619,16 @@ def _workflow_provenance_errors( event_name: str | None, workflow_attempt: str | None, workflow_sha: str | None, + run_head_sha: str | None, ) -> list[str]: + """Bind a workflow run record to the execution that names it. + + ``run_head_sha`` is the commit GitHub must have recorded as the run's + ``head_sha``. For ``issue_comment`` and ``merge_group`` that is + ``GITHUB_SHA``. For ``pull_request_target`` GitHub records the pull request + head there, while ``GITHUB_SHA`` is a base-branch commit, so the caller + passes the event's pull request head instead. + """ if workflow_run is None: return [] errors: list[str] = [] @@ -640,7 +649,11 @@ def _workflow_provenance_errors( expected_attempt = -1 if workflow_run.get("run_attempt") != expected_attempt: errors.append("workflow run attempt does not match this execution") - if workflow_sha and workflow_run.get("head_sha") != workflow_sha: + if not isinstance(run_head_sha, str) or not SHA.fullmatch(run_head_sha): + errors.append( + "triggering event SHA for the workflow run is missing or malformed" + ) + elif workflow_run.get("head_sha") != run_head_sha: errors.append("workflow run SHA does not match the triggering event SHA") return errors @@ -793,6 +806,7 @@ def _passed_pr_head_verdict( event_name=event_name, workflow_attempt=attempt, workflow_sha=source_sha if isinstance(source_sha, str) else None, + run_head_sha=source_sha if isinstance(source_sha, str) else None, ) if isinstance(source_sha, str) and isinstance(main_sha, str): errors.extend( @@ -855,6 +869,11 @@ def run( event_name=event_name, workflow_attempt=workflow_attempt, workflow_sha=workflow_sha, + run_head_sha=( + head_sha + if event_name == "pull_request_target" + else workflow_sha + ), ) ) if event.get("repository", {}).get("full_name", REPOSITORY) != REPOSITORY: diff --git a/tests/test_protected_safety_review.py b/tests/test_protected_safety_review.py index f9f804b9dc..3a44b6de96 100644 --- a/tests/test_protected_safety_review.py +++ b/tests/test_protected_safety_review.py @@ -776,6 +776,107 @@ def test_workflow_uses_trusted_main_for_merge_group_not_group_content() -> None: assert "ref: ${{ github.event.merge_group.head_sha }}" not in text +MAIN_TIP = "c" * 40 + + +class PullRequestTargetApi(FakeApi): + """A ``pull_request_target`` run as GitHub reports it. + + The run record's ``head_sha`` is the pull request head. ``GITHUB_SHA`` is a + base-branch commit (here main's tip), and the checkout is the pull + request's recorded base, so all three differ. + """ + + def __init__(self, *, run_head_sha: str = HEAD, files: list[str]) -> None: + super().__init__() + self.run_head_sha = run_head_sha + self.files = [{"filename": name} for name in files] + + def request(self, path: str, *, method: str = "GET", data: Any = None) -> Any: + value = super().request(path, method=method, data=data) + if path == "/actions/runs/77": + value["event"] = "pull_request_target" + value["head_sha"] = self.run_head_sha + if path == "/git/ref/heads/main": + value = {"object": {"sha": MAIN_TIP}} + return value + + def pages(self, path: str) -> list[dict[str, Any]]: + if path == "/pulls/42/files": + return self.files + if path == "/issues/42/comments": + return [] + return super().pages(path) + + +def _run_pull_request_target( + api: FakeApi, event_head: Any = HEAD +) -> tuple[str, list[str]]: + return run( + {"pull_request": {"number": 42, "head": {"sha": event_head}}}, + BASE, + api, + event_name="pull_request_target", + workflow_run_id="77", + workflow_attempt=None, + workflow_sha=MAIN_TIP, + ) + + +def test_pull_request_target_run_on_unprotected_change_passes() -> None: + api = PullRequestTargetApi(files=["README.md"]) + conclusion, errors = _run_pull_request_target(api) + assert conclusion == "success", errors + assert api.check_run is not None + assert api.check_run["head_sha"] == HEAD + + +def test_pull_request_target_run_on_unreviewed_protected_change_fails() -> None: + api = PullRequestTargetApi(files=["packages/flow/openadapt_flow/verify_run.py"]) + conclusion, errors = _run_pull_request_target(api) + assert conclusion == "failure" + assert not any("workflow run SHA" in error for error in errors) + assert any("review" in error for error in errors), errors + assert api.check_run is not None + assert api.check_run["head_sha"] == HEAD + assert api.check_run["conclusion"] == "failure" + + +@pytest.mark.parametrize("run_head_sha", [BASE, MAIN_TIP, "e" * 40]) +def test_pull_request_target_run_for_another_commit_fails(run_head_sha: str) -> None: + api = PullRequestTargetApi(run_head_sha=run_head_sha, files=["README.md"]) + conclusion, errors = _run_pull_request_target(api) + assert conclusion == "failure" + assert "workflow run SHA does not match the triggering event SHA" in errors + + +@pytest.mark.parametrize("event_head", [None, "", "b" * 39, "B" * 40]) +def test_pull_request_target_event_without_exact_head_never_passes( + event_head: Any, +) -> None: + api = PullRequestTargetApi(files=["README.md"]) + try: + conclusion, _errors = _run_pull_request_target(api, event_head) + except EvidenceError: + assert api.check_run is None + return + assert conclusion == "failure" + + +def test_comment_run_is_still_bound_to_the_workflow_sha() -> None: + api = FakeApi() + conclusion, errors = run( + {"issue": {"number": 42, "pull_request": {}}}, + BASE, + api, + event_name="issue_comment", + workflow_run_id="77", + workflow_sha=HEAD, + ) + assert conclusion == "failure" + assert "workflow run SHA does not match the triggering event SHA" in errors + + def test_first_api_failure_publishes_failure_for_known_pr_head() -> None: class FailedFirstRequestApi(FakeApi): def request(self, path: str, *, method: str = "GET", data: Any = None) -> Any: