Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion node/maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ 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
regenerate every dependency. It rebuilds only verified vulnerable package and
snapshot records, retaining parent references and all other locked records.
This prevents a shared parent's stale snapshot from replacing a resolved patch.
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.
Expand Down
14 changes: 12 additions & 2 deletions node/runtime/templates/dependency_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def fix_security(
# 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 = []
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(
Expand All @@ -850,14 +850,24 @@ def fix_security(
for version in detail.get("_verifiedVulnerableVersions", [])
}
if locked and locked <= vulnerable:
targets.append(name)
targets[name] = vulnerable
if not targets:
if not verified:
restore_snapshot(directory, snapshot)
diagnostics.append(
"Skipped pnpm security refresh: no verified wholly vulnerable package targets."
)
return result
# A shared parent can reuse a vulnerable snapshot after another path
# resolves its patch. Rebuild only proven vulnerable target records;
# retain parent references so pnpm still applies their constraints.
lock = yaml.safe_load(current[1]["pnpm-lock.yaml"])
for section in ("packages", "snapshots"):
for key in list(lock.get(section, {})):
name, _, version = key.split("(", 1)[0].rpartition("@")
if version in targets.get(name, set()):
del lock[section][key]
(directory / "pnpm-lock.yaml").write_text(yaml.safe_dump(lock, sort_keys=False))
with TemporaryDirectory(prefix="dependency-audit-resolution-") as modules:
resolved = run(
directory,
Expand Down
40 changes: 34 additions & 6 deletions node/tests/test_security_resolution_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,54 @@

@pytest.mark.parametrize("name", ["child", "@scope/child"])
@pytest.mark.parametrize("failure", [None, "exit", "invalid"])
@pytest.mark.parametrize("already_patched", [False, True])
def test_verified_target_refresh_preserves_lock_and_rolls_back_failures(
project, monkeypatch, name, failure
project, monkeypatch, name, failure, already_patched
):
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.setdefault("snapshots", {})[name + "@1.0.0"] = {}
lock["snapshots"][name + "@1.0.0(peer@2.0.0)"] = {"optional": True}
lock["packages"]["unrelated@2.0.0"] = {"resolution": {"integrity": "keep"}}
lock["snapshots"]["unrelated@2.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)
preceding = copy.deepcopy(lock)
if already_patched:
preceding["packages"][name + "@1.0.1"] = {"resolution": {"integrity": "keep"}}
preceding["snapshots"][name + "@1.0.1"] = {}
preceding_bytes = yaml.safe_dump(preceding).encode()
commands = []
checkpoints = []

def invoke(directory, *args, **kwargs):
commands.append(args)
if args[0] == "audit":
lockfile.write_bytes(preceding_bytes)
return subprocess.CompletedProcess(args, 1, "remaining findings", "")
assert args[:2] == ("update", "--no-save")
checkpoints.append(engine["valid_snapshot"](project))
checkpoints.append((project / "pnpm-workspace.yaml").read_bytes())
assert args[2] == name and "--ignore-scripts" in args
assert lockfile.read_bytes() == initial[1]["pnpm-lock.yaml"]
retained = yaml.safe_load(lockfile.read_bytes())
expected = copy.deepcopy(preceding)
for section in ("packages", "snapshots"):
expected[section].pop(name + "@1.0.0")
expected["snapshots"].pop(name + "@1.0.0(peer@2.0.0)")
assert retained == expected
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 = copy.deepcopy(preceding)
proposed["packages"].pop(name + "@1.0.0")
proposed["packages"][name + "@1.0.1"] = {}
proposed["snapshots"].pop(name + "@1.0.0")
proposed["snapshots"].pop(name + "@1.0.0(peer@2.0.0)")
proposed["snapshots"][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, "", "")
Expand All @@ -61,10 +80,19 @@ def describe(directory, data, *args, **kwargs):
engine["fix_security"](project, float("inf"), notes, before=before)
assert len(commands) == 2
if failure:
assert engine["valid_snapshot"](project) == checkpoints[0]
assert lockfile.read_bytes() == preceding_bytes
assert (project / "package.json").read_bytes() == initial[0]
assert (project / "pnpm-workspace.yaml").read_bytes() == checkpoints[0]
assert "refresh failed" in notes[-1]
else:
assert name + "@1.0.1" in engine["valid_snapshot"](project)[3]["packages"]
final = engine["valid_snapshot"](project)
assert name + "@1.0.1" in final[3]["packages"]
assert (
yaml.safe_load(final[1]["pnpm-lock.yaml"])["snapshots"]["parent@1.0.0"][
"dependencies"
][name]
== "1.0.1"
)
assert (project / "package.json").read_bytes() == initial[0]


Expand Down
Loading