From faec0dc3e0d20bbb49954210e7a25381e7da9585 Mon Sep 17 00:00:00 2001 From: huang47 <157390+huang47@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:55:08 +0800 Subject: [PATCH] fix: reject malformed review handoff labels --- .github/cursor-review/auto-approve.py | 5 ++++- .../cursor-review/tests/test_auto_approve.py | 22 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.github/cursor-review/auto-approve.py b/.github/cursor-review/auto-approve.py index 98ded56d..d3ae6fff 100644 --- a/.github/cursor-review/auto-approve.py +++ b/.github/cursor-review/auto-approve.py @@ -4269,7 +4269,10 @@ def cmd_round_cap(args) -> int: try: # For the `approve_gate` output only: a PR a human already handed to a # human reports `capped` even when auto-approve (and so decide) is off. - if has_label(read_pr(args.repo, args.pr_number), HUMAN_REVIEW_LABEL): + pr = read_pr(args.repo, args.pr_number) + if strict and not live_labels_readable(pr): + raise ValueError("the PR label list is missing or malformed") + if has_label(pr, HUMAN_REVIEW_LABEL): set_output("labelled", "true") except (RuntimeError, ValueError) as e: print(f"::warning::Could not read this PR's labels: {e}") diff --git a/.github/cursor-review/tests/test_auto_approve.py b/.github/cursor-review/tests/test_auto_approve.py index 69626b77..874aa986 100644 --- a/.github/cursor-review/tests/test_auto_approve.py +++ b/.github/cursor-review/tests/test_auto_approve.py @@ -1907,7 +1907,8 @@ class RoundCapCommandTest(unittest.TestCase): """The cap path end to end, with gh stubbed by endpoint.""" def run_cap(self, reviews, max_rounds="5", timeline=(), comments=(), label_exists=True, fail=(), labels=(), - probe_error="HTTP 404", strict=False, count_superseded=False, preserve=False, after_review=False): + probe_error="HTTP 404", strict=False, count_superseded=False, preserve=False, after_review=False, + pr_payload=None): writes = [] def fake_gh(args, payload=None): @@ -1937,7 +1938,7 @@ def fake_gh(args, payload=None): raise RuntimeError(probe_error) return "{}" if path == "repos/o/r/pulls/1": - return json.dumps({"labels": [{"name": n} for n in labels]}) + return json.dumps(pr_payload if pr_payload is not None else {"labels": [{"name": n} for n in labels]}) raise AssertionError(path) with tempfile.TemporaryDirectory() as d: @@ -2069,6 +2070,23 @@ def test_strict_cap_allows_two_and_hands_off_at_three(self): rc, out, _ = self.run_cap(self.FIVE[:3], max_rounds="3", strict=True) self.assertEqual((rc, out["capped"], out["rounds"]), (0, "true", "3")) + def test_fail_closed_rejects_malformed_label_payloads(self): + for payload in ({}, {"labels": None}, {"labels": "bad"}, {"labels": [{"name": 4}]}): + with self.subTest(payload=payload): + rc, _, writes = self.run_cap(self.FIVE[:2], max_rounds="3", strict=True, pr_payload=payload) + self.assertNotEqual(rc, 0) + self.assertFalse(writes) + + def test_default_policy_keeps_legacy_behavior_for_missing_labels(self): + rc, out, writes = self.run_cap(self.FIVE[:2], max_rounds="3", pr_payload={}) + self.assertEqual((rc, out["capped"], out["rounds"], writes), (0, "false", "2", [])) + + def test_after_review_marks_handoff_when_label_payload_is_malformed(self): + rc, _, writes = self.run_cap(self.FIVE[:3], max_rounds="3", after_review=True, + pr_payload={"labels": None}) + self.assertNotEqual(rc, 0) + self.assertIn(("POST", "repos/o/r/issues/1/labels"), [(m, p) for m, p, _ in writes]) + def test_strict_cap_stops_if_labels_cannot_be_read(self): rc, _, _ = self.run_cap(self.FIVE[:2], max_rounds="3", fail=("repos/o/r/pulls/1$",), strict=True) self.assertNotEqual(rc, 0)