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
7 changes: 7 additions & 0 deletions node/maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 35 additions & 12 deletions node/runtime/templates/dependency_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,33 +830,56 @@ 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
if failed:
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
Expand Down
4 changes: 1 addition & 3 deletions node/tests/test_dependency_audit_release_age.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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
Expand All @@ -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"
Expand Down
82 changes: 58 additions & 24 deletions node/tests/test_dependency_audit_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("[")
Expand All @@ -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):
Expand Down Expand Up @@ -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())
Expand All @@ -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")
Expand All @@ -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
)
Expand All @@ -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])
Expand Down Expand Up @@ -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",
}
]
}
Expand Down Expand Up @@ -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"])
Expand All @@ -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
)
Expand Down
Loading
Loading