Skip to content

Commit 0e535ed

Browse files
Add path-hermetic lint for negative greps over absolute-path logs
A test that runs `! grep` for a path-segment word (worktrees, home, tmp, ...) over a log holding absolute paths passes in CI but fails in a lane worktree, because the checkout path contains the word. path_hermetic_lint.py flags that in scripts/test-*.sh. Each line is split on ; && || | outside quotes. In every segment starting with `! grep`, any listed word appearing as a whole word in the literal (non-$var) text is a hit, so a second grep on a line, unbalanced quotes, -e/-eX/--regexp=, and -m N/-A N all flag. A hit is exempt only when the grep's input file (its last arg) is written on that line or the previous one by a sed/replace that uses $ROOT/$PWD; the strip may sit in a loop body (`while read ...; do ${l//"$ROOT"/}; done >f`), but a strip that writes a different file does not count. The baseline is keyed on `path<TAB>stripped line text` and messages print path:line, so a new offender in a baselined file fails. Two reviewed offenders are baselined. Regression test covers each reviewer case. Tests: 25/25 (6 path-hermetic lint unit tests, incl. loop-body strip and strip-into-other-file cases, + 19 scan-summary checks); path_hermetic_lint over repo clean, no stale rows Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ea73f66 commit 0e535ed

5 files changed

Lines changed: 171 additions & 0 deletions

File tree

‎changelog.d/path-hermetic-lint.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Added
2+
- scripts/path_hermetic_lint.py flags a negative grep (`! grep ... worktrees ...`) in scripts/test-*.sh whose pattern is a path-segment word that also appears in the checkout path, unless the line or the one before it strips $ROOT/$PWD. Such a test passes in CI and fails in a lane worktree. Existing offenders are frozen in scripts/path-hermetic-baseline.tsv (one row today, fixed by an open PR); the regression test is registered through ci-tests.d.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Registers scripts/test_path_hermetic_lint.py with CI (see the "Per-PR tests" step in ci.yml).
2+
python3 scripts/test_path_hermetic_lint.py

‎scripts/path-hermetic-baseline.tsv‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# path<TAB>stripped line text. Frozen offenders; fixing one means deleting its row. Reviewed: pentest-load HOME row is an anchored literal subdir, ci-gates row is a full phrase, neither can match a checkout path.
2+
scripts/test-app-pentest-load.sh grep -qE '^HOME=.*/ap\.[A-Za-z0-9]+$' "$T/fake/env" && ! grep -q "^HOME=$T/home" "$T/fake/env" && grep -qF "PATH=$T/fake:/usr/bin:/bin" "$T/fake/env"; ok $? "k6 HOME is the run's temp dir; PATH is passed on"
3+
scripts/test-ci-gates.sh && ! grep -q 'DISCRIMINATE FAIL deep-code-review' "$WORK/last.log"; then

‎scripts/path_hermetic_lint.py‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
"""path_hermetic_lint.py - a negative grep for a path-segment word is not hermetic: the checkout path is part of every absolute path in a log.
3+
4+
WHY: `! grep -E 'worktrees' "$LOG"` passes in CI (checkout /home/runner/work/...) but fails in a lane worktree
5+
(/repo/.claude/worktrees/lane/...), because the scanner argv/log holds absolute paths that contain the word.
6+
Flags, in scripts/test-*.sh, every `! grep` command segment (a line is split on ; && || | outside quotes) whose literal,
7+
non-$var text holds a bare path-segment word (worktrees, Desktop, Users, home, tmp, var, ...) as a whole word, unless the
8+
grep's input (its last arg) is written, on that line or the one before, by a sed/`${x//...}`/replace that uses $ROOT/$PWD.
9+
Existing offenders are frozen in scripts/path-hermetic-baseline.tsv (`path<TAB>stripped line text`; a row whose line no longer
10+
flags also fails, so the baseline only shrinks). Usage: path_hermetic_lint.py [root]. Exit 0 clean, 1 on a violation.
11+
Side effects: none. Edge case: a pattern built from a variable is not seen (false negative, accepted).
12+
"""
13+
import glob
14+
import os
15+
import re
16+
import sys
17+
from pathlib import Path
18+
19+
WORDS = {"worktree", "worktrees", "desktop", "users", "home", "tmp", "private", "var", "checkout", "deep-code-review", "documents"}
20+
STRIP = re.compile(r"(\bsed\b|\$\{[^}]*//|\breplace\b)")
21+
ROOTS = re.compile(r"\$\{?(ROOT|PWD|REPO\w*|CHECKOUT\w*|SRC)\b|\$\(pwd\)|\$\(git rev-parse --show-toplevel\)")
22+
23+
24+
def segments(line):
25+
"""Split a shell line on ; && || | outside quotes."""
26+
out, cur, q, i = [], "", "", 0
27+
while i < len(line):
28+
c = line[i]
29+
if q:
30+
q = "" if c == q else q
31+
elif c in "\"'":
32+
q = c
33+
elif (c == "|" and cur.endswith(">")) is False and c in ";|" or (c == "&" and line[i:i + 2] == "&&"):
34+
out.append(cur)
35+
cur = ""
36+
i += 2 if line[i:i + 2] in ("&&", "||") else 1
37+
continue
38+
cur += c
39+
i += 1
40+
return out + [cur]
41+
42+
43+
def hit_words(seg):
44+
"""Return the WORDS found as whole words in the literal (non-$var) text of a `! grep` segment, else []."""
45+
seg = seg.strip()
46+
if not re.match(r"!\s+grep\b", seg):
47+
return []
48+
lit = re.sub(r"\$\{[^}]*\}|\$\([^)]*\)|\$\w+", " ", seg).lower()
49+
lit = re.sub(r"(?<=\s)-[a-z]*?e(?=\w)", r"\g<0> ", lit) # -ePATTERN / -qePATTERN glue
50+
return [w for w in WORDS if re.search(r"(?<![\w-])" + re.escape(w) + r"(?![\w-])", lit)]
51+
52+
53+
def exempt(seg, lines):
54+
"""True if this or the previous line has a non-grep segment with STRIP and ROOTS and a non-grep segment that redirects (> f, >f, >| f) exactly into the grep's input file (last arg, quotes stripped)."""
55+
f = seg.split()[-1].strip("\"'")
56+
for l in lines:
57+
segs = [s for s in segments(l) if not s.strip().startswith("! grep")]
58+
outs = [[t.strip("\"'") for t in re.findall(r"(?<![>&\d])>\|?\s*(\"[^\"]*\"|'[^']*'|[^\s;&|]+)", s)] for s in segs]
59+
# A strip counts if it writes f or nothing (a loop body: while read; do ${l//"$ROOT"/}; done >f); a strip into another file does not.
60+
if any(STRIP.search(s) and ROOTS.search(s) and o in ([], [f]) for s, o in zip(segs, outs)) and any(f in o for o in outs):
61+
return True
62+
return False
63+
64+
65+
def lint(root="."):
66+
"""Return (new, stale): new = `path:line` of unexempt hits whose `path<TAB>text` key is not in the baseline, stale = baseline keys that no longer hit."""
67+
with open(os.path.join(root, "scripts/path-hermetic-baseline.tsv"), encoding="utf-8") as f: # missing = fail closed
68+
base = {l.rstrip("\n") for l in f if l.strip() and not l.startswith("#")}
69+
hits = {}
70+
for p in sorted(glob.glob(os.path.join(root, "scripts/test-*.sh"))):
71+
rel = os.path.relpath(p, root)
72+
ls = Path(p).read_text(encoding="utf-8").splitlines()
73+
for n, l in enumerate(ls):
74+
if l.lstrip().startswith("#"):
75+
continue
76+
for seg in segments(l):
77+
if hit_words(seg) and not exempt(seg, [l] + ls[n - 1:n]):
78+
hits[f"{rel}\t{l.strip()}"] = f"{rel}:{n + 1}"
79+
return sorted(v for k, v in hits.items() if k not in base), sorted(base - set(hits))
80+
81+
82+
def main(argv):
83+
new, stale = lint(argv[1] if len(argv) > 1 else ".")
84+
for k in new:
85+
print(f"path_hermetic_lint: {k} has a `! grep` for a path-segment word that is also in the checkout path; strip $ROOT/$PWD first (sed \"s|$ROOT||g\")", file=sys.stderr)
86+
for k in stale:
87+
print(f"path_hermetic_lint: baseline row no longer flags (delete it from scripts/path-hermetic-baseline.tsv): {k.replace(chr(9), ' :: ')}", file=sys.stderr)
88+
return 1 if new or stale else 0
89+
90+
91+
if __name__ == "__main__":
92+
sys.exit(main(sys.argv))

‎scripts/test_path_hermetic_lint.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
"""Tests for path_hermetic_lint.py: a bare path-word negative grep fails, a $ROOT-stripped or non-path one passes, a baselined one is exempt; the repo is clean."""
3+
import os
4+
import subprocess
5+
import sys
6+
import tempfile
7+
import unittest
8+
from pathlib import Path
9+
10+
ROOT = Path(__file__).resolve().parent.parent
11+
sys.path.insert(0, str(ROOT / "scripts"))
12+
import path_hermetic_lint as p # noqa: E402
13+
14+
BAD = '! grep -Eq "node_modules|worktrees|vendor" "$LOG"\n'
15+
GOOD = 'sed "s|$ROOT||g" "$LOG" >"$LOG.rel"\n! grep -Eq "worktrees" "$LOG.rel"\n'
16+
17+
18+
def tree(body, baseline=""):
19+
d = Path(tempfile.mkdtemp())
20+
(d / "scripts").mkdir()
21+
(d / "scripts/path-hermetic-baseline.tsv").write_text(baseline)
22+
(d / "scripts/test-x.sh").write_text(body)
23+
return str(d)
24+
25+
26+
class Lint(unittest.TestCase):
27+
def test_bad_fixture_fails(self):
28+
self.assertEqual(p.lint(tree(BAD)), (["scripts/test-x.sh:1"], []))
29+
30+
def test_reviewer_cases_fail(self):
31+
for body in (
32+
'! grep -q fatal "$L"; ! grep -q worktrees "$L"\n', # 2nd grep on a line
33+
'! grep -q "unterminated worktrees "$L\n', # unbalanced quote
34+
'! grep -qE -e worktrees "$L"\n', # -e not last
35+
'! grep -m 1 -A 2 worktrees "$L"\n', # -m N / -A N
36+
'! grep --regexp=worktrees "$L"\n',
37+
'! grep -eworktrees "$L"\n',
38+
'! grep -qeworktrees "$L"\n', # glued combined flags
39+
'sed "s|$ROOT||g" "$LOG" >"$LOG2"\n! grep -q worktrees "$LOG"\n', # sed writes $LOG2, not $LOG
40+
'sed "s|$ROOT||g" "$LOG" | tee x\n! grep -q worktrees "$LOG"\n', # read-only sed
41+
'sed "s|$ROOT||g" "$A" >"$A.rel"\n! grep -q worktrees "$L"\n', # sed unrelated to the input
42+
'sed "s|$ROOT||g" "$A" >"$A.rel"; cat "$B" >"$L"\n! grep -q worktrees "$L"\n', # strip into another file, raw copy into the input
43+
):
44+
self.assertEqual(p.lint(tree(body))[0], ["scripts/test-x.sh:%d" % (body.count("\n"))], body)
45+
46+
def test_good_fixture_passes(self):
47+
self.assertEqual(p.lint(tree(GOOD)), ([], []))
48+
loop = 'cat "$A" | while IFS= read -r l; do printf "%s\\n" "${l//"$ROOT"/}"; done >"$L"\n! grep -q worktrees "$L"\n'
49+
self.assertEqual(p.lint(tree(loop)), ([], [])) # strip in a loop body, redirect after done
50+
self.assertEqual(p.lint(tree('! grep -q "$ROOT" "$L"; ! grep -q fatal "$L"\n# ! grep -q worktrees "$L"\n')), ([], []))
51+
52+
def test_baseline_exempts_then_goes_stale(self):
53+
k = "scripts/test-x.sh\t" + BAD.strip() + "\n"
54+
self.assertEqual(p.lint(tree(BAD, k)), ([], []))
55+
self.assertEqual(p.lint(tree(GOOD, k)), ([], [k.strip()]))
56+
# a new offender in a baselined file is not covered by the row
57+
self.assertEqual(p.lint(tree(BAD + '! grep -q home "$L"\n', k)), (["scripts/test-x.sh:2"], []))
58+
59+
def test_missing_baseline_fails_closed(self):
60+
r = tree(GOOD)
61+
os.remove(os.path.join(r, "scripts/path-hermetic-baseline.tsv"))
62+
with self.assertRaises(OSError):
63+
p.lint(r)
64+
65+
def test_repo_is_clean(self):
66+
r = subprocess.run([sys.executable, "-I", str(ROOT / "scripts/path_hermetic_lint.py"), str(ROOT)], capture_output=True, text=True,
67+
env={"PATH": os.environ["PATH"]})
68+
self.assertEqual(r.returncode, 0, r.stderr)
69+
70+
71+
if __name__ == "__main__":
72+
unittest.main()

0 commit comments

Comments
 (0)