harden(pilot): contain hostile PR content, bound the webhook, fix anchoring
The reviewer runs an opencode agent with `bash: "*": allow` over a checkout of the PR author's branch, and the pod holds a Gitea Write credential. Those two facts had no wall between them. Security - _build_env now allow-lists the subprocess environment instead of inheriting it, so PRAGENT_BOT_TOKEN and WEBHOOK_SECRET never reach the agent. This was the live hole: a PR body or an AGENTS.md could ask the agent to `curl` the token out, and it had both the value and the tool. - sanitize_workdir deletes author-controlled agent-instruction files from the checkout before opencode starts (AGENTS.md at any depth, CLAUDE.md, .cursorrules, a repo opencode.json/.opencode, copilot-instructions.md). opencode loads nested AGENTS.md as instructions, so a PR could otherwise ship its own system prompt. They are still reviewed, as data. - The brief fences PR title/body and diff in --- UNTRUSTED --- markers under a trust-boundary preamble; the pragent agent, the three lens subagents and the review-methodology skill now treat injection attempts as a critical finding to report rather than an instruction to obey. - .pr-review.json is read from the PR's base branch, not the head sha. Its `instructions` field is spliced into the reviewer's prompt, so head-ref reading let any author rewrite the reviewer's rules. Fields are length-capped. - Untar rejects escaping symlinks, parent traversal, and writes through a planted symlink (tar-slip). - The image runs as uid 10001 instead of root. Robustness - Bounded review concurrency (PRAGENT_MAX_CONCURRENT_REVIEWS, default 2). Each review forks an opencode process; a thread per delivery was a fork bomb on a burst of labels or Gitea retries. - An in-flight (repo, index, sha) claim closes the check-then-act race in the sha-marker dedupe, where two deliveries a second apart both read "not yet reviewed" and both posted. - Request bodies are capped before being read into memory. Correctness - parse_diff_anchors counts a whitespace-stripped blank context line. Skipping it desynced the new-line counter for the rest of the hunk and silently misplaced every later inline comment in that file. - post_inline_review's body-only fallback folds the anchored findings into the body. It previously posted a summary saying "N inline comment(s) below" with no comments and no findings — losing them all on the one path that matters. - fetch_pr_diff's files-endpoint fallback emits real a// b/ prefixes (so changed_files and the anchor parser work on it) and reports both HTTP statuses in its error instead of the same one twice. - The CI workflow template pins PRAGENT_ENGINE=ollama; review_pr defaults to opencode, which does not exist on a Gitea Actions runner. Tests: 68 -> 101, covering each of the above. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B11e8TZZxJyzHW7jj7KWUN
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
"""Unit tests for pragent pilot pure helpers. No network."""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
@@ -7,6 +9,7 @@ HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.abspath(os.path.join(HERE, "..", ".."))
|
||||
sys.path.insert(0, os.path.join(ROOT, "pilot"))
|
||||
|
||||
import ai_review # noqa: E402
|
||||
from ai_review import ( # noqa: E402
|
||||
build_user_prompt,
|
||||
compute_attribution,
|
||||
@@ -548,4 +551,189 @@ def test_format_review_body_usage_section_between_summary_and_findings():
|
||||
|
||||
def test_format_review_body_no_usage_section_omitted():
|
||||
body = format_review_body("- [high] x:1 — b", "glm-5.2:cloud", "abcdef1234567890")
|
||||
assert "AI usage" not in body
|
||||
assert "AI usage" not in body
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# parse_diff_anchors — empty context lines
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_parse_diff_anchors_counts_empty_context_line():
|
||||
# A context line that is *blank* arrives as "" when trailing whitespace was
|
||||
# stripped somewhere upstream. If it isn't counted, every later line in the
|
||||
# hunk is off by one.
|
||||
diff = (
|
||||
"diff --git a/x.py b/x.py\n"
|
||||
"--- a/x.py\n"
|
||||
"+++ b/x.py\n"
|
||||
"@@ -1,4 +1,5 @@\n"
|
||||
" import os\n"
|
||||
"\n" # blank context line, whitespace stripped
|
||||
" def f():\n"
|
||||
"+ return 1\n"
|
||||
" # tail\n"
|
||||
)
|
||||
anchors = parse_diff_anchors(diff)
|
||||
assert anchors["x.py"] == {1, 2, 3, 4, 5}
|
||||
|
||||
|
||||
def test_parse_diff_anchors_space_prefixed_blank_line_still_counts():
|
||||
diff = (
|
||||
"+++ b/y.py\n"
|
||||
"@@ -1,3 +1,4 @@\n"
|
||||
" a\n"
|
||||
" \n" # properly space-prefixed blank context line
|
||||
"+b\n"
|
||||
" c\n"
|
||||
)
|
||||
assert parse_diff_anchors(diff)["y.py"] == {1, 2, 3, 4}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# parse_repo_config — caps
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_parse_repo_config_caps_instructions_length():
|
||||
cfg = parse_repo_config(json.dumps({"instructions": "x" * 99999}))
|
||||
assert len(cfg["instructions"]) == ai_review.CONFIG_MAX_INSTRUCTIONS_CHARS
|
||||
|
||||
|
||||
def test_parse_repo_config_caps_list_length_and_items():
|
||||
cfg = parse_repo_config(json.dumps({
|
||||
"focus": ["a" * 9999] * 500,
|
||||
"exclude_paths": ["vendor/**"],
|
||||
}))
|
||||
assert len(cfg["focus"]) == ai_review.CONFIG_MAX_LIST_ITEMS
|
||||
assert all(len(x) == ai_review.CONFIG_MAX_ITEM_CHARS for x in cfg["focus"])
|
||||
assert cfg["exclude_paths"] == ["vendor/**"]
|
||||
|
||||
|
||||
def test_parse_repo_config_still_accepts_normal_config():
|
||||
cfg = parse_repo_config(json.dumps({
|
||||
"focus": ["security"], "languages": ["go"], "instructions": "No bare throw.",
|
||||
}))
|
||||
assert cfg == {"focus": ["security"], "languages": ["go"], "instructions": "No bare throw."}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# fetch_repo_config — reads the BASE ref, never the PR head
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _stub_config_response(payload: dict):
|
||||
blob = base64.b64encode(json.dumps(payload).encode()).decode()
|
||||
return 200, json.dumps({"content": blob}).encode()
|
||||
|
||||
|
||||
def test_fetch_repo_config_uses_given_base_ref(monkeypatch):
|
||||
seen = {}
|
||||
|
||||
def fake_get(api, repo, path, token, accept="application/json"):
|
||||
seen["path"] = path
|
||||
return _stub_config_response({"focus": ["security"]})
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_get", fake_get)
|
||||
cfg = ai_review.fetch_repo_config("http://g", "o/r", "tok", ref="main")
|
||||
assert cfg == {"focus": ["security"]}
|
||||
assert seen["path"] == "contents/.pr-review.json?ref=main"
|
||||
|
||||
|
||||
def test_fetch_repo_config_without_ref_omits_ref_param(monkeypatch):
|
||||
seen = {}
|
||||
|
||||
def fake_get(api, repo, path, token, accept="application/json"):
|
||||
seen["path"] = path
|
||||
return _stub_config_response({})
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_get", fake_get)
|
||||
ai_review.fetch_repo_config("http://g", "o/r", "tok")
|
||||
assert "?ref=" not in seen["path"]
|
||||
|
||||
|
||||
def test_fetch_repo_config_quotes_ref_with_slashes(monkeypatch):
|
||||
seen = {}
|
||||
|
||||
def fake_get(api, repo, path, token, accept="application/json"):
|
||||
seen["path"] = path
|
||||
return _stub_config_response({})
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_get", fake_get)
|
||||
ai_review.fetch_repo_config("http://g", "o/r", "tok", ref="release/v1 x")
|
||||
assert "release%2Fv1%20x" in seen["path"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# post_inline_review — degraded fallback must not lose findings
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_post_inline_review_fallback_keeps_anchored_findings(monkeypatch):
|
||||
posted = []
|
||||
|
||||
def fake_post(api, repo, path, token, body):
|
||||
posted.append((path, body))
|
||||
# Reject the inline review, accept the plain one.
|
||||
if "reviews" in path and body.get("comments"):
|
||||
return 422, b"bad line"
|
||||
return 201, b"{}"
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_post", fake_post)
|
||||
anchored = [{
|
||||
"severity": "high", "path": "a.py", "line": 7,
|
||||
"problem": "off-by-one", "fix": "use <=", "suggestion": "", "reference": "",
|
||||
}]
|
||||
ai_review.post_inline_review("http://g", "o/r", "1", "tok", "SUMMARY", anchored)
|
||||
|
||||
final_body = posted[-1][1]["body"]
|
||||
assert "off-by-one" in final_body
|
||||
assert "a.py:7" in final_body
|
||||
assert "SUMMARY" in final_body
|
||||
|
||||
|
||||
def test_post_inline_review_success_posts_no_fallback(monkeypatch):
|
||||
posted = []
|
||||
|
||||
def fake_post(api, repo, path, token, body):
|
||||
posted.append(path)
|
||||
return 201, b"{}"
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_post", fake_post)
|
||||
ai_review.post_inline_review("http://g", "o/r", "1", "tok", "S", [])
|
||||
assert len(posted) == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# fetch_pr_diff — files-endpoint fallback
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_fetch_pr_diff_fallback_emits_git_style_prefixes(monkeypatch):
|
||||
def fake_get(api, repo, path, token, accept="application/json"):
|
||||
if path.endswith(".diff"):
|
||||
return 404, b"nope"
|
||||
return 200, json.dumps([
|
||||
{"filename": "src/a.py", "patch": "@@ -1 +1,2 @@\n a\n+b"},
|
||||
]).encode()
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_get", fake_get)
|
||||
diff, truncated, _ = ai_review.fetch_pr_diff("http://g", "o/r", "1", "tok", 10000)
|
||||
assert "--- a/src/a.py" in diff
|
||||
assert "+++ b/src/a.py" in diff
|
||||
assert truncated is False
|
||||
# And the synthesized diff must actually anchor.
|
||||
assert parse_diff_anchors(diff)["src/a.py"] == {1, 2}
|
||||
|
||||
|
||||
def test_fetch_pr_diff_error_reports_both_statuses(monkeypatch):
|
||||
def fake_get(api, repo, path, token, accept="application/json"):
|
||||
return (404, b"") if path.endswith(".diff") else (500, b"")
|
||||
|
||||
monkeypatch.setattr(ai_review, "gitea_get", fake_get)
|
||||
try:
|
||||
ai_review.fetch_pr_diff("http://g", "o/r", "1", "tok", 10000)
|
||||
except RuntimeError as e:
|
||||
assert ".diff=404" in str(e)
|
||||
assert "files=500" in str(e)
|
||||
else:
|
||||
raise AssertionError("expected RuntimeError")
|
||||
|
||||
Reference in New Issue
Block a user