From 1096218c3da24052f72307d8d1d8060eee46ca08 Mon Sep 17 00:00:00 2001 From: it-start <199481349+it-start@users.noreply.github.com> Date: Sun, 1 Nov 2026 13:39:29 +0000 Subject: [PATCH 1/2] fix(guardian): drop the gh dependency and bump action pins (#742) The reusable workflow shelled out to `gh` for the trigger reaction and the base-branch lookup, and github_poster.py posted the inline review with `gh api`; a self-hosted runner without the CLI failed on the second step. Both workflow steps now call the REST API through actions/github-script, and the poster POSTs over HTTPS with urllib and GH_TOKEN (GITHUB_TOKEN locally). An API rejection raises RuntimeError carrying GitHub's response body. Bump the guardian workflows to current, node24 action majors: checkout v7, setup-uv v10.3.3, github-script v9, create-or-update-comment v5, upload-artifact v7, create-github-app-token v3.3.0, download-artifact v8. The workflow notes the v2.327.1+ self-hosted runner requirement. Tests pin the absence of gh in both workflow steps and in guardian code, and hold every remote action pin to the current majors. README says that local --inline needs GH_TOKEN. Co-authored-by: zaebee <305761+zaebee@users.noreply.github.com> --- .github/workflows/guardian-reusable.yml | 57 ++++++--- .github/workflows/guardian.yml | 10 +- README.md | 4 +- src/cgis/guardian/github_poster.py | 63 ++++++++-- src/cgis/guardian/runner.py | 2 +- tests/unit/test_guardian_poster.py | 125 ++++++++++++++++---- tests/unit/test_guardian_workflow.py | 146 ++++++++++++++++++++++-- 7 files changed, 344 insertions(+), 63 deletions(-) diff --git a/.github/workflows/guardian-reusable.yml b/.github/workflows/guardian-reusable.yml index a36aee38..e0069648 100644 --- a/.github/workflows/guardian-reusable.yml +++ b/.github/workflows/guardian-reusable.yml @@ -25,6 +25,11 @@ name: Guardian Review (reusable) # (actions/*, astral-sh/setup-uv, peter-evans/create-or-update-comment). # Pin `uses:` and `tooling_ref` to the SAME commit SHA, never a tag: the # tooling runs on the caller's runner, and a tag can move. +# +# The pins are current majors and run on node24, so a self-hosted runner must +# be v2.327.1 or later (GitHub-hosted runners already are). The job needs no +# GitHub CLI: the reaction and the base-branch lookup are +# actions/github-script, and the poster talks to the REST API over HTTPS (#742). on: workflow_call: @@ -79,7 +84,7 @@ jobs: steps: # Here rather than in each caller, so no caller can forget it. - name: Check commenter has write access - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 with: script: | const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({ @@ -93,19 +98,23 @@ jobs: } - name: React to trigger comment - env: - GH_TOKEN: ${{ github.token }} - COMMENT_ID: ${{ github.event.comment.id }} - run: | - gh api repos/$GITHUB_REPOSITORY/issues/comments/$COMMENT_ID/reactions \ - -X POST -f content=eyes + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 + with: + # The comment the caller's `if` matched: `context.payload.comment`. + # The REST call, not `gh api` — a self-hosted runner need not have gh (#742). + script: | + await github.rest.reactions.createForIssueComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: context.payload.comment.id, + content: 'eyes', + }); - - name: Validate inputs and get base branch + - name: Validate inputs env: EVENT_NAME: ${{ github.event_name }} PR_NUMBER: ${{ inputs.pr_number }} SOURCE_ROOT: ${{ inputs.source_root }} - GH_TOKEN: ${{ github.token }} run: | if [ "$EVENT_NAME" != issue_comment ]; then echo "Call this on issue_comment (a /guardian review comment), not $EVENT_NAME" && exit 1 @@ -124,8 +133,20 @@ jobs: echo "source_root must be a plain path inside the repository: '$SOURCE_ROOT'" && exit 1 ;; esac echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV - BASE_BRANCH=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json baseRefName --jq '.baseRefName') - echo "BASE_BRANCH=$BASE_BRANCH" >> $GITHUB_ENV + + # Read through the REST API, not `gh pr view`: gh is not on every runner + # (#742). PR_NUMBER was validated as digits-only in the step above, so + # the number cannot reach the API malformed. + - name: Get base branch + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 + with: + script: | + const { data: pr } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: Number(process.env.PR_NUMBER), + }); + core.exportVariable('BASE_BRANCH', pr.base.ref); # The PR is data, never code (#730). Everything that runs — uv sync, the # guardian scripts, cgis itself — comes from codegraph-brain at @@ -133,7 +154,7 @@ jobs: # read and parsed. This step and the next keep the job's provider keys # out of reach of whoever wrote the PR. - name: Checkout trusted tooling (codegraph-brain) - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: repository: zaebee/codegraph-brain ref: ${{ inputs.tooling_ref }} @@ -149,7 +170,7 @@ jobs: } - name: Checkout PR branch as data - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: repository: ${{ github.repository }} # PR_NUMBER validated as digits-only in a previous step @@ -174,7 +195,7 @@ jobs: GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $cred" git fetch origin "$BASE_BRANCH" - name: Install uv - uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3 + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 - name: Install dependencies run: uv sync --frozen --extra guardian @@ -216,7 +237,7 @@ jobs: - name: Mint GitHub App token id: app-token if: vars.GUARDIAN_APP_CLIENT_ID != '' - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ vars.GUARDIAN_APP_CLIENT_ID }} private-key: ${{ secrets.GUARDIAN_APP_PRIVATE_KEY }} @@ -258,14 +279,14 @@ jobs: --base-branch "$BASE_BRANCH" - name: Upload metrics artifact - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: name: guardian-metrics-${{ env.PR_NUMBER }} path: guardian_metrics.jsonl retention-days: 7 - name: Upload finder recording artifact - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: name: guardian-finder-${{ env.PR_NUMBER }} path: guardian_finder.json @@ -273,7 +294,7 @@ jobs: - name: Post review as PR comment if: steps.guardian.outputs.posted_inline != 'true' - uses: peter-evans/create-or-update-comment@a111a2c3bacd7be7898ee22d0d71d9aec2bb972c # v4 + uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5 with: token: ${{ steps.app-token.outputs.token || github.token }} issue-number: ${{ env.PR_NUMBER }} diff --git a/.github/workflows/guardian.yml b/.github/workflows/guardian.yml index c69cea86..e221d225 100644 --- a/.github/workflows/guardian.yml +++ b/.github/workflows/guardian.yml @@ -35,13 +35,13 @@ jobs: steps: - name: Checkout main - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: ref: main fetch-depth: 0 - name: Download metrics artifact - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8 with: name: guardian-metrics-${{ github.event.issue.number }} @@ -75,7 +75,7 @@ jobs: steps: - name: Check commenter has write access - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 with: script: | const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({ @@ -104,13 +104,13 @@ jobs: echo "APPLIED=$APPLIED" >> $GITHUB_ENV - name: Checkout main - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: ref: main fetch-depth: 0 - name: Install uv - uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3 + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 - name: Install dependencies run: uv sync --frozen diff --git a/README.md b/README.md index 999f9930..eaac3e85 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,9 @@ The details — and a pipeline graph CGIS regenerates from its own source on eve * **Dogfooded & measured:** Guardian reviews CGIS's *own* pull requests, and a benchmark harness scores it against curated ground truth — so prompt changes are validated, not guessed. ```bash -# Build the graph, then review a PR with a local model (no API key) +# Build the graph, then review a PR with a local model (no API key). +# --inline posts over HTTPS with GH_TOKEN (or GITHUB_TOKEN); no gh CLI needed. +# The reusable workflow exports GH_TOKEN for you. cgis ingest ./src --output graph.db GUARDIAN_PROVIDER=ollama GUARDIAN_MODEL=qwen2.5-coder:14b \ uv run python scripts/guardian_review.py --pr 123 --db graph.db --inline diff --git a/src/cgis/guardian/github_poster.py b/src/cgis/guardian/github_poster.py index 9f2cbd51..cd044e3c 100644 --- a/src/cgis/guardian/github_poster.py +++ b/src/cgis/guardian/github_poster.py @@ -1,12 +1,24 @@ """Posts a ReviewResult as one GitHub review with inline comments (spec §6).""" import json -import subprocess +import os +import urllib.error +import urllib.request from cgis.guardian.findings import Finding, ReviewResult from cgis.guardian.render import render_inline_comment, render_review_body from cgis.guardian.skeptic import visible_findings +#: The REST API the workflow token writes to. A fixed host, not a variable: a +#: caller-supplied URL would send the token somewhere else. +_API_ROOT = "https://api.github.com" +#: Bounded so a stalled connection cannot hold the job past the app token's +#: lifetime; an inline review is a few kilobytes. +_TIMEOUT_S = 30 +#: GitHub rejects a request without one (403); urllib's default names only the +#: library, which says nothing useful in the audit log. +_USER_AGENT = "codegraph-brain-guardian" + #: Below this length a quote is too generic to substring-match safely — short #: keywords/patterns (``)``, ``else:``, ``self.``, ``return``) would collide with #: unrelated lines (``something_else:``, ``myself.foo``). Such a quote may only @@ -98,6 +110,23 @@ def build_review( return render_review_body(result, outside=outside, threshold=threshold) + footer, comments +def _token() -> str: + """The GitHub token, read from the environment the way the gh CLI reads it. + + GH_TOKEN is what the reusable workflow exports (the app token when one is + configured, else the caller's GITHUB_TOKEN); GITHUB_TOKEN is the fallback + for a local run of the script. + """ + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") + if not token: + _msg = ( + "No GitHub token: set GH_TOKEN (or GITHUB_TOKEN) to post the review. " + "The reusable workflow exports GH_TOKEN for exactly this call." + ) + raise RuntimeError(_msg) + return token + + def post_inline_review( *, repo: str, @@ -109,11 +138,13 @@ def post_inline_review( diff_content: dict[str, dict[int, str]] | None = None, threshold: int = 0, ) -> None: - """POST one COMMENT review via `gh api` (auto-auth in Actions, spec §6.4). + """POST one COMMENT review over plain HTTPS (spec §6.4). Always event=COMMENT, never REQUEST_CHANGES — guardian is an advisor, - not a gate. Raises CalledProcessError on API rejection; the caller - decides the fallback (spec §6.5). + not a gate. The request needs no GitHub CLI: the token comes from the + environment and urllib sends the request, so a self-hosted runner needs + neither gh nor its config (#742). Raises RuntimeError on API rejection; + the caller decides the fallback (spec §6.5). """ body, comments = build_review( result, @@ -124,10 +155,22 @@ def post_inline_review( threshold=threshold, ) payload = {"event": "COMMENT", "body": body, "comments": comments} - subprocess.run( - ["gh", "api", f"repos/{repo}/pulls/{pr}/reviews", "-X", "POST", "--input", "-"], - input=json.dumps(payload), - text=True, - check=True, - capture_output=True, + request = urllib.request.Request( + f"{_API_ROOT}/repos/{repo}/pulls/{pr}/reviews", + data=json.dumps(payload).encode("utf-8"), + headers={ + "Authorization": f"Bearer {_token()}", + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "Content-Type": "application/json", + "User-Agent": _USER_AGENT, + }, + method="POST", ) + try: + with urllib.request.urlopen(request, timeout=_TIMEOUT_S) as response: + response.read() + except urllib.error.HTTPError as exc: + detail = exc.read().decode("utf-8", "replace")[:1000] + _msg = f"GitHub rejected the inline review ({exc.code} {exc.reason}): {detail}" + raise RuntimeError(_msg) from exc diff --git a/src/cgis/guardian/runner.py b/src/cgis/guardian/runner.py index 132495f2..8d340524 100644 --- a/src/cgis/guardian/runner.py +++ b/src/cgis/guardian/runner.py @@ -568,7 +568,7 @@ async def run_guardian( try: diff_text = collector.source.get_git_diff() content = diff_line_content(diff_text) - await asyncio.to_thread( # subprocess `gh api` call — keep the loop responsive + await asyncio.to_thread( # blocking HTTPS call — keep the loop responsive post_inline_review, repo=inline_repo, pr=pr, diff --git a/tests/unit/test_guardian_poster.py b/tests/unit/test_guardian_poster.py index 8a43c3c4..da2b0a34 100644 --- a/tests/unit/test_guardian_poster.py +++ b/tests/unit/test_guardian_poster.py @@ -1,7 +1,13 @@ -"""Unit tests for the GitHub inline-review poster (mocked subprocess, spec §6.4).""" +"""Unit tests for the GitHub inline-review poster (mocked HTTP, spec §6.4).""" +import io import json -from unittest.mock import patch +import os +import urllib.error +import urllib.request +from unittest.mock import MagicMock, patch + +import pytest from cgis.guardian.findings import Finding, ReviewResult from cgis.guardian.github_poster import build_review, post_inline_review @@ -30,6 +36,27 @@ ) _REFUTED = _IN_DIFF.model_copy(update={"verdict": "refuted", "title": "dead"}) +_REVIEW_URL = "https://api.github.com/repos/zaebee/codegraph-brain/pulls/153/reviews" + + +def _post(result: ReviewResult, *, pr: int = 153, footer: str = "") -> urllib.request.Request: + """Post through a stand-in for urlopen and return the request it received.""" + with ( + patch.dict(os.environ, {"GH_TOKEN": "tok"}), + patch("cgis.guardian.github_poster.urllib.request.urlopen") as mock_open, + ): + mock_open.return_value.__enter__.return_value = MagicMock() + post_inline_review( + repo="zaebee/codegraph-brain", + pr=pr, + result=result, + diff_index={"src/x.py": {11}}, + skeptic_model=None, + footer=footer, + ) + request: urllib.request.Request = mock_open.call_args.args[0] + return request + def test_build_review_splits_inline_and_outside() -> None: """In-index findings become comments; others go to the body; refuted vanish.""" @@ -56,9 +83,81 @@ def test_build_review_line_none_goes_to_body() -> None: def test_post_inline_review_payload() -> None: - """The gh api call posts one COMMENT review with the built payload on stdin.""" + """One COMMENT review, sent as JSON to the reviews endpoint (#742).""" + request = _post(ReviewResult(findings=[_IN_DIFF], summary="s")) + assert request.full_url == _REVIEW_URL + assert request.get_method() == "POST" + payload = json.loads(request.data) + assert payload["event"] == "COMMENT" + assert payload["comments"][0]["line"] == 11 + + +def test_post_inline_review_authenticates_with_the_workflow_token() -> None: + """The bearer token and API headers the workflow exports; no gh config is read.""" + request = _post(ReviewResult(findings=[_IN_DIFF], summary="s")) + headers = {key.lower(): value for key, value in request.header_items()} + assert headers["authorization"] == "Bearer tok" + assert headers["accept"] == "application/vnd.github+json" + assert headers["content-type"] == "application/json" + assert headers["x-github-api-version"] == "2022-11-28" + assert headers["user-agent"] + + +def test_post_inline_review_falls_back_to_the_actions_token( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """GITHUB_TOKEN is accepted where GH_TOKEN is unset, as gh itself accepts both.""" + monkeypatch.delenv("GH_TOKEN", raising=False) + monkeypatch.setenv("GITHUB_TOKEN", "actions") + with patch("cgis.guardian.github_poster.urllib.request.urlopen") as mock_open: + mock_open.return_value.__enter__.return_value = MagicMock() + post_inline_review( + repo="zaebee/codegraph-brain", + pr=153, + result=ReviewResult(findings=[_IN_DIFF], summary="s"), + diff_index={"src/x.py": {11}}, + skeptic_model=None, + ) + request = mock_open.call_args.args[0] + assert {key.lower(): value for key, value in request.header_items()}["authorization"] == ( + "Bearer actions" + ) + + +def test_post_without_a_token_fails_before_sending(monkeypatch: pytest.MonkeyPatch) -> None: + """No token is a configuration error, reported without a network round-trip.""" + monkeypatch.delenv("GH_TOKEN", raising=False) + monkeypatch.delenv("GITHUB_TOKEN", raising=False) result = ReviewResult(findings=[_IN_DIFF], summary="s") - with patch("cgis.guardian.github_poster.subprocess.run") as mock_run: + with ( + patch("cgis.guardian.github_poster.urllib.request.urlopen") as mock_open, + pytest.raises(RuntimeError, match="GH_TOKEN"), + ): + post_inline_review( + repo="zaebee/codegraph-brain", + pr=153, + result=result, + diff_index={"src/x.py": {11}}, + skeptic_model=None, + ) + mock_open.assert_not_called() + + +def test_api_rejection_reports_the_response_body(monkeypatch: pytest.MonkeyPatch) -> None: + """A non-2xx carries GitHub's message into the exception the runner logs.""" + monkeypatch.setenv("GH_TOKEN", "tok") + rejected = urllib.error.HTTPError( + _REVIEW_URL, + 422, + "Unprocessable Entity", + {}, + io.BytesIO(b'{"message":"line not in diff"}'), + ) + result = ReviewResult(findings=[_IN_DIFF], summary="s") + with ( + patch("cgis.guardian.github_poster.urllib.request.urlopen", side_effect=rejected), + pytest.raises(RuntimeError, match="line not in diff"), + ): post_inline_review( repo="zaebee/codegraph-brain", pr=153, @@ -66,12 +165,6 @@ def test_post_inline_review_payload() -> None: diff_index={"src/x.py": {11}}, skeptic_model=None, ) - args, kwargs = mock_run.call_args - assert args[0][:3] == ["gh", "api", "repos/zaebee/codegraph-brain/pulls/153/reviews"] - payload = json.loads(kwargs["input"]) - assert payload["event"] == "COMMENT" - assert payload["comments"][0]["line"] == 11 - assert kwargs["check"] is True def test_footer_rides_in_review_body() -> None: @@ -81,16 +174,8 @@ def test_footer_rides_in_review_body() -> None: footer = "\n\n---\n> 🤖 **gemini-2.5-flash** · 1,000 prompt" body, _ = build_review(result, diff_index={"src/x.py": {11}}, skeptic_model=None, footer=footer) assert body.endswith(footer) - with patch("cgis.guardian.github_poster.subprocess.run") as mock_run: - post_inline_review( - repo="zaebee/codegraph-brain", - pr=157, - result=result, - diff_index={"src/x.py": {11}}, - skeptic_model=None, - footer=footer, - ) - payload = json.loads(mock_run.call_args.kwargs["input"]) + request = _post(result, pr=157, footer=footer) + payload = json.loads(request.data) assert payload["body"].endswith(footer) diff --git a/tests/unit/test_guardian_workflow.py b/tests/unit/test_guardian_workflow.py index 6c3d75e6..767c29b3 100644 --- a/tests/unit/test_guardian_workflow.py +++ b/tests/unit/test_guardian_workflow.py @@ -7,6 +7,7 @@ nothing running from inside it but one fixed `git fetch`. """ +import ast import os import re import subprocess @@ -323,18 +324,147 @@ def test_cross_org_callers_can_pass_the_secrets(workflow: dict[Any, Any]) -> Non def test_the_inputs_are_checked_by_the_steps_own_shell( steps: list[dict[str, Any]], tmp_path: Path, event: str, source_root: str, ok: bool ) -> None: - """Run the validation step itself, with a stand-in `gh`, as the ingest test does.""" - validate = next(s for s in steps if s.get("name") == "Validate inputs and get base branch") - fake = tmp_path / "gh" - fake.write_text("#!/bin/sh\necho main\n") - fake.chmod(0o755) + """Run the validation step itself, as the ingest test does.""" + validate = next(s for s in steps if s.get("name") == "Validate inputs") out = tmp_path / "github_env" - env = {**os.environ, "PATH": f"{tmp_path}:{os.environ.get('PATH', '')}"} - env |= {"EVENT_NAME": event, "PR_NUMBER": "7", "SOURCE_ROOT": source_root} + env = {**os.environ, "EVENT_NAME": event, "PR_NUMBER": "7", "SOURCE_ROOT": source_root} env |= {"GITHUB_ENV": str(out), "GITHUB_REPOSITORY": "o/r"} done = subprocess.run( ["bash", "-c", str(validate["run"])], env=env, capture_output=True, text=True, check=False ) assert (done.returncode == 0) is ok if ok: - assert "BASE_BRANCH=main" in out.read_text() + assert "PR_NUMBER=7" in out.read_text() + + +# The job must not assume the GitHub CLI (#742). A self-hosted runner without +# `gh` has to reach the same result as a GitHub-hosted one; two steps shelled +# out to it and one module posted through it. These tests keep both free of it +# and hold the action pins to the current majors. + +#: A `gh` command word in a shell script: start, or after whitespace or a shell +#: separator, and followed by whitespace or end of line. `$GITHUB_…` has a `$` +#: before it, and `grep` does not start with `gh`. +_GH_COMMAND = re.compile(r"(?:^|[\s;|&(`])gh(?=\s|$)", re.M) + +#: Guardian library code (what the review job runs) plus the workflow's entry +#: script. `scripts/guardian_martian.py` is a developer bench tool, not part of +#: this job, and keeps its own `gh` calls. +_ROOT = Path(__file__).parent.parent.parent +GUARDIAN_SOURCES = [ + *sorted((_ROOT / "src" / "cgis" / "guardian").glob("*.py")), + _ROOT / "scripts" / "guardian_review.py", +] + +#: Ways Python can spawn a command; the scanned first argument names it. +_SPAWNERS = { + "os": {"system", "popen"}, + "subprocess": { + "run", + "call", + "check_call", + "check_output", + "Popen", + "getoutput", + "getstatusoutput", + }, + "asyncio": {"create_subprocess_exec", "create_subprocess_shell"}, +} + + +def _names_gh(word: str) -> bool: + """Whether a command argument's first word is `gh` (or a path to it).""" + words = word.split() + return bool(words) and PurePosixPath(words[0]).name == "gh" + + +def _spawns_gh(source: Path) -> list[int]: + """Line numbers of calls in `source` that spawn `gh`.""" + found: list[int] = [] + tree = ast.parse(source.read_text(encoding="utf-8")) + for node in ast.walk(tree): + if not isinstance(node, ast.Call): + continue + func = node.func + if not isinstance(func, ast.Attribute) or not isinstance(func.value, ast.Name): + continue + if func.attr not in _SPAWNERS.get(func.value.id, set()) or not node.args: + continue + first = node.args[0] + words: list[str] = [] + if isinstance(first, ast.Constant) and isinstance(first.value, str): + words = [first.value] + elif isinstance(first, (ast.List, ast.Tuple)): + words = [ + element.value + for element in first.elts + if isinstance(element, ast.Constant) and isinstance(element.value, str) + ] + if any(_names_gh(word) for word in words): + found.append(node.lineno) + return found + + +@pytest.mark.parametrize("source", GUARDIAN_SOURCES, ids=lambda path: path.name) +def test_guardian_code_never_spawns_gh(source: Path) -> None: + """The library and the workflow entry script must run without the CLI installed.""" + assert _spawns_gh(source) == [] + + +def test_no_workflow_step_shells_out_to_gh( + workflow: dict[Any, Any], caller: dict[Any, Any] +) -> None: + """A `run:` block that calls gh fails on a runner that does not have it.""" + for name, document in (("guardian-reusable.yml", workflow), ("guardian.yml", caller)): + for job in document["jobs"].values(): + for step in job.get("steps", []): + run = str(step.get("run", "")) + assert not _GH_COMMAND.search(run), (name, step.get("name")) + + +def test_the_reaction_and_base_branch_go_through_github_script(steps: list[dict[str, Any]]) -> None: + """The two steps that used gh now call the REST API from actions/github-script.""" + reaction = next(s for s in steps if s.get("name") == "React to trigger comment") + assert reaction["uses"].startswith("actions/github-script@") + assert "createForIssueComment" in reaction["with"]["script"] + assert "context.payload.comment.id" in reaction["with"]["script"] + + base = next(s for s in steps if s.get("name") == "Get base branch") + assert base["uses"].startswith("actions/github-script@") + script = base["with"]["script"] + assert "github.rest.pulls.get" in script + assert "exportVariable('BASE_BRANCH'" in script + + validate = next(s for s in steps if s.get("name") == "Validate inputs") + fetch = next(s for s in steps if s.get("name") == "Fetch base branch for diff") + assert steps.index(validate) < steps.index(base) < steps.index(fetch) + + +# The pins below are the current majors at the time of #742 — the previous ones +# (checkout v4, setup-uv v3, github-script v7, create-or-update-comment v4, +# upload/download-artifact v4) ran on node20. A bump updates this table with the +# pin, so the majors cannot silently regress. +CURRENT_PINS = { + "actions/checkout": "3d3c42e5aac5ba805825da76410c181273ba90b1", + "astral-sh/setup-uv": "68559e99cc807c1eb7e9238fd43a15bba460eec2", + "actions/github-script": "3a2844b7e9c422d3c10d287c895573f7108da1b3", + "actions/upload-artifact": "cf430e030ddbb5b0abf93d22962f4752f3646cd9", + "actions/download-artifact": "9000827ccba6bdab643e8b6fd33ac0654aef8333", + "actions/create-github-app-token": "64ab729a6294000f5aa4a4bc0b73d1f32bc45510", + "peter-evans/create-or-update-comment": "e8674b075228eee787fea43ef493e45ece1004c9", +} + + +def test_the_guardian_action_pins_are_current_majors( + workflow: dict[Any, Any], caller: dict[Any, Any] +) -> None: + """Every remote action is SHA-pinned, and pinned to the node24 majors.""" + for name, document in (("guardian-reusable.yml", workflow), ("guardian.yml", caller)): + for job in document["jobs"].values(): + for step in job.get("steps", []): + uses = str(step.get("uses", "")) + if not uses or uses.startswith(("./", "docker://")): + continue + action, _, sha = uses.partition("@") + assert action in CURRENT_PINS, (name, uses) + assert sha == CURRENT_PINS[action], (name, uses) From 926e23cc6d4d7ff528d22f4479da01265d9ed99e Mon Sep 17 00:00:00 2001 From: it-start <199481349+it-start@users.noreply.github.com> Date: Sun, 1 Nov 2026 13:42:13 +0000 Subject: [PATCH 2/2] test(guardian): cover gh import styles and strip token whitespace Review follow-up on #773: the AST scan now sees direct imports and aliases (from subprocess import run as spawn, import subprocess as sp), with cases pinning each spelling; a token pasted with surrounding whitespace is stripped before it reaches the Authorization header. Co-authored-by: zaebee <305761+zaebee@users.noreply.github.com> --- src/cgis/guardian/github_poster.py | 5 ++- tests/unit/test_guardian_poster.py | 14 ++++++- tests/unit/test_guardian_workflow.py | 57 +++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/cgis/guardian/github_poster.py b/src/cgis/guardian/github_poster.py index cd044e3c..8682922e 100644 --- a/src/cgis/guardian/github_poster.py +++ b/src/cgis/guardian/github_poster.py @@ -115,9 +115,10 @@ def _token() -> str: GH_TOKEN is what the reusable workflow exports (the app token when one is configured, else the caller's GITHUB_TOKEN); GITHUB_TOKEN is the fallback - for a local run of the script. + for a local run of the script. Surrounding whitespace is stripped: a token + pasted with a trailing space or newline must not become an auth failure. """ - token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") + token = (os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") or "").strip() if not token: _msg = ( "No GitHub token: set GH_TOKEN (or GITHUB_TOKEN) to post the review. " diff --git a/tests/unit/test_guardian_poster.py b/tests/unit/test_guardian_poster.py index da2b0a34..b8b327fd 100644 --- a/tests/unit/test_guardian_poster.py +++ b/tests/unit/test_guardian_poster.py @@ -39,10 +39,12 @@ _REVIEW_URL = "https://api.github.com/repos/zaebee/codegraph-brain/pulls/153/reviews" -def _post(result: ReviewResult, *, pr: int = 153, footer: str = "") -> urllib.request.Request: +def _post( + result: ReviewResult, *, pr: int = 153, footer: str = "", token: str = "tok" +) -> urllib.request.Request: """Post through a stand-in for urlopen and return the request it received.""" with ( - patch.dict(os.environ, {"GH_TOKEN": "tok"}), + patch.dict(os.environ, {"GH_TOKEN": token}), patch("cgis.guardian.github_poster.urllib.request.urlopen") as mock_open, ): mock_open.return_value.__enter__.return_value = MagicMock() @@ -103,6 +105,14 @@ def test_post_inline_review_authenticates_with_the_workflow_token() -> None: assert headers["user-agent"] +def test_a_token_pasted_with_whitespace_still_authenticates() -> None: + """A trailing space or newline from a copy-paste must not become an auth failure.""" + request = _post(ReviewResult(findings=[_IN_DIFF], summary="s"), token=" tok \n") + assert {key.lower(): value for key, value in request.header_items()}["authorization"] == ( + "Bearer tok" + ) + + def test_post_inline_review_falls_back_to_the_actions_token( monkeypatch: pytest.MonkeyPatch, ) -> None: diff --git a/tests/unit/test_guardian_workflow.py b/tests/unit/test_guardian_workflow.py index 767c29b3..33d56469 100644 --- a/tests/unit/test_guardian_workflow.py +++ b/tests/unit/test_guardian_workflow.py @@ -378,17 +378,44 @@ def _names_gh(word: str) -> bool: return bool(words) and PurePosixPath(words[0]).name == "gh" +def _spawner_aliases(tree: ast.Module) -> tuple[dict[str, str], set[str]]: + """How a source names its spawn functions. + + Returns the imported module aliases (`import subprocess as sp`) and the + directly imported spawner names (`from subprocess import run as spawn`), so + the scan does not depend on how the call is spelled. + """ + modules: dict[str, str] = {} + direct: set[str] = set() + for node in ast.walk(tree): + if isinstance(node, ast.Import): + for alias in node.names: + if alias.name in _SPAWNERS: + modules[alias.asname or alias.name] = alias.name + elif isinstance(node, ast.ImportFrom) and node.module in _SPAWNERS: + for alias in node.names: + if alias.name in _SPAWNERS[node.module]: + direct.add(alias.asname or alias.name) + return modules, direct + + +def _is_spawner(func: ast.expr, modules: dict[str, str], direct: set[str]) -> bool: + """Whether a call target is one of the spawn functions.""" + if isinstance(func, ast.Attribute) and isinstance(func.value, ast.Name): + module = modules.get(func.value.id, func.value.id) + return func.attr in _SPAWNERS.get(module, set()) + return isinstance(func, ast.Name) and func.id in direct + + def _spawns_gh(source: Path) -> list[int]: """Line numbers of calls in `source` that spawn `gh`.""" found: list[int] = [] tree = ast.parse(source.read_text(encoding="utf-8")) + modules, direct = _spawner_aliases(tree) for node in ast.walk(tree): - if not isinstance(node, ast.Call): - continue - func = node.func - if not isinstance(func, ast.Attribute) or not isinstance(func.value, ast.Name): + if not isinstance(node, ast.Call) or not _is_spawner(node.func, modules, direct): continue - if func.attr not in _SPAWNERS.get(func.value.id, set()) or not node.args: + if not node.args: continue first = node.args[0] words: list[str] = [] @@ -405,6 +432,26 @@ def _spawns_gh(source: Path) -> list[int]: return found +@pytest.mark.parametrize( + ("source", "spawned"), + [ + ("import subprocess\nsubprocess.run(['gh', 'api'])", [2]), + ("from subprocess import run\nrun(['gh', 'api'])", [2]), + ("from subprocess import run as spawn\nspawn(['/usr/bin/gh', 'api'])", [2]), + ("import subprocess as sp\nsp.run(['gh'])", [2]), + ("from os import system\nsystem('gh pr view')", [2]), + ("import subprocess\nsubprocess.run(['git', 'fetch'])", []), + ], +) +def test_the_gh_scanner_sees_through_import_styles( + source: str, spawned: list[int], tmp_path: Path +) -> None: + """A direct import or an alias must not hide a spawn from the scan.""" + path = tmp_path / "probe.py" + path.write_text(source, encoding="utf-8") + assert _spawns_gh(path) == spawned + + @pytest.mark.parametrize("source", GUARDIAN_SOURCES, ids=lambda path: path.name) def test_guardian_code_never_spawns_gh(source: Path) -> None: """The library and the workflow entry script must run without the CLI installed."""