# SPDX-License-Identifier: Apache-2.0 """No lab or lesson snippet may keep state in a predictable shared path or kill an unchecked pid. Issue #260. A fixed name in a shared folder such as /tmp lets another local user plant a file, a link or a pid file before the lab uses it. This test scans labs/ (every text file) and the fenced code blocks of docs/wiki/*.md for three patterns: fixed-shared-path /tmp/, /var/tmp/, /dev/shm/, $TMPDIR/, a quoted "/tmp" in code, or a Windows temp folder joined to a fixed name kill-by-pattern pkill, pgrep or killall with a literal (it can match your own shell) unchecked-pid-file kill of a pid read straight from a file, in a file or code block that never checks the pid is digits and the process identity (ps -o command=) Safe forms are not flagged: mktemp (mktemp -d), $XDG_RUNTIME_DIR, $HOME/.cache, Python tempfile, a per-user folder made with mkdir -m 700, and kill of a pid taken from $! in the same shell. Exceptions live in docs/shared-temp-allowlist.txt, one per line, with a required reason: : | A stale entry (one that no longer matches anything) also fails, so the list cannot rot. """ import re from pathlib import Path ROOT = Path(__file__).resolve().parent.parent ALLOWLIST = ROOT / "docs" / "shared-temp-allowlist.txt" SHARED = r"(?:/tmp|/var/tmp|/dev/shm)" FIXED_PATH = re.compile( rf"(?'") path, colon, hit = key.partition(":") if not colon or not path.strip() or not hit.strip(): raise ValueError(f"line {number}: expected ': | '") entries[(path.strip(), hit.strip())] = reason.strip() return entries def test_no_shared_temp_paths_or_unchecked_kills_in_labs_and_lessons(): allowed = parse_allowlist(ALLOWLIST.read_text()) bad = [ f"{rel}:{n} {rule} {hit!r}" for rel, n, rule, hit in findings() if (rel, hit) not in allowed ] assert not bad, ( "Use a private per-user folder (mkdir -m 700 under $XDG_RUNTIME_DIR or $HOME/.cache, or mktemp -d), " "check a pid file and the process before kill, and never pkill or pgrep with a literal. " "A true exception goes in docs/shared-temp-allowlist.txt with a reason.\n" + "\n".join(bad) ) def test_every_allowlist_entry_has_a_reason_and_still_matches_something(): allowed = parse_allowlist(ALLOWLIST.read_text()) live = {(rel, hit) for rel, _n, _rule, hit in findings()} stale = sorted(set(allowed) - live) assert not stale, f"allowlist entries that match nothing now (remove them): {stale}" def test_allowlist_without_a_reason_is_rejected(): for bad in ("a.md:/tmp/x", "a.md:/tmp/x | ", "a.md:/tmp/x | ", "nocolon | reason"): try: parse_allowlist(bad) except ValueError: continue raise AssertionError(f"accepted: {bad!r}") assert parse_allowlist("# c\n\na.md:/tmp/x | it is data, not state\n") == {("a.md", "/tmp/x"): "it is data, not state"} # ---- the scanner itself: it catches each banned pattern ---------------------------------------- def rules(text, markdown=False): return {rule for _n, rule, _hit in scan_text(text, markdown)} def test_scanner_catches_fixed_shared_paths(): for snippet in ( "mkdir -p /tmp/lab", "echo hi > /tmp/lab.txt", "pid /tmp/nginx.pid;", "cd /var/tmp/work", "touch /dev/shm/state", 'LAB="$TMPDIR/lab"', "LAB=${TMPDIR}/lab", 'p = Path("/tmp") / "state"', "d = tempfile.gettempdir() + '/lab'", "$d = Join-Path ([IO.Path]::GetTempPath()) 'cah-lab'", ): assert "fixed-shared-path" in rules(snippet), snippet def test_scanner_catches_kill_by_pattern(): for snippet in ("pkill -f nginx", "pkill nginx", "pgrep -f 'my server'", "killall python3", "kill $(pgrep -f app.py)"): assert "kill-by-pattern" in rules(snippet), snippet def test_scanner_catches_kill_of_an_unvalidated_pid_file(): for snippet in ( 'kill "$(cat app.pid)"', "kill $(cat ~/lab/auth.pid)", 'kill -9 "$(cat "$WORK/a.pid")" || true', 'kill "$(