cve-lite-cli 1.41.0 → 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/output/override-fix-command.js +5 -7
- 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/overrides/parsing/registry.js +19 -69
- package/dist/parsers/npm-alias-resolution.js +248 -0
- package/dist/parsers/npm-lock-graph.js +77 -33
- package/dist/parsers/yarn-lock-graph.js +31 -15
- package/dist/parsers/yarn-lock.js +91 -23
- package/dist/remediation/fix-commands.js +8 -9
- package/dist/remediation/npm-registry.js +10 -2
- package/dist/remediation/transitive-chain-resolver.js +2 -2
- package/dist/scan/override-audit.js +6 -4
- package/dist/scanner.js +8 -1
- package/dist/utils/checked-address-policy.js +44 -0
- package/dist/utils/network.js +1 -0
- package/dist/utils/osv-fetch.js +12 -48
- package/dist/utils/package-json.js +1 -0
- package/dist/utils/registry-config.js +49 -2
- package/dist/utils/registry-fetch.js +42 -0
- package/dist/utils/shell-quote.js +12 -0
- package/package.json +1 -1
|
@@ -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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import { parse as parseYarnLock } from "yarn-lockfile";
|
|
3
|
-
import { isYarnBerry, parseBerryDependencies, parseYarnPackageKey } from "./yarn-lock.js";
|
|
3
|
+
import { isYarnBerry, parseBerryDependencies, parseYarnClassicSelectorKey, parseYarnPackageKey } from "./yarn-lock.js";
|
|
4
4
|
const EMPTY = Object.freeze([]);
|
|
5
5
|
/**
|
|
6
6
|
* Builds the index for a Berry (v2+) lockfile.
|
|
@@ -51,7 +51,17 @@ function buildBerryIndex(content) {
|
|
|
51
51
|
}
|
|
52
52
|
graph.set(canonicalKey, [...children]);
|
|
53
53
|
}
|
|
54
|
-
return { selectorToKey, graph };
|
|
54
|
+
return { selectorToKey, graph, registryNameByKey: new Map() };
|
|
55
|
+
}
|
|
56
|
+
function classicCanonicalKey(selectorKey, version) {
|
|
57
|
+
const identity = parseYarnClassicSelectorKey(selectorKey);
|
|
58
|
+
if (!identity)
|
|
59
|
+
return null;
|
|
60
|
+
return {
|
|
61
|
+
key: `${identity.name}@${version}`,
|
|
62
|
+
selectors: identity.selectors,
|
|
63
|
+
...(identity.registryName ? { registryName: identity.registryName } : {}),
|
|
64
|
+
};
|
|
55
65
|
}
|
|
56
66
|
/**
|
|
57
67
|
* Builds the index for a classic (v1) lockfile, which the `yarn-lockfile`
|
|
@@ -60,28 +70,27 @@ function buildBerryIndex(content) {
|
|
|
60
70
|
function buildClassicIndex(parsed) {
|
|
61
71
|
const selectorToKey = new Map();
|
|
62
72
|
const graph = new Map();
|
|
73
|
+
const registryNameByKey = new Map();
|
|
63
74
|
const entries = Object.entries(parsed?.object ?? {});
|
|
64
75
|
for (const [selectorKey, meta] of entries) {
|
|
65
76
|
const version = meta?.version;
|
|
66
77
|
if (!version)
|
|
67
78
|
continue;
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
if (atIndex <= 0)
|
|
79
|
+
const entry = classicCanonicalKey(selectorKey, version);
|
|
80
|
+
if (!entry)
|
|
71
81
|
continue;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
if (entry.registryName)
|
|
83
|
+
registryNameByKey.set(entry.key, entry.registryName);
|
|
84
|
+
for (const selector of entry.selectors)
|
|
85
|
+
selectorToKey.set(selector, entry.key);
|
|
75
86
|
}
|
|
76
87
|
for (const [selectorKey, meta] of entries) {
|
|
77
88
|
const version = meta?.version;
|
|
78
89
|
if (!version)
|
|
79
90
|
continue;
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
if (atIndex <= 0)
|
|
91
|
+
const entry = classicCanonicalKey(selectorKey, version);
|
|
92
|
+
if (!entry)
|
|
83
93
|
continue;
|
|
84
|
-
const canonicalKey = `${selectors[0].slice(0, atIndex)}@${version}`;
|
|
85
94
|
const children = new Set();
|
|
86
95
|
// An optional dependency is installed whenever it can be, so its parent
|
|
87
96
|
// constrains the installed copy as much as a regular one does.
|
|
@@ -94,9 +103,9 @@ function buildClassicIndex(parsed) {
|
|
|
94
103
|
children.add(childKey);
|
|
95
104
|
}
|
|
96
105
|
}
|
|
97
|
-
graph.set(
|
|
106
|
+
graph.set(entry.key, [...children]);
|
|
98
107
|
}
|
|
99
|
-
return { selectorToKey, graph };
|
|
108
|
+
return { selectorToKey, graph, registryNameByKey };
|
|
100
109
|
}
|
|
101
110
|
/**
|
|
102
111
|
* Builds the complete child-to-parents map for a `yarn.lock`, covering both the
|
|
@@ -145,7 +154,14 @@ export function loadYarnLockGraph(filePath) {
|
|
|
145
154
|
parentsFor: nodeId => parentsByKey.get(nodeId) ?? EMPTY,
|
|
146
155
|
getNode: nodeId => {
|
|
147
156
|
const parsedKey = parseYarnPackageKey(nodeId);
|
|
148
|
-
|
|
157
|
+
if (!parsedKey)
|
|
158
|
+
return null;
|
|
159
|
+
const registryName = index.registryNameByKey.get(nodeId);
|
|
160
|
+
return {
|
|
161
|
+
name: parsedKey.name,
|
|
162
|
+
version: parsedKey.version,
|
|
163
|
+
...(registryName ? { registryName } : {}),
|
|
164
|
+
};
|
|
149
165
|
},
|
|
150
166
|
};
|
|
151
167
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { parse as parseYarnLock } from "yarn-lockfile";
|
|
4
|
+
import { npmAliasRegistryName, packageInstanceKey } from "../utils/package-identity.js";
|
|
4
5
|
import { normalizeRawVersion } from "../utils/version.js";
|
|
5
6
|
import { upsertPackage, MAX_PATHS_PER_PACKAGE } from "./utils.js";
|
|
6
7
|
const MAX_PATH_DEPTH = 10;
|
|
@@ -144,6 +145,75 @@ export function parseYarnPackageKey(key) {
|
|
|
144
145
|
return null;
|
|
145
146
|
return { key, name, version };
|
|
146
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Yarn v1 records an alias as `<alias>@npm:<package>@<range>`.
|
|
150
|
+
*
|
|
151
|
+
* `yarn 1.22` quotes that key. A scoped target keeps the leading `@` after the
|
|
152
|
+
* protocol (`frames@npm:@babel/code-frame@7.23.5`), and a scoped alias keeps
|
|
153
|
+
* its own (`@my/frames@npm:@babel/code-frame@7.23.5`). The range in the key is
|
|
154
|
+
* what was requested. The installed version is the lockfile `version` field,
|
|
155
|
+
* so this only returns names.
|
|
156
|
+
*
|
|
157
|
+
* A normal key (`lodash@4.17.20`, `@babel/code-frame@^7.23.4`) has no `@npm:`
|
|
158
|
+
* marker. Splitting those on the final `@` is still the package name. Doing
|
|
159
|
+
* that to an alias key yields `util-lib@npm:lodash`, which is not a package
|
|
160
|
+
* OSV knows about.
|
|
161
|
+
*/
|
|
162
|
+
export function parseYarnClassicDescriptor(selector) {
|
|
163
|
+
const marker = "@npm:";
|
|
164
|
+
const markerAt = selector.indexOf(marker);
|
|
165
|
+
if (markerAt > 0) {
|
|
166
|
+
const name = selector.slice(0, markerAt);
|
|
167
|
+
const target = selector.slice(markerAt + marker.length);
|
|
168
|
+
if (!name || !target)
|
|
169
|
+
return null;
|
|
170
|
+
// One definition of an alias target, shared with npm lockfiles: a later `@`
|
|
171
|
+
// starts the requested range, and the leading `@` of `@scope/pkg` does not.
|
|
172
|
+
const registryName = npmAliasRegistryName("npm:" + target);
|
|
173
|
+
if (!registryName)
|
|
174
|
+
return null;
|
|
175
|
+
// `lodash@npm:lodash@4.17.20` is the same package under its own name.
|
|
176
|
+
if (registryName === name)
|
|
177
|
+
return { name };
|
|
178
|
+
return { name, registryName };
|
|
179
|
+
}
|
|
180
|
+
const atIndex = selector.lastIndexOf("@");
|
|
181
|
+
if (atIndex <= 0)
|
|
182
|
+
return null;
|
|
183
|
+
const name = selector.slice(0, atIndex);
|
|
184
|
+
if (!name)
|
|
185
|
+
return null;
|
|
186
|
+
return { name };
|
|
187
|
+
}
|
|
188
|
+
/** Every descriptor in one Yarn v1 entry, and the package the first one names. */
|
|
189
|
+
export function parseYarnClassicSelectorKey(selectorKey) {
|
|
190
|
+
const selectors = String(selectorKey).split(",").map(part => part.trim()).filter(Boolean);
|
|
191
|
+
const identity = selectors[0] ? parseYarnClassicDescriptor(selectors[0]) : null;
|
|
192
|
+
if (!identity)
|
|
193
|
+
return null;
|
|
194
|
+
return {
|
|
195
|
+
name: identity.name,
|
|
196
|
+
...(identity.registryName ? { registryName: identity.registryName } : {}),
|
|
197
|
+
selectors,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function readYarnClassicEntry(selectorKey, meta) {
|
|
201
|
+
const version = meta?.version;
|
|
202
|
+
if (typeof version !== "string" || version.length === 0)
|
|
203
|
+
return null;
|
|
204
|
+
const identity = parseYarnClassicSelectorKey(selectorKey);
|
|
205
|
+
if (!identity)
|
|
206
|
+
return null;
|
|
207
|
+
return {
|
|
208
|
+
...identity,
|
|
209
|
+
version,
|
|
210
|
+
instanceKey: packageInstanceKey({
|
|
211
|
+
name: identity.name,
|
|
212
|
+
registryName: identity.registryName,
|
|
213
|
+
version,
|
|
214
|
+
}),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
147
217
|
function collectYarnPaths(seeds, graph, map, taggedPathsByKey) {
|
|
148
218
|
const queue = seeds.map(seed => ({ key: seed.key, path: seed.path }));
|
|
149
219
|
const visitedStates = new Set();
|
|
@@ -153,7 +223,12 @@ function collectYarnPaths(seeds, graph, map, taggedPathsByKey) {
|
|
|
153
223
|
const ref = parseYarnPackageKey(current.key);
|
|
154
224
|
if (!ref)
|
|
155
225
|
continue;
|
|
156
|
-
|
|
226
|
+
// An aliased entry's canonical key is `alias\0registry@version`. The path
|
|
227
|
+
// shows the name that was installed, which is the part before the NUL.
|
|
228
|
+
const installName = ref.name.split("\0")[0];
|
|
229
|
+
if (!installName)
|
|
230
|
+
continue;
|
|
231
|
+
const nextPath = [...current.path, installName];
|
|
157
232
|
const stateKey = `${ref.key}>${nextPath.join(">")}`;
|
|
158
233
|
if (visitedStates.has(stateKey))
|
|
159
234
|
continue;
|
|
@@ -302,32 +377,25 @@ function loadFromYarnClassicLock(parsed, filePath) {
|
|
|
302
377
|
const graph = new Map();
|
|
303
378
|
const selectorToKey = new Map();
|
|
304
379
|
for (const [selectorKey, meta] of Object.entries(parsed.object)) {
|
|
305
|
-
const
|
|
306
|
-
if (!
|
|
380
|
+
const entry = readYarnClassicEntry(selectorKey, meta);
|
|
381
|
+
if (!entry)
|
|
307
382
|
continue;
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
const atIndex = firstSelector.lastIndexOf("@");
|
|
311
|
-
if (atIndex <= 0)
|
|
312
|
-
continue;
|
|
313
|
-
const name = firstSelector.slice(0, atIndex);
|
|
314
|
-
const canonicalKey = `${name}@${version}`;
|
|
315
|
-
for (const selector of selectors) {
|
|
316
|
-
selectorToKey.set(selector, canonicalKey);
|
|
383
|
+
for (const selector of entry.selectors) {
|
|
384
|
+
selectorToKey.set(selector, entry.instanceKey);
|
|
317
385
|
}
|
|
318
|
-
upsertPackage(map, {
|
|
386
|
+
upsertPackage(map, {
|
|
387
|
+
name: entry.name,
|
|
388
|
+
version: entry.version,
|
|
389
|
+
ecosystem: "npm",
|
|
390
|
+
paths: [],
|
|
391
|
+
resolvedUrl: meta?.resolved,
|
|
392
|
+
...(entry.registryName ? { registryName: entry.registryName } : {}),
|
|
393
|
+
});
|
|
319
394
|
}
|
|
320
395
|
for (const [selectorKey, meta] of Object.entries(parsed.object)) {
|
|
321
|
-
const
|
|
322
|
-
if (!
|
|
323
|
-
continue;
|
|
324
|
-
const selectors = String(selectorKey).split(",").map(selector => selector.trim()).filter(Boolean);
|
|
325
|
-
const firstSelector = selectors[0];
|
|
326
|
-
const atIndex = firstSelector.lastIndexOf("@");
|
|
327
|
-
if (atIndex <= 0)
|
|
396
|
+
const entry = readYarnClassicEntry(selectorKey, meta);
|
|
397
|
+
if (!entry)
|
|
328
398
|
continue;
|
|
329
|
-
const name = firstSelector.slice(0, atIndex);
|
|
330
|
-
const canonicalKey = `${name}@${version}`;
|
|
331
399
|
const depKeys = new Set();
|
|
332
400
|
const deps = meta?.dependencies;
|
|
333
401
|
if (deps && typeof deps === "object") {
|
|
@@ -337,7 +405,7 @@ function loadFromYarnClassicLock(parsed, filePath) {
|
|
|
337
405
|
depKeys.add(childKey);
|
|
338
406
|
}
|
|
339
407
|
}
|
|
340
|
-
graph.set(
|
|
408
|
+
graph.set(entry.instanceKey, [...depKeys]);
|
|
341
409
|
}
|
|
342
410
|
const taggedPathsByKey = new Map();
|
|
343
411
|
const prodFallbackKeys = new Set();
|
|
@@ -5,6 +5,7 @@ import { getPrimaryParent } from "../utils/finding.js";
|
|
|
5
5
|
import { isNpmLockSource, packageManagerForSource } from "../utils/package-manager-source.js";
|
|
6
6
|
import { calculatePathCoverage, formatDependencyPath } from "../utils/path-coverage.js";
|
|
7
7
|
import { pluralize } from "../utils/string.js";
|
|
8
|
+
import { quoteShellPath, shellQuote } from "../utils/shell-quote.js";
|
|
8
9
|
import { buildNpmWorkspaceMap } from "../parsers/package-lock.js";
|
|
9
10
|
import { buildPnpmWorkspaceMap } from "../parsers/pnpm-lock.js";
|
|
10
11
|
import { buildYarnWorkspaceMap } from "../parsers/yarn-lock.js";
|
|
@@ -320,7 +321,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
320
321
|
adjusted: false,
|
|
321
322
|
adjustmentNote: null,
|
|
322
323
|
reason: buildParentUpgradeReason(finding, coverage),
|
|
323
|
-
command: finding.chainResolution.
|
|
324
|
+
command: buildPackageInstallCommand(packageManager, `${finding.chainResolution.directDep}@${finding.chainResolution.targetVersion}`),
|
|
324
325
|
chainProof: finding.chainResolution.chain,
|
|
325
326
|
chainSafeVersion: finding.chainResolution.safeVersion,
|
|
326
327
|
chainVulnerablePackage: finding.pkg.name,
|
|
@@ -432,7 +433,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
432
433
|
return true;
|
|
433
434
|
}).length;
|
|
434
435
|
if (options?.subfolder) {
|
|
435
|
-
const prefix = `cd ${options.subfolder} && `;
|
|
436
|
+
const prefix = `cd ${quoteShellPath(options.subfolder)} && `;
|
|
436
437
|
if (plan.command)
|
|
437
438
|
plan.command = prefix + plan.command;
|
|
438
439
|
for (const section of plan.sections) {
|
|
@@ -473,12 +474,6 @@ function buildYarnUpgradeCommand(pkg, workspaces) {
|
|
|
473
474
|
function buildBunUpdateCommand(pkg) {
|
|
474
475
|
return `bun update ${shellQuote(pkg)}`;
|
|
475
476
|
}
|
|
476
|
-
/** Quote a token for human-facing plan strings that users may paste into a shell. */
|
|
477
|
-
function shellQuote(token) {
|
|
478
|
-
if (/^[A-Za-z0-9_@%+./:=,-]+$/.test(token))
|
|
479
|
-
return token;
|
|
480
|
-
return "'" + token.replace(/'/g, "'\\''") + "'";
|
|
481
|
-
}
|
|
482
477
|
export function commandPrefix(packageManager) {
|
|
483
478
|
if (packageManager === "npm")
|
|
484
479
|
return "npm install";
|
|
@@ -488,6 +483,10 @@ export function commandPrefix(packageManager) {
|
|
|
488
483
|
return "bun add";
|
|
489
484
|
return "yarn add";
|
|
490
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
|
+
}
|
|
491
490
|
function devFlag(packageManager) {
|
|
492
491
|
return packageManager === "bun" ? "--dev" : "-D";
|
|
493
492
|
}
|
|
@@ -583,7 +582,7 @@ export function buildTargetCommand(target, packageManager) {
|
|
|
583
582
|
if (target.command)
|
|
584
583
|
return target.command;
|
|
585
584
|
const flag = target.isDev ? ` ${devFlag(packageManager)}` : "";
|
|
586
|
-
return
|
|
585
|
+
return buildPackageInstallCommand(packageManager, installPackageSpec(target), flag);
|
|
587
586
|
}
|
|
588
587
|
export function findSuggestedCommandForFinding(plan, finding) {
|
|
589
588
|
const target = findFixTargetForFinding(plan, finding);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
|
|
2
2
|
import { registryForPackage } from "../utils/registry-config.js";
|
|
3
|
+
import { fetchRegistryMetadata, RegistryNetworkPolicyError } from "../utils/registry-fetch.js";
|
|
3
4
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
5
|
const packumentCache = new Map();
|
|
5
6
|
const DEFAULT_NPM_REGISTRY_URL = "https://registry.npmjs.org";
|
|
@@ -24,6 +25,8 @@ function notifyPackumentObserver(result, packageName) {
|
|
|
24
25
|
const observer = observerStorage.getStore();
|
|
25
26
|
if (result.status === "transient-failure") {
|
|
26
27
|
observer?.onTransientFailure?.(packageName);
|
|
28
|
+
if (result.policyBlocked)
|
|
29
|
+
observer?.onPolicyBlocked?.();
|
|
27
30
|
}
|
|
28
31
|
else {
|
|
29
32
|
observer?.onResolved?.(packageName);
|
|
@@ -61,6 +64,7 @@ let registryConfig = null;
|
|
|
61
64
|
*/
|
|
62
65
|
export function configureNpmRegistry(config) {
|
|
63
66
|
registryConfig = config;
|
|
67
|
+
clearPackumentCache();
|
|
64
68
|
}
|
|
65
69
|
/**
|
|
66
70
|
* Names the registry a lookup for this package actually went to, for use inside
|
|
@@ -99,7 +103,7 @@ async function doFetchPackument(packageName) {
|
|
|
99
103
|
registryDebugLog("Registry fetch", { batchId, package: packageName, url });
|
|
100
104
|
}
|
|
101
105
|
try {
|
|
102
|
-
const response = await
|
|
106
|
+
const response = await fetchRegistryMetadata(base, url, !!registryConfig?.allowPrivate);
|
|
103
107
|
if (!response.ok) {
|
|
104
108
|
if (registryDebugLog) {
|
|
105
109
|
registryDebugLog("Registry response", {
|
|
@@ -138,7 +142,11 @@ async function doFetchPackument(packageName) {
|
|
|
138
142
|
: String(error),
|
|
139
143
|
});
|
|
140
144
|
}
|
|
141
|
-
return {
|
|
145
|
+
return {
|
|
146
|
+
status: "transient-failure",
|
|
147
|
+
packument: null,
|
|
148
|
+
policyBlocked: error instanceof RegistryNetworkPolicyError,
|
|
149
|
+
};
|
|
142
150
|
}
|
|
143
151
|
}
|
|
144
152
|
export async function packageVersionExists(packageName, version) {
|
|
@@ -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
|
};
|
|
@@ -33,6 +33,10 @@ export async function runOverrideAudit(params) {
|
|
|
33
33
|
})
|
|
34
34
|
: null;
|
|
35
35
|
try {
|
|
36
|
+
const registry = resolveRegistryConfig({
|
|
37
|
+
projectPath: params.projectPath,
|
|
38
|
+
packageManager: packageManagerForSource(params.scanSource),
|
|
39
|
+
});
|
|
36
40
|
const result = await audit(ctx, {
|
|
37
41
|
checkNetwork: params.checkNetwork,
|
|
38
42
|
advisorySource: advisory?.advisorySource,
|
|
@@ -45,10 +49,8 @@ export async function runOverrideAudit(params) {
|
|
|
45
49
|
// the scanner's module state, because the audit runs before the scan configures it.
|
|
46
50
|
registry: {
|
|
47
51
|
fetchImpl: params.fetchImpl,
|
|
48
|
-
baseUrl:
|
|
49
|
-
|
|
50
|
-
packageManager: packageManagerForSource(params.scanSource),
|
|
51
|
-
}).registry,
|
|
52
|
+
baseUrl: registry.registry,
|
|
53
|
+
allowPrivate: registry.allowPrivate,
|
|
52
54
|
},
|
|
53
55
|
});
|
|
54
56
|
return { findings: result.findings, skipped: result.skippedDetectors };
|
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";
|
|
@@ -525,12 +526,14 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
525
526
|
}
|
|
526
527
|
const lockfileWorkspaceMap = pnpmWorkspaceMap ?? bunWorkspaceMap ?? null;
|
|
527
528
|
const packumentFailures = new Set();
|
|
529
|
+
let registryPolicyBlocked = false;
|
|
528
530
|
const remediationFailures = new Map();
|
|
529
531
|
let chainResolutionFailureCount = 0;
|
|
530
532
|
let directCount = 0;
|
|
531
533
|
let transitiveCount = 0;
|
|
532
534
|
await runWithObserver({
|
|
533
535
|
onTransientFailure(name) { packumentFailures.add(name); },
|
|
536
|
+
onPolicyBlocked() { registryPolicyBlocked = true; },
|
|
534
537
|
}, async () => {
|
|
535
538
|
const npmRemediationCache = new Map();
|
|
536
539
|
const parentUpgradeCache = new Map();
|
|
@@ -587,7 +590,7 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
587
590
|
continue;
|
|
588
591
|
updateAnalysisProgress("resolving remediation for", `${finding.pkg.name}@${finding.pkg.version}`);
|
|
589
592
|
if (aliasRemediationFindings.has(finding)) {
|
|
590
|
-
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.`;
|
|
591
594
|
finding.fixVersionValidationNote = [finding.fixVersionValidationNote, aliasNote].filter(Boolean).join(" ");
|
|
592
595
|
continue;
|
|
593
596
|
}
|
|
@@ -723,6 +726,10 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
723
726
|
}
|
|
724
727
|
}
|
|
725
728
|
});
|
|
729
|
+
if (registryPolicyBlocked && registryConfig.sourceFile) {
|
|
730
|
+
const source = path.basename(registryConfig.sourceFile);
|
|
731
|
+
console.warn(`Warning: The registry from ${source} was blocked because repository settings cannot use plain HTTP or private addresses. To use it, set NPM_CONFIG_REGISTRY for the scan, or on Windows remove the registry from the project's config and set it in your user-level config (~/.npmrc or ~/.yarnrc.yml).`);
|
|
732
|
+
}
|
|
726
733
|
const severityCounts = countBySeverity(findings);
|
|
727
734
|
log("Findings classified", {
|
|
728
735
|
total: findings.length,
|