cve-lite-cli 1.41.4 → 1.42.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 +22 -19
- package/dist/advisory/detail-resolution.js +62 -0
- package/dist/advisory/local-db.js +90 -15
- package/dist/advisory/osv-advisory-source.js +113 -62
- package/dist/advisory/osv-sync.js +18 -16
- package/dist/advisory/sqlite.js +50 -0
- package/dist/audit-log/ndjson-writer.js +6 -3
- package/dist/audit-log/open-error.js +22 -0
- package/dist/cli/args.js +145 -87
- package/dist/cli/flags.js +2 -0
- package/dist/cli/help.js +6 -2
- package/dist/cli/overrides.js +5 -2
- package/dist/cli/validate.js +45 -5
- package/dist/constants.js +10 -0
- package/dist/index.js +15 -6
- package/dist/licenses/license-check.js +17 -4
- package/dist/osv/cache.js +7 -13
- package/dist/output/cyclonedx.js +57 -18
- package/dist/output/finding-display.js +8 -1
- package/dist/output/formatters.js +52 -11
- package/dist/output/html-reporter.js +40 -8
- package/dist/output/override-findings-terminal.js +21 -13
- package/dist/output/printers.js +185 -76
- package/dist/output/sarif.js +182 -16
- package/dist/output/spdx.js +15 -1
- package/dist/output/write-outputs.js +29 -20
- package/dist/overrides/context-builder.js +1 -7
- package/dist/overrides/detectors/oa005-nested-ineffective.js +6 -1
- package/dist/overrides/detectors/oa008-materialized.js +33 -10
- package/dist/overrides/detectors/oa009-stale-floor.js +2 -2
- package/dist/overrides/detectors/oa010-vulnerable-floor.js +19 -13
- package/dist/overrides/detectors/phantom-utils.js +2 -10
- package/dist/overrides/fixer.js +13 -2
- package/dist/overrides/index.js +1 -0
- package/dist/overrides/types.js +15 -1
- package/dist/parsers/bun-lock.js +6 -0
- package/dist/parsers/index.js +177 -40
- package/dist/parsers/multi-package.js +4 -0
- package/dist/parsers/package-lock.js +6 -2
- package/dist/parsers/pnpm-lock.js +4 -7
- package/dist/parsers/requirements-txt.js +185 -0
- package/dist/parsers/utils.js +11 -6
- package/dist/parsers/yarn-lock.js +1 -5
- package/dist/remediation/fix-commands.js +60 -36
- package/dist/remediation/parent-update-child-note.js +3 -2
- package/dist/scan/completeness.js +57 -4
- package/dist/scan/multi-folder-scan.js +27 -13
- package/dist/scan/single-scan.js +118 -37
- package/dist/scanner.js +59 -36
- package/dist/types.js +1 -1
- package/dist/usage/scanner.js +345 -45
- package/dist/utils/array.js +5 -0
- package/dist/utils/baseline.js +8 -2
- package/dist/utils/chalk.js +1 -3
- package/dist/utils/create-pr.js +2 -2
- package/dist/utils/debug-bundle.js +37 -22
- package/dist/utils/debug-project.js +6 -5
- package/dist/utils/debug-redaction.js +1 -1
- package/dist/utils/effective-options.js +5 -1
- package/dist/utils/finding.js +4 -0
- package/dist/utils/fix-runner.js +110 -36
- package/dist/utils/import-type-queries.js +5 -1
- package/dist/utils/install-command.js +6 -0
- package/dist/utils/malicious.js +11 -0
- package/dist/utils/package-identity.js +12 -2
- package/dist/utils/path-coverage.js +1 -5
- package/dist/utils/sanitize.js +59 -0
- package/dist/utils/sbom-format.js +17 -8
- package/dist/utils/severity.js +61 -7
- package/dist/utils/url.js +175 -0
- package/dist/utils/version.js +44 -0
- package/dist/utils/vuln.js +11 -14
- package/package.json +2 -4
package/dist/output/sarif.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
+
import semver from "semver";
|
|
4
5
|
import { getCompletenessImpact } from "../scan/completeness.js";
|
|
5
6
|
import { findFixTargetForFinding } from "../remediation/fix-commands.js";
|
|
6
7
|
import { getRecommendedAction, formatCooldownWarning } from "./formatters.js";
|
|
@@ -33,27 +34,177 @@ const LOCKFILE_NAMES = {
|
|
|
33
34
|
"npm-shrinkwrap": "npm-shrinkwrap.json",
|
|
34
35
|
"pnpm-lock": "pnpm-lock.yaml",
|
|
35
36
|
"yarn-lock": "yarn.lock",
|
|
36
|
-
"bun-lock": "bun.
|
|
37
|
+
"bun-lock": "bun.lock",
|
|
37
38
|
"package-json": "package.json",
|
|
39
|
+
"requirements-txt": "requirements.txt",
|
|
38
40
|
"unknown": "lockfile",
|
|
39
41
|
};
|
|
40
42
|
function findVulnForRuleId(ruleId, vulns) {
|
|
41
43
|
return vulns.find(v => v.id === ruleId || v.aliases?.includes(ruleId));
|
|
42
44
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
+
function extractWindowsFromEvents(events) {
|
|
46
|
+
const windows = [];
|
|
47
|
+
let currentIntroduced = undefined;
|
|
48
|
+
const sortedEvents = [...events].sort((a, b) => {
|
|
49
|
+
const aVersion = a.introduced ?? a.fixed ?? a.last_affected;
|
|
50
|
+
const bVersion = b.introduced ?? b.fixed ?? b.last_affected;
|
|
51
|
+
if (aVersion === "0")
|
|
52
|
+
return bVersion === "0" ? 0 : -1;
|
|
53
|
+
if (bVersion === "0")
|
|
54
|
+
return 1;
|
|
55
|
+
if (!aVersion || !bVersion)
|
|
56
|
+
return 0;
|
|
57
|
+
const aParsed = semver.valid(aVersion);
|
|
58
|
+
const bParsed = semver.valid(bVersion);
|
|
59
|
+
if (!aParsed || !bParsed)
|
|
60
|
+
return 0;
|
|
61
|
+
return semver.compare(aParsed, bParsed);
|
|
62
|
+
});
|
|
63
|
+
for (const event of sortedEvents) {
|
|
64
|
+
if (event.introduced !== undefined && (event.fixed !== undefined || event.last_affected !== undefined)) {
|
|
65
|
+
if (currentIntroduced !== undefined) {
|
|
66
|
+
windows.push({ introduced: currentIntroduced });
|
|
67
|
+
currentIntroduced = undefined;
|
|
68
|
+
}
|
|
69
|
+
windows.push({
|
|
70
|
+
introduced: event.introduced,
|
|
71
|
+
fixed: event.fixed,
|
|
72
|
+
lastAffected: event.last_affected,
|
|
73
|
+
});
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (event.introduced !== undefined) {
|
|
77
|
+
if (currentIntroduced !== undefined) {
|
|
78
|
+
windows.push({ introduced: currentIntroduced });
|
|
79
|
+
}
|
|
80
|
+
currentIntroduced = event.introduced;
|
|
81
|
+
}
|
|
82
|
+
if (event.fixed !== undefined || event.last_affected !== undefined) {
|
|
83
|
+
windows.push({
|
|
84
|
+
introduced: currentIntroduced,
|
|
85
|
+
fixed: event.fixed,
|
|
86
|
+
lastAffected: event.last_affected,
|
|
87
|
+
});
|
|
88
|
+
currentIntroduced = undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (currentIntroduced !== undefined) {
|
|
92
|
+
windows.push({ introduced: currentIntroduced });
|
|
93
|
+
}
|
|
94
|
+
return windows;
|
|
95
|
+
}
|
|
96
|
+
function formatRange(window) {
|
|
97
|
+
const parts = [];
|
|
98
|
+
if (window.introduced && window.introduced !== "0") {
|
|
99
|
+
parts.push(`>= ${window.introduced}`);
|
|
100
|
+
}
|
|
101
|
+
if (window.fixed) {
|
|
102
|
+
parts.push(`< ${window.fixed}`);
|
|
103
|
+
}
|
|
104
|
+
else if (window.lastAffected) {
|
|
105
|
+
parts.push(`<= ${window.lastAffected}`);
|
|
106
|
+
}
|
|
107
|
+
return parts.length > 0 ? parts.join(" ") : "*";
|
|
108
|
+
}
|
|
109
|
+
function matchesWindow(installedVersion, rangeStr) {
|
|
110
|
+
try {
|
|
111
|
+
if (semver.valid(installedVersion)) {
|
|
112
|
+
return semver.satisfies(installedVersion, rangeStr, { includePrerelease: true });
|
|
113
|
+
}
|
|
114
|
+
const coerced = semver.coerce(installedVersion, { includePrerelease: true });
|
|
115
|
+
return coerced ? semver.satisfies(coerced, rangeStr, { includePrerelease: true }) : false;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function compareWindows(a, b) {
|
|
122
|
+
const aIntro = a.introduced ?? "0";
|
|
123
|
+
const bIntro = b.introduced ?? "0";
|
|
124
|
+
if (aIntro !== bIntro) {
|
|
125
|
+
if (aIntro === "0")
|
|
126
|
+
return -1;
|
|
127
|
+
if (bIntro === "0")
|
|
128
|
+
return 1;
|
|
129
|
+
const aParsed = semver.valid(aIntro);
|
|
130
|
+
const bParsed = semver.valid(bIntro);
|
|
131
|
+
if (aParsed && bParsed) {
|
|
132
|
+
const cmp = semver.compare(aParsed, bParsed);
|
|
133
|
+
if (cmp !== 0)
|
|
134
|
+
return cmp;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const aEnd = a.fixed ?? a.lastAffected;
|
|
138
|
+
const bEnd = b.fixed ?? b.lastAffected;
|
|
139
|
+
if (aEnd && bEnd) {
|
|
140
|
+
const aParsed = semver.valid(aEnd);
|
|
141
|
+
const bParsed = semver.valid(bEnd);
|
|
142
|
+
if (aParsed && bParsed) {
|
|
143
|
+
const cmp = semver.compare(aParsed, bParsed);
|
|
144
|
+
if (cmp !== 0)
|
|
145
|
+
return cmp;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if (aEnd && !bEnd) {
|
|
149
|
+
return -1;
|
|
150
|
+
}
|
|
151
|
+
else if (!aEnd && bEnd) {
|
|
152
|
+
return 1;
|
|
153
|
+
}
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
export function extractAffectedRange(vuln, installedVersion, packageName) {
|
|
157
|
+
const windows = [];
|
|
158
|
+
const seenRanges = new Set();
|
|
159
|
+
const entries = vuln.affected ?? [];
|
|
160
|
+
const packageEntries = packageName
|
|
161
|
+
? entries.filter(e => e.package?.name === packageName)
|
|
162
|
+
: [];
|
|
163
|
+
const entriesToScan = packageEntries.length > 0 ? packageEntries : entries;
|
|
164
|
+
for (const entry of entriesToScan) {
|
|
45
165
|
for (const range of entry.ranges ?? []) {
|
|
46
166
|
if (range.type !== "SEMVER")
|
|
47
167
|
continue;
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
168
|
+
const parsedWindows = extractWindowsFromEvents(range.events ?? []);
|
|
169
|
+
for (const w of parsedWindows) {
|
|
170
|
+
const formatted = formatRange(w);
|
|
171
|
+
if (formatted && !seenRanges.has(formatted)) {
|
|
172
|
+
seenRanges.add(formatted);
|
|
173
|
+
windows.push({ window: w, range: formatted });
|
|
174
|
+
}
|
|
51
175
|
}
|
|
52
176
|
}
|
|
53
177
|
}
|
|
54
|
-
|
|
178
|
+
if (windows.length === 0)
|
|
179
|
+
return null;
|
|
180
|
+
windows.sort((a, b) => compareWindows(a.window, b.window));
|
|
181
|
+
const validFixedVersions = [
|
|
182
|
+
...new Set(windows
|
|
183
|
+
.map(w => w.window.fixed)
|
|
184
|
+
.filter((f) => Boolean(f))
|
|
185
|
+
.filter(f => !windows.some(w => matchesWindow(f, w.range)))),
|
|
186
|
+
];
|
|
187
|
+
const hasAnyConstrainedWindow = windows.some(w => w.range !== "*");
|
|
188
|
+
if (!hasAnyConstrainedWindow)
|
|
189
|
+
return null;
|
|
190
|
+
if (installedVersion) {
|
|
191
|
+
const matching = windows.find(w => w.range !== "*" && matchesWindow(installedVersion, w.range)) ??
|
|
192
|
+
windows.find(w => matchesWindow(installedVersion, w.range));
|
|
193
|
+
if (matching) {
|
|
194
|
+
const fixed = matching.window.fixed;
|
|
195
|
+
const isFixedStillAffected = fixed ? windows.some(w => matchesWindow(fixed, w.range)) : false;
|
|
196
|
+
return {
|
|
197
|
+
affected: matching.range,
|
|
198
|
+
...(fixed && !isFixedStillAffected ? { patched: fixed } : {}),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
affected: windows.map(w => w.range).join(" or "),
|
|
204
|
+
...(validFixedVersions.length > 0 ? { patched: validFixedVersions.join(", ") } : {}),
|
|
205
|
+
};
|
|
55
206
|
}
|
|
56
|
-
function buildRuleHelp(vuln, depPath, action) {
|
|
207
|
+
function buildRuleHelp(vuln, depPath, action, packageName) {
|
|
57
208
|
const title = vuln.summary ?? vuln.id;
|
|
58
209
|
const body = vuln.details && vuln.details !== vuln.summary ? vuln.details : null;
|
|
59
210
|
const cvssEntry = (vuln.severity ?? []).find(s => s.type?.startsWith("CVSS"));
|
|
@@ -61,7 +212,7 @@ function buildRuleHelp(vuln, depPath, action) {
|
|
|
61
212
|
const dbSpecific = vuln.database_specific;
|
|
62
213
|
const rawCweIds = dbSpecific?.cwe_ids;
|
|
63
214
|
const cweIds = Array.isArray(rawCweIds) ? rawCweIds : null;
|
|
64
|
-
const range = extractAffectedRange(vuln);
|
|
215
|
+
const range = extractAffectedRange(vuln, undefined, packageName);
|
|
65
216
|
const textParts = [TEXT_ATTRIBUTION, "", `Vulnerability: ${title}`];
|
|
66
217
|
if (body)
|
|
67
218
|
textParts.push("", body);
|
|
@@ -71,11 +222,13 @@ function buildRuleHelp(vuln, depPath, action) {
|
|
|
71
222
|
textParts.push(`CWE: ${cweIds.join(", ")}`);
|
|
72
223
|
if (range) {
|
|
73
224
|
textParts.push(`Affected: ${range.affected}`);
|
|
74
|
-
|
|
225
|
+
if (range.patched)
|
|
226
|
+
textParts.push(`Patched: ${range.patched}`);
|
|
75
227
|
}
|
|
76
228
|
if (depPath)
|
|
77
229
|
textParts.push("", `Dependency path: ${depPath}`);
|
|
78
|
-
|
|
230
|
+
if (action)
|
|
231
|
+
textParts.push("", `Fix: ${action}`);
|
|
79
232
|
const mdParts = [MARKDOWN_ATTRIBUTION, "", `## ${title}`];
|
|
80
233
|
if (body)
|
|
81
234
|
mdParts.push("", body);
|
|
@@ -85,11 +238,13 @@ function buildRuleHelp(vuln, depPath, action) {
|
|
|
85
238
|
mdParts.push(`**CWE:** ${cweIds.join(", ")}`);
|
|
86
239
|
if (range) {
|
|
87
240
|
mdParts.push(`**Affected versions:** ${range.affected}`);
|
|
88
|
-
|
|
241
|
+
if (range.patched)
|
|
242
|
+
mdParts.push(`**Patched version:** ${range.patched}`);
|
|
89
243
|
}
|
|
90
244
|
if (depPath)
|
|
91
245
|
mdParts.push("", `**Dependency path:** ${depPath}`);
|
|
92
|
-
|
|
246
|
+
if (action)
|
|
247
|
+
mdParts.push("", "## Fix", "", action);
|
|
93
248
|
return { text: textParts.join("\n"), markdown: mdParts.join("\n") };
|
|
94
249
|
}
|
|
95
250
|
export function deriveLockfileUri(scanInput) {
|
|
@@ -167,7 +322,7 @@ export function buildSarifOutput(findings, lockfileUri, version, plan, options =
|
|
|
167
322
|
: "Re-run the scan to retry.";
|
|
168
323
|
for (const { finding, accepted } of taggedFindings) {
|
|
169
324
|
const level = severityToSarifLevel(finding.severity);
|
|
170
|
-
const action = getRecommendedAction(finding);
|
|
325
|
+
const action = getRecommendedAction(finding, plan?.packageManager);
|
|
171
326
|
// If the recommended fix for this finding lands on a version inside the
|
|
172
327
|
// project's configured release cooldown, surface that on the result so it
|
|
173
328
|
// reaches Code Scanning (message text for humans, property for tooling).
|
|
@@ -200,7 +355,8 @@ export function buildSarifOutput(findings, lockfileUri, version, plan, options =
|
|
|
200
355
|
const summary = vuln?.summary;
|
|
201
356
|
const details = vuln?.details;
|
|
202
357
|
const depPath = finding.pkg.paths?.[0]?.join(" → ") ?? "";
|
|
203
|
-
const
|
|
358
|
+
const registryName = finding.pkg.registryName ?? finding.pkg.name;
|
|
359
|
+
const help = vuln ? buildRuleHelp(vuln, depPath, action, registryName) : undefined;
|
|
204
360
|
ruleMap.set(ruleId, {
|
|
205
361
|
id: ruleId,
|
|
206
362
|
name: summary ?? ruleId,
|
|
@@ -214,6 +370,16 @@ export function buildSarifOutput(findings, lockfileUri, version, plan, options =
|
|
|
214
370
|
// Fix metadata describes the resolved advisories, so it stays off the unavailable result.
|
|
215
371
|
const resultProperties = {};
|
|
216
372
|
if (!detailUnavailable) {
|
|
373
|
+
const vuln = findVulnForRuleId(ruleId, finding.vulnerabilities);
|
|
374
|
+
const selectedRange = vuln
|
|
375
|
+
? extractAffectedRange(vuln, finding.pkg.version, finding.pkg.registryName ?? finding.pkg.name)
|
|
376
|
+
: null;
|
|
377
|
+
if (selectedRange) {
|
|
378
|
+
resultProperties.advisoryAffectedRange = selectedRange.affected;
|
|
379
|
+
if (selectedRange.patched) {
|
|
380
|
+
resultProperties.advisoryPatchedVersion = selectedRange.patched;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
217
383
|
if (cooldown)
|
|
218
384
|
resultProperties.cooldownWarning = cooldown;
|
|
219
385
|
if (finding.firstFixedVersion)
|
|
@@ -251,7 +417,7 @@ export function buildSarifOutput(findings, lockfileUri, version, plan, options =
|
|
|
251
417
|
message: {
|
|
252
418
|
text: detailUnavailable
|
|
253
419
|
? `${finding.pkg.name}@${finding.pkg.version} matched ${unresolvedIds.length > 0 ? unresolvedIds.join(", ") : "a known advisory"}, but the details could not be retrieved during this scan. ${detailRetryStep}`
|
|
254
|
-
: `${finding.pkg.name}@${finding.pkg.version} is vulnerable (${finding.severity})
|
|
420
|
+
: `${finding.pkg.name}@${finding.pkg.version} is vulnerable (${finding.severity}).${action ? ` ${action}` : ""}`
|
|
255
421
|
+ (cooldown ? ` Release cooldown: ${formatCooldownWarning(cooldown)}` : ""),
|
|
256
422
|
},
|
|
257
423
|
locations: [location],
|
package/dist/output/spdx.js
CHANGED
|
@@ -80,7 +80,7 @@ function buildFindingAnnotation(findings, plan, annotator, timestamp) {
|
|
|
80
80
|
// The advisory IDs are already enumerated as SECURITY externalRefs, so the
|
|
81
81
|
// annotation carries only what SPDX has no field for: severity and the fix.
|
|
82
82
|
const parts = findings.map(finding => {
|
|
83
|
-
const fix = (plan ? findSuggestedCommandForFinding(plan, finding) : null) ?? getRecommendedAction(finding);
|
|
83
|
+
const fix = (plan ? findSuggestedCommandForFinding(plan, finding) : null) ?? getRecommendedAction(finding, plan?.packageManager);
|
|
84
84
|
return `severity=${finding.severity}; advisories=${finding.vulnerabilities.length}; fix=${fix}`;
|
|
85
85
|
});
|
|
86
86
|
return {
|
|
@@ -298,6 +298,9 @@ export function buildSpdxDocument(allPackages, findings, projectMeta, version, p
|
|
|
298
298
|
relationships.push(...(options.dependencyEdges?.length
|
|
299
299
|
? buildEdgesFromResolved(options.dependencyEdges, indexed, root?.pkg.SPDXID ?? null)
|
|
300
300
|
: buildDependencyEdges(indexed, indexPackagesByPath(indexed, root?.pkg.SPDXID ?? null), root?.pkg.SPDXID ?? null)));
|
|
301
|
+
const advisoryComment = options.advisoryMetadata
|
|
302
|
+
? formatSpdxAdvisoryComment(options.advisoryMetadata)
|
|
303
|
+
: undefined;
|
|
301
304
|
return {
|
|
302
305
|
spdxVersion: "SPDX-2.3",
|
|
303
306
|
SPDXID: "SPDXRef-DOCUMENT",
|
|
@@ -308,11 +311,22 @@ export function buildSpdxDocument(allPackages, findings, projectMeta, version, p
|
|
|
308
311
|
creationInfo: {
|
|
309
312
|
created: timestamp,
|
|
310
313
|
creators: [annotator],
|
|
314
|
+
...(advisoryComment ? { comment: advisoryComment } : {}),
|
|
311
315
|
},
|
|
312
316
|
packages,
|
|
313
317
|
relationships,
|
|
314
318
|
};
|
|
315
319
|
}
|
|
320
|
+
function formatSpdxAdvisoryComment(metadata) {
|
|
321
|
+
const parts = [`offline=${metadata.offline ? "true" : "false"}`];
|
|
322
|
+
if (metadata.syncTimestamp) {
|
|
323
|
+
parts.push(`syncTimestamp=${metadata.syncTimestamp}`);
|
|
324
|
+
}
|
|
325
|
+
if (metadata.sourceUrl) {
|
|
326
|
+
parts.push(`sourceUrl=${metadata.sourceUrl}`);
|
|
327
|
+
}
|
|
328
|
+
return `Advisory database: ${parts.join("; ")}`;
|
|
329
|
+
}
|
|
316
330
|
export function writeSpdxReport(allPackages, findings, plan, projectMeta, options = {}, outputDir) {
|
|
317
331
|
const filename = options.filename ?? `cve-lite-scan-${sbomTimestamp()}.spdx.json`;
|
|
318
332
|
const effectiveOptions = {
|
|
@@ -6,7 +6,7 @@ import { writeSarifReport, deriveLockfileUri } from "./sarif.js";
|
|
|
6
6
|
import { writeCycloneDxReport } from "./cyclonedx.js";
|
|
7
7
|
import { writeSpdxReport } from "./spdx.js";
|
|
8
8
|
import { resolveDependencyEdgesResult } from "./sbom-dependency-edges.js";
|
|
9
|
-
import {
|
|
9
|
+
import { resolveSbomFormats } from "../utils/sbom-format.js";
|
|
10
10
|
import { buildScanJson } from "./scan-json.js";
|
|
11
11
|
import { resolveOutputTarget, displayOutputPath, checkFormatExtensionMismatch, drainStdout } from "./output-dir.js";
|
|
12
12
|
import { getCliVersion } from "../utils/version-info.js";
|
|
@@ -101,27 +101,36 @@ function stageJsonOutput(options, scanState, scanInput, projectPath, outputDir,
|
|
|
101
101
|
return jsonOutputPath;
|
|
102
102
|
}
|
|
103
103
|
function stageSbomOutput(options, scanState, scanInput, projectPath, outputDir, customFilename, stagedOutputs, isStdout) {
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
104
|
+
const sbomFormats = resolveSbomFormats(options);
|
|
105
|
+
if (sbomFormats.length === 0)
|
|
106
106
|
return;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (sbomFormat === "spdx") {
|
|
110
|
-
const { edges: dependencyEdges, graphLoaded } = resolveDependencyEdgesResult(scanInput, scanState.allPackages);
|
|
111
|
-
const spdxNote = scanInput.source === "pnpm-lock" ||
|
|
112
|
-
scanInput.source === "yarn-lock" ||
|
|
113
|
-
scanInput.source === "bun-lock"
|
|
114
|
-
? chalk.gray(SPDX_NOASSERTION_LOCKFILE_NOTE)
|
|
115
|
-
: undefined;
|
|
116
|
-
stageOrWrite(outputDir, customFilename, "SPDX SBOM written to", (tempFilename) => writeSpdxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, {
|
|
117
|
-
inventoryOnly: options.sbomInventoryOnly,
|
|
118
|
-
dependencyEdges,
|
|
119
|
-
graphFailed: !graphLoaded,
|
|
120
|
-
...(tempFilename ? { filename: tempFilename } : isStdout ? { filename: "-" } : {}),
|
|
121
|
-
}, outputDir), stagedOutputs, spdxNote, isStdout);
|
|
107
|
+
if (customFilename && sbomFormats.length > 1) {
|
|
108
|
+
throw new Error("--output cannot be a file path when multiple output formats are specified. Use an output directory instead.");
|
|
122
109
|
}
|
|
123
|
-
|
|
124
|
-
|
|
110
|
+
const projectMeta = readProjectMeta(projectPath);
|
|
111
|
+
for (const sbomFormat of sbomFormats) {
|
|
112
|
+
warnOnFormatMismatch(customFilename, sbomFormat);
|
|
113
|
+
if (sbomFormat === "spdx") {
|
|
114
|
+
const { edges: dependencyEdges, graphLoaded } = resolveDependencyEdgesResult(scanInput, scanState.allPackages);
|
|
115
|
+
const spdxNote = scanInput.source === "pnpm-lock" ||
|
|
116
|
+
scanInput.source === "yarn-lock" ||
|
|
117
|
+
scanInput.source === "bun-lock"
|
|
118
|
+
? chalk.gray(SPDX_NOASSERTION_LOCKFILE_NOTE)
|
|
119
|
+
: undefined;
|
|
120
|
+
stageOrWrite(outputDir, customFilename, "SPDX SBOM written to", (tempFilename) => writeSpdxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, {
|
|
121
|
+
inventoryOnly: options.sbomInventoryOnly,
|
|
122
|
+
dependencyEdges,
|
|
123
|
+
graphFailed: !graphLoaded,
|
|
124
|
+
...(scanState.advisoryMetadata ? { advisoryMetadata: scanState.advisoryMetadata } : {}),
|
|
125
|
+
...(tempFilename ? { filename: tempFilename } : isStdout ? { filename: "-" } : {}),
|
|
126
|
+
}, outputDir), stagedOutputs, spdxNote, isStdout);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
stageOrWrite(outputDir, customFilename, "CycloneDX BOM written to", (tempFilename) => {
|
|
130
|
+
const cdxFilename = tempFilename ?? (isStdout ? "-" : undefined);
|
|
131
|
+
return writeCycloneDxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, outputDir, cdxFilename, { advisoryMetadata: scanState.advisoryMetadata });
|
|
132
|
+
}, stagedOutputs, undefined, isStdout);
|
|
133
|
+
}
|
|
125
134
|
}
|
|
126
135
|
}
|
|
127
136
|
function stageSarifOutput(options, scanState, scanInput, outputDir, customFilename, stagedOutputs, isStdout) {
|
|
@@ -3,6 +3,7 @@ import { basename, join } from "node:path";
|
|
|
3
3
|
import { extractOverrideEntries } from "./parsing/package-json.js";
|
|
4
4
|
import { walkInstalledTree } from "./parsing/installed-tree.js";
|
|
5
5
|
import { readSelectedLockfile, selectRootLockfile } from "../parsers/index.js";
|
|
6
|
+
import { PACKAGE_MANAGER_BY_LOCKFILE } from "../constants.js";
|
|
6
7
|
import { scanAllImports, usageScanTruncationWarning } from "../usage/scanner.js";
|
|
7
8
|
import { readWorkspaceMemberManifests } from "../utils/package-json.js";
|
|
8
9
|
import { isNodeBuiltinSpecifier } from "../utils/node-builtins.js";
|
|
@@ -104,13 +105,6 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
104
105
|
logger: opts.logger,
|
|
105
106
|
};
|
|
106
107
|
}
|
|
107
|
-
const PACKAGE_MANAGER_BY_LOCKFILE = {
|
|
108
|
-
"package-lock.json": "npm",
|
|
109
|
-
"npm-shrinkwrap.json": "npm",
|
|
110
|
-
"pnpm-lock.yaml": "pnpm",
|
|
111
|
-
"yarn.lock": "yarn",
|
|
112
|
-
"bun.lock": "bun",
|
|
113
|
-
};
|
|
114
108
|
/**
|
|
115
109
|
* Groups resolved lockfile versions by package name. A package can appear at
|
|
116
110
|
* several versions in one tree, so every one is kept: an override selector only
|
|
@@ -101,6 +101,8 @@ function outerParentProblem(ctx, outerKey) {
|
|
|
101
101
|
}
|
|
102
102
|
function classify(args) {
|
|
103
103
|
const { ctx, outerKey, innerKey, innerValue, entryPath, lookup, form } = args;
|
|
104
|
+
// An object-form entry holds several pins; remove only this one.
|
|
105
|
+
const fixPath = form === "object" ? [...entryPath, innerKey] : entryPath;
|
|
104
106
|
const findingBase = (subId, severity, message, details, action) => ({
|
|
105
107
|
ruleId: RULE_ID,
|
|
106
108
|
subRuleId: subId,
|
|
@@ -113,7 +115,7 @@ function classify(args) {
|
|
|
113
115
|
? {
|
|
114
116
|
fix: {
|
|
115
117
|
type: "rfc6902",
|
|
116
|
-
patch: [{ op: "remove", path: jsonPointer(
|
|
118
|
+
patch: [{ op: "remove", path: jsonPointer(fixPath) }],
|
|
117
119
|
runnableCommand: `${OVERRIDES_COMMAND} --fix --rule OA005`,
|
|
118
120
|
},
|
|
119
121
|
}
|
|
@@ -138,6 +140,9 @@ function classify(args) {
|
|
|
138
140
|
if (outerProblem) {
|
|
139
141
|
return findingBase("OA005.b", "high", "Nested override outer parent not in resolved tree", `Outer parent ${outerKey} ${outerProblem}. The nested override "${innerKey}" has no parent to apply to.`, "remove");
|
|
140
142
|
}
|
|
143
|
+
// "." is npm's self-pin for the outer package, not a dependency.
|
|
144
|
+
if (innerKey === ".")
|
|
145
|
+
return null;
|
|
141
146
|
// .c - outer in tree but inner is not declared in parent's manifest deps.
|
|
142
147
|
const parentManifest = lookup(outerKey);
|
|
143
148
|
const declaredAsDep = !!parentManifest &&
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { satisfiesRange, coerceVersion, compareVersions, looksLikeVersion, isValidRange, positionAgainstRange, } from "../../utils/version.js";
|
|
2
|
+
import { parseNpmAliasTarget } from "../../utils/package-identity.js";
|
|
2
3
|
import { pluralize } from "../../utils/string.js";
|
|
3
4
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
4
5
|
const RULE_ID = "OA008";
|
|
@@ -22,22 +23,23 @@ export function detect(ctx) {
|
|
|
22
23
|
if (typeof entry.value !== "string")
|
|
23
24
|
continue;
|
|
24
25
|
const pinValue = entry.value.trim();
|
|
25
|
-
|
|
26
|
+
const floorRange = extractFloorRange(pinValue, entry.packageName);
|
|
27
|
+
if (!isCheckableFloor(floorRange))
|
|
26
28
|
continue;
|
|
27
29
|
const copies = ctx.installedCopies.get(entry.packageName) ?? [];
|
|
28
30
|
if (copies.length === 0)
|
|
29
31
|
continue;
|
|
30
|
-
const isRangePin = isValidRange(
|
|
32
|
+
const isRangePin = isValidRange(floorRange) && !isConcrete(floorRange);
|
|
31
33
|
const vulnerable = copies.filter(c => {
|
|
32
34
|
const v = looksLikeVersion(c.version);
|
|
33
35
|
if (!v)
|
|
34
36
|
return false;
|
|
35
37
|
// For range pins, check `satisfiesRange`. For exact pins, the floor is the pin itself.
|
|
36
38
|
if (isRangePin) {
|
|
37
|
-
return !satisfiesRange(c.version,
|
|
39
|
+
return !satisfiesRange(c.version, floorRange);
|
|
38
40
|
}
|
|
39
41
|
// Concrete pin: floor IS the pin; below it is vulnerable.
|
|
40
|
-
const floor = coerceVersion(
|
|
42
|
+
const floor = coerceVersion(floorRange);
|
|
41
43
|
return floor ? compareVersions(c.version, floor) < 0 : false;
|
|
42
44
|
});
|
|
43
45
|
if (vulnerable.length === 0)
|
|
@@ -51,22 +53,43 @@ export function detect(ctx) {
|
|
|
51
53
|
location: { file: "package.json", jsonPath: jsonPointer(entry.path) },
|
|
52
54
|
message: "Override floor not applied - vulnerable copy still on disk",
|
|
53
55
|
details: isRangePin
|
|
54
|
-
? rangeMismatchDetails(entry.packageName, pinValue, vulnerable.length, distinct)
|
|
56
|
+
? rangeMismatchDetails(entry.packageName, pinValue, floorRange, vulnerable.length, distinct)
|
|
55
57
|
: concreteFloorDetails(entry.packageName, pinValue, vulnerable.length, distinct),
|
|
56
58
|
references: ["https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/OA008.md"],
|
|
57
59
|
});
|
|
58
60
|
}
|
|
59
61
|
return findings;
|
|
60
62
|
}
|
|
63
|
+
const LOCAL_PROTOCOLS = ["workspace:", "file:", "link:"];
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the semver version or range portion of a pin value, unwrapping
|
|
66
|
+
* same-package `npm:` alias specifications (e.g. `npm:minimist@^1.2.8` -> `^1.2.8`
|
|
67
|
+
* or `npm:@scope/pkg@1.2.3` -> `1.2.3`). When the alias points at a different
|
|
68
|
+
* package (fork), installed copies under node_modules carry the folder name
|
|
69
|
+
* rather than the fork name, so forks are skipped (matching main). Returns null
|
|
70
|
+
* for local protocols, unversioned aliases, or fork aliases.
|
|
71
|
+
*/
|
|
72
|
+
function extractFloorRange(value, packageName) {
|
|
73
|
+
if (LOCAL_PROTOCOLS.some(p => value.startsWith(p)))
|
|
74
|
+
return null;
|
|
75
|
+
if (value.startsWith("npm:")) {
|
|
76
|
+
const alias = parseNpmAliasTarget(value);
|
|
77
|
+
if (!alias || alias.name !== packageName || !alias.spec) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return alias.spec;
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
61
84
|
/**
|
|
62
85
|
* True if the pin value is something we can compare against a concrete version:
|
|
63
86
|
* a valid semver range OR a concrete version. Excludes floating tags and
|
|
64
87
|
* workspace/file/link protocols (those are OA002 or out of scope).
|
|
65
88
|
*/
|
|
66
|
-
function isCheckableFloor(
|
|
67
|
-
if (
|
|
89
|
+
function isCheckableFloor(range) {
|
|
90
|
+
if (!range)
|
|
68
91
|
return false;
|
|
69
|
-
if (!isValidRange(
|
|
92
|
+
if (!isValidRange(range))
|
|
70
93
|
return false;
|
|
71
94
|
return true;
|
|
72
95
|
}
|
|
@@ -91,12 +114,12 @@ function concreteFloorDetails(packageName, pinValue, copyCount, versions) {
|
|
|
91
114
|
`${copyCount === 1 ? "is" : "are"} below the floor. ` +
|
|
92
115
|
CAUSE_HINT);
|
|
93
116
|
}
|
|
94
|
-
function rangeMismatchDetails(packageName, pinValue, copyCount, versions) {
|
|
117
|
+
function rangeMismatchDetails(packageName, pinValue, floorRange, copyCount, versions) {
|
|
95
118
|
const below = [];
|
|
96
119
|
const above = [];
|
|
97
120
|
const outside = [];
|
|
98
121
|
for (const version of versions) {
|
|
99
|
-
const position = positionAgainstRange(version,
|
|
122
|
+
const position = positionAgainstRange(version, floorRange);
|
|
100
123
|
if (position === "below")
|
|
101
124
|
below.push(version);
|
|
102
125
|
else if (position === "above")
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
2
|
-
import { looksLikeVersion, coerceVersion, compareVersions } from "../../utils/version.js";
|
|
2
|
+
import { looksLikeVersion, coerceVersion, compareVersions, rangeMinVersion } from "../../utils/version.js";
|
|
3
3
|
import { OVERRIDES_COMMAND } from "../../constants.js";
|
|
4
4
|
import { getDeclaredPackages, undeclaredImportFiles } from "./phantom-utils.js";
|
|
5
5
|
const RULE_ID = "OA009";
|
|
@@ -30,7 +30,7 @@ export function detect(ctx) {
|
|
|
30
30
|
continue;
|
|
31
31
|
const floorMajor = pin.startsWith("^") ? majorOf(floor) : -1;
|
|
32
32
|
const allParentsMeetFloor = parents.every((parent) => {
|
|
33
|
-
const parentFloor =
|
|
33
|
+
const parentFloor = rangeMinVersion(parent.declaredValue);
|
|
34
34
|
if (!parentFloor)
|
|
35
35
|
return false;
|
|
36
36
|
// For ^ ranges the allowed range is [floor, nextMajor). A parent in a
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { admittedKey } from "../context.js";
|
|
2
2
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
3
|
-
import { looksLikeVersion, compareVersions,
|
|
3
|
+
import { looksLikeVersion, compareVersions, raiseCaretRangeFloor } from "../../utils/version.js";
|
|
4
4
|
import { OVERRIDES_COMMAND } from "../../constants.js";
|
|
5
5
|
import { FLAG_CHECK_NETWORK } from "../../cli/flags.js";
|
|
6
6
|
const RULE_ID = "OA010";
|
|
@@ -27,7 +27,7 @@ function worstSeverity(hits) {
|
|
|
27
27
|
/**
|
|
28
28
|
* Rewrite the floor of an override range, preserving whatever ceiling it already
|
|
29
29
|
* carries. `>=5.7.0 <6` with a 5.10.1 floor becomes `>=5.10.1 <6`; a bare `^5.7.0`
|
|
30
|
-
* becomes `>=5.10.1 <6.0.0`
|
|
30
|
+
* becomes `>=5.10.1 <6.0.0-0` to retain the original prerelease boundary.
|
|
31
31
|
* Returns null when the shape is not one we can safely rewrite.
|
|
32
32
|
*/
|
|
33
33
|
function raiseFloor(pin, newFloor) {
|
|
@@ -36,8 +36,7 @@ function raiseFloor(pin, newFloor) {
|
|
|
36
36
|
return tokens.map((t) => (t.startsWith(">=") ? `>=${newFloor}` : t)).join(" ");
|
|
37
37
|
}
|
|
38
38
|
if (tokens.length === 1 && tokens[0].startsWith("^")) {
|
|
39
|
-
|
|
40
|
-
return major === null ? null : `>=${newFloor} <${major + 1}.0.0`;
|
|
39
|
+
return raiseCaretRangeFloor(pin, newFloor);
|
|
41
40
|
}
|
|
42
41
|
return null;
|
|
43
42
|
}
|
|
@@ -80,12 +79,20 @@ export function detect(ctx) {
|
|
|
80
79
|
.reduce((a, b) => (compareVersions(a, b) >= 0 ? a : b));
|
|
81
80
|
// The replacement floor must clear every vulnerable version the range admits,
|
|
82
81
|
// not merely the lowest one, so search above the highest of them.
|
|
83
|
-
const
|
|
82
|
+
const candidates = admitted
|
|
84
83
|
.filter((v) => !vulnerableVersions.has(v) && compareVersions(v, highestVulnerable) > 0)
|
|
85
|
-
.sort(compareVersions)
|
|
84
|
+
.sort(compareVersions);
|
|
85
|
+
let floorFix;
|
|
86
|
+
for (const version of candidates) {
|
|
87
|
+
const range = raiseFloor(pin, version);
|
|
88
|
+
if (range !== null) {
|
|
89
|
+
floorFix = { version, range };
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
86
93
|
const advisoryIds = [...new Set(hits.map((h) => h.advisoryId))];
|
|
87
94
|
const severity = worstSeverity(hits);
|
|
88
|
-
const versionList =
|
|
95
|
+
const versionList = [...vulnerableVersions].sort(compareVersions).join(", ");
|
|
89
96
|
const base = {
|
|
90
97
|
ruleId: RULE_ID,
|
|
91
98
|
severity,
|
|
@@ -95,14 +102,13 @@ export function detect(ctx) {
|
|
|
95
102
|
"https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/OA010.md",
|
|
96
103
|
],
|
|
97
104
|
};
|
|
98
|
-
|
|
99
|
-
if (!newFloor || !rewritten) {
|
|
105
|
+
if (!floorFix) {
|
|
100
106
|
findings.push({
|
|
101
107
|
...base,
|
|
102
108
|
message: "Override floor admits a known-vulnerable version",
|
|
103
109
|
details: `${entry.packageName} is floored to ${pin}, which admits ${versionList} ` +
|
|
104
|
-
`(${advisoryIds.join(", ")}). There is no clean version above them
|
|
105
|
-
`the
|
|
110
|
+
`(${advisoryIds.join(", ")}). There is no clean version above them that can ` +
|
|
111
|
+
`raise the floor automatically while preserving the original range's ceiling and prerelease rules.`,
|
|
106
112
|
});
|
|
107
113
|
continue;
|
|
108
114
|
}
|
|
@@ -112,10 +118,10 @@ export function detect(ctx) {
|
|
|
112
118
|
details: `${entry.packageName} is floored to ${pin}, which admits ${versionList} ` +
|
|
113
119
|
`(${advisoryIds.join(", ")}). The installed version may be clean today, but ` +
|
|
114
120
|
`any resolution inside this range is permitted to pick a vulnerable one. ` +
|
|
115
|
-
`Raise the floor to ${
|
|
121
|
+
`Raise the floor to ${floorFix.version}.`,
|
|
116
122
|
fix: {
|
|
117
123
|
type: "rfc6902",
|
|
118
|
-
patch: [{ op: "replace", path: jsonPointer(entry.path), value:
|
|
124
|
+
patch: [{ op: "replace", path: jsonPointer(entry.path), value: floorFix.range }],
|
|
119
125
|
// --check-network is part of the command because without it this very rule does
|
|
120
126
|
// not run, so the command would answer "OA010 did not run" for a finding OA010
|
|
121
127
|
// just reported.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isSafeInEveryShell, shellQuote } from "../../utils/shell-quote.js";
|
|
2
|
+
import { INSTALL_COMMANDS } from "../../utils/install-command.js";
|
|
2
3
|
export function getDeclaredPackages(packageJson) {
|
|
3
4
|
const declared = new Set();
|
|
4
5
|
for (const section of ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]) {
|
|
@@ -40,19 +41,10 @@ function owningWorkspaceMember(members, file) {
|
|
|
40
41
|
}
|
|
41
42
|
return best;
|
|
42
43
|
}
|
|
43
|
-
export function installCmd(pm) {
|
|
44
|
-
if (pm === "pnpm")
|
|
45
|
-
return "pnpm add";
|
|
46
|
-
if (pm === "yarn")
|
|
47
|
-
return "yarn add";
|
|
48
|
-
if (pm === "bun")
|
|
49
|
-
return "bun add";
|
|
50
|
-
return "npm install";
|
|
51
|
-
}
|
|
52
44
|
/** The closing instruction for a phantom-dependency finding: a copyable install command, or a plain step when the name is not safe in every shell. */
|
|
53
45
|
export function phantomInstallInstruction(pm, pkgName) {
|
|
54
46
|
if (!isSafeInEveryShell(pkgName)) {
|
|
55
47
|
return "Declare it in package.json (no command is shown because the name is not safe to paste into every shell).";
|
|
56
48
|
}
|
|
57
|
-
return `Run: ${
|
|
49
|
+
return `Run: ${INSTALL_COMMANDS[pm === "unknown" ? "npm" : pm].join(" ")} ${shellQuote(pkgName)}`;
|
|
58
50
|
}
|