cve-lite-cli 1.7.0 → 1.8.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 +6 -0
- package/dist/cli/args.js +9 -0
- package/dist/cli/help.js +2 -0
- package/dist/index.js +21 -2
- package/dist/output/formatters.js +35 -14
- package/dist/output/printers.js +81 -29
- package/dist/remediation/fix-commands.js +11 -0
- package/dist/remediation/npm-registry.js +2 -1
- package/dist/remediation/parent-upgrade.js +3 -1
- package/dist/scanner.js +2 -2
- package/dist/usage/scanner.js +99 -0
- package/dist/utils/version.js +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -137,6 +137,12 @@ cve-lite /path/to/project --fail-on high
|
|
|
137
137
|
# JSON output
|
|
138
138
|
cve-lite /path/to/project --json
|
|
139
139
|
|
|
140
|
+
# Scan project source files to check if vulnerable dependencies are actually imported
|
|
141
|
+
cve-lite /path/to/project --usage
|
|
142
|
+
|
|
143
|
+
# Filter out noise by only showing vulnerabilities in packages that are imported in your source code
|
|
144
|
+
cve-lite /path/to/project --usage --only-used
|
|
145
|
+
|
|
140
146
|
# Sync the local advisory DB for offline scans
|
|
141
147
|
cve-lite advisories sync
|
|
142
148
|
|
package/dist/cli/args.js
CHANGED
|
@@ -129,6 +129,15 @@ export function parseArgs(argv) {
|
|
|
129
129
|
options.minSeverity = arg.slice("--min-severity=".length);
|
|
130
130
|
continue;
|
|
131
131
|
}
|
|
132
|
+
if (arg === "--usage" || arg === "--usage-hints") {
|
|
133
|
+
options.usage = true;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (arg === "--only-used") {
|
|
137
|
+
options.onlyUsed = true;
|
|
138
|
+
options.usage = true;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
132
141
|
if (arg.startsWith("-"))
|
|
133
142
|
throw new Error(`Unknown option: ${arg}`);
|
|
134
143
|
if (!projectArg) {
|
package/dist/cli/help.js
CHANGED
|
@@ -37,6 +37,8 @@ export function printHelp() {
|
|
|
37
37
|
" --prod-only Exclude dev dependencies where available",
|
|
38
38
|
" --fail-on <severity> Exit non-zero at or above severity (default: critical)",
|
|
39
39
|
" --batch-size <number> OSV batch size (default: 100)",
|
|
40
|
+
" --usage Scan project source files to check if vulnerable dependencies are imported",
|
|
41
|
+
" --only-used Filter out findings for packages that are not imported in your source code",
|
|
40
42
|
" --offline Scan using the local advisory database",
|
|
41
43
|
" --offline-db <path> Use a specific local advisory database file",
|
|
42
44
|
" --cache-dir <path> Override cache directory",
|
package/dist/index.js
CHANGED
|
@@ -13,9 +13,10 @@ import { severityOrder } from "./constants.js";
|
|
|
13
13
|
import { chalk } from "./utils/chalk.js";
|
|
14
14
|
import { createSpinner } from "./output/spinner.js";
|
|
15
15
|
import { buildSuggestedFixCommandPlan } from "./remediation/fix-commands.js";
|
|
16
|
+
import { scanProjectForPackageUsage } from "./usage/scanner.js";
|
|
16
17
|
import { getCliVersion } from "./utils/version-info.js";
|
|
17
18
|
import { isLikelyBlockedAdvisoryRequestError } from "./utils/network.js";
|
|
18
|
-
import { logInfo, logWarn, printCacheSummary, serializeFinding } from "./output/formatters.js";
|
|
19
|
+
import { logInfo, logWarn, printCacheSummary, serializeFinding, sortFindingsForOutput } from "./output/formatters.js";
|
|
19
20
|
import { printSummary, printActionSummary, printPriorityFixes, printFixPlan, printSuggestedFixCommands, printSuggestedFixCommandSkips, printCoverage, printSkippedDependencies, printGroupSummary, printTable, printPathHints, printFinalStatus, printCompactOutput } from "./output/printers.js";
|
|
20
21
|
let parsedArgs = null;
|
|
21
22
|
try {
|
|
@@ -246,7 +247,25 @@ function formatAdvisorySourceLine(sourceLabel) {
|
|
|
246
247
|
async function scanProject(params) {
|
|
247
248
|
const directDependencyNames = readDirectDependencyNames(params.projectPath, !!params.options.prodOnly);
|
|
248
249
|
const findings = await scanPackages(params.scanInput.packages, params.batchSize, params.options, { directDependencyNames });
|
|
249
|
-
|
|
250
|
+
if (params.options.usage) {
|
|
251
|
+
console.log(chalk.gray(`Scanning project source for usage hints...`));
|
|
252
|
+
const pkgNames = new Set(findings.map(f => f.pkg.name));
|
|
253
|
+
const usageData = scanProjectForPackageUsage(params.projectPath, pkgNames);
|
|
254
|
+
for (const finding of findings) {
|
|
255
|
+
const files = usageData[finding.pkg.name];
|
|
256
|
+
if (files) {
|
|
257
|
+
finding.usage = {
|
|
258
|
+
imported: files.length > 0,
|
|
259
|
+
files,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
let finalFindings = findings;
|
|
265
|
+
if (params.options.onlyUsed) {
|
|
266
|
+
finalFindings = finalFindings.filter(f => f.usage?.imported);
|
|
267
|
+
}
|
|
268
|
+
const sorted = sortFindingsForOutput(finalFindings);
|
|
250
269
|
const coverage = buildCoverageNotes(params.scanInput, !!params.options.offline || !!params.options.offlineDb);
|
|
251
270
|
const minSeverity = normalizeSeverity(params.options.minSeverity || "medium");
|
|
252
271
|
const tableFindings = params.options.all
|
|
@@ -17,9 +17,9 @@ export function formatSeverityLabel(severity) {
|
|
|
17
17
|
return severity;
|
|
18
18
|
}
|
|
19
19
|
export function formatRelationshipLabel(value) {
|
|
20
|
-
if (value
|
|
20
|
+
if (value.startsWith("direct"))
|
|
21
21
|
return chalk.green(value);
|
|
22
|
-
if (value
|
|
22
|
+
if (value.startsWith("transitive"))
|
|
23
23
|
return chalk.yellow(value);
|
|
24
24
|
return chalk.gray(value);
|
|
25
25
|
}
|
|
@@ -52,25 +52,37 @@ export function getRecommendedAction(finding) {
|
|
|
52
52
|
return `Inspect the dependency path for ${finding.pkg.name} and choose the safest upgrade path.`;
|
|
53
53
|
}
|
|
54
54
|
export function summarizeRisk(finding) {
|
|
55
|
+
let risk = "";
|
|
55
56
|
if (finding.severity === "critical" && finding.relationship === "direct") {
|
|
56
|
-
|
|
57
|
+
risk = "Critical direct dependency. Prioritize this first because the project controls it directly.";
|
|
57
58
|
}
|
|
58
|
-
if (finding.severity === "high" && finding.relationship === "direct") {
|
|
59
|
-
|
|
59
|
+
else if (finding.severity === "high" && finding.relationship === "direct") {
|
|
60
|
+
risk = "High-severity direct dependency. A direct upgrade is likely the fastest path.";
|
|
60
61
|
}
|
|
61
|
-
if (finding.relationship === "transitive" && finding.recommendedParentUpgrade) {
|
|
62
|
-
|
|
62
|
+
else if (finding.relationship === "transitive" && finding.recommendedParentUpgrade) {
|
|
63
|
+
risk = `Transitive issue. A specific parent upgrade target was found for ${finding.recommendedParentUpgrade.package}.`;
|
|
63
64
|
}
|
|
64
|
-
if (finding.relationship === "transitive" && finding.firstFixedVersion) {
|
|
65
|
-
|
|
65
|
+
else if (finding.relationship === "transitive" && finding.firstFixedVersion) {
|
|
66
|
+
risk = `Transitive issue. Look for a parent dependency upgrade that pulls in ${finding.firstFixedVersion}+`;
|
|
66
67
|
}
|
|
67
|
-
if (finding.relationship === "transitive") {
|
|
68
|
-
|
|
68
|
+
else if (finding.relationship === "transitive") {
|
|
69
|
+
risk = "Transitive issue. Review the parent path and check whether an upstream package can be upgraded.";
|
|
69
70
|
}
|
|
70
|
-
if (finding.firstFixedVersion) {
|
|
71
|
-
|
|
71
|
+
else if (finding.firstFixedVersion) {
|
|
72
|
+
risk = `A fixed-version hint exists. Aim for at least ${finding.firstFixedVersion}.`;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
risk = "Review this finding and inspect the dependency path for the safest upgrade path.";
|
|
76
|
+
}
|
|
77
|
+
if (finding.usage && !finding.usage.imported) {
|
|
78
|
+
if (finding.relationship === "transitive") {
|
|
79
|
+
risk = risk.replace("Transitive issue.", "Transitive issue. No direct imports found in source code, so practical reachability may be low.");
|
|
80
|
+
}
|
|
81
|
+
else if (finding.relationship === "direct") {
|
|
82
|
+
risk = risk.replace("direct dependency.", "direct dependency. No direct imports found in source code, so practical reachability may be low.");
|
|
83
|
+
}
|
|
72
84
|
}
|
|
73
|
-
return
|
|
85
|
+
return risk;
|
|
74
86
|
}
|
|
75
87
|
export function summarizeNextAction(finding) {
|
|
76
88
|
if (finding.relationship === "direct" && finding.firstFixedVersion) {
|
|
@@ -103,6 +115,7 @@ export function serializeFinding(finding) {
|
|
|
103
115
|
recommendedParentUpgrade: finding.recommendedParentUpgrade,
|
|
104
116
|
cves: finding.cveAliases,
|
|
105
117
|
dependencyPaths: finding.dependencyPaths,
|
|
118
|
+
usage: finding.usage ?? null,
|
|
106
119
|
vulnerabilities: finding.vulnerabilities.map(v => ({
|
|
107
120
|
id: v.id,
|
|
108
121
|
aliases: v.aliases ?? [],
|
|
@@ -141,6 +154,14 @@ export function sortFindingsForOutput(findings) {
|
|
|
141
154
|
const sevDelta = severityOrder[b.severity] - severityOrder[a.severity];
|
|
142
155
|
if (sevDelta !== 0)
|
|
143
156
|
return sevDelta;
|
|
157
|
+
const usageScore = (f) => f.usage?.imported ? 1 : 0;
|
|
158
|
+
const usageDelta = usageScore(b) - usageScore(a);
|
|
159
|
+
if (usageDelta !== 0)
|
|
160
|
+
return usageDelta;
|
|
161
|
+
const relScore = (f) => f.relationship === "direct" ? 1 : 0;
|
|
162
|
+
const relDelta = relScore(b) - relScore(a);
|
|
163
|
+
if (relDelta !== 0)
|
|
164
|
+
return relDelta;
|
|
144
165
|
return a.pkg.name.localeCompare(b.pkg.name);
|
|
145
166
|
});
|
|
146
167
|
}
|
package/dist/output/printers.js
CHANGED
|
@@ -61,7 +61,11 @@ export function printPriorityFixes(findings) {
|
|
|
61
61
|
console.log("");
|
|
62
62
|
console.log(chalk.bold.magenta("🚀 Top priority fixes"));
|
|
63
63
|
for (const finding of top) {
|
|
64
|
-
|
|
64
|
+
let usageNote = "";
|
|
65
|
+
if (finding.usage) {
|
|
66
|
+
usageNote = finding.usage.imported ? `, imported in ${finding.usage.files.length} file${finding.usage.files.length === 1 ? "" : "s"}` : `, no direct import found`;
|
|
67
|
+
}
|
|
68
|
+
console.log(`- ${chalk.whiteBright(finding.pkg.name)}@${chalk.white(finding.pkg.version)} (${formatSeverityLabel(finding.severity)}, ${formatRelationshipLabel(finding.relationship)}${usageNote})`);
|
|
65
69
|
console.log(` Risk: ${summarizeRisk(finding)}`);
|
|
66
70
|
const parent = getPrimaryParent(finding);
|
|
67
71
|
if (parent)
|
|
@@ -135,7 +139,7 @@ export function printSuggestedFixCommands(findings, scanInput) {
|
|
|
135
139
|
if (section.kind === "direct" || section.kind === "direct-adjusted" || (section.kind === "urgent" && hasDirectTargets)) {
|
|
136
140
|
const validationSummary = summarizeAdjustedValidation(section.targets);
|
|
137
141
|
const remainingNotes = [];
|
|
138
|
-
// Urgent sections compute their own widths so the
|
|
142
|
+
// Urgent sections compute their own widths so the Breaking? column
|
|
139
143
|
// is not truncated by widths derived from smaller non-urgent sections.
|
|
140
144
|
const tableWidths = section.kind === "urgent" ? undefined : sharedDirectTableWidths;
|
|
141
145
|
printDirectTargetsTable(section.targets, remainingNotes, validationSummary, tableWidths);
|
|
@@ -211,15 +215,22 @@ export function printGroupSummary(findings) {
|
|
|
211
215
|
}
|
|
212
216
|
}
|
|
213
217
|
export function printTable(findings, threshold) {
|
|
214
|
-
const headers = ["Package", "Version", "Severity", "Type", "Fixed", "IDs"];
|
|
215
|
-
const rawRows = findings.map(f =>
|
|
216
|
-
|
|
217
|
-
f.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
218
|
+
const headers = ["Package", "Version", "Severity", "Type", "Usage", "Fixed", "IDs"];
|
|
219
|
+
const rawRows = findings.map(f => {
|
|
220
|
+
let usageText = "n/a";
|
|
221
|
+
if (f.usage) {
|
|
222
|
+
usageText = f.usage.imported ? `${f.usage.files.length} file(s)` : "unused";
|
|
223
|
+
}
|
|
224
|
+
return [
|
|
225
|
+
f.pkg.name,
|
|
226
|
+
f.pkg.version,
|
|
227
|
+
f.severity,
|
|
228
|
+
f.relationship,
|
|
229
|
+
usageText,
|
|
230
|
+
f.firstFixedVersion ?? "-",
|
|
231
|
+
f.vulnerabilities.map(v => v.id).join(", ")
|
|
232
|
+
];
|
|
233
|
+
});
|
|
223
234
|
const widths = headers.map((header, index) => Math.min(40, Math.max(header.length, ...rawRows.map(r => String(r[index]).length))));
|
|
224
235
|
console.log("");
|
|
225
236
|
if (threshold) {
|
|
@@ -230,13 +241,21 @@ export function printTable(findings, threshold) {
|
|
|
230
241
|
console.log(renderRow(headers, widths));
|
|
231
242
|
console.log(line("├", "┼", "┤"));
|
|
232
243
|
for (const row of rawRows) {
|
|
244
|
+
let usageDecorated = String(row[4]);
|
|
245
|
+
if (usageDecorated.includes("file(s)"))
|
|
246
|
+
usageDecorated = chalk.red(usageDecorated);
|
|
247
|
+
else if (usageDecorated.includes("unused"))
|
|
248
|
+
usageDecorated = chalk.green(usageDecorated);
|
|
249
|
+
else
|
|
250
|
+
usageDecorated = chalk.gray(usageDecorated);
|
|
233
251
|
const decorated = [
|
|
234
252
|
row[0],
|
|
235
253
|
row[1],
|
|
236
254
|
formatSeverityLabel(String(row[2])),
|
|
237
255
|
formatRelationshipLabel(String(row[3])),
|
|
238
|
-
|
|
239
|
-
row[5]
|
|
256
|
+
usageDecorated,
|
|
257
|
+
row[5],
|
|
258
|
+
row[6]
|
|
240
259
|
];
|
|
241
260
|
console.log(renderRow(decorated, widths));
|
|
242
261
|
}
|
|
@@ -311,8 +330,17 @@ export function printCompactOutput(findings, scanInput) {
|
|
|
311
330
|
: finding.relationship === "transitive"
|
|
312
331
|
? "Transitive dependency"
|
|
313
332
|
: "Unknown dependency";
|
|
333
|
+
let usageContext = "";
|
|
334
|
+
if (finding.usage) {
|
|
335
|
+
if (finding.usage.imported) {
|
|
336
|
+
usageContext = chalk.red(` (imported in ${finding.usage.files.length} file${finding.usage.files.length === 1 ? "" : "s"})`);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
usageContext = chalk.green(` (no direct import found)`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
314
342
|
console.log(`${formatSeverityLabel(sevLabel)} ${chalk.whiteBright(finding.pkg.name)}@${finding.pkg.version}`);
|
|
315
|
-
console.log(` ${typeLabel}`);
|
|
343
|
+
console.log(` ${typeLabel}${usageContext}`);
|
|
316
344
|
if (finding.recommendedParentUpgrade) {
|
|
317
345
|
console.log(` ${chalk.gray(`Fix: upgrade ${finding.recommendedParentUpgrade.package} to ${finding.recommendedParentUpgrade.targetVersion}`)}`);
|
|
318
346
|
}
|
|
@@ -499,7 +527,7 @@ function summarizeAdjustedValidation(targets) {
|
|
|
499
527
|
return { checked, vulnerable };
|
|
500
528
|
}
|
|
501
529
|
function printDirectTargetsTable(targets, remainingNotes, validationSummary, widthsOverride) {
|
|
502
|
-
const headers = ["Package", "Current", "
|
|
530
|
+
const headers = ["Package", "Current", "Target", "Usage", "Versions scanned", "Still known vulnerable", "Breaking?"];
|
|
503
531
|
const rows = targets.map(target => {
|
|
504
532
|
if (target.adjustmentNote) {
|
|
505
533
|
const isCountedVulnerabilityNote = target.scannedVersions !== null &&
|
|
@@ -510,13 +538,15 @@ function printDirectTargetsTable(targets, remainingNotes, validationSummary, wid
|
|
|
510
538
|
}
|
|
511
539
|
}
|
|
512
540
|
const isBreaking = !!target.currentVersion && isMajorVersionBump(target.currentVersion, target.targetVersion);
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
541
|
+
let usageText = chalk.gray("n/a");
|
|
542
|
+
if (target.usage) {
|
|
543
|
+
usageText = target.usage.imported ? chalk.red(`${target.usage.files.length} file(s)`) : chalk.green("unused");
|
|
544
|
+
}
|
|
516
545
|
return [
|
|
517
546
|
target.package,
|
|
518
547
|
target.currentVersion ?? "-",
|
|
519
|
-
|
|
548
|
+
chalk.cyan(target.targetVersion),
|
|
549
|
+
usageText,
|
|
520
550
|
target.scannedVersions !== null && target.scannedVersions !== undefined
|
|
521
551
|
? chalk.blue(String(target.scannedVersions))
|
|
522
552
|
: "-",
|
|
@@ -525,6 +555,7 @@ function printDirectTargetsTable(targets, remainingNotes, validationSummary, wid
|
|
|
525
555
|
? chalk.yellow(String(target.knownVulnerableVersions))
|
|
526
556
|
: chalk.green(String(target.knownVulnerableVersions))
|
|
527
557
|
: "-",
|
|
558
|
+
isBreaking ? chalk.yellow("⚠") : "",
|
|
528
559
|
];
|
|
529
560
|
});
|
|
530
561
|
if (rows.length === 0)
|
|
@@ -543,22 +574,31 @@ function printDirectTargetsTable(targets, remainingNotes, validationSummary, wid
|
|
|
543
574
|
chalk.bold("Total"),
|
|
544
575
|
"-",
|
|
545
576
|
"-",
|
|
577
|
+
"-",
|
|
546
578
|
chalk.blue(String(validationSummary.checked)),
|
|
547
579
|
validationSummary.vulnerable > 0
|
|
548
580
|
? chalk.yellow(String(validationSummary.vulnerable))
|
|
549
581
|
: chalk.green(String(validationSummary.vulnerable)),
|
|
582
|
+
"",
|
|
550
583
|
], widths));
|
|
551
584
|
}
|
|
552
585
|
console.log(line("└", "┴", "┘"));
|
|
553
586
|
}
|
|
554
587
|
function printParentUpgradeTargetsTable(targets, widthsOverride) {
|
|
555
|
-
const headers = ["Package", "Current", "Recommended target", "Context"];
|
|
556
|
-
const rows = targets.map(target =>
|
|
557
|
-
|
|
558
|
-
target.
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
588
|
+
const headers = ["Package", "Current", "Recommended target", "Usage", "Context"];
|
|
589
|
+
const rows = targets.map(target => {
|
|
590
|
+
let usageText = chalk.gray("n/a");
|
|
591
|
+
if (target.usage) {
|
|
592
|
+
usageText = target.usage.imported ? chalk.red(`${target.usage.files.length} file(s)`) : chalk.green("unused");
|
|
593
|
+
}
|
|
594
|
+
return [
|
|
595
|
+
target.package,
|
|
596
|
+
target.currentVersion ?? "-",
|
|
597
|
+
chalk.cyan(target.targetVersion),
|
|
598
|
+
usageText,
|
|
599
|
+
chalk.gray(target.reason),
|
|
600
|
+
];
|
|
601
|
+
});
|
|
562
602
|
if (rows.length === 0)
|
|
563
603
|
return;
|
|
564
604
|
const widths = widthsOverride ?? computeTableWidths(headers, rows);
|
|
@@ -578,20 +618,27 @@ function computeSharedDirectTableWidths(sections) {
|
|
|
578
618
|
const directSections = sections.filter(section => section.kind === "direct" || section.kind === "direct-adjusted");
|
|
579
619
|
if (directSections.length === 0)
|
|
580
620
|
return undefined;
|
|
581
|
-
const headers = ["Package", "Current", "
|
|
621
|
+
const headers = ["Package", "Current", "Target", "Usage", "Versions scanned", "Still known vulnerable", "Breaking?"];
|
|
582
622
|
const rows = [];
|
|
583
623
|
for (const section of directSections) {
|
|
584
624
|
for (const target of section.targets) {
|
|
625
|
+
const isBreaking = !!target.currentVersion && isMajorVersionBump(target.currentVersion, target.targetVersion);
|
|
626
|
+
let usageText = "n/a";
|
|
627
|
+
if (target.usage) {
|
|
628
|
+
usageText = target.usage.imported ? `${target.usage.files.length} file(s)` : "unused";
|
|
629
|
+
}
|
|
585
630
|
rows.push([
|
|
586
631
|
target.package,
|
|
587
632
|
target.currentVersion ?? "-",
|
|
588
633
|
target.targetVersion,
|
|
634
|
+
usageText,
|
|
589
635
|
target.scannedVersions !== null && target.scannedVersions !== undefined ? String(target.scannedVersions) : "-",
|
|
590
636
|
target.knownVulnerableVersions !== null && target.knownVulnerableVersions !== undefined ? String(target.knownVulnerableVersions) : "-",
|
|
637
|
+
isBreaking ? "⚠" : "",
|
|
591
638
|
]);
|
|
592
639
|
}
|
|
593
640
|
const summary = summarizeAdjustedValidation(section.targets);
|
|
594
|
-
rows.push(["Total", "-", "-", String(summary.checked), String(summary.vulnerable)]);
|
|
641
|
+
rows.push(["Total", "-", "-", "-", String(summary.checked), String(summary.vulnerable), ""]);
|
|
595
642
|
}
|
|
596
643
|
return computeTableWidths(headers, rows);
|
|
597
644
|
}
|
|
@@ -602,14 +649,19 @@ function computeSharedParentUpgradeTableWidths(sections) {
|
|
|
602
649
|
const parentSections = sections.filter(section => shouldRenderParentUpgradeTable(section.targets));
|
|
603
650
|
if (parentSections.length === 0)
|
|
604
651
|
return undefined;
|
|
605
|
-
const headers = ["Package", "Current", "Recommended target", "Context"];
|
|
652
|
+
const headers = ["Package", "Current", "Recommended target", "Usage", "Context"];
|
|
606
653
|
const rows = [];
|
|
607
654
|
for (const section of parentSections) {
|
|
608
655
|
for (const target of section.targets) {
|
|
656
|
+
let usageText = "n/a";
|
|
657
|
+
if (target.usage) {
|
|
658
|
+
usageText = target.usage.imported ? `${target.usage.files.length} file(s)` : "unused";
|
|
659
|
+
}
|
|
609
660
|
rows.push([
|
|
610
661
|
target.package,
|
|
611
662
|
target.currentVersion ?? "-",
|
|
612
663
|
target.targetVersion,
|
|
664
|
+
usageText,
|
|
613
665
|
target.reason,
|
|
614
666
|
]);
|
|
615
667
|
}
|
|
@@ -10,6 +10,10 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
10
10
|
const sevDelta = severityOrder[b.severity] - severityOrder[a.severity];
|
|
11
11
|
if (sevDelta !== 0)
|
|
12
12
|
return sevDelta;
|
|
13
|
+
const usageScore = (finding) => finding.usage?.imported ? 1 : 0;
|
|
14
|
+
const usageDelta = usageScore(b) - usageScore(a);
|
|
15
|
+
if (usageDelta !== 0)
|
|
16
|
+
return usageDelta;
|
|
13
17
|
const relScore = (finding) => finding.relationship === "direct" ? 1 : 0;
|
|
14
18
|
const relDelta = relScore(b) - relScore(a);
|
|
15
19
|
if (relDelta !== 0)
|
|
@@ -26,6 +30,10 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
26
30
|
const sevDelta = severityOrder[b.severity] - severityOrder[a.severity];
|
|
27
31
|
if (sevDelta !== 0)
|
|
28
32
|
return sevDelta;
|
|
33
|
+
const usageScore = (finding) => finding.usage?.imported ? 1 : 0;
|
|
34
|
+
const usageDelta = usageScore(b) - usageScore(a);
|
|
35
|
+
if (usageDelta !== 0)
|
|
36
|
+
return usageDelta;
|
|
29
37
|
const relScore = (finding) => finding.relationship === "direct" ? 1 : 0;
|
|
30
38
|
const relDelta = relScore(b) - relScore(a);
|
|
31
39
|
if (relDelta !== 0)
|
|
@@ -59,6 +67,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
59
67
|
? finding.fixVersionValidationNote
|
|
60
68
|
: null,
|
|
61
69
|
reason: `Direct upgrade target for ${finding.pkg.name}@${finding.pkg.version}`,
|
|
70
|
+
usage: finding.usage ?? null,
|
|
62
71
|
});
|
|
63
72
|
}
|
|
64
73
|
else if (finding.fixVersionValidationNote) {
|
|
@@ -104,6 +113,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput) {
|
|
|
104
113
|
adjusted: false,
|
|
105
114
|
adjustmentNote: null,
|
|
106
115
|
reason: `Parent upgrade for vulnerable ${finding.pkg.name}@${finding.pkg.version}`,
|
|
116
|
+
usage: finding.usage ?? null,
|
|
107
117
|
});
|
|
108
118
|
continue;
|
|
109
119
|
}
|
|
@@ -199,6 +209,7 @@ function upsertTarget(targetsByPackage, next) {
|
|
|
199
209
|
adjusted: existing.adjusted || next.adjusted,
|
|
200
210
|
adjustmentNote: existing.adjustmentNote ?? next.adjustmentNote ?? null,
|
|
201
211
|
kind: existing.kind === "direct" || next.kind === "direct" ? "direct" : "parent-upgrade",
|
|
212
|
+
usage: existing.usage || next.usage,
|
|
202
213
|
};
|
|
203
214
|
if (looksLikeVersion(existing.targetVersion) && looksLikeVersion(next.targetVersion)) {
|
|
204
215
|
if (compareVersions(next.targetVersion, existing.targetVersion) > 0) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
1
|
+
import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
|
|
2
2
|
const packumentCache = new Map();
|
|
3
3
|
const DEFAULT_NPM_REGISTRY_URL = "https://registry.npmjs.org";
|
|
4
4
|
export function clearPackumentCache() {
|
|
@@ -111,6 +111,7 @@ export async function resolveLowestKnownNonVulnerableVersion(packageName, instal
|
|
|
111
111
|
}
|
|
112
112
|
const candidates = Object.keys(packument.versions ?? {})
|
|
113
113
|
.filter(looksLikeVersion)
|
|
114
|
+
.filter(v => !isPreReleaseVersion(v))
|
|
114
115
|
.filter(version => compareVersions(version, installedVersion) > 0)
|
|
115
116
|
.sort(compareVersions);
|
|
116
117
|
if (candidates.length === 0) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
1
|
+
import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
|
|
2
2
|
import { fetchPackument } from "./npm-registry.js";
|
|
3
3
|
export async function resolveRecommendedParentUpgrade(finding, packages) {
|
|
4
4
|
if (finding.relationship !== "transitive")
|
|
@@ -75,6 +75,7 @@ async function findUpgradeForExactDirectChild(args) {
|
|
|
75
75
|
const packument = await fetchPackument(args.directParentName);
|
|
76
76
|
const versions = Object.keys(packument?.versions ?? {})
|
|
77
77
|
.filter(looksLikeVersion)
|
|
78
|
+
.filter(v => !isPreReleaseVersion(v))
|
|
78
79
|
.filter(version => compareVersions(version, args.directParentVersion) > 0)
|
|
79
80
|
.sort(compareVersions);
|
|
80
81
|
for (const version of versions) {
|
|
@@ -108,6 +109,7 @@ async function findUpgradeForImmediateIntermediate(args) {
|
|
|
108
109
|
const packument = await fetchPackument(args.directParentName);
|
|
109
110
|
const versions = Object.keys(packument?.versions ?? {})
|
|
110
111
|
.filter(looksLikeVersion)
|
|
112
|
+
.filter(v => !isPreReleaseVersion(v))
|
|
111
113
|
.filter(version => compareVersions(version, args.directParentVersion) > 0)
|
|
112
114
|
.sort(compareVersions);
|
|
113
115
|
for (const version of versions) {
|
package/dist/scanner.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { chunk, unique } from "./utils/array.js";
|
|
2
|
-
import { compareVersions, looksLikeVersion } from "./utils/version.js";
|
|
2
|
+
import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "./utils/version.js";
|
|
3
3
|
import { loadCache, saveCache } from "./osv/cache.js";
|
|
4
4
|
import { maxSeverity } from "./osv/severity.js";
|
|
5
5
|
import { createSpinner } from "./output/spinner.js";
|
|
@@ -271,7 +271,7 @@ function findFirstFixedVersion(vulns) {
|
|
|
271
271
|
for (const range of affected.ranges ?? []) {
|
|
272
272
|
for (const event of range.events ?? []) {
|
|
273
273
|
const fixed = event.fixed;
|
|
274
|
-
if (fixed && looksLikeVersion(fixed)) {
|
|
274
|
+
if (fixed && looksLikeVersion(fixed) && !isPreReleaseVersion(fixed)) {
|
|
275
275
|
fixedVersions.push(fixed);
|
|
276
276
|
}
|
|
277
277
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const IGNORED_DIRS = new Set(["node_modules", ".git", "dist", "build", "coverage"]);
|
|
4
|
+
const ALLOWED_EXTS = new Set([".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs"]);
|
|
5
|
+
// Matches:
|
|
6
|
+
// import { foo } from 'pkg'
|
|
7
|
+
// import * as foo from "pkg"
|
|
8
|
+
// export { foo } from 'pkg'
|
|
9
|
+
// import 'pkg'
|
|
10
|
+
// require('pkg')
|
|
11
|
+
// await import('pkg')
|
|
12
|
+
const IMPORT_REQUIRE_REGEX = /(?:(?:import|export)\s+[\w\s{},*]+\s+from\s+['"]([^'"]+)['"]|import\s+['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\)|import\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
13
|
+
function getBareModuleName(importPath) {
|
|
14
|
+
if (importPath.startsWith(".") || importPath.startsWith("/")) {
|
|
15
|
+
return "";
|
|
16
|
+
}
|
|
17
|
+
if (importPath.startsWith("@")) {
|
|
18
|
+
const parts = importPath.split("/");
|
|
19
|
+
return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : importPath;
|
|
20
|
+
}
|
|
21
|
+
return importPath.split("/")[0];
|
|
22
|
+
}
|
|
23
|
+
export function scanProjectForPackageUsage(projectPath, packagesToLookFor) {
|
|
24
|
+
const results = {};
|
|
25
|
+
for (const pkg of packagesToLookFor) {
|
|
26
|
+
results[pkg] = [];
|
|
27
|
+
}
|
|
28
|
+
if (packagesToLookFor.size === 0) {
|
|
29
|
+
return results;
|
|
30
|
+
}
|
|
31
|
+
// Cap at 5000 files to prevent performance issues on massive projects
|
|
32
|
+
const MAX_FILES_TO_SCAN = 5000;
|
|
33
|
+
let scannedCount = 0;
|
|
34
|
+
function walk(dir) {
|
|
35
|
+
if (scannedCount >= MAX_FILES_TO_SCAN)
|
|
36
|
+
return;
|
|
37
|
+
let entries;
|
|
38
|
+
try {
|
|
39
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
for (const entry of entries) {
|
|
45
|
+
if (scannedCount >= MAX_FILES_TO_SCAN)
|
|
46
|
+
return;
|
|
47
|
+
const fullPath = path.join(dir, entry.name);
|
|
48
|
+
if (entry.isDirectory()) {
|
|
49
|
+
if (!IGNORED_DIRS.has(entry.name) && !entry.name.startsWith(".")) {
|
|
50
|
+
walk(fullPath);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (entry.isFile()) {
|
|
54
|
+
const ext = path.extname(entry.name);
|
|
55
|
+
if (ALLOWED_EXTS.has(ext)) {
|
|
56
|
+
scanFile(fullPath);
|
|
57
|
+
scannedCount++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function scanFile(filePath) {
|
|
63
|
+
let content;
|
|
64
|
+
try {
|
|
65
|
+
content = fs.readFileSync(filePath, "utf8");
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// Fast path: skip regex if the file doesn't contain any target package names
|
|
71
|
+
let hasPotentialMatch = false;
|
|
72
|
+
for (const pkg of packagesToLookFor) {
|
|
73
|
+
if (content.includes(pkg)) {
|
|
74
|
+
hasPotentialMatch = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (!hasPotentialMatch)
|
|
79
|
+
return;
|
|
80
|
+
const matches = content.matchAll(IMPORT_REQUIRE_REGEX);
|
|
81
|
+
const foundPackages = new Set();
|
|
82
|
+
for (const match of matches) {
|
|
83
|
+
const importPath = match[1] || match[2] || match[3] || match[4];
|
|
84
|
+
if (importPath) {
|
|
85
|
+
const bare = getBareModuleName(importPath);
|
|
86
|
+
if (bare && packagesToLookFor.has(bare)) {
|
|
87
|
+
foundPackages.add(bare);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const pkg of foundPackages) {
|
|
92
|
+
// Store relative path for cleaner output
|
|
93
|
+
const relPath = path.relative(projectPath, filePath);
|
|
94
|
+
results[pkg].push(relPath);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
walk(projectPath);
|
|
98
|
+
return results;
|
|
99
|
+
}
|
package/dist/utils/version.js
CHANGED
|
@@ -32,6 +32,9 @@ export function isMajorVersionBump(from, to) {
|
|
|
32
32
|
const toMajor = Number(to.split(".")[0]);
|
|
33
33
|
return !Number.isNaN(fromMajor) && !Number.isNaN(toMajor) && toMajor > fromMajor;
|
|
34
34
|
}
|
|
35
|
+
export function isPreReleaseVersion(version) {
|
|
36
|
+
return /^\d+\.\d+\.\d+-[^\s]+$/.test(version);
|
|
37
|
+
}
|
|
35
38
|
export function normalizeRawVersion(value) {
|
|
36
39
|
if (typeof value !== "string")
|
|
37
40
|
return null;
|