cve-lite-cli 1.18.1 → 1.18.2
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 +4 -1
- package/dist/advisory/local-advisory-source.js +1 -1
- package/dist/advisory/osv-advisory-source.js +83 -13
- package/dist/cli/args.js +4 -0
- package/dist/cli/help.js +1 -0
- package/dist/index.js +68 -4
- package/dist/learn/keypress.js +50 -0
- package/dist/learn/runner.js +96 -0
- package/dist/learn/scenario.js +47 -0
- package/dist/learn/session.js +36 -0
- package/dist/learn/types.js +1 -0
- package/dist/osv/cache.js +46 -7
- package/dist/output/debug.js +38 -0
- package/dist/output/finding-display.js +30 -0
- package/dist/output/multi-folder-html-reporter.js +323 -0
- package/dist/output/multi-folder-printer.js +46 -0
- package/dist/output/printers.js +4 -4
- package/dist/output/sarif.js +3 -9
- package/dist/output/spinner.js +1 -1
- package/dist/parsers/multi-package.js +58 -0
- package/dist/parsers/pnpm-lock.js +4 -0
- package/dist/remediation/fix-commands.js +36 -4
- package/dist/remediation/npm-registry.js +44 -1
- package/dist/remediation/parent-upgrade.js +14 -8
- package/dist/scan/multi-folder-scan.js +97 -0
- package/dist/scanner.js +94 -16
- package/dist/utils/fix-runner.js +6 -2
- package/dist/utils/network.js +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { EXCLUDED_DIRS } from "../constants.js";
|
|
4
|
+
import { loadPackages } from "./index.js";
|
|
5
|
+
const LOCKFILE_NAMES = ["bun.lock", "npm-shrinkwrap.json", "package-lock.json", "pnpm-lock.yaml", "yarn.lock"];
|
|
6
|
+
export function hasRootLockfile(projectRoot) {
|
|
7
|
+
return LOCKFILE_NAMES.some(name => fs.existsSync(path.join(projectRoot, name)));
|
|
8
|
+
}
|
|
9
|
+
export function findNestedLockfiles(projectRoot, maxDepth) {
|
|
10
|
+
const results = [];
|
|
11
|
+
function walk(dir, depth) {
|
|
12
|
+
if (depth > maxDepth)
|
|
13
|
+
return;
|
|
14
|
+
let entries = [];
|
|
15
|
+
try {
|
|
16
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const lockfile = entries.find(e => e.isFile() && LOCKFILE_NAMES.includes(e.name));
|
|
22
|
+
if (lockfile) {
|
|
23
|
+
results.push(path.join(dir, lockfile.name));
|
|
24
|
+
return; // stop recursing — this lockfile covers its subtree
|
|
25
|
+
}
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
if (!entry.isDirectory())
|
|
28
|
+
continue;
|
|
29
|
+
if (EXCLUDED_DIRS.has(entry.name))
|
|
30
|
+
continue;
|
|
31
|
+
walk(path.join(dir, entry.name), depth + 1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
let rootEntries = [];
|
|
35
|
+
try {
|
|
36
|
+
rootEntries = fs.readdirSync(projectRoot, { withFileTypes: true });
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return results;
|
|
40
|
+
}
|
|
41
|
+
for (const entry of rootEntries) {
|
|
42
|
+
if (!entry.isDirectory())
|
|
43
|
+
continue;
|
|
44
|
+
if (EXCLUDED_DIRS.has(entry.name))
|
|
45
|
+
continue;
|
|
46
|
+
walk(path.join(projectRoot, entry.name), 1);
|
|
47
|
+
}
|
|
48
|
+
return results;
|
|
49
|
+
}
|
|
50
|
+
export function loadMultiplePackages(projectRoot, prodOnly, maxDepth) {
|
|
51
|
+
const lockfiles = findNestedLockfiles(projectRoot, maxDepth);
|
|
52
|
+
return lockfiles.map(lockfilePath => {
|
|
53
|
+
const subfolderAbs = path.dirname(lockfilePath);
|
|
54
|
+
const subfolder = path.relative(projectRoot, subfolderAbs);
|
|
55
|
+
const scanInput = loadPackages(subfolderAbs, prodOnly, maxDepth);
|
|
56
|
+
return { scanInput, subfolder };
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -219,6 +219,10 @@ function normalizePnpmDepRefV9(depName, depRef) {
|
|
|
219
219
|
return null;
|
|
220
220
|
if (looksLikeVersion(cleaned))
|
|
221
221
|
return `${depName}@${cleaned}`;
|
|
222
|
+
// Handle aliased deps where the ref is a full "pkgName@version" key (e.g. '@remix-run/dev': '@vercel/remix-run-dev@1.16.1')
|
|
223
|
+
const atIdx = cleaned.lastIndexOf("@");
|
|
224
|
+
if (atIdx > 0 && looksLikeVersion(cleaned.slice(atIdx + 1)))
|
|
225
|
+
return cleaned;
|
|
222
226
|
}
|
|
223
227
|
if (depRef && typeof depRef === "object") {
|
|
224
228
|
const version = depRef.version ?? depRef.specifier;
|
|
@@ -71,13 +71,26 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
71
71
|
];
|
|
72
72
|
for (const finding of orderedFindings) {
|
|
73
73
|
const urgent = finding.severity === "critical" || finding.severity === "high";
|
|
74
|
-
if (finding.relationship === "direct"
|
|
74
|
+
if (finding.relationship === "direct") {
|
|
75
75
|
const hasValidatedField = Object.prototype.hasOwnProperty.call(finding, "validatedFirstFixedVersion");
|
|
76
76
|
const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
|
|
77
77
|
// In offline mode validateDirectFixTargets never runs, so an unset
|
|
78
78
|
// validatedFirstFixedVersion is "did not validate" rather than "validation
|
|
79
79
|
// failed" — fall back to the advisory hint instead of dropping the target.
|
|
80
80
|
const validatedFieldOk = offline || !hasValidatedField || finding.validatedFirstFixedVersion !== null;
|
|
81
|
+
const allVersionsVulnerable = finding.validatedTargetScannedVersions != null &&
|
|
82
|
+
finding.validatedTargetKnownVulnerableVersions != null &&
|
|
83
|
+
finding.validatedTargetScannedVersions > 0 &&
|
|
84
|
+
finding.validatedTargetKnownVulnerableVersions >= finding.validatedTargetScannedVersions;
|
|
85
|
+
if (directTarget && allVersionsVulnerable) {
|
|
86
|
+
skippedByKey.set(`direct:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
87
|
+
package: finding.pkg.name,
|
|
88
|
+
version: finding.pkg.version,
|
|
89
|
+
relationship: finding.relationship,
|
|
90
|
+
reason: `No safe upgrade: all ${finding.validatedTargetScannedVersions} scanned ${pluralize(finding.validatedTargetScannedVersions ?? 0, "version")} are still known-vulnerable. This package may be unmaintained.`,
|
|
91
|
+
});
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
81
94
|
if (directTarget &&
|
|
82
95
|
isUpgradeTarget(finding.pkg.version, directTarget) &&
|
|
83
96
|
validatedFieldOk) {
|
|
@@ -132,6 +145,15 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
132
145
|
}
|
|
133
146
|
continue;
|
|
134
147
|
}
|
|
148
|
+
if (finding.relationship === "unknown") {
|
|
149
|
+
skippedByKey.set(`unknown:${finding.pkg.name}@${finding.pkg.version}`, {
|
|
150
|
+
package: finding.pkg.name,
|
|
151
|
+
version: finding.pkg.version,
|
|
152
|
+
relationship: finding.relationship,
|
|
153
|
+
reason: `Dependency path for ${finding.pkg.name}@${finding.pkg.version} could not be resolved in the lockfile. Inspect your lockfile to identify which package pulls it in.`,
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
135
157
|
if (packageManager === "npm" &&
|
|
136
158
|
finding.relationship === "transitive" &&
|
|
137
159
|
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
@@ -337,8 +359,14 @@ export function findSuggestedCommandForFinding(plan, finding) {
|
|
|
337
359
|
(item.package === finding.recommendedNpmTransitiveRemediation.package || item.package === finding.pkg.name));
|
|
338
360
|
}
|
|
339
361
|
if (finding.recommendedParentUpgrade) {
|
|
340
|
-
|
|
341
|
-
|
|
362
|
+
if (item.package !== finding.recommendedParentUpgrade.package)
|
|
363
|
+
return false;
|
|
364
|
+
if (item.targetVersion === finding.recommendedParentUpgrade.targetVersion)
|
|
365
|
+
return true;
|
|
366
|
+
// A command targeting a higher version also satisfies a lower-version recommendation
|
|
367
|
+
return (looksLikeVersion(item.targetVersion) &&
|
|
368
|
+
looksLikeVersion(finding.recommendedParentUpgrade.targetVersion) &&
|
|
369
|
+
compareVersions(item.targetVersion, finding.recommendedParentUpgrade.targetVersion) >= 0);
|
|
342
370
|
}
|
|
343
371
|
const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
|
|
344
372
|
return item.package === finding.pkg.name && item.targetVersion === directTarget;
|
|
@@ -399,7 +427,11 @@ function upsertTarget(targetsByPackage, next) {
|
|
|
399
427
|
if (compareVersions(next.targetVersion, existing.targetVersion) > 0) {
|
|
400
428
|
merged.targetVersion = next.targetVersion;
|
|
401
429
|
merged.currentVersion = next.currentVersion ?? merged.currentVersion;
|
|
402
|
-
|
|
430
|
+
// Keep the reason from the higher-severity finding so the most critical issue is visible
|
|
431
|
+
// in the context column. Only update the reason if next has strictly higher severity.
|
|
432
|
+
if (severityOrder[next.severity] > severityOrder[existing.severity]) {
|
|
433
|
+
merged.reason = next.reason;
|
|
434
|
+
}
|
|
403
435
|
merged.scannedVersions = next.scannedVersions ?? merged.scannedVersions ?? null;
|
|
404
436
|
merged.knownVulnerableVersions = next.knownVulnerableVersions ?? merged.knownVulnerableVersions ?? null;
|
|
405
437
|
}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
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
|
+
let registryDebugLog;
|
|
5
|
+
let registryBatchCounter = 0;
|
|
6
|
+
export function configureNpmRegistryDebug(debugLog) {
|
|
7
|
+
registryDebugLog = debugLog;
|
|
8
|
+
registryBatchCounter = 0;
|
|
9
|
+
}
|
|
10
|
+
function nextRegistryBatchId() {
|
|
11
|
+
registryBatchCounter += 1;
|
|
12
|
+
return `r-${String(registryBatchCounter).padStart(2, "0")}`;
|
|
13
|
+
}
|
|
4
14
|
export function clearPackumentCache() {
|
|
5
15
|
packumentCache.clear();
|
|
6
16
|
}
|
|
@@ -11,17 +21,50 @@ export async function fetchPackument(packageName) {
|
|
|
11
21
|
const url = `${DEFAULT_NPM_REGISTRY_URL}/${encodeURIComponent(packageName)
|
|
12
22
|
.replace(/%40/g, "@")
|
|
13
23
|
.replace(/%2F/g, "/")}`;
|
|
24
|
+
const batchId = registryDebugLog ? nextRegistryBatchId() : "";
|
|
25
|
+
const startedAt = registryDebugLog ? Date.now() : 0;
|
|
26
|
+
if (registryDebugLog) {
|
|
27
|
+
registryDebugLog("Registry fetch", { batchId, package: packageName, url });
|
|
28
|
+
}
|
|
14
29
|
try {
|
|
15
30
|
const response = await fetch(url);
|
|
16
31
|
if (!response.ok) {
|
|
32
|
+
if (registryDebugLog) {
|
|
33
|
+
registryDebugLog("Registry response", {
|
|
34
|
+
batchId,
|
|
35
|
+
package: packageName,
|
|
36
|
+
status: response.status,
|
|
37
|
+
durationMs: Date.now() - startedAt,
|
|
38
|
+
versionsCount: 0,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
17
41
|
packumentCache.set(packageName, null);
|
|
18
42
|
return null;
|
|
19
43
|
}
|
|
20
44
|
const json = (await response.json());
|
|
45
|
+
if (registryDebugLog) {
|
|
46
|
+
registryDebugLog("Registry response", {
|
|
47
|
+
batchId,
|
|
48
|
+
package: packageName,
|
|
49
|
+
status: response.status,
|
|
50
|
+
durationMs: Date.now() - startedAt,
|
|
51
|
+
versionsCount: Object.keys(json.versions ?? {}).length,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
21
54
|
packumentCache.set(packageName, json);
|
|
22
55
|
return json;
|
|
23
56
|
}
|
|
24
|
-
catch {
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (registryDebugLog) {
|
|
59
|
+
registryDebugLog("Registry fetch failed", {
|
|
60
|
+
batchId,
|
|
61
|
+
package: packageName,
|
|
62
|
+
durationMs: Date.now() - startedAt,
|
|
63
|
+
error: error instanceof Error
|
|
64
|
+
? { message: error.message, stack: error.stack }
|
|
65
|
+
: String(error),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
25
68
|
packumentCache.set(packageName, null);
|
|
26
69
|
return null;
|
|
27
70
|
}
|
|
@@ -28,11 +28,17 @@ export async function resolveRecommendedParentUpgrade(finding, packages, directD
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
// Best-effort fallback for deeper chains.
|
|
31
|
+
// Use the immediate child of the direct parent (not the immediate parent of the vulnerable
|
|
32
|
+
// package) — that's what the direct parent actually lists in its own dependencies.
|
|
33
|
+
const directParentIdx = viaPath.indexOf(directParentName);
|
|
34
|
+
if (directParentIdx < 0)
|
|
35
|
+
return null;
|
|
36
|
+
const immediateChildName = viaPath[directParentIdx + 1] ?? immediateParentName;
|
|
31
37
|
return findUpgradeForImmediateIntermediate({
|
|
32
38
|
directParentName,
|
|
33
39
|
directParentVersion: directParent.version,
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
immediateChildName,
|
|
41
|
+
immediateChildInstalledVersion: findPackageVersion(packages, immediateChildName, viaPath.slice(0, directParentIdx + 2)) ?? "",
|
|
36
42
|
vulnerableName,
|
|
37
43
|
viaPath,
|
|
38
44
|
});
|
|
@@ -123,7 +129,7 @@ async function findUpgradeForExactDirectChild(args) {
|
|
|
123
129
|
return null;
|
|
124
130
|
}
|
|
125
131
|
async function findUpgradeForImmediateIntermediate(args) {
|
|
126
|
-
if (!args.
|
|
132
|
+
if (!args.immediateChildInstalledVersion || !looksLikeVersion(args.immediateChildInstalledVersion)) {
|
|
127
133
|
return null;
|
|
128
134
|
}
|
|
129
135
|
const packument = await fetchPackument(args.directParentName);
|
|
@@ -134,12 +140,12 @@ async function findUpgradeForImmediateIntermediate(args) {
|
|
|
134
140
|
.sort(compareVersions);
|
|
135
141
|
for (const version of versions) {
|
|
136
142
|
const manifest = packument?.versions?.[version];
|
|
137
|
-
const depRange = manifest?.dependencies?.[args.
|
|
138
|
-
manifest?.optionalDependencies?.[args.
|
|
143
|
+
const depRange = manifest?.dependencies?.[args.immediateChildName] ??
|
|
144
|
+
manifest?.optionalDependencies?.[args.immediateChildName];
|
|
139
145
|
if (!depRange)
|
|
140
146
|
continue;
|
|
141
|
-
const
|
|
142
|
-
if (!
|
|
147
|
+
const stillAllowsInstalledChild = versionSatisfiesRange(args.immediateChildInstalledVersion, depRange);
|
|
148
|
+
if (!stillAllowsInstalledChild) {
|
|
143
149
|
return {
|
|
144
150
|
package: args.directParentName,
|
|
145
151
|
currentVersion: args.directParentVersion,
|
|
@@ -147,7 +153,7 @@ async function findUpgradeForImmediateIntermediate(args) {
|
|
|
147
153
|
viaPath: args.viaPath,
|
|
148
154
|
vulnerablePackage: args.vulnerableName,
|
|
149
155
|
confidence: "best-effort",
|
|
150
|
-
reason: `${args.directParentName}@${version} no longer allows ${args.
|
|
156
|
+
reason: `${args.directParentName}@${version} no longer allows ${args.immediateChildName}@${args.immediateChildInstalledVersion} in the current path`,
|
|
151
157
|
};
|
|
152
158
|
}
|
|
153
159
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { loadMultiplePackages } from "../parsers/multi-package.js";
|
|
4
|
+
import { scanPackages, buildCoverageNotes } from "../scanner.js";
|
|
5
|
+
import { sortFindingsForOutput } from "../output/formatters.js";
|
|
6
|
+
import { normalizeSeverity } from "../osv/severity.js";
|
|
7
|
+
import { selectFindingsForTable } from "../output/finding-display.js";
|
|
8
|
+
import { buildSuggestedFixCommandPlan } from "../remediation/fix-commands.js";
|
|
9
|
+
import { readDirectDependencyNames } from "../utils/package-json.js";
|
|
10
|
+
import { DEFAULT_SEARCH_DEPTH, severityOrder } from "../constants.js";
|
|
11
|
+
import { printMultiFolderResults } from "../output/multi-folder-printer.js";
|
|
12
|
+
import { writeMultiFolderHtmlReport } from "../output/multi-folder-html-reporter.js";
|
|
13
|
+
import { chalk } from "../utils/chalk.js";
|
|
14
|
+
import { getCliVersion } from "../utils/version-info.js";
|
|
15
|
+
export async function runMultiFolderScan(params) {
|
|
16
|
+
const searchDepth = Math.max(0, Number(params.options.searchDepth || DEFAULT_SEARCH_DEPTH));
|
|
17
|
+
const folders = loadMultiplePackages(params.projectRoot, !!params.options.prodOnly, searchDepth);
|
|
18
|
+
const offline = !!params.options.offline || !!params.options.offlineDb;
|
|
19
|
+
const results = [];
|
|
20
|
+
for (const { scanInput, subfolder } of folders) {
|
|
21
|
+
if (scanInput.packages.length === 0)
|
|
22
|
+
continue;
|
|
23
|
+
if (!params.options.json) {
|
|
24
|
+
process.stdout.write(`\n${chalk.bold.cyan(`📁 ${subfolder}/`)}\n`);
|
|
25
|
+
}
|
|
26
|
+
const subfolderAbs = path.join(params.projectRoot, subfolder);
|
|
27
|
+
const directDependencyNames = readDirectDependencyNames(subfolderAbs, !!params.options.prodOnly);
|
|
28
|
+
const findings = await scanPackages(scanInput.packages, params.batchSize, params.options, {
|
|
29
|
+
directDependencyNames,
|
|
30
|
+
scanSource: scanInput.source,
|
|
31
|
+
scanFilePath: scanInput.filePath,
|
|
32
|
+
});
|
|
33
|
+
const sorted = sortFindingsForOutput(findings);
|
|
34
|
+
const coverage = buildCoverageNotes(scanInput, offline);
|
|
35
|
+
const minSeverity = normalizeSeverity(params.options.minSeverity || "medium");
|
|
36
|
+
const tableFindings = params.options.all ? sorted : selectFindingsForTable(sorted, minSeverity);
|
|
37
|
+
const suggestedFixCommands = buildSuggestedFixCommandPlan(sorted, scanInput, { offline, subfolder });
|
|
38
|
+
results.push({
|
|
39
|
+
subfolder,
|
|
40
|
+
scanInput,
|
|
41
|
+
sorted,
|
|
42
|
+
suggestedFixCommands,
|
|
43
|
+
coverage,
|
|
44
|
+
minSeverity,
|
|
45
|
+
tableFindings,
|
|
46
|
+
allPackages: scanInput.packages,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return results;
|
|
50
|
+
}
|
|
51
|
+
export async function handleMultiFolderScan(params) {
|
|
52
|
+
const results = await runMultiFolderScan(params);
|
|
53
|
+
if (results.length === 0) {
|
|
54
|
+
console.log(chalk.yellow("No scannable packages found in any subfolder."));
|
|
55
|
+
process.exit(0);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (params.options.fix) {
|
|
59
|
+
console.error(chalk.yellow("--fix is not yet supported in multi-folder mode. Run cve-lite . from each subfolder individually."));
|
|
60
|
+
process.exit(1);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (params.options.sarif || params.options.cdx) {
|
|
64
|
+
console.error(chalk.yellow("--sarif and --cdx are not yet supported in multi-folder mode."));
|
|
65
|
+
process.exit(1);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (params.options.json) {
|
|
69
|
+
const { serializeFinding } = await import("../output/formatters.js");
|
|
70
|
+
const allFindings = results.flatMap(r => r.sorted.map(f => ({ ...serializeFinding(f, r.suggestedFixCommands), subfolder: r.subfolder })));
|
|
71
|
+
console.log(JSON.stringify({
|
|
72
|
+
multiFolder: true,
|
|
73
|
+
folders: results.map(r => r.subfolder),
|
|
74
|
+
findings: allFindings,
|
|
75
|
+
scannedAt: new Date().toISOString(),
|
|
76
|
+
}, null, 2));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
printMultiFolderResults(results, params.options);
|
|
80
|
+
}
|
|
81
|
+
if (params.options.report) {
|
|
82
|
+
const outputDir = path.resolve(typeof params.options.report === "string" ? params.options.report : "./cve-report");
|
|
83
|
+
const cliVersion = getCliVersion();
|
|
84
|
+
const { reportPath } = await writeMultiFolderHtmlReport({
|
|
85
|
+
outputDir,
|
|
86
|
+
results,
|
|
87
|
+
projectPath: params.projectRoot,
|
|
88
|
+
cliVersion,
|
|
89
|
+
autoOpen: !params.options.noOpen,
|
|
90
|
+
});
|
|
91
|
+
console.log(`${chalk.gray("Report:")} ${chalk.cyan(reportPath)}`);
|
|
92
|
+
}
|
|
93
|
+
const failLevel = normalizeSeverity(params.options.failOn);
|
|
94
|
+
const allSorted = results.flatMap(r => r.sorted);
|
|
95
|
+
const shouldFail = allSorted.some(f => severityOrder[f.severity] >= severityOrder[failLevel]);
|
|
96
|
+
process.exit(shouldFail ? 1 : 0);
|
|
97
|
+
}
|
package/dist/scanner.js
CHANGED
|
@@ -8,7 +8,8 @@ import { LocalAdvisorySource } from "./advisory/local-advisory-source.js";
|
|
|
8
8
|
import { LocalAdvisoryDatabase } from "./advisory/local-db.js";
|
|
9
9
|
import { ADVISORY_DB_STALE_AFTER_MS, getDefaultAdvisoryDbPath } from "./advisory/osv-sync.js";
|
|
10
10
|
import { resolveRecommendedParentUpgrade } from "./remediation/parent-upgrade.js";
|
|
11
|
-
import { resolveLowestKnownNonVulnerableVersion, resolvePublishedFixVersion } from "./remediation/npm-registry.js";
|
|
11
|
+
import { configureNpmRegistryDebug, resolveLowestKnownNonVulnerableVersion, resolvePublishedFixVersion, } from "./remediation/npm-registry.js";
|
|
12
|
+
import { countBySeverity } from "./utils/severity.js";
|
|
12
13
|
import { resolveNpmTransitiveRemediation, resolveTransitiveRemediationViaRegistry } from "./remediation/npm-transitive-resolution.js";
|
|
13
14
|
import { loadNpmLockGraph } from "./parsers/npm-lock-graph.js";
|
|
14
15
|
import { buildPnpmWorkspaceMap } from "./parsers/pnpm-lock.js";
|
|
@@ -30,7 +31,7 @@ export function createAdvisorySource(options) {
|
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
return {
|
|
33
|
-
advisorySource: new OsvAdvisorySource(options?.osvUrl),
|
|
34
|
+
advisorySource: new OsvAdvisorySource(options?.osvUrl, options?.debugLog),
|
|
34
35
|
offline: false,
|
|
35
36
|
sourceLabel: options?.osvUrl
|
|
36
37
|
? `custom OSV endpoint (${options.osvUrl})`
|
|
@@ -43,11 +44,13 @@ export function createAdvisorySource(options) {
|
|
|
43
44
|
function getPackageCacheKey(pkg) {
|
|
44
45
|
return `${pkg.ecosystem}:${pkg.name}@${pkg.version}`;
|
|
45
46
|
}
|
|
46
|
-
export async function scanPackages(packages, batchSize, options, context) {
|
|
47
|
+
export async function scanPackages(packages, batchSize, options, context, debugLog) {
|
|
48
|
+
const log = debugLog ?? (() => { });
|
|
47
49
|
const sourceContext = createAdvisorySource({
|
|
48
50
|
osvUrl: options.osvUrl,
|
|
49
51
|
offline: options.offline,
|
|
50
52
|
offlineDb: options.offlineDb,
|
|
53
|
+
debugLog: log,
|
|
51
54
|
});
|
|
52
55
|
const offline = sourceContext.offline;
|
|
53
56
|
const cacheDirOverride = options.cacheDir;
|
|
@@ -55,28 +58,43 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
55
58
|
? "Scanning dependencies against the local advisory database..."
|
|
56
59
|
: "Scanning dependencies against OSV...", options);
|
|
57
60
|
const advisorySource = sourceContext.advisorySource;
|
|
58
|
-
|
|
61
|
+
if (!offline && options.debug) {
|
|
62
|
+
configureNpmRegistryDebug(log);
|
|
63
|
+
}
|
|
64
|
+
const cache = loadCache(cacheDirOverride, options.debug ? log : undefined);
|
|
59
65
|
try {
|
|
60
66
|
const results = [];
|
|
61
67
|
const uncachedPackages = [];
|
|
62
68
|
if (!offline) {
|
|
63
69
|
const nowMs = Date.now();
|
|
70
|
+
let hits = 0;
|
|
71
|
+
let misses = 0;
|
|
64
72
|
for (const pkg of packages) {
|
|
65
73
|
const cacheKey = getPackageCacheKey(pkg);
|
|
66
74
|
if (!options.noCache) {
|
|
67
75
|
const cached = cache.queryEntries[cacheKey];
|
|
68
76
|
if (cached && !isEntryStale(cached, nowMs)) {
|
|
77
|
+
hits += 1;
|
|
69
78
|
if (cached.vulnIds.length > 0) {
|
|
70
79
|
results.push({ pkg, vulnIds: cached.vulnIds });
|
|
71
80
|
}
|
|
72
81
|
continue;
|
|
73
82
|
}
|
|
74
83
|
}
|
|
84
|
+
misses += 1;
|
|
75
85
|
uncachedPackages.push(pkg);
|
|
76
86
|
}
|
|
77
87
|
const chunks = chunk(uncachedPackages, batchSize);
|
|
88
|
+
const reason = options.noCache ? "no-cache mode" : "stale or missing entry";
|
|
89
|
+
log("Cache check", { hits, misses, reason, uncachedBatches: chunks.length });
|
|
78
90
|
spinner.update(`Scanning OSV in ${chunks.length} parallel ${pluralize(chunks.length, "batch", "batches")}...`);
|
|
79
|
-
const
|
|
91
|
+
const osvScanStartedAt = Date.now();
|
|
92
|
+
let batchCounter = 0;
|
|
93
|
+
const allAdvisoryResults = await runWithConcurrency(chunks, 5, c => {
|
|
94
|
+
batchCounter += 1;
|
|
95
|
+
const batchId = `b-${String(batchCounter).padStart(2, "0")}`;
|
|
96
|
+
return advisorySource.queryBatch(c, { batchId });
|
|
97
|
+
});
|
|
80
98
|
for (let i = 0; i < chunks.length; i++) {
|
|
81
99
|
const chunkItems = chunks[i];
|
|
82
100
|
const rows = allAdvisoryResults[i] ?? [];
|
|
@@ -90,6 +108,13 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
90
108
|
}
|
|
91
109
|
}
|
|
92
110
|
}
|
|
111
|
+
if (chunks.length > 0) {
|
|
112
|
+
log("OSV scan complete", {
|
|
113
|
+
totalBatches: chunks.length,
|
|
114
|
+
totalDurationMs: Date.now() - osvScanStartedAt,
|
|
115
|
+
packagesWithVulns: results.length,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
93
118
|
if (chunks.length === 0) {
|
|
94
119
|
spinner.succeed("Loaded package matches from cache");
|
|
95
120
|
}
|
|
@@ -98,7 +123,7 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
98
123
|
}
|
|
99
124
|
}
|
|
100
125
|
else {
|
|
101
|
-
const advisoryResult = await advisorySource.queryBatch(packages);
|
|
126
|
+
const advisoryResult = await advisorySource.queryBatch(packages, { batchId: "offline" });
|
|
102
127
|
const rows = advisoryResult ?? [];
|
|
103
128
|
for (let i = 0; i < packages.length; i++) {
|
|
104
129
|
const pkg = packages[i];
|
|
@@ -165,7 +190,7 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
165
190
|
}
|
|
166
191
|
}
|
|
167
192
|
if (!offline && idSet.size > 0) {
|
|
168
|
-
saveCache(cache, cacheDirOverride);
|
|
193
|
+
saveCache(cache, cacheDirOverride, options.debug ? log : undefined);
|
|
169
194
|
}
|
|
170
195
|
const findings = results.map(result => {
|
|
171
196
|
const vulnerabilities = result.vulnIds
|
|
@@ -193,35 +218,59 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
193
218
|
};
|
|
194
219
|
});
|
|
195
220
|
const npmTransitiveGraph = context?.scanSource === "package-lock" && context.scanFilePath
|
|
196
|
-
? createNpmTransitiveGraphFromLockfile(context.scanFilePath)
|
|
221
|
+
? createNpmTransitiveGraphFromLockfile(context.scanFilePath, log, packages.length)
|
|
197
222
|
: null;
|
|
198
223
|
const npmWorkspaceMap = (() => {
|
|
199
224
|
try {
|
|
200
225
|
return context?.scanSource === "package-lock" && context.scanFilePath
|
|
201
226
|
? buildNpmWorkspaceMap(context.scanFilePath) : null;
|
|
202
227
|
}
|
|
203
|
-
catch {
|
|
228
|
+
catch (error) {
|
|
229
|
+
log("Workspace map", {
|
|
230
|
+
type: "npm",
|
|
231
|
+
status: "failed",
|
|
232
|
+
error: error instanceof Error ? error.message : String(error),
|
|
233
|
+
});
|
|
204
234
|
return null;
|
|
205
235
|
}
|
|
206
236
|
})();
|
|
237
|
+
if (npmWorkspaceMap) {
|
|
238
|
+
log("Workspace map", { type: "npm", workspaces: npmWorkspaceMap.size });
|
|
239
|
+
}
|
|
207
240
|
const pnpmWorkspaceMap = (() => {
|
|
208
241
|
try {
|
|
209
242
|
return context?.scanSource === "pnpm-lock" && context.scanFilePath
|
|
210
243
|
? buildPnpmWorkspaceMap(context.scanFilePath) : null;
|
|
211
244
|
}
|
|
212
|
-
catch {
|
|
245
|
+
catch (error) {
|
|
246
|
+
log("Workspace map", {
|
|
247
|
+
type: "pnpm",
|
|
248
|
+
status: "failed",
|
|
249
|
+
error: error instanceof Error ? error.message : String(error),
|
|
250
|
+
});
|
|
213
251
|
return null;
|
|
214
252
|
}
|
|
215
253
|
})();
|
|
254
|
+
if (pnpmWorkspaceMap) {
|
|
255
|
+
log("Workspace map", { type: "pnpm", workspaces: pnpmWorkspaceMap.size });
|
|
256
|
+
}
|
|
216
257
|
const bunWorkspaceMap = (() => {
|
|
217
258
|
try {
|
|
218
259
|
return context?.scanSource === "bun-lock" && context.scanFilePath
|
|
219
260
|
? buildBunWorkspaceMap(context.scanFilePath) : null;
|
|
220
261
|
}
|
|
221
|
-
catch {
|
|
262
|
+
catch (error) {
|
|
263
|
+
log("Workspace map", {
|
|
264
|
+
type: "bun",
|
|
265
|
+
status: "failed",
|
|
266
|
+
error: error instanceof Error ? error.message : String(error),
|
|
267
|
+
});
|
|
222
268
|
return null;
|
|
223
269
|
}
|
|
224
270
|
})();
|
|
271
|
+
if (bunWorkspaceMap) {
|
|
272
|
+
log("Workspace map", { type: "bun", workspaces: bunWorkspaceMap.size });
|
|
273
|
+
}
|
|
225
274
|
const lockfileWorkspaceMap = pnpmWorkspaceMap ?? bunWorkspaceMap ?? null;
|
|
226
275
|
const npmRemediationCache = new Map();
|
|
227
276
|
const parentUpgradeCache = new Map();
|
|
@@ -239,7 +288,7 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
239
288
|
if (!offline) {
|
|
240
289
|
await validateDirectFixTargets(findings, (finding) => {
|
|
241
290
|
updateAnalysisProgress("validating fix target for", `${finding.pkg.name}@${finding.pkg.version}`);
|
|
242
|
-
});
|
|
291
|
+
}, log);
|
|
243
292
|
}
|
|
244
293
|
for (const finding of findings) {
|
|
245
294
|
if (finding.relationship !== "transitive")
|
|
@@ -326,8 +375,19 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
326
375
|
}
|
|
327
376
|
}
|
|
328
377
|
else if (!offline) {
|
|
329
|
-
await validateDirectFixTargets(findings);
|
|
378
|
+
await validateDirectFixTargets(findings, undefined, log);
|
|
330
379
|
}
|
|
380
|
+
const severityCounts = countBySeverity(findings);
|
|
381
|
+
log("Findings classified", {
|
|
382
|
+
total: findings.length,
|
|
383
|
+
direct: findings.filter(finding => finding.relationship === "direct").length,
|
|
384
|
+
transitive: findings.filter(finding => finding.relationship === "transitive").length,
|
|
385
|
+
critical: severityCounts.critical,
|
|
386
|
+
high: severityCounts.high,
|
|
387
|
+
medium: severityCounts.medium,
|
|
388
|
+
low: severityCounts.low,
|
|
389
|
+
unknown: severityCounts.unknown,
|
|
390
|
+
});
|
|
331
391
|
return findings;
|
|
332
392
|
}
|
|
333
393
|
catch (error) {
|
|
@@ -338,9 +398,10 @@ export async function scanPackages(packages, batchSize, options, context) {
|
|
|
338
398
|
sourceContext.cleanup();
|
|
339
399
|
}
|
|
340
400
|
}
|
|
341
|
-
function createNpmTransitiveGraphFromLockfile(filePath) {
|
|
401
|
+
function createNpmTransitiveGraphFromLockfile(filePath, log, packageCount) {
|
|
342
402
|
try {
|
|
343
403
|
const lockGraph = loadNpmLockGraph(filePath, { includePaths: false });
|
|
404
|
+
log("npm transitive graph", { status: "built", nodes: packageCount });
|
|
344
405
|
return {
|
|
345
406
|
nodeIdsFor(name, version) {
|
|
346
407
|
return lockGraph.nodeIdsFor(name, version);
|
|
@@ -359,14 +420,26 @@ function createNpmTransitiveGraphFromLockfile(filePath) {
|
|
|
359
420
|
},
|
|
360
421
|
};
|
|
361
422
|
}
|
|
362
|
-
catch {
|
|
423
|
+
catch (error) {
|
|
424
|
+
log("npm transitive graph", {
|
|
425
|
+
status: "failed",
|
|
426
|
+
error: error instanceof Error ? error.message : String(error),
|
|
427
|
+
});
|
|
363
428
|
return null;
|
|
364
429
|
}
|
|
365
430
|
}
|
|
366
|
-
async function validateDirectFixTargets(findings, onFinding) {
|
|
431
|
+
async function validateDirectFixTargets(findings, onFinding, debugLog) {
|
|
367
432
|
const directCandidates = findings.filter(finding => finding.vulnerabilities.length > 0);
|
|
368
433
|
for (const finding of directCandidates) {
|
|
369
434
|
onFinding?.(finding);
|
|
435
|
+
const logFixValidation = () => {
|
|
436
|
+
debugLog?.("Fix validation", {
|
|
437
|
+
package: finding.pkg.name,
|
|
438
|
+
installed: finding.pkg.version,
|
|
439
|
+
resolvedFixVersion: finding.validatedFirstFixedVersion,
|
|
440
|
+
verified: Boolean(finding.validatedFirstFixedVersion),
|
|
441
|
+
});
|
|
442
|
+
};
|
|
370
443
|
const lowestKnownResolution = await resolveLowestKnownNonVulnerableVersion(finding.pkg.name, finding.pkg.version, finding.vulnerabilities);
|
|
371
444
|
const fixedVersionHint = finding.firstFixedVersion;
|
|
372
445
|
if (lowestKnownResolution.resolvedVersion) {
|
|
@@ -375,16 +448,19 @@ async function validateDirectFixTargets(findings, onFinding) {
|
|
|
375
448
|
finding.validatedTargetKnownVulnerableVersions = lowestKnownResolution.candidatesKnownVulnerable;
|
|
376
449
|
if (!fixedVersionHint || fixedVersionHint === lowestKnownResolution.resolvedVersion) {
|
|
377
450
|
finding.fixVersionValidationNote = null;
|
|
451
|
+
logFixValidation();
|
|
378
452
|
continue;
|
|
379
453
|
}
|
|
380
454
|
const hintResolution = await resolvePublishedFixVersion(finding.pkg.name, fixedVersionHint);
|
|
381
455
|
if (hintResolution.resolvedVersion === lowestKnownResolution.resolvedVersion &&
|
|
382
456
|
hintResolution.note) {
|
|
383
457
|
finding.fixVersionValidationNote = hintResolution.note;
|
|
458
|
+
logFixValidation();
|
|
384
459
|
continue;
|
|
385
460
|
}
|
|
386
461
|
finding.fixVersionValidationNote =
|
|
387
462
|
`Advisory fixed-version hint ${fixedVersionHint} is still known vulnerable for ${finding.pkg.name}; scanned ${lowestKnownResolution.candidatesChecked} package ${pluralize(lowestKnownResolution.candidatesChecked, "version")} above current version (${lowestKnownResolution.candidatesKnownVulnerable} still known vulnerable); using lowest known non-vulnerable version ${lowestKnownResolution.resolvedVersion}.`;
|
|
463
|
+
logFixValidation();
|
|
388
464
|
continue;
|
|
389
465
|
}
|
|
390
466
|
if (!fixedVersionHint) {
|
|
@@ -392,6 +468,7 @@ async function validateDirectFixTargets(findings, onFinding) {
|
|
|
392
468
|
finding.fixVersionValidationNote = lowestKnownResolution.note;
|
|
393
469
|
finding.validatedTargetScannedVersions = null;
|
|
394
470
|
finding.validatedTargetKnownVulnerableVersions = null;
|
|
471
|
+
logFixValidation();
|
|
395
472
|
continue;
|
|
396
473
|
}
|
|
397
474
|
const resolution = await resolvePublishedFixVersion(finding.pkg.name, fixedVersionHint);
|
|
@@ -399,6 +476,7 @@ async function validateDirectFixTargets(findings, onFinding) {
|
|
|
399
476
|
finding.fixVersionValidationNote = resolution.note ?? lowestKnownResolution.note;
|
|
400
477
|
finding.validatedTargetScannedVersions = null;
|
|
401
478
|
finding.validatedTargetKnownVulnerableVersions = null;
|
|
479
|
+
logFixValidation();
|
|
402
480
|
}
|
|
403
481
|
}
|
|
404
482
|
function isAdvisoryDbStale(metadata) {
|