|
| 1 | +# ADR-17: Isolate the coverage driver from module shadowing |
| 2 | + |
| 3 | +> Status: **Accepted** |
| 4 | +> Date: 2026-09-06 |
| 5 | +> Deciders: @omsherikar |
| 6 | +> Closes: GHSA-739m-x9gc-9wjv (private advisory). |
| 7 | +
|
| 8 | +## Context |
| 9 | + |
| 10 | +`reportCoverage` invoked the coverage driver as `python3 -m coverage run|json …` |
| 11 | +with `cwd = the shadow tree`. For `python -m`, CPython puts cwd first on |
| 12 | +`sys.path`, so a **`coverage.py` file committed at the repo root** shadows the |
| 13 | +real tool. A hostile PR ships one that fabricates `coverage.json` marking every |
| 14 | +changed line executed → the changed-line coverage rule (ADR-11) is satisfied on |
| 15 | +fabricated evidence and the verdict is a clean, undisclosed **false SAFE**, |
| 16 | +reachable by any PR with no machine access. Reproduced end-to-end through the real |
| 17 | +MCP server across 0.4.0–0.4.5. |
| 18 | + |
| 19 | +The constraint that makes this non-trivial: the coverage run must stay |
| 20 | +**observationally equivalent** to the tests-gate suite run (the governing rule at |
| 21 | +the top of `python-line-coverage.ts` — two prior false SAFEs came from the two |
| 22 | +runs diverging). So the fix must isolate the **driver** (the `import coverage`) |
| 23 | +without changing what the **suite** sees on `sys.path`. |
| 24 | + |
| 25 | +## The trap (why the obvious fix is wrong) |
| 26 | + |
| 27 | +Launching the driver by absolute package path (`python <site-packages>/coverage |
| 28 | +run …`) sets `sys.path[0]` to the coverage dir and looked sufficient. It is **not**: |
| 29 | +measured empirically, it still forges under `PYTHONPATH=.` — the *documented* |
| 30 | +shadow-bypass remedy. `import coverage` from the package-dir launch does not |
| 31 | +resolve at `sys.path[0]` (the coverage package's own dir holds no top-level |
| 32 | +`coverage`), so it falls through to `PYTHONPATH` **before** site-packages, and a |
| 33 | +repo-local `coverage.py` on `PYTHONPATH=.` (= the shadow cwd) is imported. This is |
| 34 | +exactly the "the fix for one false SAFE introduces another" failure this module |
| 35 | +keeps relearning — caught here before shipping. |
| 36 | + |
| 37 | +## Decision |
| 38 | + |
| 39 | +**Launch the coverage CLI from a `-c` program that loads coverage from the real |
| 40 | +site-packages FIRST, then restores the suite's `sys.path`:** |
| 41 | + |
| 42 | +```python |
| 43 | +import sys |
| 44 | +sys.path.insert(0, <resolved site-packages>) # ahead of cwd AND PYTHONPATH |
| 45 | +import coverage.cmdline as C |
| 46 | +del sys.path[0] # restore: the suite runs as the gate ran it |
| 47 | +sys.exit(C.main()) |
| 48 | +``` |
| 49 | + |
| 50 | +- **Resolve** the site-packages dir with `<runner> -c "import coverage, os; |
| 51 | + print(os.path.dirname(os.path.dirname(coverage.__file__)))"` run from a |
| 52 | + **freshly-created empty temp dir** (neutral cwd) with the **base env only — |
| 53 | + never the testCmd's `PYTHONPATH`**. Both properties matter: a neutral cwd |
| 54 | + defeats cwd-shadowing; dropping the attacker-hoisted `PYTHONPATH` defeats |
| 55 | + path-shadowing of the resolve itself. |
| 56 | +- **Insert / import / delete.** Inserting site-packages at `sys.path[0]` makes the |
| 57 | + driver's `import coverage` resolve the real tool ahead of cwd and `PYTHONPATH`. |
| 58 | + Deleting the inserted entry before `C.main()` runs the suite means the suite |
| 59 | + sees the exact `sys.path` the gate gave it. Verified: the suite's `sys.path[0]` |
| 60 | + is byte-identical to the gate's under `PYTHONPATH=.`, so the package-under-test |
| 61 | + still imports from the shadow. |
| 62 | +- **Never fall back to `-m coverage`.** If resolution fails (coverage not |
| 63 | + importable for the interpreter, or a namespace `coverage/` dir with |
| 64 | + `__file__ = None`), the run the gate performed cannot be measured, so decline |
| 65 | + (`coverageToolFound: false` → UNPROVEN). This is the load-bearing invariant, the |
| 66 | + single line most likely to be "simplified" back into the hole. |
| 67 | + |
| 68 | +`shadow-tree.ts` is untouched: the repo-local `coverage.py` stays in the shadow |
| 69 | +(the gate's suite legitimately sees it); it is only denied to the **driver**. |
| 70 | + |
| 71 | +## Alternatives considered |
| 72 | + |
| 73 | +- **Absolute-path package launch alone.** Rejected: forges under `PYTHONPATH=.` |
| 74 | + (above). |
| 75 | +- **`-P` / `PYTHONSAFEPATH=1`.** Rejected: Python 3.11+ only; the repo supports |
| 76 | + 3.8+. The `-c` insert/import/delete is universal. |
| 77 | +- **Denylist a repo-local `coverage.py` / `sitecustomize.py` in the shadow.** |
| 78 | + Rejected: a denylist against a whitelist problem — `coverage.py` is not the only |
| 79 | + shadowing name, and it would diverge the suite from the gate. The launcher |
| 80 | + immunizes the driver's import against all cwd/`PYTHONPATH` shadowing at once. |
| 81 | + |
| 82 | +## Consequences |
| 83 | + |
| 84 | +- **Positive.** Closes GHSA-739m-x9gc-9wjv, including under `PYTHONPATH=.`. |
| 85 | + Fail-safe by construction: the only verdict move is toward UNPROVEN. |
| 86 | + |
| 87 | + Correction (2026-09-07): an earlier version of this bullet claimed the fix also |
| 88 | + closed a repo-root `sitecustomize.py` driver-startup vector "because the driver's |
| 89 | + neutral cwd never imports it." That was WRONG — only the site-packages RESOLVE |
| 90 | + runs from a neutral cwd; the DRIVER runs from the shadow tree, so `site` imports |
| 91 | + a shadow `sitecustomize.py` at startup exactly as before. The launcher does NOT |
| 92 | + close the `sitecustomize` sys.modules substitution (F1), and no in-process |
| 93 | + launcher can (attempts with `-S` and with a `sys.modules` purge both failed; |
| 94 | + see ADR-19). That vector and the general in-process forgery family are mitigated |
| 95 | + by the trust-mode gate (ADR-19), which withholds SAFE for an untrusted diff. |
| 96 | +- **Negative, fail-safe.** A repo whose coverage is importable ONLY via the |
| 97 | + testCmd's `PYTHONPATH` (not installed in the interpreter) now declines to |
| 98 | + UNPROVEN. A repo that vendors a root `coverage.py` without pip-installing |
| 99 | + coverage declines (indistinguishable from the attack, correctly refused). Both |
| 100 | + are rare and move only toward UNPROVEN. |
| 101 | +- **Out of scope, filed separately.** The suite runs *in the same process* as |
| 102 | + coverage's collector, so a malicious test can still tamper with the data file / |
| 103 | + tracer in-process, and a `[tool.coverage] plugins = evil` entry makes the driver |
| 104 | + import an attacker plugin. A1's fix does not touch these; they are their own |
| 105 | + findings/advisories. |
| 106 | + |
| 107 | +## Compliance |
| 108 | + |
| 109 | +- Red-first: `tests/integration/coverage-driver-shadow.test.ts` — a repo-root |
| 110 | + `coverage.py` fabricating coverage returns SAFE on the pre-fix tree and UNPROVEN |
| 111 | + after, **and** a second case pins the `PYTHONPATH=.` regression so the abs-path |
| 112 | + form cannot be reintroduced. |
| 113 | +- Equivalence preserved: `tests/integration/coverage-reporter.test.ts` (30 cases, |
| 114 | + including the shebang-interpreter and json-failure paths) stays green. |
| 115 | +- Isolation at the redaction site: `tests/unit/verify/runner-env-redaction.test.ts` |
| 116 | + — a planted repo-root `coverage.py` is now never executed by any coverage spawn. |
0 commit comments