diff --git a/node/maintenance.md b/node/maintenance.md index 1ee815c..5913775 100644 --- a/node/maintenance.md +++ b/node/maintenance.md @@ -29,6 +29,13 @@ retain changes only when before/after audit evidence confirms a security fix; unrelated changes are discarded. The engine does not force incompatible upgrades or remove project overrides automatically. +When pnpm's initial security fix leaves findings, the engine keeps the lockfile +and refreshes named packages with verified compatible patches. It does not +regenerate every dependency. A package name with both safe and vulnerable locked +versions is not targeted, because that command would also update the safe +instances. The final audit and unrelated-change guard still decide whether the +proposal can be retained. + Node runtime updates use Node's official release index, independently of package advisory counts. A Node-only security update can therefore open a PR when no npm or pnpm dependency fix is available. Reports show the original declaration, the diff --git a/node/runtime/templates/dependency_audit.py b/node/runtime/templates/dependency_audit.py index 59c2413..4f5ae97 100644 --- a/node/runtime/templates/dependency_audit.py +++ b/node/runtime/templates/dependency_audit.py @@ -830,22 +830,45 @@ def fix_security( return result current = valid_snapshot(directory) assert current is not None - # pnpm can reintroduce vulnerable pins during automatic peer resolution. - # A fresh compatible resolution avoids those pins, including the installed - # virtual store's lockfile, without adding overrides or running scripts. - (directory / "pnpm-lock.yaml").unlink() + # Refresh verified security targets without deleting the lockfile: + # a full regeneration also moves unrelated dependencies. A name selector + # updates every installed version, so retain mixed safe/vulnerable names. + targets = [] + verified_names = {item.rpartition("@")[0] for item in verified} + for name, vulnerability in original["vulnerabilities"].items(): + if name not in verified_names or not re.fullmatch( + REPAIR_CHOICES["PACKAGE"], name + ): + continue + locked = { + version.split("(", 1)[0] + for version in package_paths(snapshot[3], name, "pnpm").values() + } + vulnerable = { + version + for detail in vulnerability["via"] + for version in detail.get("_verifiedVulnerableVersions", []) + } + if locked and locked <= vulnerable: + targets.append(name) + if not targets: + if not verified: + restore_snapshot(directory, snapshot) + diagnostics.append( + "Skipped pnpm security refresh: no verified wholly vulnerable package targets." + ) + return result with TemporaryDirectory(prefix="dependency-audit-resolution-") as modules: resolved = run( directory, - "install", + "update", + "--no-save", + *sorted(targets), "--lockfile-only", - "--no-frozen-lockfile", "--ignore-scripts", "--config.optimistic-repeat-install=false", - "--modules-dir", - modules, - "--virtual-store-dir", - str(Path(modules) / ".pnpm"), + f"--config.modules-dir={modules}", + f"--config.virtual-store-dir={Path(modules) / '.pnpm'}", deadline=deadline, ) failed = bool(resolved.returncode) or valid_snapshot(directory) is None @@ -853,10 +876,10 @@ def fix_security( restore_snapshot(directory, current) if notes is not None: notes.append( - "Regenerated the pnpm lockfile within existing manifest constraints " + "Refreshed verified pnpm security targets within existing constraints " "using isolated module metadata after audit --fix=update left findings." if not failed - else f"Compatible lockfile regeneration failed (exit {resolved.returncode}); " + else f"Targeted security refresh failed (exit {resolved.returncode}); " "restored the preceding security-fix proposal." ) return result if failed else resolved diff --git a/node/tests/test_dependency_audit_release_age.py b/node/tests/test_dependency_audit_release_age.py index 0dc11c6..67e166c 100644 --- a/node/tests/test_dependency_audit_release_age.py +++ b/node/tests/test_dependency_audit_release_age.py @@ -268,7 +268,6 @@ def invoke(command, *, cwd, **kwargs): fixes += 1 if fixes == 1: set_lock("1.0.5") - code = 1 else: code = 1 elif args[0] == "view": @@ -280,8 +279,6 @@ def invoke(command, *, cwd, **kwargs): elif args[0] == "install" and "--lockfile-only" in args: installs += 1 if installs == 1: - set_lock("1.0.5") - elif installs == 2: set_lock("1.0.4") else: code = 1 @@ -293,6 +290,7 @@ def invoke(command, *, cwd, **kwargs): for name, content in security_files.items(): assert (project / name).read_bytes() == content assert fixes == 1 + assert installs == 1 assert "Stable upgrade resolution failed (exit 0)" in report assert ( "restored the manifest and lockfile from the initial security fix attempt" diff --git a/node/tests/test_dependency_audit_repairs.py b/node/tests/test_dependency_audit_repairs.py index d914010..b3ed116 100644 --- a/node/tests/test_dependency_audit_repairs.py +++ b/node/tests/test_dependency_audit_repairs.py @@ -428,12 +428,19 @@ def invoke(command, *, cwd, **kwargs): commands.append(args) if args[0] == "audit": return subprocess.CompletedProcess(command, 1, "5 advisories remain", "") - assert not (cwd / "pnpm-lock.yaml").exists() + assert (cwd / "pnpm-lock.yaml").exists() + assert args[:3] == ["update", "--no-save", "child"] assert "--ignore-scripts" in args and "--force" not in args assert "--config.optimistic-repeat-install=false" in args - modules = Path(args[args.index("--modules-dir") + 1]) + modules = Path( + next( + a.split("=", 1)[1] + for a in args + if a.startswith("--config.modules-dir=") + ) + ) assert modules != cwd / "node_modules" - assert Path(args[args.index("--virtual-store-dir") + 1]) == modules / ".pnpm" + assert f"--config.virtual-store-dir={modules / '.pnpm'}" in args (cwd / "pnpm-lock.yaml").write_bytes(snapshot[1]["pnpm-lock.yaml"]) if failure == "invalid": (cwd / "pnpm-lock.yaml").write_text("[") @@ -442,18 +449,31 @@ def invoke(command, *, cwd, **kwargs): return subprocess.CompletedProcess(command, int(failure == "exit"), "", "") monkeypatch.setattr(subprocess, "run", invoke) + monkeypatch.setitem( + module["fix_security"].__globals__, "describe_pnpm_fixes", lambda *a, **k: None + ) notes = [] - result = module["fix_security"](project, float("inf"), notes) + evidence = { + "package_manager": "pnpm", + "vulnerabilities": { + "child": { + "via": [ + {"fixVersion": "1.0.1", "_verifiedVulnerableVersions": ["1.0.0"]} + ] + } + }, + } + result = module["fix_security"](project, float("inf"), notes, before=evidence) assert result.returncode == (1 if failure else 0) assert len(commands) == 2 if failure: assert (project / "pnpm-lock.yaml").read_bytes() == snapshot[1][ "pnpm-lock.yaml" ] - assert "regeneration failed" in notes[0] + assert "refresh failed" in notes[0] else: assert "child@1.0.1" in module["valid_snapshot"](project)[3]["packages"] - assert "Regenerated" in notes[0] + assert "Refreshed" in notes[0] def test_weakened_policy_prevents_fresh_resolution(project, monkeypatch): @@ -674,7 +694,9 @@ def invoke(command, *, cwd, **kwargs): return subprocess.CompletedProcess(command, 1, json.dumps(response), "") if args[0] == "view": value, code = ["4.3.0"], 0 - if args[1] == "child@>=4.2.1" and metadata == "unavailable": + if args[1] == "child@4.2.0 <=4.2.0": + value = ["4.2.0"] + elif args[1] == "child@>=4.2.1" and metadata == "unavailable": value, code = {"error": {"code": "ETIMEDOUT"}}, 1 elif args[1] == "child@>=4.2.1" and metadata == "mutated": policy = yaml.safe_load(workspace.read_bytes()) @@ -689,7 +711,8 @@ def invoke(command, *, cwd, **kwargs): elif args[1] == "child@~4.2.0 >=4.2.1": value = [] return subprocess.CompletedProcess(command, code, json.dumps(value), "") - assert args[0] == "install" and "--ignore-scripts" in args + assert args[:3] == ["update", "--no-save", "child"] + assert "--ignore-scripts" in args exclusions = yaml.safe_load(workspace.read_bytes())["minimumReleaseAgeExclude"] assert "child@4.2.1" not in exclusions assert ("child@4.3.0" in exclusions) == (metadata == "published") @@ -705,11 +728,19 @@ def invoke(command, *, cwd, **kwargs): initial_audit = { "package_manager": "pnpm", "vulnerabilities": { - "child": {"via": [{"patchedRange": ">=4.2.1", "nodes": ["child@4.2.0"]}]} + "child": { + "via": [ + { + "patchedRange": ">=4.2.1", + "range": "<=4.2.0", + "nodes": ["child@4.2.0"], + } + ] + } }, } result = module["fix_security"](project, float("inf"), notes, before=initial_audit) - assert result.returncode == 0 + assert result.returncode == (0 if metadata == "published" else 1) assert not module["restore_invalid_state"]( project, before, notes, "Security fix", security_fix=True ) @@ -720,7 +751,10 @@ def invoke(command, *, cwd, **kwargs): assert yaml.safe_load(workspace.read_bytes())["blockExoticSubdeps"] is True assert commands.index( ["audit", "--fix=update", "--audit-level", "info", "--ignore-scripts"] - ) < next(i for i, args in enumerate(commands) if args[0] == "install") + ) < next( + (i for i, args in enumerate(commands) if args[0] == "update"), len(commands) + ) + assert any(args[0] == "update" for args in commands) is (metadata == "published") @pytest.mark.parametrize("fix_exit", [0, 1]) @@ -923,6 +957,7 @@ def test_security_resolution_retains_verified_already_applied_patch( else ">=1.0.1" ), "nodes": ["child@1.0.0"], + "range": "<=1.0.0", } ] } @@ -965,14 +1000,16 @@ def invoke(command, **kwargs): return subprocess.CompletedProcess(command, 1, json.dumps(response), "") if args[0] == "view": value, code = "1.0.1", 0 - if args[1] in ("parent@1.0.0", "other-parent@1.0.0"): + if args[1] == "child@1.0.0 <=1.0.0": + value = "1.0.0" + elif args[1] in ("parent@1.0.0", "other-parent@1.0.0"): value = {"dependencies": {"child": "^1.0.0"}} elif metadata == "unavailable": value, code = {"error": {"code": "ETIMEDOUT"}}, 1 elif metadata == "unrelated-target": value = "2.0.0" if args[1] == "child@>=2.0.0" else [] return subprocess.CompletedProcess(command, code, json.dumps(value), "") - assert args[0] == "install" + assert args[:3] == ["update", "--no-save", "child"] exclusions = yaml.safe_load(workspace.read_bytes())["minimumReleaseAgeExclude"] assert ("child@1.0.1" in exclusions) == retained lockfile.write_bytes(before[1]["pnpm-lock.yaml"]) @@ -981,17 +1018,14 @@ def invoke(command, **kwargs): return subprocess.CompletedProcess(command, 0, "", "") monkeypatch.setattr(subprocess, "run", invoke) - assert ( - module["fix_security"]( - project, - float("inf"), - [], - before="Initial audit unavailable" - if metadata == "missing-initial" - else initial_audit, - ).returncode - == 0 - ) + assert module["fix_security"]( + project, + float("inf"), + [], + before="Initial audit unavailable" + if metadata == "missing-initial" + else initial_audit, + ).returncode == (0 if retained and not already_locked else 1) assert not module["restore_invalid_state"]( project, before, [], "Security fix", security_fix=True ) diff --git a/node/tests/test_maintenance_options.py b/node/tests/test_maintenance_options.py index 6293b97..aff58aa 100644 --- a/node/tests/test_maintenance_options.py +++ b/node/tests/test_maintenance_options.py @@ -969,7 +969,7 @@ def invoke(directory, *args, **kwargs): value["importers"]["."]["dependencies"]["renderer"]["version"] = "1.3.0" lock_path.write_text(yaml.safe_dump(value)) return subprocess.CompletedProcess(args, 1, "", "") - if args[0] == "install" and "--lockfile-only" in args: + if args[0] == "update" and "--lockfile-only" in args: value = yaml.safe_load(yaml.safe_dump(resolution_base)) value["importers"]["."]["dependencies"]["renderer"]["version"] = "1.3.0" value["importers"]["."]["dependencies"]["unaffected"]["version"] = "1.1.0" @@ -989,9 +989,13 @@ def audit(*args, **kwargs): monkeypatch.setitem(helper["prepare"].__globals__, "run", invoke) monkeypatch.setitem(helper["prepare"].__globals__, "audit", audit) - monkeypatch.setitem( - helper["prepare"].__globals__, "describe_pnpm_fixes", lambda *a, **k: None - ) + + def describe(directory, findings, *args, **kwargs): + for detail in findings["vulnerabilities"].get("renderer", {}).get("via", []): + detail["fixVersion"] = "1.3.0" + detail["_verifiedVulnerableVersions"] = ["1.2.3"] + + monkeypatch.setitem(helper["prepare"].__globals__, "describe_pnpm_fixes", describe) report = helper["prepare"](site, maintenance_mode="security-only") final = helper["valid_snapshot"](site) assert helper["direct_versions"](final) == helper["direct_versions"](original) @@ -1131,7 +1135,7 @@ def invoke(directory, *args, **kwargs): ) if ( package_manager == "pnpm" - and args[0] == "install" + and args[0] == "update" and "--lockfile-only" in args ): write_fixed_lock() @@ -1139,9 +1143,12 @@ def invoke(directory, *args, **kwargs): monkeypatch.setitem(helper["prepare"].__globals__, "audit", audit) monkeypatch.setitem(helper["prepare"].__globals__, "run", invoke) - monkeypatch.setitem( - helper["prepare"].__globals__, "describe_pnpm_fixes", lambda *a, **k: None - ) + + def describe(directory, findings, *args, **kwargs): + for detail in findings["vulnerabilities"].get("fixed", {}).get("via", []): + detail["fixVersion"] = "1.1.0" + + monkeypatch.setitem(helper["prepare"].__globals__, "describe_pnpm_fixes", describe) monkeypatch.setitem( helper["prepare"].__globals__, "describe_npm_fixes", lambda *a, **k: None ) @@ -1354,7 +1361,7 @@ def invoke(directory, *args, **kwargs): ) if ( package_manager == "pnpm" - and args[0] == "install" + and args[0] == "update" and "--lockfile-only" in args ): write_fixed_lock() @@ -1362,9 +1369,12 @@ def invoke(directory, *args, **kwargs): monkeypatch.setitem(helper["prepare"].__globals__, "audit", audit) monkeypatch.setitem(helper["prepare"].__globals__, "run", invoke) - monkeypatch.setitem( - helper["prepare"].__globals__, "describe_pnpm_fixes", lambda *a, **k: None - ) + + def describe(directory, findings, *args, **kwargs): + for detail in findings["vulnerabilities"].get("child", {}).get("via", []): + detail["fixVersion"] = "1.1.0" + + monkeypatch.setitem(helper["prepare"].__globals__, "describe_pnpm_fixes", describe) monkeypatch.setitem( helper["prepare"].__globals__, "describe_npm_fixes", lambda *a, **k: None ) @@ -1653,14 +1663,17 @@ def test_security_only_allows_related_transitive_lockfile_changes(site): assert helper["security_only_direct_violation"](before, after, {"renderer"}) is None -def test_security_only_discards_disjoint_transitive_regeneration(site, monkeypatch): +@pytest.mark.parametrize("unrelated_churn", [False, True]) +def test_security_only_refresh_keeps_fixes_without_disjoint_churn( + site, monkeypatch, unrelated_churn +): manifest = json.loads((site / "package.json").read_text()) manifest["dependencies"] = {"renderer": "^1.2.3", "carrier": "1.0.0"} (site / "package.json").write_text(json.dumps(manifest)) - def write_lock(fixed): + def write_lock(fixed, *, churn=False): renderer = "1.3.0" if fixed else "1.2.3" - unrelated = "2.1.0" if fixed else "2.0.0" + unrelated = "2.1.0" if churn else "2.0.0" lock = { "lockfileVersion": "9.0", "importers": { @@ -1694,9 +1707,9 @@ def transport(directory, *args, **kwargs): commands.append(args) if args[:2] == ("audit", "--fix=update"): return subprocess.CompletedProcess(args, 1, "", "") - if args[:2] == ("install", "--lockfile-only"): - assert not (site / "pnpm-lock.yaml").exists() - write_lock(True) + if args[:2] == ("update", "--no-save"): + assert "renderer" in args and "unrelated" not in args + write_lock(True, churn=unrelated_churn) if args[:2] == ("audit", "--json"): fixed = ( "renderer@1.3.0" @@ -1747,8 +1760,9 @@ def transport(directory, *args, **kwargs): assert "unrelated@2.0.0" in lock["packages"] assert "unrelated@2.1.0" not in lock["packages"] assert validation["passed"] - assert "Discarded dependency changes" in report - assert any(args[:2] == ("install", "--lockfile-only") for args in commands) + assert ("renderer@1.3.0" in lock["packages"]) is not unrelated_churn + assert ("Discarded dependency changes" in report) is unrelated_churn + assert not any(args[:2] == ("install", "--lockfile-only") for args in commands) @pytest.mark.parametrize("update_renderer_child", [False, True]) @@ -1825,8 +1839,8 @@ def transport(directory, *args, **kwargs): commands.append(args) if args[:2] == ("audit", "--fix=update"): return subprocess.CompletedProcess(args, 1, "", "") - if args[:2] == ("install", "--lockfile-only"): - assert not (site / "pnpm-lock.yaml").exists() + if args[:2] == ("update", "--no-save"): + assert (site / "pnpm-lock.yaml").exists() write_lock(True) if args[:2] == ("audit", "--json"): fixed = ( @@ -1880,7 +1894,7 @@ def transport(directory, *args, **kwargs): assert "intermediate@2.0.0(peer@2.0.0)" in final["snapshots"] assert validation["passed"] assert "Discarded dependency changes" in report - assert any(args[:2] == ("install", "--lockfile-only") for args in commands) + assert any(args[:2] == ("update", "--no-save") for args in commands) @pytest.mark.parametrize("nested", [False, True]) diff --git a/node/tests/test_node_standard.py b/node/tests/test_node_standard.py index 8b12b7e..58a3ba4 100644 --- a/node/tests/test_node_standard.py +++ b/node/tests/test_node_standard.py @@ -1427,13 +1427,7 @@ def invoke(command, *, cwd, **kwargs): ], ["audit", "--fix=update", "--audit-level", "info", "--ignore-scripts"], ] - resolution = next(args for args in commands[2:] if args[0] == "install") - assert resolution[:4] == [ - "install", - "--lockfile-only", - "--no-frozen-lockfile", - "--ignore-scripts", - ] + assert not any(args[0] in {"install", "update"} for args in commands[2:]) lock = audit_module["valid_snapshot"](pnpm_project)[3] for report in (format_remaining_audit(data, lock), format_audit(data, lock)): for detail in ( diff --git a/node/tests/test_security_resolution_targets.py b/node/tests/test_security_resolution_targets.py new file mode 100644 index 0000000..9c2d8b1 --- /dev/null +++ b/node/tests/test_security_resolution_targets.py @@ -0,0 +1,106 @@ +import copy +import runpy +import subprocess +from pathlib import Path + +import pytest +import yaml +from test_dependency_audit_repairs import project as project + +engine = runpy.run_path( + str(Path(__file__).parents[1] / "runtime/templates/dependency_audit.py") +) + + +@pytest.mark.parametrize("name", ["child", "@scope/child"]) +@pytest.mark.parametrize("failure", [None, "exit", "invalid"]) +def test_verified_target_refresh_preserves_lock_and_rolls_back_failures( + project, monkeypatch, name, failure +): + lockfile = project / "pnpm-lock.yaml" + lock = yaml.safe_load(lockfile.read_bytes()) + lock["packages"].pop("child@1.0.0") + lock["packages"][name + "@1.0.0"] = {} + lock["snapshots"]["parent@1.0.0"]["dependencies"] = {name: "1.0.0"} + lockfile.write_text(yaml.safe_dump(lock)) + initial = engine["valid_snapshot"](project) + commands = [] + checkpoints = [] + + def invoke(directory, *args, **kwargs): + commands.append(args) + if args[0] == "audit": + return subprocess.CompletedProcess(args, 1, "remaining findings", "") + assert args[:2] == ("update", "--no-save") + checkpoints.append(engine["valid_snapshot"](project)) + assert args[2] == name and "--ignore-scripts" in args + assert lockfile.read_bytes() == initial[1]["pnpm-lock.yaml"] + modules = next( + a.split("=", 1)[1] for a in args if a.startswith("--config.modules-dir=") + ) + assert Path(modules) != project / "node_modules" + assert f"--config.virtual-store-dir={Path(modules) / '.pnpm'}" in args + proposed = copy.deepcopy(lock) + proposed["packages"].pop(name + "@1.0.0") + proposed["packages"][name + "@1.0.1"] = {} + proposed["snapshots"]["parent@1.0.0"]["dependencies"][name] = "1.0.1" + lockfile.write_text("[" if failure == "invalid" else yaml.safe_dump(proposed)) + return subprocess.CompletedProcess(args, 2 if failure == "exit" else 0, "", "") + + def describe(directory, data, *args, **kwargs): + for finding in data["vulnerabilities"].values(): + for detail in finding["via"]: + detail.update(fixVersion="1.0.1", _verifiedVulnerableVersions=["1.0.0"]) + + monkeypatch.setitem(engine["fix_security"].__globals__, "run", invoke) + monkeypatch.setitem( + engine["fix_security"].__globals__, "describe_pnpm_fixes", describe + ) + notes = [] + before = {"package_manager": "pnpm", "vulnerabilities": {name: {"via": [{}]}}} + engine["fix_security"](project, float("inf"), notes, before=before) + assert len(commands) == 2 + if failure: + assert engine["valid_snapshot"](project) == checkpoints[0] + assert "refresh failed" in notes[-1] + else: + assert name + "@1.0.1" in engine["valid_snapshot"](project)[3]["packages"] + assert (project / "package.json").read_bytes() == initial[0] + + +@pytest.mark.parametrize("reason", ["mixed-versions", "no-proof", "option-name"]) +def test_unverified_or_mixed_targets_never_trigger_broad_refresh( + project, monkeypatch, reason +): + name = "--latest" if reason == "option-name" else "child" + lockfile = project / "pnpm-lock.yaml" + lock = yaml.safe_load(lockfile.read_bytes()) + if reason == "mixed-versions": + lock["packages"]["child@2.0.0"] = {} + elif reason == "option-name": + lock["packages"].pop("child@1.0.0") + lock["packages"]["--latest@1.0.0"] = {} + lockfile.write_text(yaml.safe_dump(lock)) + initial = engine["valid_snapshot"](project) + + def invoke(directory, *args, **kwargs): + assert args[:2] == ("audit", "--fix=update") + return subprocess.CompletedProcess(args, 1, "remaining findings", "") + + def describe(directory, data, *args, **kwargs): + for finding in data["vulnerabilities"].values(): + for detail in finding["via"]: + if reason != "no-proof": + detail.update( + fixVersion="1.0.1", _verifiedVulnerableVersions=["1.0.0"] + ) + + monkeypatch.setitem(engine["fix_security"].__globals__, "run", invoke) + monkeypatch.setitem( + engine["fix_security"].__globals__, "describe_pnpm_fixes", describe + ) + before = {"package_manager": "pnpm", "vulnerabilities": {name: {"via": [{}]}}} + result = engine["fix_security"](project, float("inf"), before=before) + assert result.returncode == 1 + assert (project / "package.json").read_bytes() == initial[0] + assert lockfile.read_bytes() == initial[1]["pnpm-lock.yaml"] diff --git a/node/tests/test_transferred_invariants.py b/node/tests/test_transferred_invariants.py index a975b5b..4220668 100644 --- a/node/tests/test_transferred_invariants.py +++ b/node/tests/test_transferred_invariants.py @@ -105,12 +105,7 @@ def invoke(command, *, cwd, **kwargs): ], ["audit", "--fix=update", "--audit-level", "info", "--ignore-scripts"], ] - assert commands[2][:4] == [ - "install", - "--lockfile-only", - "--no-frozen-lockfile", - "--ignore-scripts", - ] + assert not any(args[0] in {"install", "update"} for args in commands[2:]) lock = audit_module["valid_snapshot"](pnpm_project)[3] for report in (format_remaining_audit(data, lock), format_audit(data, lock)): for detail in ( diff --git a/node/tests/test_transferred_repairs.py b/node/tests/test_transferred_repairs.py index 36e0f5c..1a8ab6a 100644 --- a/node/tests/test_transferred_repairs.py +++ b/node/tests/test_transferred_repairs.py @@ -268,12 +268,19 @@ def invoke(command, *, cwd, **kwargs): commands.append(args) if args[0] == "audit": return subprocess.CompletedProcess(command, 1, "5 advisories remain", "") - assert not (cwd / "pnpm-lock.yaml").exists() + assert (cwd / "pnpm-lock.yaml").exists() + assert args[:3] == ["update", "--no-save", "child"] assert "--ignore-scripts" in args and "--force" not in args assert "--config.optimistic-repeat-install=false" in args - modules = Path(args[args.index("--modules-dir") + 1]) + modules = Path( + next( + a.split("=", 1)[1] + for a in args + if a.startswith("--config.modules-dir=") + ) + ) assert modules != cwd / "node_modules" - assert Path(args[args.index("--virtual-store-dir") + 1]) == modules / ".pnpm" + assert f"--config.virtual-store-dir={modules / '.pnpm'}" in args (cwd / "pnpm-lock.yaml").write_bytes(snapshot[1]["pnpm-lock.yaml"]) if failure == "invalid": (cwd / "pnpm-lock.yaml").write_text("[") @@ -282,17 +289,30 @@ def invoke(command, *, cwd, **kwargs): return subprocess.CompletedProcess(command, int(failure == "exit"), "", "") monkeypatch.setattr(subprocess, "run", invoke) + monkeypatch.setitem( + module["fix_security"].__globals__, "describe_pnpm_fixes", lambda *a, **k: None + ) notes = [] - module["fix_security"](project, float("inf"), notes) + evidence = { + "package_manager": "pnpm", + "vulnerabilities": { + "child": { + "via": [ + {"fixVersion": "1.0.1", "_verifiedVulnerableVersions": ["1.0.0"]} + ] + } + }, + } + module["fix_security"](project, float("inf"), notes, before=evidence) assert len(commands) == 2 if failure: assert (project / "pnpm-lock.yaml").read_bytes() == snapshot[1][ "pnpm-lock.yaml" ] - assert "regeneration failed" in notes[0] + assert "refresh failed" in notes[0] else: assert "child@1.0.1" in module["valid_snapshot"](project)[3]["packages"] - assert "Regenerated" in notes[0] + assert "Refreshed" in notes[0] def test_weakened_policy_prevents_fresh_resolution(project, monkeypatch):