cve-lite-cli 1.41.1 → 1.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/output/formatters.js +5 -3
- package/dist/output/html-reporter.js +2 -1
- package/dist/overrides/detectors/pd001-override-only-phantom.js +2 -3
- package/dist/overrides/detectors/pd002-transitive-only-phantom.js +2 -3
- package/dist/overrides/detectors/phantom-utils.js +5 -0
- package/dist/overrides/parsing/package-json.js +1 -1
- package/dist/parsers/npm-alias-resolution.js +248 -0
- package/dist/parsers/npm-lock-graph.js +77 -33
- package/dist/remediation/fix-commands.js +6 -2
- package/dist/remediation/transitive-chain-resolver.js +2 -2
- package/dist/scanner.js +2 -1
- package/dist/utils/package-json.js +1 -0
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@ import { inferSeverity } from "../osv/severity.js";
|
|
|
6
6
|
import { getPrimaryParent, getRootDependencies, isUnconfirmedFix } from "../utils/finding.js";
|
|
7
7
|
import { calculatePathCoverage, formatDependencyPath } from "../utils/path-coverage.js";
|
|
8
8
|
import { pluralize, isOutputStdout } from "../utils/string.js";
|
|
9
|
+
import { shellQuote } from "../utils/shell-quote.js";
|
|
9
10
|
import { hasMaliciousAdvisory } from "../utils/vuln.js";
|
|
10
11
|
import { computePrioritySignal } from "../utils/priority-signal.js";
|
|
11
12
|
import { computeContextualSignals } from "../utils/contextual-signals.js";
|
|
@@ -113,10 +114,11 @@ export function getRecommendedAction(finding) {
|
|
|
113
114
|
// a degenerate length-2 chain (project -> vulnerable, common when the
|
|
114
115
|
// package is hoisted to top-level node_modules). The advice is the same
|
|
115
116
|
// either way - inspect the lockfile or run `<pm> ls` to find the parent.
|
|
117
|
+
const inspectMessage = `No parent dependency was identified for ${finding.pkg.name} in the lockfile. Run \`npm ls ${shellQuote(finding.pkg.name)}\` (or your package manager equivalent) to find which package pulls it in`;
|
|
116
118
|
if (directTarget) {
|
|
117
|
-
return
|
|
119
|
+
return `${inspectMessage}, then upgrade toward ${directTarget}+.`;
|
|
118
120
|
}
|
|
119
|
-
return
|
|
121
|
+
return `${inspectMessage}.`;
|
|
120
122
|
}
|
|
121
123
|
export function summarizeRisk(finding) {
|
|
122
124
|
if (finding.maliciousUnverifiable) {
|
|
@@ -228,7 +230,7 @@ export function summarizeNextAction(finding) {
|
|
|
228
230
|
return `Upgrade ${parent} - no safe version identified. Find a release resolving ${finding.pkg.name} to ${directTarget}+.`;
|
|
229
231
|
}
|
|
230
232
|
if (directTarget) {
|
|
231
|
-
return `No parent identified in the lockfile. Run \`npm ls ${finding.pkg.name}\` to find the parent, then upgrade toward ${directTarget}+.`;
|
|
233
|
+
return `No parent identified in the lockfile. Run \`npm ls ${shellQuote(finding.pkg.name)}\` to find the parent, then upgrade toward ${directTarget}+.`;
|
|
232
234
|
}
|
|
233
235
|
return `Inspect the parent dependency chain for ${finding.pkg.name} and choose the safest available upgrade.`;
|
|
234
236
|
}
|
|
@@ -6,6 +6,7 @@ import { serializeFinding, summarizeNextAction, summarizeRisk, formatCooldownWar
|
|
|
6
6
|
import { loadLogoDataUri } from "./logos.js";
|
|
7
7
|
import { isBreakingUpgrade } from "../utils/version.js";
|
|
8
8
|
import { pluralize } from "../utils/string.js";
|
|
9
|
+
import { shellQuote } from "../utils/shell-quote.js";
|
|
9
10
|
import { MAL_GIT_SOURCE_PINNED_DISPLAY, MAL_GIT_SOURCE_FLOATING_DISPLAY, severityOrder } from "../constants.js";
|
|
10
11
|
import { hasMaliciousAdvisory } from "../utils/vuln.js";
|
|
11
12
|
import { renderOverrideFindingsHtml } from "./override-findings-html.js";
|
|
@@ -739,7 +740,7 @@ function renderTransitiveContextCol(finding) {
|
|
|
739
740
|
return `<div class="detail-col" style="max-width:200px">
|
|
740
741
|
<h4>Context</h4>
|
|
741
742
|
<span class="tier-err">✕ No parent identified</span>
|
|
742
|
-
<p style="font-size:12px;color:var(--text-2);margin-top:4px">Run <code>npm ls ${escapeHtml(finding.package)}</code> to find which package pulls it in.</p>
|
|
743
|
+
<p style="font-size:12px;color:var(--text-2);margin-top:4px">Run <code>npm ls ${escapeHtml(shellQuote(finding.package))}</code> to find which package pulls it in.</p>
|
|
743
744
|
</div>`;
|
|
744
745
|
}
|
|
745
746
|
export function renderCompletenessBanner(completeness) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { formatPhantomInstallCommand, getDeclaredPackages, undeclaredImportFiles } from "./phantom-utils.js";
|
|
2
2
|
const RULE_ID = "PD001";
|
|
3
3
|
export function detect(ctx) {
|
|
4
4
|
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
@@ -7,7 +7,6 @@ export function detect(ctx) {
|
|
|
7
7
|
return [];
|
|
8
8
|
const declared = getDeclaredPackages(ctx.packageJson);
|
|
9
9
|
const overrideNames = new Set(ctx.overrideEntries.map((e) => e.packageName));
|
|
10
|
-
const cmd = installCmd(ctx.packageManager);
|
|
11
10
|
const findings = [];
|
|
12
11
|
for (const [pkgName, importedIn] of ctx.importedPackageNames) {
|
|
13
12
|
if (!overrideNames.has(pkgName))
|
|
@@ -25,7 +24,7 @@ export function detect(ctx) {
|
|
|
25
24
|
message: `${pkgName} is imported in source but only present via an override pin - declare it as a dependency`,
|
|
26
25
|
details: `Imported in: ${shown.join(", ")}${extra}. ` +
|
|
27
26
|
`In pnpm strict mode (default on Vercel), override-only packages are not importable and will cause a deploy failure. ` +
|
|
28
|
-
`Run: ${
|
|
27
|
+
`Run: ${formatPhantomInstallCommand(ctx.packageManager, pkgName)}`,
|
|
29
28
|
references: ["https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/PD001.md"],
|
|
30
29
|
});
|
|
31
30
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { formatPhantomInstallCommand, getDeclaredPackages, undeclaredImportFiles } from "./phantom-utils.js";
|
|
2
2
|
const RULE_ID = "PD002";
|
|
3
3
|
export function detect(ctx) {
|
|
4
4
|
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
@@ -7,7 +7,6 @@ export function detect(ctx) {
|
|
|
7
7
|
return [];
|
|
8
8
|
const declared = getDeclaredPackages(ctx.packageJson);
|
|
9
9
|
const overrideNames = new Set(ctx.overrideEntries.map((e) => e.packageName));
|
|
10
|
-
const cmd = installCmd(ctx.packageManager);
|
|
11
10
|
const findings = [];
|
|
12
11
|
for (const [pkgName, importedIn] of ctx.importedPackageNames) {
|
|
13
12
|
if (overrideNames.has(pkgName))
|
|
@@ -27,7 +26,7 @@ export function detect(ctx) {
|
|
|
27
26
|
message: `${pkgName} is imported in source but only present as a transitive dependency - declare it explicitly`,
|
|
28
27
|
details: `Imported in: ${shown.join(", ")}${extra}. ` +
|
|
29
28
|
`If the parent package drops or changes this dependency, your code will break without warning. ` +
|
|
30
|
-
`Run: ${
|
|
29
|
+
`Run: ${formatPhantomInstallCommand(ctx.packageManager, pkgName)}`,
|
|
31
30
|
references: ["https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/PD002.md"],
|
|
32
31
|
});
|
|
33
32
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { shellQuote } from "../../utils/shell-quote.js";
|
|
1
2
|
export function getDeclaredPackages(packageJson) {
|
|
2
3
|
const declared = new Set();
|
|
3
4
|
for (const section of ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]) {
|
|
@@ -48,3 +49,7 @@ export function installCmd(pm) {
|
|
|
48
49
|
return "bun add";
|
|
49
50
|
return "npm install";
|
|
50
51
|
}
|
|
52
|
+
/** Format a copyable phantom-dependency install command with shell-quoted package name. */
|
|
53
|
+
export function formatPhantomInstallCommand(pm, pkgName) {
|
|
54
|
+
return `${installCmd(pm)} ${shellQuote(pkgName)}`;
|
|
55
|
+
}
|
|
@@ -49,7 +49,7 @@ export function bareName(overrideKey) {
|
|
|
49
49
|
* or -1 if there is none. For scoped names the leading `@` (index 0) is the
|
|
50
50
|
* scope, not a specifier, so we look past it.
|
|
51
51
|
*/
|
|
52
|
-
function specifierAt(selector) {
|
|
52
|
+
export function specifierAt(selector) {
|
|
53
53
|
if (selector.startsWith('@')) {
|
|
54
54
|
return selector.indexOf('@', 1);
|
|
55
55
|
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { bareName, specifierAt } from "../overrides/parsing/package-json.js";
|
|
4
|
+
import { isGitSource } from "../utils/advisory.js";
|
|
5
|
+
import { isRecord } from "../utils/package-json.js";
|
|
6
|
+
import { npmAliasRegistryName } from "../utils/package-identity.js";
|
|
7
|
+
import { isValidRange, satisfiesRange } from "../utils/version.js";
|
|
8
|
+
/**
|
|
9
|
+
* Resolve true npm registry identities onto lockfile graph nodes without
|
|
10
|
+
* re-attributing non-registry git/tarball packages or un-installed peers.
|
|
11
|
+
*/
|
|
12
|
+
export function resolveNpmLockAliasIdentities(ctx) {
|
|
13
|
+
const overrideRules = loadOverrideAliasRules(ctx.filePath, ctx.rootMeta);
|
|
14
|
+
const legacyAliasByPath = collectLegacyAliasRegistryNames(ctx.rawLegacyDependencies);
|
|
15
|
+
for (const node of ctx.nodesById.values()) {
|
|
16
|
+
const registryName = resolveNodeRegistryName(node, ctx, overrideRules, legacyAliasByPath);
|
|
17
|
+
if (registryName) {
|
|
18
|
+
node.registryName = registryName;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Format the canonical key tracking a fallback edge added from `peerDependencies`. */
|
|
23
|
+
export function peerFallbackEdgeKey(parentNodeId, childNodeId) {
|
|
24
|
+
return `${parentNodeId}\u0000${childNodeId}`;
|
|
25
|
+
}
|
|
26
|
+
function collectRegularDeclaredRanges(node, ctx) {
|
|
27
|
+
const ranges = [];
|
|
28
|
+
if (ctx.rootEntryNodeIds.has(node.id)) {
|
|
29
|
+
const rootRange = ctx.rootRanges[node.name];
|
|
30
|
+
if (rootRange)
|
|
31
|
+
ranges.push(rootRange);
|
|
32
|
+
}
|
|
33
|
+
for (const parentId of ctx.parentNodeIdsByChildNodeId.get(node.id) ?? []) {
|
|
34
|
+
if (ctx.peerFallbackEdgeKeys.has(peerFallbackEdgeKey(parentId, node.id)))
|
|
35
|
+
continue;
|
|
36
|
+
const range = ctx.rangeByParentNodeId.get(parentId)?.get(node.name);
|
|
37
|
+
if (range)
|
|
38
|
+
ranges.push(range);
|
|
39
|
+
}
|
|
40
|
+
return ranges;
|
|
41
|
+
}
|
|
42
|
+
function resolveNodeRegistryName(node, ctx, overrideRules, legacyAliasByPath) {
|
|
43
|
+
const regularDeclaredRanges = collectRegularDeclaredRanges(node, ctx);
|
|
44
|
+
const manifestName = ctx.manifestNameByNodeId.get(node.id);
|
|
45
|
+
const resolvedUrl = ctx.resolvedUrlByNodeId.get(node.id);
|
|
46
|
+
// 1. Explicit `npm:` spec on root or regular parent dependencies.
|
|
47
|
+
const explicitAlias = regularDeclaredRanges
|
|
48
|
+
.map(npmAliasRegistryName)
|
|
49
|
+
.find((name) => name && name !== node.name);
|
|
50
|
+
if (explicitAlias)
|
|
51
|
+
return explicitAlias;
|
|
52
|
+
// 2. Lockfile v2 legacy `dependencies` entry.
|
|
53
|
+
const legacyAlias = legacyAliasByPath.get(node.packagePath);
|
|
54
|
+
if (legacyAlias && legacyAlias !== node.name && (!manifestName || manifestName === legacyAlias)) {
|
|
55
|
+
return legacyAlias;
|
|
56
|
+
}
|
|
57
|
+
// Remaining paths rely on a differing installed manifest `name`.
|
|
58
|
+
if (!manifestName || manifestName === node.name)
|
|
59
|
+
return null;
|
|
60
|
+
// 3. Peer-derived `npm:` alias: only trust when the node's lockfile manifest `name`
|
|
61
|
+
// matches the alias target, so `--legacy-peer-deps` or optional peers cannot
|
|
62
|
+
// substitute an alias identity onto a different real package installed in that slot.
|
|
63
|
+
if (!isGitSource({ resolvedUrl }) &&
|
|
64
|
+
(regularDeclaredRanges.length === 0 || regularDeclaredRanges.every(looksLikeRegistryVersionSpec))) {
|
|
65
|
+
const peerRanges = ctx.peerRangesByChildNodeId.get(node.id) ?? [];
|
|
66
|
+
const peerAlias = peerRanges
|
|
67
|
+
.map(npmAliasRegistryName)
|
|
68
|
+
.find((name) => name === manifestName);
|
|
69
|
+
if (peerAlias)
|
|
70
|
+
return peerAlias;
|
|
71
|
+
}
|
|
72
|
+
// 4. Configured `overrides` (package.json or root lockfile metadata).
|
|
73
|
+
if (!isGitSource({ resolvedUrl })) {
|
|
74
|
+
const matchedRule = overrideRules.find((rule) => rule.target.name === node.name &&
|
|
75
|
+
rule.registryName === manifestName &&
|
|
76
|
+
matchesAncestorChain(node.id, rule.parentChain, ctx));
|
|
77
|
+
if (matchedRule)
|
|
78
|
+
return matchedRule.registryName;
|
|
79
|
+
}
|
|
80
|
+
// 5. Standalone v3 lockfiles omit `overrides` and replace the child spec only in
|
|
81
|
+
// node metadata; accept standard registry tarball layouts (including private
|
|
82
|
+
// mirrors) when every regular parent spec is a registry range/tag (#1450).
|
|
83
|
+
if (!isGitSource({ resolvedUrl }) &&
|
|
84
|
+
regularDeclaredRanges.length > 0 &&
|
|
85
|
+
regularDeclaredRanges.every(looksLikeRegistryVersionSpec) &&
|
|
86
|
+
isRegistryTarballForPackage(resolvedUrl, manifestName, node.version)) {
|
|
87
|
+
return manifestName;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
function loadOverrideAliasRules(lockfilePath, rootMeta) {
|
|
92
|
+
const pkgJson = readJsonObject(path.join(path.dirname(lockfilePath), "package.json"));
|
|
93
|
+
const rootManifestDeps = collectRootManifestDeps(rootMeta, pkgJson);
|
|
94
|
+
const rules = [];
|
|
95
|
+
walkOverrideAliasRules(pkgJson?.overrides, [], rootManifestDeps, rules);
|
|
96
|
+
walkOverrideAliasRules(rootMeta?.overrides, [], rootManifestDeps, rules);
|
|
97
|
+
return rules;
|
|
98
|
+
}
|
|
99
|
+
function readJsonObject(filePath) {
|
|
100
|
+
try {
|
|
101
|
+
const parsed = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
102
|
+
return isRecord(parsed) ? parsed : null;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function collectRootManifestDeps(rootMeta, pkgJson) {
|
|
109
|
+
const deps = {};
|
|
110
|
+
const sections = ["peerDependencies", "devDependencies", "optionalDependencies", "dependencies"];
|
|
111
|
+
for (const source of [rootMeta, pkgJson]) {
|
|
112
|
+
if (!source)
|
|
113
|
+
continue;
|
|
114
|
+
for (const sectionName of sections) {
|
|
115
|
+
const section = source[sectionName];
|
|
116
|
+
if (!isRecord(section))
|
|
117
|
+
continue;
|
|
118
|
+
for (const [name, spec] of Object.entries(section)) {
|
|
119
|
+
if (typeof spec === "string" && spec)
|
|
120
|
+
deps[name] = spec;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return deps;
|
|
125
|
+
}
|
|
126
|
+
function walkOverrideAliasRules(node, ancestors, rootManifestDeps, rules) {
|
|
127
|
+
if (!isRecord(node))
|
|
128
|
+
return;
|
|
129
|
+
for (const [key, value] of Object.entries(node)) {
|
|
130
|
+
if (key === ".") {
|
|
131
|
+
if (typeof value === "string" && ancestors.length > 0) {
|
|
132
|
+
const registryName = resolveOverrideAliasRegistryName(value, rootManifestDeps);
|
|
133
|
+
const target = ancestors[ancestors.length - 1];
|
|
134
|
+
if (registryName && target.name) {
|
|
135
|
+
rules.push({
|
|
136
|
+
parentChain: ancestors.slice(0, -1),
|
|
137
|
+
target,
|
|
138
|
+
registryName,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const selector = splitOverrideSelector(key);
|
|
145
|
+
if (!selector.name)
|
|
146
|
+
continue;
|
|
147
|
+
if (typeof value === "string") {
|
|
148
|
+
const registryName = resolveOverrideAliasRegistryName(value, rootManifestDeps);
|
|
149
|
+
if (registryName) {
|
|
150
|
+
rules.push({ parentChain: ancestors, target: selector, registryName });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (isRecord(value)) {
|
|
154
|
+
walkOverrideAliasRules(value, [...ancestors, selector], rootManifestDeps, rules);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function splitOverrideSelector(key) {
|
|
159
|
+
const trimmed = key.trim();
|
|
160
|
+
const at = specifierAt(trimmed);
|
|
161
|
+
if (at === -1)
|
|
162
|
+
return { name: trimmed };
|
|
163
|
+
const name = bareName(trimmed);
|
|
164
|
+
const range = trimmed.slice(at + 1).trim();
|
|
165
|
+
return range ? { name, range } : { name };
|
|
166
|
+
}
|
|
167
|
+
function resolveOverrideAliasRegistryName(rawSpec, rootManifestDeps) {
|
|
168
|
+
const trimmed = rawSpec.trim();
|
|
169
|
+
const resolved = trimmed.startsWith("$") ? rootManifestDeps[trimmed.slice(1)] : trimmed;
|
|
170
|
+
return npmAliasRegistryName(resolved);
|
|
171
|
+
}
|
|
172
|
+
function matchesAncestorChain(childNodeId, parentChain, ctx) {
|
|
173
|
+
if (parentChain.length === 0)
|
|
174
|
+
return true;
|
|
175
|
+
const visited = new Set();
|
|
176
|
+
const stack = [];
|
|
177
|
+
for (const parentId of ctx.parentNodeIdsByChildNodeId.get(childNodeId) ?? []) {
|
|
178
|
+
stack.push({ currentId: parentId, chainIndex: parentChain.length - 1 });
|
|
179
|
+
}
|
|
180
|
+
while (stack.length > 0) {
|
|
181
|
+
const { currentId, chainIndex } = stack.pop();
|
|
182
|
+
const visitKey = `${currentId}\u0000${chainIndex}`;
|
|
183
|
+
if (visited.has(visitKey))
|
|
184
|
+
continue;
|
|
185
|
+
visited.add(visitKey);
|
|
186
|
+
const parentNode = ctx.nodesById.get(currentId);
|
|
187
|
+
if (!parentNode)
|
|
188
|
+
continue;
|
|
189
|
+
const selector = parentChain[chainIndex];
|
|
190
|
+
const parentVersion = parentNode.version ?? ctx.ancestorVersionByNodeId.get(currentId) ?? null;
|
|
191
|
+
const selectorMatches = parentNode.name === selector.name &&
|
|
192
|
+
(!selector.range ||
|
|
193
|
+
!isValidRange(selector.range) ||
|
|
194
|
+
(parentVersion !== null && satisfiesRange(parentVersion, selector.range)));
|
|
195
|
+
if (selectorMatches) {
|
|
196
|
+
if (chainIndex === 0)
|
|
197
|
+
return true;
|
|
198
|
+
for (const grandParentId of ctx.parentNodeIdsByChildNodeId.get(currentId) ?? []) {
|
|
199
|
+
stack.push({ currentId: grandParentId, chainIndex: chainIndex - 1 });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Nested npm overrides apply through intermediate transitive dependencies.
|
|
203
|
+
for (const grandParentId of ctx.parentNodeIdsByChildNodeId.get(currentId) ?? []) {
|
|
204
|
+
stack.push({ currentId: grandParentId, chainIndex });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
function collectLegacyAliasRegistryNames(deps) {
|
|
210
|
+
const byPath = new Map();
|
|
211
|
+
const walk = (entries, prefix) => {
|
|
212
|
+
if (!isRecord(entries))
|
|
213
|
+
return;
|
|
214
|
+
for (const [name, meta] of Object.entries(entries)) {
|
|
215
|
+
if (!isRecord(meta))
|
|
216
|
+
continue;
|
|
217
|
+
const packagePath = prefix ? `${prefix}/node_modules/${name}` : `node_modules/${name}`;
|
|
218
|
+
const registryName = npmAliasRegistryName(meta.version);
|
|
219
|
+
if (registryName)
|
|
220
|
+
byPath.set(packagePath, registryName);
|
|
221
|
+
if (meta.dependencies)
|
|
222
|
+
walk(meta.dependencies, packagePath);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
walk(deps, "");
|
|
226
|
+
return byPath;
|
|
227
|
+
}
|
|
228
|
+
function looksLikeRegistryVersionSpec(spec) {
|
|
229
|
+
const trimmed = spec.trim();
|
|
230
|
+
if (!trimmed)
|
|
231
|
+
return false;
|
|
232
|
+
if (trimmed.startsWith("npm:"))
|
|
233
|
+
return true;
|
|
234
|
+
if (trimmed.includes(":") || trimmed.includes("/"))
|
|
235
|
+
return false;
|
|
236
|
+
return isValidRange(trimmed) || /^[A-Za-z0-9._-]+$/.test(trimmed);
|
|
237
|
+
}
|
|
238
|
+
function isRegistryTarballForPackage(resolvedUrl, manifestName, version) {
|
|
239
|
+
if (!resolvedUrl || !version)
|
|
240
|
+
return false;
|
|
241
|
+
const cleanUrl = resolvedUrl.split(/[?#]/, 1)[0];
|
|
242
|
+
const unscoped = manifestName.startsWith("@")
|
|
243
|
+
? manifestName.slice(manifestName.indexOf("/") + 1)
|
|
244
|
+
: manifestName;
|
|
245
|
+
if (!unscoped)
|
|
246
|
+
return false;
|
|
247
|
+
return cleanUrl.endsWith(`/${manifestName}/-/${unscoped}-${version}.tgz`);
|
|
248
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import { unique } from "../utils/array.js";
|
|
3
|
-
import {
|
|
3
|
+
import { isRecord } from "../utils/package-json.js";
|
|
4
|
+
import { peerFallbackEdgeKey, resolveNpmLockAliasIdentities, } from "./npm-alias-resolution.js";
|
|
4
5
|
export function loadNpmLockGraph(filePath, options) {
|
|
5
6
|
const raw = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
6
7
|
const packages = raw?.packages;
|
|
@@ -11,6 +12,9 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
11
12
|
const dependencyRangesByNodeId = new Map();
|
|
12
13
|
const resolutionBasePathsByNodeId = new Map();
|
|
13
14
|
const peerRangesByNodeId = new Map();
|
|
15
|
+
const manifestNameByNodeId = new Map();
|
|
16
|
+
const resolvedUrlByNodeId = new Map();
|
|
17
|
+
const ancestorVersionByNodeId = new Map();
|
|
14
18
|
const parentNodeIdsByChildNodeId = new Map();
|
|
15
19
|
const childNodeIdsByParentNodeId = new Map();
|
|
16
20
|
const rangeByParentNodeId = new Map();
|
|
@@ -49,19 +53,30 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
49
53
|
dev: !!meta?.dev,
|
|
50
54
|
});
|
|
51
55
|
nodeIdByPackagePath.set(packagePath, id);
|
|
56
|
+
if (typeof meta?.name === "string" && meta.name) {
|
|
57
|
+
manifestNameByNodeId.set(id, meta.name);
|
|
58
|
+
}
|
|
59
|
+
if (typeof meta?.resolved === "string" && meta.resolved) {
|
|
60
|
+
resolvedUrlByNodeId.set(id, meta.resolved);
|
|
61
|
+
}
|
|
52
62
|
const keySet = nodeIdsByPackageKey.get(packageKey) ?? new Set();
|
|
53
63
|
keySet.add(id);
|
|
54
64
|
nodeIdsByPackageKey.set(packageKey, keySet);
|
|
65
|
+
const depSource = resolveDependencySource(meta, rawPackages);
|
|
66
|
+
const effectiveVersion = version ?? (typeof depSource?.version === "string" ? depSource.version : undefined);
|
|
67
|
+
if (effectiveVersion) {
|
|
68
|
+
ancestorVersionByNodeId.set(id, effectiveVersion);
|
|
69
|
+
}
|
|
55
70
|
// Workspace / linked packages (link:true, resolved -> a workspace dir) are
|
|
56
71
|
// installed with their devDependencies, like the root, and are the true roots
|
|
57
72
|
// of their own dependency subtrees. Collect their full deps (incl. devDeps) and
|
|
58
73
|
// treat them as entry points; otherwise every transitive that lives only under a
|
|
59
74
|
// workspace is orphaned from "project" and reports "no parent identified".
|
|
60
75
|
dependencyRangesByNodeId.set(id, isLinkNode
|
|
61
|
-
? collectRootDependencyRanges(
|
|
62
|
-
: collectDependencyRanges(
|
|
76
|
+
? collectRootDependencyRanges(depSource)
|
|
77
|
+
: collectDependencyRanges(depSource));
|
|
63
78
|
resolutionBasePathsByNodeId.set(id, resolveBasePaths(packagePath, meta));
|
|
64
|
-
const peerRanges = collectPeerRanges(
|
|
79
|
+
const peerRanges = collectPeerRanges(depSource);
|
|
65
80
|
if (Object.keys(peerRanges).length > 0)
|
|
66
81
|
peerRangesByNodeId.set(id, peerRanges);
|
|
67
82
|
if (isLinkNode)
|
|
@@ -69,12 +84,19 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
69
84
|
}
|
|
70
85
|
const rootMeta = isRecord(rawPackages[""]) ? rawPackages[""] : null;
|
|
71
86
|
const rootRanges = collectRootDependencyRanges(rootMeta);
|
|
87
|
+
const rootPeerRanges = collectPeerRanges(rootMeta);
|
|
72
88
|
const entryPackages = resolveEdgesForParent("", rootRanges, [""], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
73
89
|
for (const node of nodesById.values()) {
|
|
74
90
|
resolveEdgesForParent(node.packagePath, dependencyRangesByNodeId.get(node.id) ?? {}, resolutionBasePathsByNodeId.get(node.id) ?? [node.packagePath], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
75
91
|
}
|
|
76
92
|
const entryNodeIds = unique([...entryPackages, ...workspaceEntryNodeIds]);
|
|
77
|
-
|
|
93
|
+
applyRootPeerFallback({
|
|
94
|
+
rootPeerRanges,
|
|
95
|
+
entryNodeIds,
|
|
96
|
+
nodeIdByPackagePath,
|
|
97
|
+
parentNodeIdsByChildNodeId,
|
|
98
|
+
});
|
|
99
|
+
const peerFallbackEdgeKeys = applyPeerDependencyFallback({
|
|
78
100
|
nodesById,
|
|
79
101
|
entryNodeIds,
|
|
80
102
|
peerRangesByNodeId,
|
|
@@ -84,21 +106,22 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
84
106
|
childNodeIdsByParentNodeId,
|
|
85
107
|
rangeByParentNodeId,
|
|
86
108
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
109
|
+
const peerRangesByChildNodeId = collectPeerRangesByChild(nodesById, rootPeerRanges, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath);
|
|
110
|
+
resolveNpmLockAliasIdentities({
|
|
111
|
+
filePath,
|
|
112
|
+
rootMeta,
|
|
113
|
+
rawLegacyDependencies: raw?.dependencies,
|
|
114
|
+
rootEntryNodeIds: new Set(entryPackages),
|
|
115
|
+
rootRanges,
|
|
116
|
+
peerRangesByChildNodeId,
|
|
117
|
+
manifestNameByNodeId,
|
|
118
|
+
resolvedUrlByNodeId,
|
|
119
|
+
ancestorVersionByNodeId,
|
|
120
|
+
nodesById,
|
|
121
|
+
parentNodeIdsByChildNodeId,
|
|
122
|
+
rangeByParentNodeId,
|
|
123
|
+
peerFallbackEdgeKeys,
|
|
124
|
+
});
|
|
102
125
|
if (options?.includePaths !== false) {
|
|
103
126
|
const MAX_PATHS_PER_NODE = 5;
|
|
104
127
|
const MAX_PATH_DEPTH = 10;
|
|
@@ -136,7 +159,7 @@ export function loadNpmLockGraph(filePath, options) {
|
|
|
136
159
|
childNodeIdsByParentNodeId: setsToArrays(childNodeIdsByParentNodeId),
|
|
137
160
|
rangeByParentNodeId,
|
|
138
161
|
pathSetByNodeId,
|
|
139
|
-
peerRangesByChildNodeId
|
|
162
|
+
peerRangesByChildNodeId,
|
|
140
163
|
});
|
|
141
164
|
}
|
|
142
165
|
function createGraph(args) {
|
|
@@ -188,23 +211,44 @@ function createGraph(args) {
|
|
|
188
211
|
},
|
|
189
212
|
};
|
|
190
213
|
}
|
|
214
|
+
function appendPeerRangesForBasePaths(peerRanges, basePaths, nodeIdByPackagePath, rangesByChild) {
|
|
215
|
+
for (const [peerName, range] of Object.entries(peerRanges)) {
|
|
216
|
+
const childPackagePath = resolveDependencyPackagePath(basePaths, peerName, nodeIdByPackagePath);
|
|
217
|
+
const childNodeId = childPackagePath ? nodeIdByPackagePath.get(childPackagePath) : undefined;
|
|
218
|
+
if (!childNodeId)
|
|
219
|
+
continue;
|
|
220
|
+
rangesByChild.set(childNodeId, [...(rangesByChild.get(childNodeId) ?? []), range]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
191
223
|
// npm refuses an install whose version breaks a peer requirement, so every
|
|
192
224
|
// peer range on an installed copy limits what it can be moved to, whether or
|
|
193
225
|
// not the peer fallback below turned that requirement into an edge.
|
|
194
|
-
function collectPeerRangesByChild(nodesById, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath) {
|
|
226
|
+
function collectPeerRangesByChild(nodesById, rootPeerRanges, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath) {
|
|
195
227
|
const rangesByChild = new Map();
|
|
228
|
+
appendPeerRangesForBasePaths(rootPeerRanges, [""], nodeIdByPackagePath, rangesByChild);
|
|
196
229
|
for (const [nodeId, peerRanges] of peerRangesByNodeId) {
|
|
197
230
|
const basePaths = resolutionBasePathsByNodeId.get(nodeId) ?? [nodesById.get(nodeId)?.packagePath ?? nodeId];
|
|
198
|
-
|
|
199
|
-
const childPackagePath = resolveDependencyPackagePath(basePaths, peerName, nodeIdByPackagePath);
|
|
200
|
-
const childNodeId = childPackagePath ? nodeIdByPackagePath.get(childPackagePath) : undefined;
|
|
201
|
-
if (!childNodeId)
|
|
202
|
-
continue;
|
|
203
|
-
rangesByChild.set(childNodeId, [...(rangesByChild.get(childNodeId) ?? []), range]);
|
|
204
|
-
}
|
|
231
|
+
appendPeerRangesForBasePaths(peerRanges, basePaths, nodeIdByPackagePath, rangesByChild);
|
|
205
232
|
}
|
|
206
233
|
return rangesByChild;
|
|
207
234
|
}
|
|
235
|
+
// npm 7+ auto-installs root peerDependencies unless suppressed; keep otherwise-orphaned
|
|
236
|
+
// root peer targets anchored at "project" so paths and aliases remain intact.
|
|
237
|
+
function applyRootPeerFallback(args) {
|
|
238
|
+
const entrySet = new Set(args.entryNodeIds);
|
|
239
|
+
for (const peerName of Object.keys(args.rootPeerRanges)) {
|
|
240
|
+
const childPackagePath = resolveDependencyPackagePath([""], peerName, args.nodeIdByPackagePath);
|
|
241
|
+
if (!childPackagePath)
|
|
242
|
+
continue;
|
|
243
|
+
const childNodeId = args.nodeIdByPackagePath.get(childPackagePath);
|
|
244
|
+
if (!childNodeId || entrySet.has(childNodeId))
|
|
245
|
+
continue;
|
|
246
|
+
if ((args.parentNodeIdsByChildNodeId.get(childNodeId)?.size ?? 0) > 0)
|
|
247
|
+
continue;
|
|
248
|
+
args.entryNodeIds.push(childNodeId);
|
|
249
|
+
entrySet.add(childNodeId);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
208
252
|
function resolveEdgesForParent(parentPackagePath, dependencyRanges, resolutionBasePaths, nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId) {
|
|
209
253
|
const resolvedChildNodeIds = [];
|
|
210
254
|
for (const [dependencyName, range] of Object.entries(dependencyRanges)) {
|
|
@@ -235,6 +279,7 @@ function resolveEdgesForParent(parentPackagePath, dependencyRanges, resolutionBa
|
|
|
235
279
|
// is also a direct dependency).
|
|
236
280
|
function applyPeerDependencyFallback(args) {
|
|
237
281
|
const { nodesById, entryNodeIds, peerRangesByNodeId, resolutionBasePathsByNodeId, nodeIdByPackagePath, parentNodeIdsByChildNodeId, } = args;
|
|
282
|
+
const fallbackEdgeKeys = new Set();
|
|
238
283
|
const entryNodeIdSet = new Set(entryNodeIds);
|
|
239
284
|
const orphanNodeIds = new Set();
|
|
240
285
|
for (const nodeId of nodesById.keys()) {
|
|
@@ -243,7 +288,7 @@ function applyPeerDependencyFallback(args) {
|
|
|
243
288
|
orphanNodeIds.add(nodeId);
|
|
244
289
|
}
|
|
245
290
|
if (orphanNodeIds.size === 0)
|
|
246
|
-
return;
|
|
291
|
+
return fallbackEdgeKeys;
|
|
247
292
|
for (const node of nodesById.values()) {
|
|
248
293
|
const peerRanges = peerRangesByNodeId.get(node.id);
|
|
249
294
|
if (!peerRanges)
|
|
@@ -257,8 +302,10 @@ function applyPeerDependencyFallback(args) {
|
|
|
257
302
|
if (!childNodeId || !orphanNodeIds.has(childNodeId))
|
|
258
303
|
continue;
|
|
259
304
|
addParentChildEdge(node.id, childNodeId, peerName, range, args);
|
|
305
|
+
fallbackEdgeKeys.add(peerFallbackEdgeKey(node.id, childNodeId));
|
|
260
306
|
}
|
|
261
307
|
}
|
|
308
|
+
return fallbackEdgeKeys;
|
|
262
309
|
}
|
|
263
310
|
function addParentChildEdge(parentNodeId, childNodeId, dependencyName, range, edges) {
|
|
264
311
|
const childSet = edges.childNodeIdsByParentNodeId.get(parentNodeId) ?? new Set();
|
|
@@ -362,9 +409,6 @@ function setsToArrays(map) {
|
|
|
362
409
|
}
|
|
363
410
|
return result;
|
|
364
411
|
}
|
|
365
|
-
function isRecord(value) {
|
|
366
|
-
return !!value && typeof value === "object";
|
|
367
|
-
}
|
|
368
412
|
function buildPackageKey(name, version) {
|
|
369
413
|
return `${name}@${version ?? "<link>"}`;
|
|
370
414
|
}
|
|
@@ -321,7 +321,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
321
321
|
adjusted: false,
|
|
322
322
|
adjustmentNote: null,
|
|
323
323
|
reason: buildParentUpgradeReason(finding, coverage),
|
|
324
|
-
command: finding.chainResolution.
|
|
324
|
+
command: buildPackageInstallCommand(packageManager, `${finding.chainResolution.directDep}@${finding.chainResolution.targetVersion}`),
|
|
325
325
|
chainProof: finding.chainResolution.chain,
|
|
326
326
|
chainSafeVersion: finding.chainResolution.safeVersion,
|
|
327
327
|
chainVulnerablePackage: finding.pkg.name,
|
|
@@ -483,6 +483,10 @@ export function commandPrefix(packageManager) {
|
|
|
483
483
|
return "bun add";
|
|
484
484
|
return "yarn add";
|
|
485
485
|
}
|
|
486
|
+
/** Format a single install command with a shell-quoted package spec. */
|
|
487
|
+
export function buildPackageInstallCommand(packageManager, packageSpec, flag = "") {
|
|
488
|
+
return `${commandPrefix(packageManager)}${flag} ${shellQuote(packageSpec)}`;
|
|
489
|
+
}
|
|
486
490
|
function devFlag(packageManager) {
|
|
487
491
|
return packageManager === "bun" ? "--dev" : "-D";
|
|
488
492
|
}
|
|
@@ -578,7 +582,7 @@ export function buildTargetCommand(target, packageManager) {
|
|
|
578
582
|
if (target.command)
|
|
579
583
|
return target.command;
|
|
580
584
|
const flag = target.isDev ? ` ${devFlag(packageManager)}` : "";
|
|
581
|
-
return
|
|
585
|
+
return buildPackageInstallCommand(packageManager, installPackageSpec(target), flag);
|
|
582
586
|
}
|
|
583
587
|
export function findSuggestedCommandForFinding(plan, finding) {
|
|
584
588
|
const target = findFixTargetForFinding(plan, finding);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildPackageInstallCommand } from "./fix-commands.js";
|
|
2
2
|
import { fetchPackument } from "./npm-registry.js";
|
|
3
3
|
import { compareVersions, looksLikeVersion, isPreReleaseVersion, satisfiesRange } from "../utils/version.js";
|
|
4
4
|
import { highestVersionSatisfyingRange } from "./npm-transitive-resolution.js";
|
|
@@ -42,7 +42,7 @@ export async function resolveChainFix(finding, installedVersions, packageManager
|
|
|
42
42
|
targetVersion: candidateVersion,
|
|
43
43
|
chain: traced.chain,
|
|
44
44
|
safeVersion: traced.safeVersion,
|
|
45
|
-
command:
|
|
45
|
+
command: buildPackageInstallCommand(packageManager, `${directDepName}@${candidateVersion}`),
|
|
46
46
|
coveredPaths: 1,
|
|
47
47
|
totalPaths: finding.dependencyPaths.length,
|
|
48
48
|
};
|
package/dist/scanner.js
CHANGED
|
@@ -28,6 +28,7 @@ import { buildPnpmWorkspaceMap } from "./parsers/pnpm-lock.js";
|
|
|
28
28
|
import { buildNpmWorkspaceMap } from "./parsers/package-lock.js";
|
|
29
29
|
import { buildBunWorkspaceMap } from "./parsers/bun-lock.js";
|
|
30
30
|
import { pluralize } from "./utils/string.js";
|
|
31
|
+
import { shellQuote } from "./utils/shell-quote.js";
|
|
31
32
|
import path from "node:path";
|
|
32
33
|
import { resolveRegistryConfig } from "./utils/registry-config.js";
|
|
33
34
|
import { extractErrorMessage } from "./utils/network.js";
|
|
@@ -589,7 +590,7 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
589
590
|
continue;
|
|
590
591
|
updateAnalysisProgress("resolving remediation for", `${finding.pkg.name}@${finding.pkg.version}`);
|
|
591
592
|
if (aliasRemediationFindings.has(finding)) {
|
|
592
|
-
const aliasNote = `Automatic parent remediation is unavailable for paths containing an npm alias. Run \`npm ls ${finding.pkg.name}\` to inspect the declaring dependency, then rescan after updating it.`;
|
|
593
|
+
const aliasNote = `Automatic parent remediation is unavailable for paths containing an npm alias. Run \`npm ls ${shellQuote(finding.pkg.name)}\` to inspect the declaring dependency, then rescan after updating it.`;
|
|
593
594
|
finding.fixVersionValidationNote = [finding.fixVersionValidationNote, aliasNote].filter(Boolean).join(" ");
|
|
594
595
|
continue;
|
|
595
596
|
}
|
|
@@ -58,6 +58,7 @@ function addDependencyNamesFromManifest(target, manifest, prodOnly) {
|
|
|
58
58
|
const sections = [
|
|
59
59
|
isRecord(manifest.dependencies) ? manifest.dependencies : undefined,
|
|
60
60
|
isRecord(manifest.optionalDependencies) ? manifest.optionalDependencies : undefined,
|
|
61
|
+
isRecord(manifest.peerDependencies) ? manifest.peerDependencies : undefined,
|
|
61
62
|
];
|
|
62
63
|
if (!prodOnly) {
|
|
63
64
|
sections.push(isRecord(manifest.devDependencies) ? manifest.devDependencies : undefined);
|