cve-lite-cli 1.28.0 → 1.29.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.
@@ -1,4 +1,5 @@
1
1
  import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
2
3
  const packumentCache = new Map();
3
4
  const DEFAULT_NPM_REGISTRY_URL = "https://registry.npmjs.org";
4
5
  let registryDebugLog;
@@ -11,75 +12,91 @@ function nextRegistryBatchId() {
11
12
  registryBatchCounter += 1;
12
13
  return `r-${String(registryBatchCounter).padStart(2, "0")}`;
13
14
  }
15
+ const observerStorage = new AsyncLocalStorage();
16
+ export async function runWithObserver(observer, fn) {
17
+ return observerStorage.run(observer, fn);
18
+ }
14
19
  export function clearPackumentCache() {
15
20
  packumentCache.clear();
16
21
  }
22
+ function notifyPackumentObserver(result, packageName) {
23
+ const observer = observerStorage.getStore();
24
+ if (result.status === "transient-failure") {
25
+ observer?.onTransientFailure?.(packageName);
26
+ }
27
+ else {
28
+ observer?.onResolved?.(packageName);
29
+ }
30
+ }
17
31
  export async function fetchPackument(packageName) {
18
32
  const cached = packumentCache.get(packageName);
19
- if (cached)
20
- return cached;
21
- const promise = (async () => {
22
- const url = `${DEFAULT_NPM_REGISTRY_URL}/${encodeURIComponent(packageName)
23
- .replace(/%40/g, "@")
24
- .replace(/%2F/g, "/")}`;
25
- const batchId = registryDebugLog ? nextRegistryBatchId() : "";
26
- const startedAt = registryDebugLog ? Date.now() : 0;
27
- if (registryDebugLog) {
28
- registryDebugLog("Registry fetch", { batchId, package: packageName, url });
33
+ let result;
34
+ if (cached) {
35
+ result = await cached;
36
+ }
37
+ else {
38
+ const promise = doFetchPackument(packageName);
39
+ packumentCache.set(packageName, promise);
40
+ result = await promise;
41
+ if (result.status === "transient-failure") {
42
+ // A later call retries the fetch instead of reusing a cached failure.
43
+ packumentCache.delete(packageName);
29
44
  }
30
- let transientFailure = false;
31
- try {
32
- const response = await fetch(url);
33
- if (!response.ok) {
34
- if (registryDebugLog) {
35
- registryDebugLog("Registry response", {
36
- batchId,
37
- package: packageName,
38
- status: response.status,
39
- durationMs: Date.now() - startedAt,
40
- versionsCount: 0,
41
- });
42
- }
43
- if (response.status === 404) {
44
- return null;
45
- }
46
- transientFailure = true;
47
- return null;
48
- }
49
- const json = (await response.json());
45
+ }
46
+ notifyPackumentObserver(result, packageName);
47
+ return result.packument;
48
+ }
49
+ async function doFetchPackument(packageName) {
50
+ const url = `${DEFAULT_NPM_REGISTRY_URL}/${encodeURIComponent(packageName)
51
+ .replace(/%40/g, "@")
52
+ .replace(/%2F/g, "/")}`;
53
+ const batchId = registryDebugLog ? nextRegistryBatchId() : "";
54
+ const startedAt = registryDebugLog ? Date.now() : 0;
55
+ if (registryDebugLog) {
56
+ registryDebugLog("Registry fetch", { batchId, package: packageName, url });
57
+ }
58
+ try {
59
+ const response = await fetch(url);
60
+ if (!response.ok) {
50
61
  if (registryDebugLog) {
51
62
  registryDebugLog("Registry response", {
52
63
  batchId,
53
64
  package: packageName,
54
65
  status: response.status,
55
66
  durationMs: Date.now() - startedAt,
56
- versionsCount: Object.keys(json.versions ?? {}).length,
67
+ versionsCount: 0,
57
68
  });
58
69
  }
59
- return json;
60
- }
61
- catch (error) {
62
- transientFailure = true;
63
- if (registryDebugLog) {
64
- registryDebugLog("Registry fetch failed", {
65
- batchId,
66
- package: packageName,
67
- durationMs: Date.now() - startedAt,
68
- error: error instanceof Error
69
- ? { message: error.message, stack: error.stack }
70
- : String(error),
71
- });
70
+ if (response.status === 404) {
71
+ return { status: "not-found", packument: null };
72
72
  }
73
- return null;
73
+ return { status: "transient-failure", packument: null };
74
74
  }
75
- finally {
76
- if (transientFailure) {
77
- packumentCache.delete(packageName);
78
- }
75
+ const json = (await response.json());
76
+ if (registryDebugLog) {
77
+ registryDebugLog("Registry response", {
78
+ batchId,
79
+ package: packageName,
80
+ status: response.status,
81
+ durationMs: Date.now() - startedAt,
82
+ versionsCount: Object.keys(json.versions ?? {}).length,
83
+ });
84
+ }
85
+ return { status: "resolved", packument: json };
86
+ }
87
+ catch (error) {
88
+ if (registryDebugLog) {
89
+ registryDebugLog("Registry fetch failed", {
90
+ batchId,
91
+ package: packageName,
92
+ durationMs: Date.now() - startedAt,
93
+ error: error instanceof Error
94
+ ? { message: error.message, stack: error.stack }
95
+ : String(error),
96
+ });
79
97
  }
80
- })();
81
- packumentCache.set(packageName, promise);
82
- return promise;
98
+ return { status: "transient-failure", packument: null };
99
+ }
83
100
  }
84
101
  export async function packageVersionExists(packageName, version) {
85
102
  const packument = await fetchPackument(packageName);
@@ -0,0 +1,49 @@
1
+ import { pluralize } from "../utils/string.js";
2
+ export function getCompletenessImpact(completeness) {
3
+ const diagnostics = completeness?.diagnostics ?? [];
4
+ return {
5
+ hasDetectionGap: diagnostics.some(diagnostic => diagnostic.impact === "detection"),
6
+ hasRemediationGap: diagnostics.some(diagnostic => diagnostic.impact === "remediation"),
7
+ };
8
+ }
9
+ export function formatDiagnosticMessage(code, count, affectedPackageCount) {
10
+ switch (code) {
11
+ case "OSV_DETAIL_TRANSIENT_FAILURE":
12
+ return `${count} advisory detail ${pluralize(count, "lookup")} failed with a transient error — re-run to retry.`;
13
+ case "OSV_DETAIL_OFFLINE_MISSING":
14
+ return `${count} advisory detail ${pluralize(count, "record")} ${pluralize(count, "is", "are")} missing from the local advisory database — refresh to sync.`;
15
+ case "OSV_DETAIL_CONFIRMED_MISSING":
16
+ return `${count} advisory detail ${pluralize(count, "record")} ${pluralize(count, "is", "are")} confirmed missing from the OSV API — affected findings lack detail data.`;
17
+ case "PACKUMENT_FETCH_FAILURE":
18
+ return `${count} packument ${pluralize(count, "fetch", "fetches")} failed — remediation guidance may be incomplete.`;
19
+ case "REMEDIATION_FAILURE":
20
+ if (affectedPackageCount !== undefined) {
21
+ return `${count} transitive remediation ${pluralize(count, "attempt")} failed across ${affectedPackageCount} ${pluralize(affectedPackageCount, "package")} — some fix recommendations may be missing.`;
22
+ }
23
+ return `${count} transitive remediation ${pluralize(count, "attempt")} failed — some fix recommendations may be missing.`;
24
+ case "CHAIN_RESOLUTION_FAILURE":
25
+ return `${count} chain resolution ${pluralize(count, "attempt")} failed — some validated upgrade chains may be missing.`;
26
+ default:
27
+ return `${count} unknown diagnostic occurred.`;
28
+ }
29
+ }
30
+ export function aggregateMultiFolderCompleteness(results) {
31
+ const deduped = new Map();
32
+ for (const result of results) {
33
+ for (const diagnostic of result.completeness.diagnostics) {
34
+ const existing = deduped.get(diagnostic.code);
35
+ if (existing) {
36
+ existing.count += diagnostic.count;
37
+ if (diagnostic.affectedPackageCount !== undefined) {
38
+ existing.affectedPackageCount =
39
+ (existing.affectedPackageCount ?? 0) + diagnostic.affectedPackageCount;
40
+ }
41
+ existing.message = formatDiagnosticMessage(diagnostic.code, existing.count, existing.affectedPackageCount);
42
+ }
43
+ else {
44
+ deduped.set(diagnostic.code, { ...diagnostic });
45
+ }
46
+ }
47
+ }
48
+ return { complete: deduped.size === 0, diagnostics: [...deduped.values()] };
49
+ }
@@ -1,9 +1,10 @@
1
+ import fs from "node:fs";
1
2
  import path from "node:path";
2
3
  import process from "node:process";
3
4
  import { EXIT_OK, EXIT_FINDINGS, EXIT_ERROR } from "../types.js";
4
5
  import { loadMultiplePackages } from "../parsers/multi-package.js";
5
6
  import { scanPackages, buildCoverageNotes } from "../scanner.js";
6
- import { sortFindingsForOutput } from "../output/formatters.js";
7
+ import { sortFindingsForOutput, serializeFinding, logInfo } from "../output/formatters.js";
7
8
  import { normalizeSeverity } from "../osv/severity.js";
8
9
  import { selectFindingsForTable } from "../output/finding-display.js";
9
10
  import { buildSuggestedFixCommandPlan } from "../remediation/fix-commands.js";
@@ -18,6 +19,10 @@ import { buildOverrideContext, audit } from "../overrides/index.js";
18
19
  import { NULL_AUDIT_LOG } from "../audit-log/index.js";
19
20
  import { detectDM001 } from "../maintenance/dm001-maintenance-risk.js";
20
21
  import { reachesFailOn } from "../utils/severity.js";
22
+ import { readBaseline, writeBaseline, filterNewFindings, ratchetOutcome } from "../utils/baseline.js";
23
+ import { pluralize } from "../utils/string.js";
24
+ import { aggregateMultiFolderCompleteness } from "./completeness.js";
25
+ import { scanProjectForPackageUsage } from "../usage/scanner.js";
21
26
  export async function runMultiFolderScan(params) {
22
27
  const searchDepth = Math.max(0, Number(params.options.searchDepth || DEFAULT_SEARCH_DEPTH));
23
28
  const folders = loadMultiplePackages(params.projectRoot, !!params.options.prodOnly, searchDepth);
@@ -26,36 +31,70 @@ export async function runMultiFolderScan(params) {
26
31
  for (const { scanInput, subfolder } of folders) {
27
32
  if (scanInput.packages.length === 0)
28
33
  continue;
29
- if (!params.options.json) {
34
+ if (!params.options.json && !params.options.ratchet) {
30
35
  process.stdout.write(`\n${chalk.bold.cyan(`📁 ${subfolder}/`)}\n`);
31
36
  }
32
37
  const subfolderAbs = path.join(params.projectRoot, subfolder);
33
38
  const directDependencyNames = readDirectDependencyNames(subfolderAbs, !!params.options.prodOnly);
34
- const findings = await scanPackages(scanInput.packages, params.batchSize, params.options, {
39
+ const { findings, completeness } = await scanPackages(scanInput.packages, params.batchSize, params.options, {
35
40
  directDependencyNames,
36
41
  scanSource: scanInput.source,
37
42
  scanFilePath: scanInput.filePath,
38
43
  }, undefined, params.fetchImpl);
39
- const sorted = sortFindingsForOutput(findings);
44
+ // Mirror single-folder scanProject: annotate / filter by source imports per subfolder.
45
+ if (params.options.usage) {
46
+ logInfo(`Scanning ${subfolder}/ source for usage hints...`, params.options);
47
+ const pkgNames = new Set(findings.map(f => f.pkg.name));
48
+ const usageData = scanProjectForPackageUsage(subfolderAbs, pkgNames);
49
+ for (const finding of findings) {
50
+ const files = usageData[finding.pkg.name];
51
+ if (files) {
52
+ finding.usage = {
53
+ imported: files.length > 0,
54
+ files,
55
+ };
56
+ }
57
+ }
58
+ }
59
+ let finalFindings = findings;
60
+ if (params.options.onlyUsed) {
61
+ finalFindings = finalFindings.filter(f => f.usage?.imported);
62
+ }
63
+ let sorted = sortFindingsForOutput(finalFindings);
64
+ let suppressedCount = 0;
65
+ // Non-ratchet: auto-apply an existing per-folder baseline before fix plan,
66
+ // table selection, and maintenance (mirrors single-folder index.ts).
67
+ // --ratchet keeps the full set so handleMultiFolderScan can save or gate.
68
+ if (!params.options.ratchet) {
69
+ const baseline = readBaseline(subfolderAbs);
70
+ if (baseline) {
71
+ const filtered = filterNewFindings(sorted, baseline);
72
+ sorted = filtered.newFindings;
73
+ suppressedCount = filtered.suppressedCount;
74
+ }
75
+ }
40
76
  const coverage = buildCoverageNotes(scanInput, offline);
41
77
  const minSeverity = normalizeSeverity(params.options.minSeverity || "medium");
42
78
  const tableFindings = params.options.all ? sorted : selectFindingsForTable(sorted, minSeverity);
43
79
  const suggestedFixCommands = buildSuggestedFixCommandPlan(sorted, scanInput, { offline, subfolder });
44
- // Override hygiene per folder, mirroring the single-folder --check-overrides path.
80
+ // Override hygiene is independent of the CVE ratchet/baseline. A --ratchet
81
+ // run skips the override audit entirely (same boundary as single-folder).
45
82
  let overrideFindings = [];
46
- if (params.options.checkOverrides) {
83
+ if (params.options.checkOverrides && !params.options.ratchet) {
47
84
  const overrideCtx = buildOverrideContext(subfolderAbs, {
48
- auditLog: NULL_AUDIT_LOG,
85
+ auditLog: params.auditLog ?? NULL_AUDIT_LOG,
49
86
  logger: { info: () => { }, warn: () => { }, error: () => { }, debug: () => { } },
50
87
  checkNetwork: !!params.options.checkNetwork,
51
88
  });
52
89
  const overrideAudit = await audit(overrideCtx, { checkNetwork: !!params.options.checkNetwork });
53
90
  overrideFindings = overrideAudit.findings;
54
91
  }
55
- // Maintenance risk per folder, mirroring the --check-overrides pattern above.
92
+ // Maintenance from the baseline-filtered set. Skipped under --ratchet because
93
+ // that path always early-exits on CVE save/gate (same as single-folder).
56
94
  let maintenanceFindings = [];
57
- if (params.options.checkMaintenance) {
58
- maintenanceFindings = await detectDM001(sorted, offline);
95
+ if (params.options.checkMaintenance && !params.options.ratchet) {
96
+ const directDependencies = scanInput.packages.filter(p => directDependencyNames?.has(p.name));
97
+ maintenanceFindings = await detectDM001(sorted, directDependencies, offline);
59
98
  }
60
99
  results.push({
61
100
  subfolder,
@@ -66,32 +105,128 @@ export async function runMultiFolderScan(params) {
66
105
  minSeverity,
67
106
  tableFindings,
68
107
  allPackages: scanInput.packages,
108
+ completeness,
69
109
  overrideFindings,
70
110
  maintenanceFindings,
111
+ suppressedCount,
71
112
  });
72
113
  }
73
114
  return results;
74
115
  }
116
+ /**
117
+ * Per-subfolder --ratchet: each folder gets its own `.cve-lite/baseline.json`.
118
+ * Folders without a baseline are saved; folders with a baseline are gated.
119
+ * Exit 1 if any gated folder has findings above its baseline.
120
+ */
121
+ function handleMultiFolderRatchet(results, projectRoot, options) {
122
+ let anyNewFindings = false;
123
+ for (const r of results) {
124
+ const subfolderAbs = path.join(projectRoot, r.subfolder);
125
+ const baseline = readBaseline(subfolderAbs);
126
+ const outcome = ratchetOutcome(baseline, r.sorted);
127
+ if (outcome.action === "save") {
128
+ writeBaseline(subfolderAbs, r.sorted);
129
+ const count = r.sorted.length;
130
+ console.log(chalk.green(`✓ ${r.subfolder}/: Baseline saved to .cve-lite/baseline.json with ${count} ${pluralize(count, "finding")}. Future scans will only report findings above this baseline.`));
131
+ continue;
132
+ }
133
+ const { newFindings, suppressedCount } = outcome;
134
+ const suppressedLabel = `${suppressedCount} existing ${pluralize(suppressedCount, "finding")} suppressed`;
135
+ if (newFindings.length === 0) {
136
+ console.log(chalk.green(`✓ ${r.subfolder}/: No new findings above baseline - ${suppressedLabel}`));
137
+ continue;
138
+ }
139
+ anyNewFindings = true;
140
+ console.log(chalk.red(`${r.subfolder}/: ${newFindings.length} new ${pluralize(newFindings.length, "finding")} above baseline - ${suppressedLabel}`));
141
+ for (const f of newFindings) {
142
+ const ids = f.vulnerabilities.map(v => v.id).join(", ");
143
+ console.log(` ${chalk.yellow(f.severity)} ${f.pkg.name}@${f.pkg.version}${ids ? ` (${ids})` : ""}`);
144
+ }
145
+ }
146
+ if (options.checkOverrides) {
147
+ console.log(chalk.gray("Note: override hygiene (--check-overrides) is not part of the ratchet baseline; run `cve-lite overrides` to audit overrides."));
148
+ }
149
+ if (anyNewFindings) {
150
+ console.log(chalk.gray("To accept these, re-baseline intentionally by deleting the folder's .cve-lite/baseline.json and re-running --ratchet."));
151
+ return EXIT_FINDINGS;
152
+ }
153
+ return EXIT_OK;
154
+ }
75
155
  export async function handleMultiFolderScan(params) {
156
+ const auditLog = params.auditLog ?? NULL_AUDIT_LOG;
157
+ const scanStartedAt = Date.now();
76
158
  const results = await runMultiFolderScan(params);
159
+ const scanFinishedAt = Date.now();
160
+ const first = results[0];
161
+ auditLog.emit({
162
+ ts: new Date(scanStartedAt).toISOString(),
163
+ type: "scan.started",
164
+ schemaVersion: 1,
165
+ projectPath: params.projectRoot,
166
+ mode: first?.scanInput.mode ?? "resolved-lockfile",
167
+ source: first?.scanInput.source ?? "package-lock",
168
+ flags: {
169
+ fix: params.options.fix === true,
170
+ json: params.options.json === true,
171
+ prodOnly: params.options.prodOnly === true,
172
+ offline: params.options.offline === true,
173
+ checkOverrides: params.options.checkOverrides === true,
174
+ checkMaintenance: params.options.checkMaintenance === true,
175
+ folderCount: String(results.length),
176
+ },
177
+ });
77
178
  if (results.length === 0) {
78
179
  console.log(chalk.yellow("No scannable packages found in any subfolder."));
180
+ auditLog.emit({
181
+ ts: new Date(scanFinishedAt).toISOString(),
182
+ type: "scan.finished",
183
+ schemaVersion: 1,
184
+ durationMs: scanFinishedAt - scanStartedAt,
185
+ findingsCount: 0,
186
+ exitCode: EXIT_OK,
187
+ });
79
188
  return EXIT_OK;
80
189
  }
81
190
  if (params.options.fix) {
82
191
  console.error(chalk.yellow("--fix is not yet supported in multi-folder mode. Run cve-lite . from each subfolder individually."));
192
+ auditLog.emit({
193
+ ts: new Date().toISOString(),
194
+ type: "scan.finished",
195
+ schemaVersion: 1,
196
+ durationMs: Date.now() - scanStartedAt,
197
+ findingsCount: results.reduce((count, result) => count + result.sorted.length, 0),
198
+ exitCode: EXIT_ERROR,
199
+ });
83
200
  return EXIT_ERROR;
84
201
  }
85
202
  if (params.options.sarif || params.options.cdx) {
86
203
  console.error(chalk.yellow("--sarif and --cdx are not yet supported in multi-folder mode."));
204
+ auditLog.emit({
205
+ ts: new Date().toISOString(),
206
+ type: "scan.finished",
207
+ schemaVersion: 1,
208
+ durationMs: Date.now() - scanStartedAt,
209
+ findingsCount: results.reduce((count, result) => count + result.sorted.length, 0),
210
+ exitCode: EXIT_ERROR,
211
+ });
87
212
  return EXIT_ERROR;
88
213
  }
214
+ if (params.options.ratchet) {
215
+ return handleMultiFolderRatchet(results, params.projectRoot, params.options);
216
+ }
89
217
  if (params.options.json) {
90
- const { serializeFinding } = await import("../output/formatters.js");
218
+ // Match single-folder writeOutputs: timestamped file in cwd, not stdout dump.
219
+ // Help text and CI both treat --json as "save a JSON file".
91
220
  const allFindings = results.flatMap(r => r.sorted.map(f => ({ ...serializeFinding(f, r.suggestedFixCommands), subfolder: r.subfolder })));
92
- console.log(JSON.stringify({
221
+ const completeness = aggregateMultiFolderCompleteness(results);
222
+ const payload = {
93
223
  multiFolder: true,
224
+ projectPath: params.projectRoot,
94
225
  folders: results.map(r => r.subfolder),
226
+ findingCount: allFindings.length,
227
+ status: completeness.complete ? "ok" : "partial",
228
+ complete: completeness.complete,
229
+ diagnostics: completeness.diagnostics,
95
230
  findings: allFindings,
96
231
  ...(params.options.checkOverrides
97
232
  ? {
@@ -104,7 +239,12 @@ export async function handleMultiFolderScan(params) {
104
239
  }
105
240
  : {}),
106
241
  scannedAt: new Date().toISOString(),
107
- }, null, 2));
242
+ };
243
+ const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
244
+ const jsonFilename = `cve-lite-scan-${ts}.json`;
245
+ const jsonOutputPath = path.join(process.cwd(), jsonFilename);
246
+ fs.writeFileSync(jsonOutputPath, JSON.stringify(payload, null, 2));
247
+ console.log(`${chalk.gray("JSON saved to")} ${chalk.cyan(jsonFilename)}`);
108
248
  }
109
249
  else {
110
250
  printMultiFolderResults(results, params.options);
@@ -141,6 +281,10 @@ export async function handleMultiFolderScan(params) {
141
281
  projectPath: params.projectRoot,
142
282
  cliVersion,
143
283
  autoOpen: !params.options.noOpen,
284
+ // Same undefined-vs-[] contract as single-folder buildReportData: pass the
285
+ // flag so empty panels render when the check ran with no hits.
286
+ includeOverrides: !!params.options.checkOverrides,
287
+ includeMaintenance: !!params.options.checkMaintenance,
144
288
  });
145
289
  console.log(`${chalk.gray("Report:")} ${chalk.cyan(reportPath)}`);
146
290
  }
@@ -154,5 +298,14 @@ export async function handleMultiFolderScan(params) {
154
298
  const shouldFail = allSorted.some(f => severityOrder[f.severity] >= severityOrder[failLevel]) ||
155
299
  reachesFailOn(allOverrideFindings, params.options.failOn) ||
156
300
  reachesFailOn(allMaintenanceFindings, params.options.failOn);
157
- return shouldFail ? EXIT_FINDINGS : EXIT_OK;
301
+ const exitCode = shouldFail ? EXIT_FINDINGS : EXIT_OK;
302
+ auditLog.emit({
303
+ ts: new Date(scanFinishedAt).toISOString(),
304
+ type: "scan.finished",
305
+ schemaVersion: 1,
306
+ durationMs: scanFinishedAt - scanStartedAt,
307
+ findingsCount: allSorted.length,
308
+ exitCode,
309
+ });
310
+ return exitCode;
158
311
  }