diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md index 857436e082..228f50b79c 100644 --- a/docs/release-control/v6/internal/subsystems/deployment-installability.md +++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md @@ -7191,7 +7191,15 @@ fixtures, including production argument forwarding and findings accompanied by transport errors, positive package/severity evidence with missing or zero totals, malformed zero summaries under both outage modes, failed/signalled zero summaries, zero and positive reports written before a real watchdog stop, -and metadata annotation injection. The shell smoke provider models a clean +and metadata annotation injection. The stdlib-only audit supervisor and report +classifier run with Python isolated/no-site startup (`-I -S`): caller-local +modules, `PYTHONPATH`, `PYTHONHOME` and site hooks must not execute or replace +the captured report's interpretation. Synthetic startup-hook and local-JSON +shadow controls retain positive findings and unavailable evidence, while clean +reports and literal audit arguments remain usable under the same environments. +This does not isolate npm itself or change the selected Python executable, +severity policy, retries, deadlines or process-group cleanup. +The shell smoke provider models a clean report with exit zero and findings with exit one, like npm's terminal contract. Retrying is bounded by wall clock and not by attempt count alone, because npm's own `fetch-timeout` defaults to five minutes and it retries diff --git a/scripts/npm-audit-retry.sh b/scripts/npm-audit-retry.sh index d7ea2bc0fe..0135ffb806 100755 --- a/scripts/npm-audit-retry.sh +++ b/scripts/npm-audit-retry.sh @@ -113,7 +113,9 @@ run_audit() { # Keep Bash's existing executable-script fallback (ENOEXEC), including on # macOS. The fixed exec program forwards argv literally; no npm argument or # executable path is interpolated into shell code. - python3 "${audit_runner_dir}/npm_audit_process.py" \ + # Both Python helpers are stdlib-only. Do not let the caller's PYTHONPATH, + # local modules or site startup replace the supervisor or captured verdict. + python3 -I -S "${audit_runner_dir}/npm_audit_process.py" \ "${limit}" "${out}" "${BASH}" -c 'exec "$@"' npm-audit \ "${NPM_BIN}" "${AUDIT_ARGS[@]}" } @@ -124,7 +126,7 @@ run_audit() { # vulnerable — audit completed, vulnerabilities present # unreachable — npm could not get an answer from the advisory endpoint classify_report() { - python3 -c ' + python3 -I -S -c ' import json, sys raw = sys.stdin.read().strip() diff --git a/scripts/tests/test_npm_audit_retry.py b/scripts/tests/test_npm_audit_retry.py index a71eb430e4..1ddc99fea3 100755 --- a/scripts/tests/test_npm_audit_retry.py +++ b/scripts/tests/test_npm_audit_retry.py @@ -5,8 +5,10 @@ from __future__ import annotations import json import os from pathlib import Path +import shlex import signal import subprocess +import sys import tempfile import textwrap import time @@ -145,6 +147,7 @@ class NpmAuditRetryTest(unittest.TestCase): def run_check( self, mode: str, *arguments: str, require: str = "true", report=None, attempts: int = 3, attempt_timeout: int = 60, max_seconds: int = 240, + python_startup: str = "", ): with tempfile.TemporaryDirectory() as directory: fake_bin = Path(directory) @@ -255,15 +258,50 @@ class NpmAuditRetryTest(unittest.TestCase): "NPM_AUDIT_RETRY_DELAY": "0", } ) + caller = ROOT + startup_marker = fake_bin / "python-startup" + python_calls = fake_bin / "python-calls" + if python_startup: + # Use the actual helpers and parser, not replacement verdict + # adapters. Only the interpreter entry logs their real argv. + # Each hook is confined to this subprocess's synthetic caller. + interpreter = fake_bin / "python3" + interpreter.write_text( + '#!/bin/sh\nprintf "%s\\000" "$1" "${2-}" "${3-}" >> "$AUDIT_PYTHON_CALLS"\n' + f'exec {shlex.quote(sys.executable)} "$@"\n', encoding="utf-8") + interpreter.chmod(0o700) + env.update(PATH=f"{fake_bin}{os.pathsep}{os.environ['PATH']}", + AUDIT_PYTHON_CALLS=str(python_calls)) + clean = {"metadata": {"vulnerabilities": dict.fromkeys( + ("info", "low", "moderate", "high", "critical", "total"), 0)}} + marker = (f"with open({str(startup_marker)!r}, 'a') as marker: " + "marker.write('unexpected startup\\n')\n") + injected = fake_bin / "injected" + injected.mkdir() + if python_startup == "site": + (injected / "sitecustomize.py").write_text( + marker + f"import json\njson.loads = lambda *args, **kwargs: {clean!r}\n") + env["PYTHONPATH"] = str(injected) + elif python_startup == "shadow": + (injected / "json.py").write_text( + marker + f"def loads(*args, **kwargs): return {clean!r}\n") + caller = injected + elif python_startup == "home": + env["PYTHONHOME"] = str(injected / "not-a-python-home") + else: + raise AssertionError("unknown synthetic Python startup") result = subprocess.run( [str(SCRIPT), *arguments], - cwd=ROOT, + cwd=caller, env=env, text=True, capture_output=True, check=False, timeout=30, ) + result.python_startup = startup_marker.exists() + result.python_calls = (python_calls.read_bytes().split(b"\0")[:-1] + if python_calls.exists() else []) recorded_calls = ( calls.read_text(encoding="utf-8").splitlines() if calls.exists() @@ -271,6 +309,52 @@ class NpmAuditRetryTest(unittest.TestCase): ) return result, recorded_calls + def assert_isolated_helpers(self, result) -> None: + self.assertFalse(result.python_startup, result.stdout + result.stderr) + self.assertEqual(len(result.python_calls), 6, result.stdout + result.stderr) + self.assertEqual(result.python_calls[:2], [b"-I", b"-S"]) + self.assertTrue(result.python_calls[2].endswith(b"/npm_audit_process.py")) + self.assertEqual(result.python_calls[3:], [b"-I", b"-S", b"-c"]) + + def test_python_startup_cannot_replace_captured_positive_findings(self) -> None: + for startup in ("site", "shadow"): + for require in ("true", "false"): + with self.subTest(startup=startup, require=require): + result, calls = self.run_check( + "vulnerability-success", "all", require=require, + python_startup=startup, + ) + self.assertEqual(result.returncode, 1 if require == "true" else 0, + result.stdout + result.stderr) + self.assertEqual(calls, ["audit --json"]) + self.assertIn("vulnerabilities present", result.stdout) + self.assertIn("::error::" if require == "true" else "::warning::", + result.stdout) + self.assertNotIn("no vulnerabilities", result.stdout) + self.assertNotIn("retrying", result.stdout) + self.assert_isolated_helpers(result) + + def test_clean_audit_ignores_caller_python_home_and_startup(self) -> None: + for startup in ("site", "shadow", "home"): + with self.subTest(startup=startup): + result, calls = self.run_check("success", "production", "--package-lock-only", + python_startup=startup) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(calls, ["audit --json --package-lock-only --omit=dev"]) + self.assertIn("no vulnerabilities", result.stdout) + self.assert_isolated_helpers(result) + + def test_python_startup_cannot_turn_unavailable_evidence_clean(self) -> None: + for startup in ("site", "shadow", "home"): + with self.subTest(startup=startup): + result, calls = self.run_check("garbage", "all", attempts=1, + python_startup=startup) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertEqual(calls, ["audit --json"]) + self.assertIn("could not reach the advisory endpoint", result.stdout) + self.assertNotIn("no vulnerabilities", result.stdout) + self.assert_isolated_helpers(result) + def test_fake_audit_budget_is_not_inherited_from_the_caller(self) -> None: # These are fixture controls, not a real advisory query. A caller's # exhausted/short budget must not prevent the canned verdict from