cve-lite-cli 1.40.0 → 1.41.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 +17 -11
- package/dist/advisory/local-advisory-source.js +9 -5
- package/dist/advisory/local-db.js +91 -13
- package/dist/advisory/osv-sync.js +187 -56
- package/dist/advisory/staging-directory.js +112 -0
- package/dist/cli/advisories-sync.js +16 -2
- package/dist/cli/args.js +46 -35
- package/dist/cli/config.js +4 -1
- package/dist/cli/help.js +4 -3
- package/dist/cli/validate.js +23 -1
- package/dist/constants.js +11 -25
- package/dist/index.js +8 -3
- package/dist/output/cyclonedx.js +23 -14
- package/dist/output/debug.js +68 -18
- package/dist/output/formatters.js +33 -5
- package/dist/output/html-reporter.js +55 -5
- package/dist/output/multi-folder-html-reporter.js +59 -3
- package/dist/output/output-dir.js +193 -7
- package/dist/output/printers.js +16 -5
- package/dist/output/sarif.js +131 -48
- package/dist/output/sbom-dependency-edges.js +16 -10
- package/dist/output/sbom-file.js +14 -6
- package/dist/output/scan-json.js +21 -3
- package/dist/output/spdx.js +61 -23
- package/dist/output/spinner.js +2 -1
- package/dist/output/write-outputs.js +142 -52
- package/dist/overrides/context-builder.js +23 -70
- package/dist/parsers/bun-lock.js +1 -2
- package/dist/parsers/index.js +22 -1
- package/dist/parsers/npm-lock-graph.js +39 -1
- package/dist/parsers/package-lock.js +25 -5
- package/dist/parsers/pnpm-lock.js +3 -2
- package/dist/parsers/utils.js +5 -3
- package/dist/parsers/yarn-lock-graph.js +5 -2
- package/dist/parsers/yarn-lock.js +1 -2
- package/dist/remediation/fix-commands.js +105 -44
- package/dist/remediation/npm-transitive-graph.js +30 -72
- package/dist/remediation/npm-transitive-resolution.js +24 -77
- package/dist/remediation/parent-upgrade-blockers.js +51 -0
- package/dist/remediation/parent-upgrade.js +5 -79
- package/dist/remediation/registry-parent-ranges.js +21 -0
- package/dist/remediation/transitive-chain-resolver.js +3 -3
- package/dist/scan/completeness.js +10 -4
- package/dist/scan/multi-folder-scan.js +133 -58
- package/dist/scan/single-scan.js +41 -25
- package/dist/scanner.js +257 -58
- package/dist/usage/scanner.js +19 -8
- package/dist/utils/array.js +11 -3
- package/dist/utils/debug-bundle.js +118 -0
- package/dist/utils/debug-project.js +102 -0
- package/dist/utils/debug-redaction.js +117 -0
- package/dist/utils/file.js +2 -1
- package/dist/utils/fix-runner.js +95 -13
- package/dist/utils/import-type-queries.js +322 -0
- package/dist/utils/network.js +13 -9
- package/dist/utils/npm-lockfile.js +12 -0
- package/dist/utils/osv-fetch.js +70 -0
- package/dist/utils/package-identity.js +43 -0
- package/dist/utils/package-manager-source.js +24 -0
- package/dist/utils/purl.js +37 -0
- package/dist/utils/registry-config.js +1 -16
- package/dist/utils/string.js +8 -0
- package/dist/utils/validate-url.js +1 -1
- package/dist/utils/version.js +30 -7
- package/dist/utils/versions/osv-range.js +191 -0
- package/dist/utils/versions/pep440.js +246 -0
- package/dist/utils/versions/scheme.js +10 -0
- package/package.json +3 -2
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import { findFixTargetForFinding, findSuggestedCommandForFinding } from "../remediation/fix-commands.js";
|
|
1
|
+
import { findFixTargetForFinding, findSuggestedCommandForFinding, formatParentUpgradeBlockedReason } from "../remediation/fix-commands.js";
|
|
2
2
|
import { chalk } from "../utils/chalk.js";
|
|
3
3
|
import { severityOrder, MAL_PRIVATE_REGISTRY_MESSAGE, MAL_GIT_SOURCE_PINNED_MESSAGE, MAL_GIT_SOURCE_FLOATING_MESSAGE, } from "../constants.js";
|
|
4
4
|
import { loadCache } from "../osv/cache.js";
|
|
5
5
|
import { inferSeverity } from "../osv/severity.js";
|
|
6
6
|
import { getPrimaryParent, getRootDependencies, isUnconfirmedFix } from "../utils/finding.js";
|
|
7
7
|
import { calculatePathCoverage, formatDependencyPath } from "../utils/path-coverage.js";
|
|
8
|
-
import { pluralize } from "../utils/string.js";
|
|
8
|
+
import { pluralize, isOutputStdout } from "../utils/string.js";
|
|
9
9
|
import { hasMaliciousAdvisory } from "../utils/vuln.js";
|
|
10
10
|
import { computePrioritySignal } from "../utils/priority-signal.js";
|
|
11
11
|
import { computeContextualSignals } from "../utils/contextual-signals.js";
|
|
12
|
+
export function formatPackageName(pkg) {
|
|
13
|
+
return pkg.registryName && pkg.registryName !== pkg.name ? `${pkg.name} (${pkg.registryName})` : pkg.name;
|
|
14
|
+
}
|
|
12
15
|
export function formatRelationshipLabel(value) {
|
|
13
16
|
if (value.startsWith("direct"))
|
|
14
17
|
return chalk.green(value);
|
|
@@ -55,6 +58,9 @@ function transitiveRiskMessage(packageName, complete) {
|
|
|
55
58
|
? `Transitive issue. A specific parent upgrade target was found for ${packageName}.`
|
|
56
59
|
: `Transitive issue. A path-specific parent upgrade target was found for ${packageName}; run it, rescan, and review remaining paths separately.`;
|
|
57
60
|
}
|
|
61
|
+
function unreadablePyPiBoundaryAction(packageName) {
|
|
62
|
+
return `The advisory data for ${packageName} has an unreadable PyPI boundary, so the fix is inconclusive.`;
|
|
63
|
+
}
|
|
58
64
|
export function getRecommendedAction(finding) {
|
|
59
65
|
if (isMaliciousAdvisory(finding)) {
|
|
60
66
|
if (finding.maliciousUnverifiable) {
|
|
@@ -64,6 +70,9 @@ export function getRecommendedAction(finding) {
|
|
|
64
70
|
? "This package has a malicious code advisory. Remove it from your dependencies."
|
|
65
71
|
: "This package has a malicious code advisory. Upgrade or remove the parent package that pulls it in.";
|
|
66
72
|
}
|
|
73
|
+
if (finding.advisoryBoundaryUnreadable) {
|
|
74
|
+
return unreadablePyPiBoundaryAction(finding.pkg.name);
|
|
75
|
+
}
|
|
67
76
|
// Prefer the registry-validated target over the raw advisory hint when
|
|
68
77
|
// available - they can disagree (the hint may not be published on npm or
|
|
69
78
|
// may itself be vulnerable), and the validated target is what the fix
|
|
@@ -78,6 +87,9 @@ export function getRecommendedAction(finding) {
|
|
|
78
87
|
if (finding.relationship === "direct") {
|
|
79
88
|
return `No known fix exists for ${finding.pkg.name}. Consider replacing it with an actively maintained alternative.`;
|
|
80
89
|
}
|
|
90
|
+
if (finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length) {
|
|
91
|
+
return formatParentUpgradeBlockedReason(finding);
|
|
92
|
+
}
|
|
81
93
|
if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
82
94
|
return `${finding.recommendedNpmTransitiveRemediation.package} already permits ${finding.pkg.name}@${finding.recommendedNpmTransitiveRemediation.targetChildVersion} - run \`npm install\` to pick it up.`;
|
|
83
95
|
}
|
|
@@ -115,6 +127,9 @@ export function summarizeRisk(finding) {
|
|
|
115
127
|
? MAL_GIT_SOURCE_PINNED_MESSAGE
|
|
116
128
|
: MAL_GIT_SOURCE_FLOATING_MESSAGE;
|
|
117
129
|
}
|
|
130
|
+
if (finding.advisoryBoundaryUnreadable) {
|
|
131
|
+
return unreadablePyPiBoundaryAction(finding.pkg.name);
|
|
132
|
+
}
|
|
118
133
|
let risk = "";
|
|
119
134
|
if (finding.severity === "critical" && finding.relationship === "direct") {
|
|
120
135
|
risk = "Critical direct dependency. Prioritize this first because the project controls it directly.";
|
|
@@ -125,6 +140,9 @@ export function summarizeRisk(finding) {
|
|
|
125
140
|
else if (finding.relationship === "transitive" && finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
126
141
|
risk = `Transitive issue. The current parent range can already absorb a safe ${finding.pkg.name} update via ${finding.recommendedNpmTransitiveRemediation.package}.`;
|
|
127
142
|
}
|
|
143
|
+
else if (finding.relationship === "transitive" && finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length) {
|
|
144
|
+
risk = `Transitive issue. ${formatParentUpgradeBlockedReason(finding)}`;
|
|
145
|
+
}
|
|
128
146
|
else if (finding.relationship === "transitive" && finding.recommendedNpmTransitiveRemediation?.kind === "upgrade-parent-to-version") {
|
|
129
147
|
const coverage = calculatePathCoverage(finding.dependencyPaths, finding.recommendedNpmTransitiveRemediation.viaPath);
|
|
130
148
|
risk = transitiveRiskMessage(finding.recommendedNpmTransitiveRemediation.package, coverage.coverage === "complete");
|
|
@@ -180,6 +198,9 @@ export function summarizeNextAction(finding) {
|
|
|
180
198
|
? "This package has a malicious code advisory. Remove it from your dependencies."
|
|
181
199
|
: "This package has a malicious code advisory. Upgrade or remove the parent package that pulls it in.";
|
|
182
200
|
}
|
|
201
|
+
if (finding.advisoryBoundaryUnreadable) {
|
|
202
|
+
return unreadablePyPiBoundaryAction(finding.pkg.name);
|
|
203
|
+
}
|
|
183
204
|
if (isUnconfirmedFix(finding)) {
|
|
184
205
|
return `Verify a safe version of ${finding.pkg.name} before upgrading.`;
|
|
185
206
|
}
|
|
@@ -192,6 +213,9 @@ export function summarizeNextAction(finding) {
|
|
|
192
213
|
if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
193
214
|
return `Lockfile refresh - ${finding.recommendedNpmTransitiveRemediation.package} already permits a safe version.`;
|
|
194
215
|
}
|
|
216
|
+
if (finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length) {
|
|
217
|
+
return formatParentUpgradeBlockedReason(finding);
|
|
218
|
+
}
|
|
195
219
|
if (finding.recommendedNpmTransitiveRemediation?.kind === "upgrade-parent-to-version" &&
|
|
196
220
|
finding.recommendedNpmTransitiveRemediation.targetVersion) {
|
|
197
221
|
return `Upgrade ${finding.recommendedNpmTransitiveRemediation.package} ${finding.recommendedNpmTransitiveRemediation.currentVersion} -> ${finding.recommendedNpmTransitiveRemediation.targetVersion}.`;
|
|
@@ -231,6 +255,7 @@ export function formatFixCommandWithPublishDates(command, targets) {
|
|
|
231
255
|
export function serializeFinding(finding, plan) {
|
|
232
256
|
return {
|
|
233
257
|
package: finding.pkg.name,
|
|
258
|
+
...(finding.pkg.registryName ? { registryPackage: finding.pkg.registryName } : {}),
|
|
234
259
|
version: finding.pkg.version,
|
|
235
260
|
severity: finding.severity,
|
|
236
261
|
relationship: finding.relationship,
|
|
@@ -247,7 +272,9 @@ export function serializeFinding(finding, plan) {
|
|
|
247
272
|
primaryParent: getPrimaryParent(finding),
|
|
248
273
|
rootDependencies: getRootDependencies(finding),
|
|
249
274
|
recommendedParentUpgrade: finding.recommendedParentUpgrade,
|
|
275
|
+
parentUpgradeBlockers: finding.parentUpgradeBlockers ?? null,
|
|
250
276
|
recommendedNpmTransitiveRemediation: finding.recommendedNpmTransitiveRemediation ?? null,
|
|
277
|
+
chainResolution: finding.chainResolution ?? null,
|
|
251
278
|
cves: finding.cveAliases,
|
|
252
279
|
epssScores: finding.epssScores ?? null,
|
|
253
280
|
prioritySignal: computePrioritySignal(finding.severity, finding.epssScores) ?? null,
|
|
@@ -258,6 +285,7 @@ export function serializeFinding(finding, plan) {
|
|
|
258
285
|
maliciousGitSource: finding.maliciousGitSource ?? false,
|
|
259
286
|
maliciousGitSourcePinned: finding.maliciousGitSourcePinned ?? false,
|
|
260
287
|
unresolvedAdvisoryIds: finding.unresolvedAdvisoryIds ?? [],
|
|
288
|
+
...(finding.advisoryBoundaryUnreadable ? { advisoryBoundaryUnreadable: true } : {}),
|
|
261
289
|
vulnerabilities: finding.vulnerabilities.map(v => ({
|
|
262
290
|
id: v.id,
|
|
263
291
|
aliases: v.aliases ?? [],
|
|
@@ -268,7 +296,7 @@ export function serializeFinding(finding, plan) {
|
|
|
268
296
|
};
|
|
269
297
|
}
|
|
270
298
|
export function printCacheSummary(cacheDirOverride, options) {
|
|
271
|
-
if (options?.json)
|
|
299
|
+
if (options?.json || isOutputStdout(options?.output))
|
|
272
300
|
return;
|
|
273
301
|
const cache = loadCache(cacheDirOverride);
|
|
274
302
|
const advisoryCount = Object.values(cache.entries).filter(entry => entry.vuln).length;
|
|
@@ -283,12 +311,12 @@ export function printCacheSummary(cacheDirOverride, options) {
|
|
|
283
311
|
: ""));
|
|
284
312
|
}
|
|
285
313
|
export function logInfo(message, options) {
|
|
286
|
-
if (options?.json)
|
|
314
|
+
if (options?.json || isOutputStdout(options?.output))
|
|
287
315
|
return;
|
|
288
316
|
console.log(chalk.gray(message));
|
|
289
317
|
}
|
|
290
318
|
export function logWarn(message, options) {
|
|
291
|
-
if (options?.json)
|
|
319
|
+
if (options?.json || isOutputStdout(options?.output))
|
|
292
320
|
return;
|
|
293
321
|
console.log(chalk.yellow(message));
|
|
294
322
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
3
4
|
import { spawn } from "node:child_process";
|
|
4
5
|
import { serializeFinding, summarizeNextAction, summarizeRisk, formatCooldownWarning } from "./formatters.js";
|
|
5
6
|
import { loadLogoDataUri } from "./logos.js";
|
|
@@ -635,7 +636,9 @@ export function renderFindingRow(finding, idx, skippedKeys) {
|
|
|
635
636
|
? `<span class="badge badge-git-source" title="MAL- advisory found but package resolves from a git source with a floating reference: verify the repository, org, and ref are safe">${escapeHtml(MAL_GIT_SOURCE_FLOATING_DISPLAY)}</span>`
|
|
636
637
|
: isMalicious
|
|
637
638
|
? `<span class="fix-hint none" title="Malicious code advisory: remove this package">⚠ Malicious</span>`
|
|
638
|
-
:
|
|
639
|
+
: finding.advisoryBoundaryUnreadable
|
|
640
|
+
? `<span class="fix-hint none" title="The advisory data has an unreadable PyPI boundary, so the fix is inconclusive.">unreadable advisory data</span>`
|
|
641
|
+
: `<span class="fix-hint none" title="No known fix: consider replacing this package">⚠ No fix</span>`;
|
|
639
642
|
const depPaths = renderDependencyPathsHtml(finding);
|
|
640
643
|
const description = finding.vulnerabilities[0]?.summary ?? "";
|
|
641
644
|
const runnable = finding.runnableFixCommand ?? null;
|
|
@@ -711,9 +714,10 @@ export function renderFindingRow(finding, idx, skippedKeys) {
|
|
|
711
714
|
</tr>`;
|
|
712
715
|
}
|
|
713
716
|
function renderTransitiveContextCol(finding) {
|
|
717
|
+
// A blocked parent upgrade names a step, not a fix (#1457).
|
|
714
718
|
const hasFixAvailable = finding.runnableFixCommand != null ||
|
|
715
|
-
finding.
|
|
716
|
-
|
|
719
|
+
(!finding.parentUpgradeBlockers?.length &&
|
|
720
|
+
(finding.recommendedNpmTransitiveRemediation != null || finding.recommendedParentUpgrade != null));
|
|
717
721
|
const rootNames = finding.rootDependencies.join(", ");
|
|
718
722
|
if (hasFixAvailable) {
|
|
719
723
|
const rootLine = rootNames
|
|
@@ -948,11 +952,57 @@ export async function writeHtmlReport(params) {
|
|
|
948
952
|
}
|
|
949
953
|
const indexPath = path.join(params.outputDir, "index.html");
|
|
950
954
|
const jsonPath = path.join(params.outputDir, "report.json");
|
|
955
|
+
const tempJsonPath = path.join(params.outputDir, `.report.json.${randomUUID()}.tmp`);
|
|
956
|
+
const tempIndexPath = path.join(params.outputDir, `.index.html.${randomUUID()}.tmp`);
|
|
957
|
+
const backupJsonPath = path.join(params.outputDir, `.report.json.bak.${randomUUID()}.tmp`);
|
|
958
|
+
let jsonBackedUp = false;
|
|
959
|
+
let jsonReplaced = false;
|
|
951
960
|
try {
|
|
952
|
-
fs.writeFileSync(
|
|
953
|
-
fs.writeFileSync(
|
|
961
|
+
fs.writeFileSync(tempJsonPath, JSON.stringify(params.jsonPayload, null, 2), "utf8");
|
|
962
|
+
fs.writeFileSync(tempIndexPath, renderHtmlReport(params.data, params.jsonPayload), "utf8");
|
|
963
|
+
if (fs.existsSync(jsonPath)) {
|
|
964
|
+
fs.renameSync(jsonPath, backupJsonPath);
|
|
965
|
+
jsonBackedUp = true;
|
|
966
|
+
}
|
|
967
|
+
fs.renameSync(tempJsonPath, jsonPath);
|
|
968
|
+
jsonReplaced = true;
|
|
969
|
+
fs.renameSync(tempIndexPath, indexPath);
|
|
970
|
+
if (jsonBackedUp) {
|
|
971
|
+
try {
|
|
972
|
+
fs.rmSync(backupJsonPath, { force: true, recursive: true });
|
|
973
|
+
}
|
|
974
|
+
catch { /* best-effort cleanup */ }
|
|
975
|
+
}
|
|
954
976
|
}
|
|
955
977
|
catch (err) {
|
|
978
|
+
let backupJsonRestored = false;
|
|
979
|
+
if (jsonBackedUp) {
|
|
980
|
+
try {
|
|
981
|
+
fs.renameSync(backupJsonPath, jsonPath);
|
|
982
|
+
backupJsonRestored = true;
|
|
983
|
+
}
|
|
984
|
+
catch { /* best-effort restore */ }
|
|
985
|
+
}
|
|
986
|
+
else if (jsonReplaced) {
|
|
987
|
+
try {
|
|
988
|
+
fs.rmSync(jsonPath, { force: true, recursive: true });
|
|
989
|
+
}
|
|
990
|
+
catch { /* best-effort cleanup */ }
|
|
991
|
+
}
|
|
992
|
+
try {
|
|
993
|
+
fs.rmSync(tempJsonPath, { force: true, recursive: true });
|
|
994
|
+
}
|
|
995
|
+
catch { /* best-effort cleanup */ }
|
|
996
|
+
try {
|
|
997
|
+
fs.rmSync(tempIndexPath, { force: true, recursive: true });
|
|
998
|
+
}
|
|
999
|
+
catch { /* best-effort cleanup */ }
|
|
1000
|
+
if (!jsonBackedUp || backupJsonRestored) {
|
|
1001
|
+
try {
|
|
1002
|
+
fs.rmSync(backupJsonPath, { force: true, recursive: true });
|
|
1003
|
+
}
|
|
1004
|
+
catch { /* best-effort cleanup */ }
|
|
1005
|
+
}
|
|
956
1006
|
if (!dirExisted) {
|
|
957
1007
|
try {
|
|
958
1008
|
fs.rmSync(params.outputDir, { recursive: true, force: true });
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
3
4
|
import { aggregateMultiFolderCompleteness } from "../scan/completeness.js";
|
|
4
5
|
import { REPORT_STYLES, escapeHtml, openInBrowser, renderFindingRow, renderFixPlan, renderCompletenessBanner, serializeHtmlFinding, renderPriorityLegendHtml, } from "./html-reporter.js";
|
|
5
6
|
import { renderOverrideFindingsHtml } from "./override-findings-html.js";
|
|
@@ -142,7 +143,13 @@ function renderFolderSection(result, folderIdx, idxOffset, serialized, includeOv
|
|
|
142
143
|
</details>`;
|
|
143
144
|
}
|
|
144
145
|
export async function writeMultiFolderHtmlReport(params) {
|
|
145
|
-
fs.
|
|
146
|
+
const dirExisted = fs.existsSync(params.outputDir);
|
|
147
|
+
try {
|
|
148
|
+
fs.mkdirSync(params.outputDir, { recursive: true });
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
throw new Error(`Failed to create report directory ${params.outputDir}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
152
|
+
}
|
|
146
153
|
const projectName = path.basename(params.projectPath);
|
|
147
154
|
const scanDate = new Date().toLocaleString();
|
|
148
155
|
const counts = summaryCounts(params.results);
|
|
@@ -407,8 +414,57 @@ document.addEventListener('click', function(e) {
|
|
|
407
414
|
};
|
|
408
415
|
const indexPath = path.join(params.outputDir, "index.html");
|
|
409
416
|
const jsonPath = path.join(params.outputDir, "report.json");
|
|
410
|
-
|
|
411
|
-
|
|
417
|
+
const tempJsonPath = path.join(params.outputDir, `.report.json.${randomUUID()}.tmp`);
|
|
418
|
+
const tempIndexPath = path.join(params.outputDir, `.index.html.${randomUUID()}.tmp`);
|
|
419
|
+
const backupJsonPath = path.join(params.outputDir, `.report.json.bak.${randomUUID()}.tmp`);
|
|
420
|
+
let jsonBackedUp = false;
|
|
421
|
+
let jsonReplaced = false;
|
|
422
|
+
try {
|
|
423
|
+
fs.writeFileSync(tempJsonPath, JSON.stringify(jsonPayload, null, 2), "utf8");
|
|
424
|
+
fs.writeFileSync(tempIndexPath, html, "utf8");
|
|
425
|
+
if (fs.existsSync(jsonPath)) {
|
|
426
|
+
fs.renameSync(jsonPath, backupJsonPath);
|
|
427
|
+
jsonBackedUp = true;
|
|
428
|
+
}
|
|
429
|
+
fs.renameSync(tempJsonPath, jsonPath);
|
|
430
|
+
jsonReplaced = true;
|
|
431
|
+
fs.renameSync(tempIndexPath, indexPath);
|
|
432
|
+
if (jsonBackedUp) {
|
|
433
|
+
try {
|
|
434
|
+
fs.rmSync(backupJsonPath, { force: true, recursive: true });
|
|
435
|
+
}
|
|
436
|
+
catch { /* best-effort cleanup */ }
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
catch (err) {
|
|
440
|
+
if (jsonBackedUp) {
|
|
441
|
+
try {
|
|
442
|
+
fs.renameSync(backupJsonPath, jsonPath);
|
|
443
|
+
}
|
|
444
|
+
catch { /* best-effort restore */ }
|
|
445
|
+
}
|
|
446
|
+
else if (jsonReplaced) {
|
|
447
|
+
try {
|
|
448
|
+
fs.rmSync(jsonPath, { force: true, recursive: true });
|
|
449
|
+
}
|
|
450
|
+
catch { /* best-effort cleanup */ }
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
fs.rmSync(tempJsonPath, { force: true, recursive: true });
|
|
454
|
+
}
|
|
455
|
+
catch { /* best-effort cleanup */ }
|
|
456
|
+
try {
|
|
457
|
+
fs.rmSync(tempIndexPath, { force: true, recursive: true });
|
|
458
|
+
}
|
|
459
|
+
catch { /* best-effort cleanup */ }
|
|
460
|
+
if (!dirExisted) {
|
|
461
|
+
try {
|
|
462
|
+
fs.rmSync(params.outputDir, { recursive: true, force: true });
|
|
463
|
+
}
|
|
464
|
+
catch { /* best-effort cleanup */ }
|
|
465
|
+
}
|
|
466
|
+
throw new Error(`Failed to write HTML report to ${params.outputDir}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
467
|
+
}
|
|
412
468
|
if (params.autoOpen)
|
|
413
469
|
openInBrowser(indexPath);
|
|
414
470
|
return { reportPath: indexPath };
|
|
@@ -1,17 +1,203 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { isOutputStdout } from "../utils/string.js";
|
|
4
|
+
/**
|
|
5
|
+
* Recognised file extensions for file-based output.
|
|
6
|
+
* Note: compound extensions like .spdx.json and .cdx.json are matched by .json.
|
|
7
|
+
*/
|
|
8
|
+
const RECOGNISED_FILE_EXTENSIONS = [
|
|
9
|
+
".json",
|
|
10
|
+
".sarif",
|
|
11
|
+
".spdx",
|
|
12
|
+
".cdx",
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Returns a warning message when `filename` has a format-specific extension
|
|
16
|
+
* (.sarif, .spdx.json, .cdx.json, .spdx, .cdx) that does not match the requested
|
|
17
|
+
* report format.
|
|
18
|
+
*
|
|
19
|
+
* Plain `.json` files stay quiet as they are compatible with all JSON-based formats.
|
|
20
|
+
*/
|
|
21
|
+
export function checkFormatExtensionMismatch(filename, requestedFormat) {
|
|
22
|
+
if (!filename || isOutputStdout(filename))
|
|
23
|
+
return undefined;
|
|
24
|
+
const lower = filename.toLowerCase();
|
|
25
|
+
let detectedExt;
|
|
26
|
+
let expectedFormat;
|
|
27
|
+
if (lower.endsWith(".spdx.json")) {
|
|
28
|
+
detectedExt = ".spdx.json";
|
|
29
|
+
expectedFormat = "spdx";
|
|
30
|
+
}
|
|
31
|
+
else if (lower.endsWith(".cdx.json")) {
|
|
32
|
+
detectedExt = ".cdx.json";
|
|
33
|
+
expectedFormat = "cyclonedx";
|
|
34
|
+
}
|
|
35
|
+
else if (lower.endsWith(".sarif")) {
|
|
36
|
+
detectedExt = ".sarif";
|
|
37
|
+
expectedFormat = "sarif";
|
|
38
|
+
}
|
|
39
|
+
else if (lower.endsWith(".spdx")) {
|
|
40
|
+
detectedExt = ".spdx";
|
|
41
|
+
expectedFormat = "spdx";
|
|
42
|
+
}
|
|
43
|
+
else if (lower.endsWith(".cdx")) {
|
|
44
|
+
detectedExt = ".cdx";
|
|
45
|
+
expectedFormat = "cyclonedx";
|
|
46
|
+
}
|
|
47
|
+
if (expectedFormat && expectedFormat !== requestedFormat) {
|
|
48
|
+
const formatDisplayNames = {
|
|
49
|
+
json: "JSON",
|
|
50
|
+
sarif: "SARIF",
|
|
51
|
+
spdx: "SPDX",
|
|
52
|
+
cyclonedx: "CycloneDX",
|
|
53
|
+
};
|
|
54
|
+
return `Output filename '${filename}' has extension '${detectedExt}', which does not match requested format ${formatDisplayNames[requestedFormat]}.`;
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
export { isOutputStdout };
|
|
59
|
+
/**
|
|
60
|
+
* Ensures any pending stdout output has drained to the operating system
|
|
61
|
+
* before the CLI exits.
|
|
62
|
+
*/
|
|
63
|
+
export function drainStdout() {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
let resolved = false;
|
|
66
|
+
const onError = (err) => {
|
|
67
|
+
cleanup();
|
|
68
|
+
if (!resolved) {
|
|
69
|
+
resolved = true;
|
|
70
|
+
reject(err ?? new Error("stdout write failed"));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const cleanup = () => {
|
|
74
|
+
process.stdout.removeListener("error", onError);
|
|
75
|
+
};
|
|
76
|
+
process.stdout.once("error", onError);
|
|
77
|
+
try {
|
|
78
|
+
let cbCalled = false;
|
|
79
|
+
const ret = process.stdout.write("", (err) => {
|
|
80
|
+
cbCalled = true;
|
|
81
|
+
cleanup();
|
|
82
|
+
if (!resolved) {
|
|
83
|
+
resolved = true;
|
|
84
|
+
if (err) {
|
|
85
|
+
reject(err);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
resolve();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
// Handle test environments where process.stdout.write is mocked and does not invoke callbacks
|
|
93
|
+
if (process.stdout.write._isMockFunction &&
|
|
94
|
+
!cbCalled &&
|
|
95
|
+
ret === true &&
|
|
96
|
+
!process.stdout.writableNeedDrain) {
|
|
97
|
+
cleanup();
|
|
98
|
+
resolved = true;
|
|
99
|
+
resolve();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
cleanup();
|
|
104
|
+
if (!resolved) {
|
|
105
|
+
resolved = true;
|
|
106
|
+
reject(err);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Returns true when `output` refers to a file path rather than a directory.
|
|
113
|
+
*
|
|
114
|
+
* An argument is treated as a file when:
|
|
115
|
+
* 1. It does not end in a path separator, is not "." or "..", and ends in a
|
|
116
|
+
* recognised file extension (.json, .sarif, .spdx, .cdx).
|
|
117
|
+
* 2. If it already exists on the filesystem, it must be a regular file (not a directory).
|
|
118
|
+
*
|
|
119
|
+
* An existing file with an unrecognised extension (such as NOTES or notes.txt) is
|
|
120
|
+
* NOT treated as an output file, preserving the existing error behavior when trying
|
|
121
|
+
* to use it as a directory.
|
|
122
|
+
*/
|
|
123
|
+
export function isOutputFile(output) {
|
|
5
124
|
if (!output)
|
|
6
|
-
return
|
|
7
|
-
|
|
125
|
+
return false;
|
|
126
|
+
if (output.endsWith("/") ||
|
|
127
|
+
output.endsWith("\\") ||
|
|
128
|
+
output === "." ||
|
|
129
|
+
output === "..") {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const lower = output.toLowerCase();
|
|
133
|
+
const hasRecognisedExt = RECOGNISED_FILE_EXTENSIONS.some((ext) => lower.endsWith(ext));
|
|
134
|
+
if (!hasRecognisedExt) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const resolved = path.resolve(process.cwd(), output);
|
|
138
|
+
try {
|
|
139
|
+
if (fs.existsSync(resolved)) {
|
|
140
|
+
const stat = fs.statSync(resolved);
|
|
141
|
+
if (stat.isDirectory()) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// If stat fails, fall back to treating it as a file based on recognised extension
|
|
148
|
+
}
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Resolves the target directory and optional filename for --output.
|
|
153
|
+
*
|
|
154
|
+
* Creates missing parent directories when writing to a file, or creates the
|
|
155
|
+
* output directory itself when writing into a directory.
|
|
156
|
+
*/
|
|
157
|
+
export function resolveOutputTarget(output) {
|
|
158
|
+
if (!output) {
|
|
159
|
+
return { isFile: false, dir: process.cwd() };
|
|
160
|
+
}
|
|
161
|
+
if (isOutputStdout(output)) {
|
|
162
|
+
return {
|
|
163
|
+
isFile: false,
|
|
164
|
+
isStdout: true,
|
|
165
|
+
dir: "-",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const resolved = path.resolve(process.cwd(), output);
|
|
169
|
+
if (isOutputFile(output)) {
|
|
170
|
+
const dir = path.dirname(resolved);
|
|
171
|
+
const filename = path.basename(resolved);
|
|
172
|
+
try {
|
|
173
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
throw new Error(`Failed to create output directory ${dir}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
isFile: true,
|
|
180
|
+
dir,
|
|
181
|
+
filename,
|
|
182
|
+
outputPath: resolved,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
8
185
|
try {
|
|
9
|
-
fs.mkdirSync(
|
|
186
|
+
fs.mkdirSync(resolved, { recursive: true });
|
|
10
187
|
}
|
|
11
188
|
catch (err) {
|
|
12
|
-
throw new Error(`Failed to create output directory ${
|
|
189
|
+
throw new Error(`Failed to create output directory ${resolved}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
13
190
|
}
|
|
14
|
-
return
|
|
191
|
+
return {
|
|
192
|
+
isFile: false,
|
|
193
|
+
dir: resolved,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** Directory for --json, --sarif and --sbom files: the working directory, or --output resolved against it and created when missing. */
|
|
197
|
+
export function resolveOutputDir(output) {
|
|
198
|
+
if (!output || isOutputStdout(output))
|
|
199
|
+
return process.cwd();
|
|
200
|
+
return resolveOutputTarget(output).dir;
|
|
15
201
|
}
|
|
16
202
|
/** Path printed for a written file: relative to the working directory, or absolute when it lies outside it. */
|
|
17
203
|
export function displayOutputPath(outputPath) {
|
package/dist/output/printers.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { chalk, stripAnsi } from "../utils/chalk.js";
|
|
2
|
-
import { buildSuggestedFixCommandPlan, planVersionForFinding, UNVERIFIED_PARENT_UPGRADE_NOTE } from "../remediation/fix-commands.js";
|
|
2
|
+
import { buildSuggestedFixCommandPlan, formatParentUpgradeBlockedReason, planVersionForFinding, UNVERIFIED_PARENT_UPGRADE_NOTE } from "../remediation/fix-commands.js";
|
|
3
3
|
import { formatParentUpdateChildNote } from "../remediation/parent-update-child-note.js";
|
|
4
4
|
import { isBreakingUpgrade } from "../utils/version.js";
|
|
5
5
|
import { getPrimaryParent } from "../utils/finding.js";
|
|
6
|
-
import { countUniqueAdvisories, countProdFindings, formatRelationshipLabel, formatRelLabel, formatRootDependencySummary, formatFixCommandWithPublishDates, formatCooldownWarning, } from "./formatters.js";
|
|
6
|
+
import { countUniqueAdvisories, countProdFindings, formatPackageName, formatRelationshipLabel, formatRelLabel, formatRootDependencySummary, formatFixCommandWithPublishDates, formatCooldownWarning, } from "./formatters.js";
|
|
7
7
|
import { formatSeverityLabel } from "../utils/severity.js";
|
|
8
8
|
import { severityOrder } from "../constants.js";
|
|
9
9
|
import { pluralize } from "../utils/string.js";
|
|
@@ -205,6 +205,7 @@ const TABLE_COLUMNS = [
|
|
|
205
205
|
name: "package",
|
|
206
206
|
header: "Package",
|
|
207
207
|
cap: PACKAGE_NAME_COLUMN_CAP,
|
|
208
|
+
wrap: true,
|
|
208
209
|
cell: row => ({ value: row.package, decorated: row.packageDecorated }),
|
|
209
210
|
},
|
|
210
211
|
{ name: "version", header: "Version", cell: row => ({ value: dash(row.version) }) },
|
|
@@ -348,7 +349,9 @@ function printColumnTable(columns, rows, widths, options) {
|
|
|
348
349
|
console.log(outputRow);
|
|
349
350
|
}
|
|
350
351
|
else {
|
|
351
|
-
|
|
352
|
+
const wrappedCells = cells.map((cell, i) => columns[i].wrap
|
|
353
|
+
? wrapCell(cell, widths[i]).join("\n") : cell);
|
|
354
|
+
console.log(renderRow(wrappedCells, widths));
|
|
352
355
|
}
|
|
353
356
|
}
|
|
354
357
|
if (options?.totalsRow) {
|
|
@@ -391,7 +394,7 @@ function findingToTableRow(finding, plan, skippedKeys) {
|
|
|
391
394
|
const ownFixVersion = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
|
|
392
395
|
const fixVersion = planTarget ?? ownFixVersion ?? null;
|
|
393
396
|
return {
|
|
394
|
-
package: finding.pkg
|
|
397
|
+
package: formatPackageName(finding.pkg),
|
|
395
398
|
version: finding.pkg.version,
|
|
396
399
|
severity: finding.severity,
|
|
397
400
|
relationship: formatRelLabel(finding),
|
|
@@ -424,6 +427,8 @@ function formatTargetDisplay(finding, fixVersion, isSkipped) {
|
|
|
424
427
|
}
|
|
425
428
|
if (hasMaliciousAdvisory(finding.vulnerabilities))
|
|
426
429
|
return chalk.yellow("⚠ Malicious");
|
|
430
|
+
if (finding.advisoryBoundaryUnreadable)
|
|
431
|
+
return chalk.yellow("⚠ unreadable advisory data");
|
|
427
432
|
return chalk.yellow("⚠ no fix");
|
|
428
433
|
}
|
|
429
434
|
function fixTargetContext(target) {
|
|
@@ -702,7 +707,7 @@ export function printCompactOutput(findings, scanInput, options) {
|
|
|
702
707
|
usageContext = chalk.green(` (no direct import found)`);
|
|
703
708
|
}
|
|
704
709
|
}
|
|
705
|
-
console.log(`${formatSeverityLabel(sevLabel)} ${chalk.whiteBright(finding.pkg
|
|
710
|
+
console.log(`${formatSeverityLabel(sevLabel)} ${chalk.whiteBright(formatPackageName(finding.pkg))}@${finding.pkg.version}`);
|
|
706
711
|
console.log(` ${typeLabel}${usageContext}`);
|
|
707
712
|
console.log(` ${chalk.gray(formatContextualSignalsLine(computeContextualSignals(finding)))}`);
|
|
708
713
|
if (finding.epssScores && finding.epssScores.length > 0) {
|
|
@@ -732,9 +737,15 @@ export function printCompactOutput(findings, scanInput, options) {
|
|
|
732
737
|
console.log(` ${chalk.red(`⚠ Malicious: ${action}`)}`);
|
|
733
738
|
}
|
|
734
739
|
}
|
|
740
|
+
else if (finding.advisoryBoundaryUnreadable) {
|
|
741
|
+
console.log(` ${chalk.gray("Fix: unreadable advisory data; the fix is inconclusive")}`);
|
|
742
|
+
}
|
|
735
743
|
else if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
736
744
|
console.log(` ${chalk.gray(`Fix: run \`npm install\` - ${finding.recommendedNpmTransitiveRemediation.package} already permits a safe version`)}`);
|
|
737
745
|
}
|
|
746
|
+
else if (finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length) {
|
|
747
|
+
console.log(` ${chalk.gray(`Fix: ${formatParentUpgradeBlockedReason(finding)}`)}`);
|
|
748
|
+
}
|
|
738
749
|
else if (finding.recommendedParentUpgrade) {
|
|
739
750
|
console.log(` ${chalk.gray(`Fix: upgrade ${finding.recommendedParentUpgrade.package} to ${finding.recommendedParentUpgrade.targetVersion}`)}`);
|
|
740
751
|
}
|