Coverage for src/governed_harness/review/checks.py: 99%
102 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
1"""The harness's own deterministic review checks (``verified_by: tool:harness:CHECK``, #57).
3They read the changed lines of the diff, never call a model and cost no token. Each one is
4language-neutral: test assertions that cannot fail, gates turned green in scripts and CI files,
5dangerous path operations, predictable temporary files, credentials (the secret scanner of the
6independent review) and, since #5, instructions addressed to an agent inside changed content."""
8from __future__ import annotations
10import re
11from collections.abc import Callable, Iterable
12from dataclasses import dataclass
13from typing import Protocol
15from governed_harness.checks.model import DiffLine, is_test_path
16from governed_harness.checks.secrets import scan_secrets
17from governed_harness.review.diff import FileChange
18from governed_harness.review.signals import is_doc_path, is_pipeline_path
20_QUOTE = 160
23@dataclass(frozen=True)
24class CheckHit:
25 check: str
26 path: str
27 side: str
28 line: int
29 message: str
30 evidence: str
33def _quote(text: str) -> str:
34 text = " ".join(text.split())
35 return text if len(text) <= _QUOTE else text[: _QUOTE - 3] + "..."
38_TAUTOLOGIES = (
39 re.compile(r"^\s*assert\s+(?:True|1)\s*(?:,|#|$)"),
40 re.compile(r"\bassert\s*\(\s*(?:true|1)\s*\)"),
41 re.compile(
42 r"\bexpect\s*\(\s*(true|false|\d+|null)\s*\)\s*\.\s*to(?:Be|Equal|StrictEqual)\s*\(\s*\1\s*\)"
43 ),
44 re.compile(r"\bassert(?:True|That)?\s*\(\s*true\s*\)"),
45 re.compile(r"\bXCTAssert(?:True)?\s*\(\s*true\s*\)"),
46 re.compile(r"\bassert(?:Equals?|_eq!?|Same)\s*\(\s*([A-Za-z_][\w.]*)\s*,\s*\1\s*\)"),
47 re.compile(r"^\s*assert\s+([A-Za-z_][\w.]*)\s*(?:==|is)\s*\1\s*(?:,|#|$)"),
48 re.compile(
49 r"\bexpect\s*\(\s*([A-Za-z_][\w.]*)\s*\)\s*\.\s*to(?:Be|Equal|StrictEqual)\s*\(\s*\1\s*\)"
50 ),
51)
54def tautological_assertions(files: Iterable[FileChange]) -> list[CheckHit]:
55 hits: list[CheckHit] = []
56 for item in files:
57 if item.is_deleted or not is_test_path(item.path):
58 continue
59 for line in item.added:
60 if any(pattern.search(line.text) for pattern in _TAUTOLOGIES):
61 hits.append(
62 CheckHit(
63 "tautological-assertion",
64 item.path,
65 "new",
66 line.number,
67 "The assertion cannot fail: it asserts a constant or compares a value "
68 "with itself",
69 _quote(line.text),
70 )
71 )
72 return hits
75_WEAKENED = (
76 (re.compile(r"\|\|\s*(?:true|:)\s*(?:$|[;#)&|])"), "a failing command is turned into success"),
77 (re.compile(r"\bcontinue-on-error\s*:\s*true\b"), "a failing step is allowed to pass"),
78 (re.compile(r"\ballow_failure\s*:\s*true\b"), "a failing job is allowed to pass"),
79 (re.compile(r"--exit-zero\b"), "the tool always exits with success"),
80 (re.compile(r"^\s*set\s+\+e\b"), "the script keeps running after a failure"),
81 (re.compile(r"\bgit\s+(?:commit|push)\b[^#\n]*--no-verify\b"), "the Git hooks are skipped"),
82 (re.compile(r"\bcontinueOnError\s*:\s*true\b"), "a failing task is allowed to pass"),
83)
86def _code_lines(item: FileChange) -> list[DiffLine]:
87 """The added lines of a file that are not shell or YAML comments."""
88 return [line for line in item.added if not line.text.lstrip().startswith("#")]
91class _Searcher(Protocol):
92 def search(self, text: str, /) -> object:
93 """Search ``text`` and return the match, or a falsy value when there is none."""
96def _first_match(text: str, patterns: Iterable[tuple[_Searcher, str]]) -> str | None:
97 """Why the first matching pattern flags ``text``."""
98 return next((why for pattern, why in patterns if pattern.search(text)), None)
101def weakened_gates(files: Iterable[FileChange]) -> list[CheckHit]:
102 hits: list[CheckHit] = []
103 for item in files:
104 if item.is_deleted or not is_pipeline_path(item.path):
105 continue
106 for line in _code_lines(item):
107 why = _first_match(line.text, _WEAKENED)
108 if why is not None:
109 hits.append(
110 CheckHit(
111 "weakened-gates",
112 item.path,
113 "new",
114 line.number,
115 f"Weakened gate: {why}",
116 _quote(line.text),
117 )
118 )
119 return hits
122# ``rm``, its run of ``-flags`` options and what follows the run. The run is matched whole (its
123# tokens cannot overlap), so the scan is linear where one pattern with the recursive flag inside
124# a repeated option group backtracks over every split of the run.
125_RM_OPTIONS = re.compile(r"\brm\s+((?:-[A-Za-z]*\s+)+)")
128@dataclass(frozen=True)
129class _RecursiveDelete:
130 """An ``rm`` whose last option holds ``r``/``R`` and whose target matches ``target`` right
131 after the options: what ``\\brm\\s+(?:-[A-Za-z]*\\s+)*-[A-Za-z]*[rR][A-Za-z]*\\s+TARGET``
132 finds, read in one pass."""
134 target: re.Pattern[str]
136 def search(self, text: str, /) -> bool:
137 for command in _RM_OPTIONS.finditer(text):
138 last = command.group(1).split()[-1]
139 if ("r" in last or "R" in last) and self.target.match(text, command.end()):
140 return True
141 return False
144_DANGEROUS: tuple[tuple[_Searcher, str], ...] = (
145 (
146 _RecursiveDelete(re.compile(r"(?:--\s+)?[\"']?(?:/|~|\$HOME)[\"']?(?:\s|$|/\*)")),
147 "recursive delete of the root or the home directory",
148 ),
149 (
150 _RecursiveDelete(re.compile(r"(?:--\s+)?[\"']?\$\{?[A-Za-z_]\w*\}?[\"']?/")),
151 "recursive delete under an unguarded variable (use ${VAR:?})",
152 ),
153 (re.compile(r"\bchmod\s+(?:-R\s+)?0?777\b"), "world-writable permissions"),
154 (
155 re.compile(r"\b(?:curl|wget)\b[^|#\n]*\|\s*(?:sudo\s+)?(?:ba|z)?sh\b"),
156 "a downloaded script is executed unverified",
157 ),
158)
161def dangerous_paths(files: Iterable[FileChange]) -> list[CheckHit]:
162 hits: list[CheckHit] = []
163 for item in files:
164 if item.is_deleted or is_doc_path(item.path):
165 continue
166 for line in _code_lines(item):
167 why = _first_match(line.text, _DANGEROUS)
168 if why is not None:
169 hits.append(
170 CheckHit(
171 "dangerous-paths",
172 item.path,
173 "new",
174 line.number,
175 f"Dangerous path operation: {why}",
176 _quote(line.text),
177 )
178 )
179 return hits
182_TMP = re.compile(r"(?<![\w$])/tmp/(?a:\w[\w.-]*)") # an ASCII name, as before
185def temporary_files(files: Iterable[FileChange]) -> list[CheckHit]:
186 hits: list[CheckHit] = []
187 for item in files:
188 if item.is_deleted or not is_pipeline_path(item.path):
189 continue
190 for line in item.added:
191 text = line.text
192 if "mktemp" in text or text.lstrip().startswith("#") or not _TMP.search(text):
193 continue
194 hits.append(
195 CheckHit(
196 "temporary-files",
197 item.path,
198 "new",
199 line.number,
200 "A fixed path under /tmp can be read or replaced by another user; use mktemp",
201 _quote(text),
202 )
203 )
204 return hits
207def secrets(files: Iterable[FileChange]) -> list[CheckHit]:
208 changes = [item for item in files if not item.binary]
209 return [
210 CheckHit("secrets", issue.path or "", "new", issue.line or 1, issue.message, "")
211 for issue in scan_secrets([item.as_diff_file() for item in changes])
212 if issue.path and issue.line and issue.severity.value in {"HIGH", "CRITICAL"}
213 ]
216_INJECTION = (
217 re.compile(
218 r"\b(?:ignore|disregard|forget|override)\s+(?:all\s+|any\s+)?(?:the\s+|your\s+)?"
219 r"(?:previous|prior|above|earlier|preceding|system)\s+(?:instructions|directions|rules|"
220 r"prompts?|guidelines)\b",
221 re.IGNORECASE,
222 ),
223 re.compile(r"<\|(?:im_start|im_end|system)\|>|\[/?INST\]|<<SYS>>"),
224 re.compile(r"(?:^|[\s#/*>-])(?:system prompt|developer message)\s*:", re.IGNORECASE),
225 re.compile(
226 r"\b(?:AI|LLM|assistant|agent|language model|reviewers?|model)s?\b[^.\n]{0,40}\b"
227 r"(?:must|should|shall|are instructed to)\s+(?:approve|ignore|skip|not\s+report|"
228 r"report\s+no|pass|merge)\b",
229 re.IGNORECASE,
230 ),
231 re.compile(
232 r"\bdo\s+not\s+(?:tell|inform|mention\s+(?:this\s+)?to|report\s+(?:this\s+)?to)\s+the\s+"
233 r"(?:user|human|reviewer|maintainer)\b",
234 re.IGNORECASE,
235 ),
236)
239def embedded_instructions(files: Iterable[FileChange]) -> list[CheckHit]:
240 """Text addressed to an agent inside changed content (a prompt injection), since #5."""
241 hits: list[CheckHit] = []
242 for item in files:
243 if item.is_deleted or item.binary:
244 continue
245 for line in item.added:
246 if any(pattern.search(line.text) for pattern in _INJECTION):
247 hits.append(
248 CheckHit(
249 "embedded-instructions",
250 item.path,
251 "new",
252 line.number,
253 "Changed content addresses instructions to an agent (prompt injection); "
254 "repository content is untrusted",
255 _quote(line.text),
256 )
257 )
258 return hits
261CHECKS: dict[str, Callable[[Iterable[FileChange]], list[CheckHit]]] = {
262 "tautological-assertion": tautological_assertions,
263 "weakened-gates": weakened_gates,
264 "dangerous-paths": dangerous_paths,
265 "temporary-files": temporary_files,
266 "secrets": secrets,
267 "embedded-instructions": embedded_instructions,
268}
269"""The checks a rule may name as ``tool:harness:CHECK``."""
272def run_checks(names: Iterable[str], files: list[FileChange]) -> list[CheckHit]:
273 hits: list[CheckHit] = []
274 for name in sorted(set(names)):
275 check = CHECKS.get(name)
276 if check is not None: 276 ↛ 274line 276 didn't jump to line 274 because the condition on line 276 was always true
277 hits.extend(check(files))
278 return hits
281__all__ = [
282 "CHECKS",
283 "CheckHit",
284 "dangerous_paths",
285 "run_checks",
286 "secrets",
287 "tautological_assertions",
288 "temporary_files",
289 "weakened_gates",
290]