cve-lite-cli 1.10.0 → 1.12.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 +36 -18
- package/dist/advisory/osv-sync.js +6 -0
- package/dist/index.js +106 -19
- package/dist/output/formatters.js +24 -1
- package/dist/output/html-reporter.js +17 -13
- package/dist/output/printers.js +26 -36
- package/dist/parsers/npm-lock-graph.js +231 -0
- package/dist/parsers/utils.js +5 -1
- package/dist/remediation/fix-commands.js +116 -6
- package/dist/remediation/npm-transitive-graph.js +123 -0
- package/dist/remediation/npm-transitive-resolution.js +238 -0
- package/dist/remediation/parent-upgrade.js +29 -9
- package/dist/scanner.js +114 -10
- 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,10 +1,11 @@
|
|
|
1
1
|
import { severityOrder } from "../constants.js";
|
|
2
2
|
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
3
3
|
import { getPrimaryParent } from "../utils/finding.js";
|
|
4
|
-
export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
4
|
+
export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
5
5
|
const packageManager = inferPackageManager(scanInput);
|
|
6
6
|
if (!packageManager)
|
|
7
7
|
return null;
|
|
8
|
+
const offline = !!options?.offline;
|
|
8
9
|
const prioritizedFindings = [...findings]
|
|
9
10
|
.filter(f => f.severity === "critical" || f.severity === "high")
|
|
10
11
|
.sort((a, b) => {
|
|
@@ -47,9 +48,13 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
47
48
|
if (finding.relationship === "direct" || finding.relationship === "unknown") {
|
|
48
49
|
const hasValidatedField = Object.prototype.hasOwnProperty.call(finding, "validatedFirstFixedVersion");
|
|
49
50
|
const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
|
|
51
|
+
// In offline mode validateDirectFixTargets never runs, so an unset
|
|
52
|
+
// validatedFirstFixedVersion is "did not validate" rather than "validation
|
|
53
|
+
// failed" — fall back to the advisory hint instead of dropping the target.
|
|
54
|
+
const validatedFieldOk = offline || !hasValidatedField || finding.validatedFirstFixedVersion !== null;
|
|
50
55
|
if (directTarget &&
|
|
51
56
|
isUpgradeTarget(finding.pkg.version, directTarget) &&
|
|
52
|
-
|
|
57
|
+
validatedFieldOk) {
|
|
53
58
|
upsertTarget(targetsByPackage, {
|
|
54
59
|
package: finding.pkg.name,
|
|
55
60
|
currentVersion: finding.pkg.version,
|
|
@@ -99,6 +104,48 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
99
104
|
}
|
|
100
105
|
continue;
|
|
101
106
|
}
|
|
107
|
+
if (packageManager === "npm" &&
|
|
108
|
+
finding.relationship === "transitive" &&
|
|
109
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
110
|
+
upsertTarget(targetsByPackage, {
|
|
111
|
+
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
112
|
+
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
113
|
+
targetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
114
|
+
displayTargetVersion: "within current range",
|
|
115
|
+
scannedVersions: null,
|
|
116
|
+
knownVulnerableVersions: null,
|
|
117
|
+
kind: "parent-update",
|
|
118
|
+
urgent,
|
|
119
|
+
severity: finding.severity,
|
|
120
|
+
adjusted: false,
|
|
121
|
+
adjustmentNote: null,
|
|
122
|
+
reason: `Update ${finding.recommendedNpmTransitiveRemediation.package} within its current dependency range to pick up ${finding.pkg.name}@${finding.recommendedNpmTransitiveRemediation.targetChildVersion}.`,
|
|
123
|
+
command: `npm update ${finding.recommendedNpmTransitiveRemediation.package}`,
|
|
124
|
+
usage: finding.usage ?? null,
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (packageManager === "npm" &&
|
|
129
|
+
finding.relationship === "transitive" &&
|
|
130
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "upgrade-parent-to-version" &&
|
|
131
|
+
finding.recommendedNpmTransitiveRemediation.targetVersion &&
|
|
132
|
+
isUpgradeTarget(finding.recommendedNpmTransitiveRemediation.currentVersion, finding.recommendedNpmTransitiveRemediation.targetVersion)) {
|
|
133
|
+
upsertTarget(targetsByPackage, {
|
|
134
|
+
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
135
|
+
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
136
|
+
targetVersion: finding.recommendedNpmTransitiveRemediation.targetVersion,
|
|
137
|
+
scannedVersions: null,
|
|
138
|
+
knownVulnerableVersions: null,
|
|
139
|
+
kind: "parent-upgrade",
|
|
140
|
+
urgent,
|
|
141
|
+
severity: finding.severity,
|
|
142
|
+
adjusted: false,
|
|
143
|
+
adjustmentNote: null,
|
|
144
|
+
reason: `Parent upgrade for vulnerable ${finding.pkg.name}@${finding.pkg.version}`,
|
|
145
|
+
usage: finding.usage ?? null,
|
|
146
|
+
});
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
102
149
|
if (finding.relationship === "transitive" &&
|
|
103
150
|
finding.recommendedParentUpgrade &&
|
|
104
151
|
isUpgradeTarget(finding.recommendedParentUpgrade.currentVersion, finding.recommendedParentUpgrade.targetVersion)) {
|
|
@@ -127,6 +174,18 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
127
174
|
});
|
|
128
175
|
continue;
|
|
129
176
|
}
|
|
177
|
+
if (packageManager !== "npm" &&
|
|
178
|
+
finding.relationship === "transitive" &&
|
|
179
|
+
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
180
|
+
const primaryParent = getPrimaryParent(finding);
|
|
181
|
+
skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
182
|
+
package: finding.pkg.name,
|
|
183
|
+
version: finding.pkg.version,
|
|
184
|
+
relationship: finding.relationship,
|
|
185
|
+
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.`,
|
|
186
|
+
});
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
130
189
|
const primaryParent = getPrimaryParent(finding);
|
|
131
190
|
if (finding.relationship === "transitive" && primaryParent) {
|
|
132
191
|
const fixClause = finding.firstFixedVersion
|
|
@@ -165,7 +224,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
165
224
|
const skipped = [...skippedByKey.values()].sort((a, b) => a.package.localeCompare(b.package));
|
|
166
225
|
const sections = buildSections(targets, packageManager);
|
|
167
226
|
const command = targets.length > 0
|
|
168
|
-
?
|
|
227
|
+
? buildCommandForTargets(targets, packageManager)
|
|
169
228
|
: null;
|
|
170
229
|
return {
|
|
171
230
|
packageManager,
|
|
@@ -196,6 +255,26 @@ function commandPrefix(packageManager) {
|
|
|
196
255
|
return "bun add";
|
|
197
256
|
return "yarn add";
|
|
198
257
|
}
|
|
258
|
+
export function findSuggestedCommandForFinding(plan, finding) {
|
|
259
|
+
const target = plan.targets.find(item => {
|
|
260
|
+
if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
261
|
+
return (item.kind === "parent-update" &&
|
|
262
|
+
item.package === finding.recommendedNpmTransitiveRemediation.package &&
|
|
263
|
+
item.targetVersion === finding.recommendedNpmTransitiveRemediation.targetChildVersion);
|
|
264
|
+
}
|
|
265
|
+
if (finding.recommendedParentUpgrade) {
|
|
266
|
+
return (item.package === finding.recommendedParentUpgrade.package &&
|
|
267
|
+
item.targetVersion === finding.recommendedParentUpgrade.targetVersion);
|
|
268
|
+
}
|
|
269
|
+
const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
|
|
270
|
+
return item.package === finding.pkg.name && item.targetVersion === directTarget;
|
|
271
|
+
});
|
|
272
|
+
if (!target)
|
|
273
|
+
return null;
|
|
274
|
+
if (target.command)
|
|
275
|
+
return target.command;
|
|
276
|
+
return `${commandPrefix(plan.packageManager)} ${target.package}@${target.targetVersion}`;
|
|
277
|
+
}
|
|
199
278
|
function packageManagerSourceLabel(scanInput) {
|
|
200
279
|
if (scanInput.filePath) {
|
|
201
280
|
return scanInput.filePath.split(/[/\\]/).pop() ?? scanInput.source;
|
|
@@ -222,7 +301,13 @@ function upsertTarget(targetsByPackage, next) {
|
|
|
222
301
|
severity: severityOrder[next.severity] > severityOrder[existing.severity] ? next.severity : existing.severity,
|
|
223
302
|
adjusted: existing.adjusted || next.adjusted,
|
|
224
303
|
adjustmentNote: existing.adjustmentNote ?? next.adjustmentNote ?? null,
|
|
225
|
-
kind: existing.kind === "direct" || next.kind === "direct"
|
|
304
|
+
kind: existing.kind === "direct" || next.kind === "direct"
|
|
305
|
+
? "direct"
|
|
306
|
+
: existing.kind === "parent-update" || next.kind === "parent-update"
|
|
307
|
+
? "parent-update"
|
|
308
|
+
: "parent-upgrade",
|
|
309
|
+
displayTargetVersion: existing.displayTargetVersion ?? next.displayTargetVersion,
|
|
310
|
+
command: existing.command ?? next.command,
|
|
226
311
|
usage: existing.usage || next.usage,
|
|
227
312
|
};
|
|
228
313
|
if (looksLikeVersion(existing.targetVersion) && looksLikeVersion(next.targetVersion)) {
|
|
@@ -256,7 +341,6 @@ function isUpgradeTarget(currentVersion, targetVersion) {
|
|
|
256
341
|
}
|
|
257
342
|
function buildSections(targets, packageManager) {
|
|
258
343
|
const groups = [];
|
|
259
|
-
const commandForTargets = (items) => `${commandPrefix(packageManager)} ${items.map(target => `${target.package}@${target.targetVersion}`).join(" ")}`;
|
|
260
344
|
const sectionOrder = [
|
|
261
345
|
{ kind: "urgent", severity: "critical" },
|
|
262
346
|
{ kind: "urgent", severity: "high" },
|
|
@@ -266,6 +350,9 @@ function buildSections(targets, packageManager) {
|
|
|
266
350
|
{ kind: "direct-adjusted", severity: "medium" },
|
|
267
351
|
{ kind: "direct-adjusted", severity: "low" },
|
|
268
352
|
{ kind: "direct-adjusted", severity: "unknown" },
|
|
353
|
+
{ kind: "parent-update", severity: "medium" },
|
|
354
|
+
{ kind: "parent-update", severity: "low" },
|
|
355
|
+
{ kind: "parent-update", severity: "unknown" },
|
|
269
356
|
{ kind: "parent-upgrade", severity: "medium" },
|
|
270
357
|
{ kind: "parent-upgrade", severity: "low" },
|
|
271
358
|
{ kind: "parent-upgrade", severity: "unknown" },
|
|
@@ -282,7 +369,7 @@ function buildSections(targets, packageManager) {
|
|
|
282
369
|
kind: entry.kind,
|
|
283
370
|
severity: entry.severity,
|
|
284
371
|
title: buildSectionTitle(entry.kind, entry.severity),
|
|
285
|
-
command:
|
|
372
|
+
command: buildCommandForTargets(sectionTargets, packageManager),
|
|
286
373
|
targets: sectionTargets,
|
|
287
374
|
});
|
|
288
375
|
}
|
|
@@ -303,8 +390,31 @@ function buildSectionTitle(kind, severity) {
|
|
|
303
390
|
if (kind === "direct-adjusted") {
|
|
304
391
|
return `${severityTitle} direct fixes (registry-adjusted)`;
|
|
305
392
|
}
|
|
393
|
+
if (kind === "parent-update") {
|
|
394
|
+
return `${severityTitle} parent updates within range`;
|
|
395
|
+
}
|
|
306
396
|
return `${severityTitle} parent upgrades`;
|
|
307
397
|
}
|
|
398
|
+
function buildCommandForTargets(targets, packageManager) {
|
|
399
|
+
const explicitCommands = targets
|
|
400
|
+
.map(target => target.command)
|
|
401
|
+
.filter((value) => Boolean(value));
|
|
402
|
+
const commandParts = [];
|
|
403
|
+
const parentUpdateTargets = targets.filter(target => target.kind === "parent-update");
|
|
404
|
+
const installTargets = targets.filter(target => target.kind !== "parent-update");
|
|
405
|
+
if (parentUpdateTargets.length > 0) {
|
|
406
|
+
const updateCommands = explicitCommands.length > 0
|
|
407
|
+
? [...new Set(explicitCommands)]
|
|
408
|
+
: packageManager === "npm"
|
|
409
|
+
? [`npm update ${parentUpdateTargets.map(target => target.package).join(" ")}`]
|
|
410
|
+
: [];
|
|
411
|
+
commandParts.push(...updateCommands);
|
|
412
|
+
}
|
|
413
|
+
if (installTargets.length > 0) {
|
|
414
|
+
commandParts.push(`${commandPrefix(packageManager)} ${installTargets.map(target => `${target.package}@${target.targetVersion}`).join(" ")}`);
|
|
415
|
+
}
|
|
416
|
+
return commandParts.join(" && ");
|
|
417
|
+
}
|
|
308
418
|
function capitalize(value) {
|
|
309
419
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
310
420
|
}
|
|
@@ -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
|
+
}
|