cve-lite-cli 1.9.0 → 1.11.0
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/README.md +132 -84
- package/dist/index.js +97 -13
- package/dist/output/formatters.js +29 -9
- package/dist/output/html-reporter.js +110 -17
- package/dist/output/printers.js +75 -24
- package/dist/parsers/npm-lock-graph.js +231 -0
- package/dist/parsers/utils.js +5 -1
- package/dist/remediation/fix-commands.js +104 -5
- package/dist/remediation/npm-transitive-graph.js +123 -0
- package/dist/remediation/npm-transitive-resolution.js +221 -0
- package/dist/remediation/parent-upgrade.js +25 -9
- package/dist/scanner.js +115 -10
- package/dist/utils/finding.js +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { unique, uniquePathArrays } from "../utils/array.js";
|
|
3
|
+
export function loadNpmLockGraph(filePath, options) {
|
|
4
|
+
const raw = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
5
|
+
const packages = raw?.packages;
|
|
6
|
+
const rawPackages = isRecord(packages) ? packages : {};
|
|
7
|
+
const nodesById = new Map();
|
|
8
|
+
const nodeIdsByPackageKey = new Map();
|
|
9
|
+
const nodeIdByPackagePath = new Map();
|
|
10
|
+
const dependencyRangesByNodeId = new Map();
|
|
11
|
+
const resolutionBasePathsByNodeId = new Map();
|
|
12
|
+
const parentNodeIdsByChildNodeId = new Map();
|
|
13
|
+
const childNodeIdsByParentNodeId = new Map();
|
|
14
|
+
const rangeByParentNodeId = new Map();
|
|
15
|
+
const pathSetByNodeId = new Map();
|
|
16
|
+
if (!packages || typeof packages !== "object") {
|
|
17
|
+
return createGraph({
|
|
18
|
+
entryPackages: [],
|
|
19
|
+
nodesById,
|
|
20
|
+
nodeIdsByPackageKey,
|
|
21
|
+
parentNodeIdsByChildNodeId,
|
|
22
|
+
childNodeIdsByParentNodeId,
|
|
23
|
+
rangeByParentNodeId,
|
|
24
|
+
pathSetByNodeId,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
for (const [packagePath, meta] of Object.entries(rawPackages)) {
|
|
28
|
+
if (!packagePath || packagePath === "")
|
|
29
|
+
continue;
|
|
30
|
+
if (!packagePath.includes("node_modules/"))
|
|
31
|
+
continue;
|
|
32
|
+
const name = packageNameFromPath(packagePath) ?? meta?.name;
|
|
33
|
+
const version = meta?.version;
|
|
34
|
+
const isLinkNode = !!meta?.link;
|
|
35
|
+
if (!name || (!version && !isLinkNode))
|
|
36
|
+
continue;
|
|
37
|
+
const id = packagePath;
|
|
38
|
+
const normalizedVersion = version ?? null;
|
|
39
|
+
const packageKey = buildPackageKey(name, normalizedVersion);
|
|
40
|
+
nodesById.set(id, {
|
|
41
|
+
id,
|
|
42
|
+
packageKey,
|
|
43
|
+
name,
|
|
44
|
+
version: normalizedVersion,
|
|
45
|
+
packagePath,
|
|
46
|
+
dev: !!meta?.dev,
|
|
47
|
+
});
|
|
48
|
+
nodeIdByPackagePath.set(packagePath, id);
|
|
49
|
+
nodeIdsByPackageKey.set(packageKey, [...(nodeIdsByPackageKey.get(packageKey) ?? []), id]);
|
|
50
|
+
dependencyRangesByNodeId.set(id, collectDependencyRanges(resolveDependencySource(meta, rawPackages)));
|
|
51
|
+
resolutionBasePathsByNodeId.set(id, resolveBasePaths(packagePath, meta));
|
|
52
|
+
}
|
|
53
|
+
const rootMeta = isRecord(rawPackages[""]) ? rawPackages[""] : null;
|
|
54
|
+
const entryPackages = resolveEdgesForParent("", collectRootDependencyRanges(rootMeta), [""], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
55
|
+
for (const node of nodesById.values()) {
|
|
56
|
+
resolveEdgesForParent(node.packagePath, dependencyRangesByNodeId.get(node.id) ?? {}, resolutionBasePathsByNodeId.get(node.id) ?? [node.packagePath], nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId);
|
|
57
|
+
}
|
|
58
|
+
if (options?.includePaths !== false) {
|
|
59
|
+
const queue = entryPackages.map((entryNodeId) => ({
|
|
60
|
+
nodeId: entryNodeId,
|
|
61
|
+
path: ["project", nodesById.get(entryNodeId)?.name ?? entryNodeId],
|
|
62
|
+
}));
|
|
63
|
+
while (queue.length > 0) {
|
|
64
|
+
const current = queue.shift();
|
|
65
|
+
if (!current)
|
|
66
|
+
continue;
|
|
67
|
+
rememberPath(pathSetByNodeId, current.nodeId, current.path);
|
|
68
|
+
for (const childNodeId of childNodeIdsByParentNodeId.get(current.nodeId) ?? []) {
|
|
69
|
+
const childNode = nodesById.get(childNodeId);
|
|
70
|
+
if (!childNode)
|
|
71
|
+
continue;
|
|
72
|
+
const nextPath = [...current.path, childNode.name];
|
|
73
|
+
const beforeSize = pathSetByNodeId.get(childNodeId)?.size ?? 0;
|
|
74
|
+
rememberPath(pathSetByNodeId, childNodeId, nextPath);
|
|
75
|
+
const afterSize = pathSetByNodeId.get(childNodeId)?.size ?? 0;
|
|
76
|
+
if (afterSize > beforeSize) {
|
|
77
|
+
queue.push({ nodeId: childNodeId, path: nextPath });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return createGraph({
|
|
83
|
+
entryPackages,
|
|
84
|
+
nodesById,
|
|
85
|
+
nodeIdsByPackageKey,
|
|
86
|
+
parentNodeIdsByChildNodeId,
|
|
87
|
+
childNodeIdsByParentNodeId,
|
|
88
|
+
rangeByParentNodeId,
|
|
89
|
+
pathSetByNodeId,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function createGraph(args) {
|
|
93
|
+
const entryPackages = Object.freeze([...unique(args.entryPackages)]);
|
|
94
|
+
return {
|
|
95
|
+
entryPackages,
|
|
96
|
+
nodeIdsFor(name, version) {
|
|
97
|
+
return Object.freeze([...(args.nodeIdsByPackageKey.get(buildPackageKey(name, version)) ?? [])]);
|
|
98
|
+
},
|
|
99
|
+
getNode(nodeId) {
|
|
100
|
+
const node = args.nodesById.get(nodeId);
|
|
101
|
+
return node ? Object.freeze({ ...node }) : null;
|
|
102
|
+
},
|
|
103
|
+
parentsFor(nodeId) {
|
|
104
|
+
return Object.freeze([...(args.parentNodeIdsByChildNodeId.get(nodeId) ?? [])]);
|
|
105
|
+
},
|
|
106
|
+
childrenFor(nodeId) {
|
|
107
|
+
return Object.freeze([...(args.childNodeIdsByParentNodeId.get(nodeId) ?? [])]);
|
|
108
|
+
},
|
|
109
|
+
rangeFor(parentNodeId, childName) {
|
|
110
|
+
return args.rangeByParentNodeId.get(parentNodeId)?.get(childName) ?? null;
|
|
111
|
+
},
|
|
112
|
+
pathsFor(nodeId) {
|
|
113
|
+
const serializedPaths = [...(args.pathSetByNodeId.get(nodeId) ?? new Set())]
|
|
114
|
+
.map((item) => item.split(">"));
|
|
115
|
+
return uniquePathArrays(serializedPaths);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function resolveEdgesForParent(parentPackagePath, dependencyRanges, resolutionBasePaths, nodeIdByPackagePath, childNodeIdsByParentNodeId, parentNodeIdsByChildNodeId, rangeByParentNodeId) {
|
|
120
|
+
const resolvedChildNodeIds = [];
|
|
121
|
+
for (const [dependencyName, range] of Object.entries(dependencyRanges)) {
|
|
122
|
+
const childPackagePath = resolveDependencyPackagePath(resolutionBasePaths, dependencyName, nodeIdByPackagePath);
|
|
123
|
+
if (!childPackagePath)
|
|
124
|
+
continue;
|
|
125
|
+
const childNodeId = nodeIdByPackagePath.get(childPackagePath);
|
|
126
|
+
if (!childNodeId)
|
|
127
|
+
continue;
|
|
128
|
+
resolvedChildNodeIds.push(childNodeId);
|
|
129
|
+
const parentNodeId = parentPackagePath ? nodeIdByPackagePath.get(parentPackagePath) : null;
|
|
130
|
+
if (!parentNodeId)
|
|
131
|
+
continue;
|
|
132
|
+
childNodeIdsByParentNodeId.set(parentNodeId, unique([...(childNodeIdsByParentNodeId.get(parentNodeId) ?? []), childNodeId]));
|
|
133
|
+
parentNodeIdsByChildNodeId.set(childNodeId, unique([...(parentNodeIdsByChildNodeId.get(childNodeId) ?? []), parentNodeId]));
|
|
134
|
+
const rangesForParent = rangeByParentNodeId.get(parentNodeId) ?? new Map();
|
|
135
|
+
rangesForParent.set(dependencyName, range);
|
|
136
|
+
rangeByParentNodeId.set(parentNodeId, rangesForParent);
|
|
137
|
+
}
|
|
138
|
+
return unique(resolvedChildNodeIds);
|
|
139
|
+
}
|
|
140
|
+
function collectDependencyRanges(meta) {
|
|
141
|
+
if (!meta)
|
|
142
|
+
return {};
|
|
143
|
+
const ranges = {};
|
|
144
|
+
for (const source of [
|
|
145
|
+
meta.dependencies,
|
|
146
|
+
meta.optionalDependencies,
|
|
147
|
+
]) {
|
|
148
|
+
if (!source || typeof source !== "object")
|
|
149
|
+
continue;
|
|
150
|
+
for (const [name, range] of Object.entries(source)) {
|
|
151
|
+
if (typeof range !== "string" || !range)
|
|
152
|
+
continue;
|
|
153
|
+
ranges[name] = range;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return ranges;
|
|
157
|
+
}
|
|
158
|
+
function collectRootDependencyRanges(meta) {
|
|
159
|
+
if (!meta)
|
|
160
|
+
return {};
|
|
161
|
+
const ranges = collectDependencyRanges(meta);
|
|
162
|
+
if (meta.devDependencies && typeof meta.devDependencies === "object") {
|
|
163
|
+
for (const [name, range] of Object.entries(meta.devDependencies)) {
|
|
164
|
+
if (typeof range !== "string" || !range)
|
|
165
|
+
continue;
|
|
166
|
+
ranges[name] = range;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return ranges;
|
|
170
|
+
}
|
|
171
|
+
function resolveDependencyPackagePath(basePaths, dependencyName, nodeIdByPackagePath) {
|
|
172
|
+
for (const basePath of basePaths) {
|
|
173
|
+
let currentPath = basePath;
|
|
174
|
+
while (currentPath !== null) {
|
|
175
|
+
const candidatePath = currentPath
|
|
176
|
+
? `${currentPath}/node_modules/${dependencyName}`
|
|
177
|
+
: `node_modules/${dependencyName}`;
|
|
178
|
+
if (nodeIdByPackagePath.has(candidatePath)) {
|
|
179
|
+
return candidatePath;
|
|
180
|
+
}
|
|
181
|
+
currentPath = nextAncestorPackagePath(currentPath);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
function nextAncestorPackagePath(packagePath) {
|
|
187
|
+
if (!packagePath)
|
|
188
|
+
return null;
|
|
189
|
+
const nestedIndex = packagePath.lastIndexOf("/node_modules/");
|
|
190
|
+
if (nestedIndex >= 0) {
|
|
191
|
+
return packagePath.slice(0, nestedIndex);
|
|
192
|
+
}
|
|
193
|
+
if (packagePath.startsWith("node_modules/")) {
|
|
194
|
+
return "";
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
function packageNameFromPath(packagePath) {
|
|
199
|
+
const marker = "node_modules/";
|
|
200
|
+
const markerIndex = packagePath.lastIndexOf(marker);
|
|
201
|
+
if (markerIndex === -1)
|
|
202
|
+
return null;
|
|
203
|
+
const name = packagePath.slice(markerIndex + marker.length);
|
|
204
|
+
return name || null;
|
|
205
|
+
}
|
|
206
|
+
function rememberPath(pathSetByNodeId, nodeId, pathParts) {
|
|
207
|
+
const serialized = pathParts.join(">");
|
|
208
|
+
const existing = pathSetByNodeId.get(nodeId) ?? new Set();
|
|
209
|
+
existing.add(serialized);
|
|
210
|
+
pathSetByNodeId.set(nodeId, existing);
|
|
211
|
+
}
|
|
212
|
+
function isRecord(value) {
|
|
213
|
+
return !!value && typeof value === "object";
|
|
214
|
+
}
|
|
215
|
+
function buildPackageKey(name, version) {
|
|
216
|
+
return `${name}@${version ?? "<link>"}`;
|
|
217
|
+
}
|
|
218
|
+
function resolveDependencySource(meta, rawPackages) {
|
|
219
|
+
if (!meta)
|
|
220
|
+
return null;
|
|
221
|
+
if (!meta.link || !meta.resolved)
|
|
222
|
+
return meta;
|
|
223
|
+
const linkedTarget = rawPackages[meta.resolved];
|
|
224
|
+
return linkedTarget ?? meta;
|
|
225
|
+
}
|
|
226
|
+
function resolveBasePaths(packagePath, meta) {
|
|
227
|
+
if (!meta?.link || !meta.resolved) {
|
|
228
|
+
return [packagePath];
|
|
229
|
+
}
|
|
230
|
+
return unique([meta.resolved, packagePath]);
|
|
231
|
+
}
|
package/dist/parsers/utils.js
CHANGED
|
@@ -12,7 +12,11 @@ export function upsertPackage(map, candidate) {
|
|
|
12
12
|
export function normalizeNodeModulesPath(pkgPath) {
|
|
13
13
|
const parts = pkgPath.split("/").filter(Boolean);
|
|
14
14
|
const names = ["project"];
|
|
15
|
-
|
|
15
|
+
const firstNodeModulesIndex = parts.indexOf("node_modules");
|
|
16
|
+
if (firstNodeModulesIndex > 0) {
|
|
17
|
+
names.push(...parts.slice(0, firstNodeModulesIndex));
|
|
18
|
+
}
|
|
19
|
+
for (let i = Math.max(firstNodeModulesIndex, 0); i < parts.length; i++) {
|
|
16
20
|
if (parts[i] !== "node_modules")
|
|
17
21
|
continue;
|
|
18
22
|
const next = parts[i + 1];
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { severityOrder } from "../constants.js";
|
|
2
2
|
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
3
|
+
import { getPrimaryParent } from "../utils/finding.js";
|
|
3
4
|
export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
4
5
|
const packageManager = inferPackageManager(scanInput);
|
|
5
6
|
if (!packageManager)
|
|
@@ -98,6 +99,48 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
98
99
|
}
|
|
99
100
|
continue;
|
|
100
101
|
}
|
|
102
|
+
if (packageManager === "npm" &&
|
|
103
|
+
finding.relationship === "transitive" &&
|
|
104
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
105
|
+
upsertTarget(targetsByPackage, {
|
|
106
|
+
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
107
|
+
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
108
|
+
targetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
109
|
+
displayTargetVersion: "within current range",
|
|
110
|
+
scannedVersions: null,
|
|
111
|
+
knownVulnerableVersions: null,
|
|
112
|
+
kind: "parent-update",
|
|
113
|
+
urgent,
|
|
114
|
+
severity: finding.severity,
|
|
115
|
+
adjusted: false,
|
|
116
|
+
adjustmentNote: null,
|
|
117
|
+
reason: `Update ${finding.recommendedNpmTransitiveRemediation.package} within its current dependency range to pick up ${finding.pkg.name}@${finding.recommendedNpmTransitiveRemediation.targetChildVersion}.`,
|
|
118
|
+
command: `npm update ${finding.recommendedNpmTransitiveRemediation.package}`,
|
|
119
|
+
usage: finding.usage ?? null,
|
|
120
|
+
});
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (packageManager === "npm" &&
|
|
124
|
+
finding.relationship === "transitive" &&
|
|
125
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "upgrade-parent-to-version" &&
|
|
126
|
+
finding.recommendedNpmTransitiveRemediation.targetVersion &&
|
|
127
|
+
isUpgradeTarget(finding.recommendedNpmTransitiveRemediation.currentVersion, finding.recommendedNpmTransitiveRemediation.targetVersion)) {
|
|
128
|
+
upsertTarget(targetsByPackage, {
|
|
129
|
+
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
130
|
+
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
131
|
+
targetVersion: finding.recommendedNpmTransitiveRemediation.targetVersion,
|
|
132
|
+
scannedVersions: null,
|
|
133
|
+
knownVulnerableVersions: null,
|
|
134
|
+
kind: "parent-upgrade",
|
|
135
|
+
urgent,
|
|
136
|
+
severity: finding.severity,
|
|
137
|
+
adjusted: false,
|
|
138
|
+
adjustmentNote: null,
|
|
139
|
+
reason: `Parent upgrade for vulnerable ${finding.pkg.name}@${finding.pkg.version}`,
|
|
140
|
+
usage: finding.usage ?? null,
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
101
144
|
if (finding.relationship === "transitive" &&
|
|
102
145
|
finding.recommendedParentUpgrade &&
|
|
103
146
|
isUpgradeTarget(finding.recommendedParentUpgrade.currentVersion, finding.recommendedParentUpgrade.targetVersion)) {
|
|
@@ -126,12 +169,37 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
126
169
|
});
|
|
127
170
|
continue;
|
|
128
171
|
}
|
|
172
|
+
if (packageManager !== "npm" &&
|
|
173
|
+
finding.relationship === "transitive" &&
|
|
174
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
175
|
+
const primaryParent = getPrimaryParent(finding);
|
|
176
|
+
skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
177
|
+
package: finding.pkg.name,
|
|
178
|
+
version: finding.pkg.version,
|
|
179
|
+
relationship: finding.relationship,
|
|
180
|
+
reason: `${finding.pkg.name}@${finding.pkg.version} is pulled in by ${primaryParent ?? finding.recommendedNpmTransitiveRemediation.package}. No safe upgrade version for ${finding.recommendedNpmTransitiveRemediation.package} was identified automatically — npm can update it within the current range, but this fix path is npm-specific.`,
|
|
181
|
+
});
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const primaryParent = getPrimaryParent(finding);
|
|
185
|
+
if (finding.relationship === "transitive" && primaryParent) {
|
|
186
|
+
const fixClause = finding.firstFixedVersion
|
|
187
|
+
? ` — check for a release that resolves ${finding.pkg.name} to ${finding.firstFixedVersion}+`
|
|
188
|
+
: "";
|
|
189
|
+
skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
190
|
+
package: finding.pkg.name,
|
|
191
|
+
version: finding.pkg.version,
|
|
192
|
+
relationship: finding.relationship,
|
|
193
|
+
reason: `${finding.pkg.name}@${finding.pkg.version} is pulled in by ${primaryParent}. No safe upgrade version for ${primaryParent} was identified automatically${fixClause}.`,
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
129
197
|
skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
130
198
|
package: finding.pkg.name,
|
|
131
199
|
version: finding.pkg.version,
|
|
132
200
|
relationship: finding.relationship,
|
|
133
201
|
reason: finding.relationship === "transitive"
|
|
134
|
-
?
|
|
202
|
+
? `No dependency path available for ${finding.pkg.name}@${finding.pkg.version}. Inspect your lockfile to find which package pulls it in.`
|
|
135
203
|
: "No confident automatic fix command could be generated for this issue.",
|
|
136
204
|
});
|
|
137
205
|
}
|
|
@@ -151,7 +219,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
151
219
|
const skipped = [...skippedByKey.values()].sort((a, b) => a.package.localeCompare(b.package));
|
|
152
220
|
const sections = buildSections(targets, packageManager);
|
|
153
221
|
const command = targets.length > 0
|
|
154
|
-
?
|
|
222
|
+
? buildCommandForTargets(targets, packageManager)
|
|
155
223
|
: null;
|
|
156
224
|
return {
|
|
157
225
|
packageManager,
|
|
@@ -208,7 +276,13 @@ function upsertTarget(targetsByPackage, next) {
|
|
|
208
276
|
severity: severityOrder[next.severity] > severityOrder[existing.severity] ? next.severity : existing.severity,
|
|
209
277
|
adjusted: existing.adjusted || next.adjusted,
|
|
210
278
|
adjustmentNote: existing.adjustmentNote ?? next.adjustmentNote ?? null,
|
|
211
|
-
kind: existing.kind === "direct" || next.kind === "direct"
|
|
279
|
+
kind: existing.kind === "direct" || next.kind === "direct"
|
|
280
|
+
? "direct"
|
|
281
|
+
: existing.kind === "parent-update" || next.kind === "parent-update"
|
|
282
|
+
? "parent-update"
|
|
283
|
+
: "parent-upgrade",
|
|
284
|
+
displayTargetVersion: existing.displayTargetVersion ?? next.displayTargetVersion,
|
|
285
|
+
command: existing.command ?? next.command,
|
|
212
286
|
usage: existing.usage || next.usage,
|
|
213
287
|
};
|
|
214
288
|
if (looksLikeVersion(existing.targetVersion) && looksLikeVersion(next.targetVersion)) {
|
|
@@ -242,7 +316,6 @@ function isUpgradeTarget(currentVersion, targetVersion) {
|
|
|
242
316
|
}
|
|
243
317
|
function buildSections(targets, packageManager) {
|
|
244
318
|
const groups = [];
|
|
245
|
-
const commandForTargets = (items) => `${commandPrefix(packageManager)} ${items.map(target => `${target.package}@${target.targetVersion}`).join(" ")}`;
|
|
246
319
|
const sectionOrder = [
|
|
247
320
|
{ kind: "urgent", severity: "critical" },
|
|
248
321
|
{ kind: "urgent", severity: "high" },
|
|
@@ -252,6 +325,9 @@ function buildSections(targets, packageManager) {
|
|
|
252
325
|
{ kind: "direct-adjusted", severity: "medium" },
|
|
253
326
|
{ kind: "direct-adjusted", severity: "low" },
|
|
254
327
|
{ kind: "direct-adjusted", severity: "unknown" },
|
|
328
|
+
{ kind: "parent-update", severity: "medium" },
|
|
329
|
+
{ kind: "parent-update", severity: "low" },
|
|
330
|
+
{ kind: "parent-update", severity: "unknown" },
|
|
255
331
|
{ kind: "parent-upgrade", severity: "medium" },
|
|
256
332
|
{ kind: "parent-upgrade", severity: "low" },
|
|
257
333
|
{ kind: "parent-upgrade", severity: "unknown" },
|
|
@@ -268,7 +344,7 @@ function buildSections(targets, packageManager) {
|
|
|
268
344
|
kind: entry.kind,
|
|
269
345
|
severity: entry.severity,
|
|
270
346
|
title: buildSectionTitle(entry.kind, entry.severity),
|
|
271
|
-
command:
|
|
347
|
+
command: buildCommandForTargets(sectionTargets, packageManager),
|
|
272
348
|
targets: sectionTargets,
|
|
273
349
|
});
|
|
274
350
|
}
|
|
@@ -289,8 +365,31 @@ function buildSectionTitle(kind, severity) {
|
|
|
289
365
|
if (kind === "direct-adjusted") {
|
|
290
366
|
return `${severityTitle} direct fixes (registry-adjusted)`;
|
|
291
367
|
}
|
|
368
|
+
if (kind === "parent-update") {
|
|
369
|
+
return `${severityTitle} parent updates within range`;
|
|
370
|
+
}
|
|
292
371
|
return `${severityTitle} parent upgrades`;
|
|
293
372
|
}
|
|
373
|
+
function buildCommandForTargets(targets, packageManager) {
|
|
374
|
+
const explicitCommands = targets
|
|
375
|
+
.map(target => target.command)
|
|
376
|
+
.filter((value) => Boolean(value));
|
|
377
|
+
const commandParts = [];
|
|
378
|
+
const parentUpdateTargets = targets.filter(target => target.kind === "parent-update");
|
|
379
|
+
const installTargets = targets.filter(target => target.kind !== "parent-update");
|
|
380
|
+
if (parentUpdateTargets.length > 0) {
|
|
381
|
+
const updateCommands = explicitCommands.length > 0
|
|
382
|
+
? [...new Set(explicitCommands)]
|
|
383
|
+
: packageManager === "npm"
|
|
384
|
+
? [`npm update ${parentUpdateTargets.map(target => target.package).join(" ")}`]
|
|
385
|
+
: [];
|
|
386
|
+
commandParts.push(...updateCommands);
|
|
387
|
+
}
|
|
388
|
+
if (installTargets.length > 0) {
|
|
389
|
+
commandParts.push(`${commandPrefix(packageManager)} ${installTargets.map(target => `${target.package}@${target.targetVersion}`).join(" ")}`);
|
|
390
|
+
}
|
|
391
|
+
return commandParts.join(" && ");
|
|
392
|
+
}
|
|
294
393
|
function capitalize(value) {
|
|
295
394
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
296
395
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
2
|
+
export function createNpmTransitiveGraph(args) {
|
|
3
|
+
const nodesById = new Map();
|
|
4
|
+
const nodeIdsByPackageKey = new Map();
|
|
5
|
+
const childNodeIdsByParentNodeId = new Map();
|
|
6
|
+
const rangeByParentNodeId = new Map();
|
|
7
|
+
for (const node of args.nodes) {
|
|
8
|
+
nodesById.set(node.id, { ...node });
|
|
9
|
+
nodeIdsByPackageKey.set(buildPackageKey(node.name, node.version), [
|
|
10
|
+
...(nodeIdsByPackageKey.get(buildPackageKey(node.name, node.version)) ?? []),
|
|
11
|
+
node.id,
|
|
12
|
+
]);
|
|
13
|
+
}
|
|
14
|
+
for (const edge of args.edges) {
|
|
15
|
+
childNodeIdsByParentNodeId.set(edge.parentNodeId, [
|
|
16
|
+
...(childNodeIdsByParentNodeId.get(edge.parentNodeId) ?? []),
|
|
17
|
+
edge.childNodeId,
|
|
18
|
+
]);
|
|
19
|
+
const ranges = rangeByParentNodeId.get(edge.parentNodeId) ?? new Map();
|
|
20
|
+
ranges.set(edge.childName, edge.range);
|
|
21
|
+
rangeByParentNodeId.set(edge.parentNodeId, ranges);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
nodeIdsFor(name, version) {
|
|
25
|
+
return Object.freeze([...(nodeIdsByPackageKey.get(buildPackageKey(name, version)) ?? [])]);
|
|
26
|
+
},
|
|
27
|
+
getNode(nodeId) {
|
|
28
|
+
const node = nodesById.get(nodeId);
|
|
29
|
+
return node ? Object.freeze({ ...node }) : null;
|
|
30
|
+
},
|
|
31
|
+
childrenFor(nodeId) {
|
|
32
|
+
return Object.freeze([...(childNodeIdsByParentNodeId.get(nodeId) ?? [])]);
|
|
33
|
+
},
|
|
34
|
+
rangeFor(parentNodeId, childName) {
|
|
35
|
+
return rangeByParentNodeId.get(parentNodeId)?.get(childName) ?? null;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function buildPackageKey(name, version) {
|
|
40
|
+
return `${name}@${version ?? "null"}`;
|
|
41
|
+
}
|
|
42
|
+
export function findSafeVersionWithinParentRange(args) {
|
|
43
|
+
const allowedRange = args.graph.rangeFor(args.parentNodeId, args.childName);
|
|
44
|
+
if (!allowedRange)
|
|
45
|
+
return null;
|
|
46
|
+
const matchingCandidates = args.candidates
|
|
47
|
+
.filter(looksLikeVersion)
|
|
48
|
+
.filter(candidate => versionSatisfiesRange(candidate, allowedRange))
|
|
49
|
+
.sort(compareVersions);
|
|
50
|
+
return matchingCandidates.at(-1) ?? null;
|
|
51
|
+
}
|
|
52
|
+
function versionSatisfiesRange(version, rawRange) {
|
|
53
|
+
const range = rawRange.trim();
|
|
54
|
+
if (!range)
|
|
55
|
+
return false;
|
|
56
|
+
if (range === "*" || range === "latest")
|
|
57
|
+
return true;
|
|
58
|
+
const orParts = range.split("||").map(part => part.trim()).filter(Boolean);
|
|
59
|
+
return orParts.some(part => satisfiesAndRange(version, part));
|
|
60
|
+
}
|
|
61
|
+
function satisfiesAndRange(version, range) {
|
|
62
|
+
const normalized = normalizeRange(range);
|
|
63
|
+
if (!normalized)
|
|
64
|
+
return false;
|
|
65
|
+
const tokens = normalized.split(/\s+/).filter(Boolean);
|
|
66
|
+
return tokens.every(token => satisfiesComparator(version, token));
|
|
67
|
+
}
|
|
68
|
+
function normalizeRange(range) {
|
|
69
|
+
const trimmed = range.trim();
|
|
70
|
+
if (!trimmed)
|
|
71
|
+
return null;
|
|
72
|
+
if (looksLikeVersion(trimmed)) {
|
|
73
|
+
return `=${trimmed}`;
|
|
74
|
+
}
|
|
75
|
+
if (trimmed.startsWith("^")) {
|
|
76
|
+
const base = trimmed.slice(1);
|
|
77
|
+
if (!looksLikeVersion(base))
|
|
78
|
+
return null;
|
|
79
|
+
const [major, minor, patch] = parseCoreVersion(base);
|
|
80
|
+
if (major > 0)
|
|
81
|
+
return `>=${base} <${major + 1}.0.0`;
|
|
82
|
+
if (minor > 0)
|
|
83
|
+
return `>=${base} <0.${minor + 1}.0`;
|
|
84
|
+
return `>=${base} <0.0.${patch + 1}`;
|
|
85
|
+
}
|
|
86
|
+
if (trimmed.startsWith("~")) {
|
|
87
|
+
const base = trimmed.slice(1);
|
|
88
|
+
if (!looksLikeVersion(base))
|
|
89
|
+
return null;
|
|
90
|
+
const [major, minor] = parseCoreVersion(base);
|
|
91
|
+
return `>=${base} <${major}.${minor + 1}.0`;
|
|
92
|
+
}
|
|
93
|
+
return trimmed;
|
|
94
|
+
}
|
|
95
|
+
function satisfiesComparator(version, comparator) {
|
|
96
|
+
const token = comparator.trim();
|
|
97
|
+
if (!token)
|
|
98
|
+
return true;
|
|
99
|
+
const match = token.match(/^(<=|>=|<|>|=)?\s*([0-9]+\.[0-9]+\.[0-9]+(?:[-+][^\s]+)?)$/);
|
|
100
|
+
if (!match)
|
|
101
|
+
return false;
|
|
102
|
+
const operator = match[1] ?? "=";
|
|
103
|
+
const target = match[2];
|
|
104
|
+
const cmp = compareVersions(version, target);
|
|
105
|
+
switch (operator) {
|
|
106
|
+
case "<":
|
|
107
|
+
return cmp < 0;
|
|
108
|
+
case "<=":
|
|
109
|
+
return cmp <= 0;
|
|
110
|
+
case ">":
|
|
111
|
+
return cmp > 0;
|
|
112
|
+
case ">=":
|
|
113
|
+
return cmp >= 0;
|
|
114
|
+
case "=":
|
|
115
|
+
return cmp === 0;
|
|
116
|
+
default:
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function parseCoreVersion(version) {
|
|
121
|
+
const [major = "0", minor = "0", patch = "0"] = version.split(/[+-]/)[0].split(".");
|
|
122
|
+
return [Number(major), Number(minor), Number(patch)];
|
|
123
|
+
}
|