cve-lite-cli 1.32.0 → 1.34.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/dist/scanner.js CHANGED
@@ -71,6 +71,23 @@ function isTransientFailure(error) {
71
71
  return false;
72
72
  return true;
73
73
  }
74
+ function mergeResultsByPackage(results) {
75
+ const map = new Map();
76
+ for (const result of results) {
77
+ const key = `${result.pkg.name}@${result.pkg.version}`;
78
+ const existing = map.get(key);
79
+ if (existing) {
80
+ for (const id of result.vulnIds) {
81
+ if (!existing.vulnIds.includes(id))
82
+ existing.vulnIds.push(id);
83
+ }
84
+ }
85
+ else {
86
+ map.set(key, { pkg: result.pkg, vulnIds: [...result.vulnIds] });
87
+ }
88
+ }
89
+ return [...map.values()];
90
+ }
74
91
  export async function scanPackages(packages, batchSize, options, context, debugLog, fetchImpl) {
75
92
  const log = debugLog ?? (() => { });
76
93
  const sourceContext = createAdvisorySource({
@@ -130,6 +147,9 @@ export async function scanPackages(packages, batchSize, options, context, debugL
130
147
  for (let j = 0; j < chunkItems.length; j++) {
131
148
  const pkg = chunkItems[j];
132
149
  const row = rows[j];
150
+ if (row && (row.package !== pkg.name || row.version !== pkg.version)) {
151
+ throw new Error(`OSV result identity mismatch at index ${j}: expected ${pkg.name}@${pkg.version}, got ${row.package}@${row.version}`);
152
+ }
133
153
  const vulnIds = (row?.vulnerabilities ?? []).map(v => v.id).filter(Boolean);
134
154
  cache.queryEntries[getPackageCacheKey(pkg)] = { vulnIds, cachedAt };
135
155
  if (vulnIds.length > 0) {
@@ -310,7 +330,7 @@ export async function scanPackages(packages, batchSize, options, context, debugL
310
330
  if (!offline && (idSet.size > 0 || wroteNpmAdvisoryEntries)) {
311
331
  saveCache(cache, cacheDirOverride, options.debug ? log : undefined);
312
332
  }
313
- const findings = results.map(result => {
333
+ const findings = mergeResultsByPackage(results).map(result => {
314
334
  const vulnerabilities = result.vulnIds
315
335
  .map(id => vulnMap.get(id))
316
336
  .filter((v) => Boolean(v));
@@ -319,7 +339,7 @@ export async function scanPackages(packages, batchSize, options, context, debugL
319
339
  const cveAliases = extractCveAliases(vulnerabilities);
320
340
  const dependencyPaths = result.pkg.paths ?? [];
321
341
  const relationship = classifyRelationship(dependencyPaths, result.pkg.name, context?.directDependencyNames);
322
- const firstFixedVersion = findFirstFixedVersion(vulnerabilities, result.pkg.name, result.pkg.ecosystem);
342
+ const firstFixedVersion = findFirstFixedVersion(vulnerabilities, result.pkg.name, result.pkg.ecosystem, result.pkg.version);
323
343
  return {
324
344
  pkg: result.pkg,
325
345
  vulnerabilities,
@@ -800,8 +820,28 @@ function affectedEntryMatchesPackage(affected, packageName, ecosystem) {
800
820
  return false;
801
821
  return true;
802
822
  }
803
- export function findFirstFixedVersion(vulns, packageName, ecosystem) {
823
+ /**
824
+ * True when `version` sits inside the half-open window `[introduced, fixed)`.
825
+ *
826
+ * `introduced` of "0" (or absent) means the window has no lower bound, which is
827
+ * how OSV expresses "every version before the fix".
828
+ */
829
+ function versionWithinAffectedWindow(version, introduced, fixed) {
830
+ if (introduced && introduced !== "0") {
831
+ if (!looksLikeVersion(introduced))
832
+ return false;
833
+ if (compareVersions(version, introduced) < 0)
834
+ return false;
835
+ }
836
+ return compareVersions(version, fixed) < 0;
837
+ }
838
+ export function findFirstFixedVersion(vulns, packageName, ecosystem, installedVersion) {
804
839
  const fixedVersions = [];
840
+ // Fixes whose affected window actually contains the installed version. An
841
+ // advisory that patches two release branches declares a fix per branch, and
842
+ // the global minimum is the other branch's fix - a downgrade across a major
843
+ // boundary rather than a remediation.
844
+ const fixesOnInstalledBranch = [];
805
845
  for (const vuln of vulns) {
806
846
  for (const affected of vuln.affected ?? []) {
807
847
  // An advisory may list several packages with different fix versions.
@@ -811,18 +851,33 @@ export function findFirstFixedVersion(vulns, packageName, ecosystem) {
811
851
  if (!affectedEntryMatchesPackage(affected, packageName, ecosystem))
812
852
  continue;
813
853
  for (const range of affected.ranges ?? []) {
854
+ // Events are ordered, and a single range can reintroduce a
855
+ // vulnerability, so `introduced` is tracked as the walk proceeds and
856
+ // reset once its matching `fixed` closes the window.
857
+ let introduced = null;
814
858
  for (const event of range.events ?? []) {
859
+ if (event.introduced) {
860
+ introduced = event.introduced;
861
+ continue;
862
+ }
815
863
  const fixed = event.fixed;
816
- if (fixed && looksLikeVersion(fixed) && !isPreReleaseVersion(fixed)) {
817
- fixedVersions.push(fixed);
864
+ if (!fixed || !looksLikeVersion(fixed) || isPreReleaseVersion(fixed))
865
+ continue;
866
+ fixedVersions.push(fixed);
867
+ if (installedVersion && versionWithinAffectedWindow(installedVersion, introduced, fixed)) {
868
+ fixesOnInstalledBranch.push(fixed);
818
869
  }
870
+ introduced = null;
819
871
  }
820
872
  }
821
873
  }
822
874
  }
823
- if (fixedVersions.length === 0)
875
+ // Falling back to every known fix keeps a finding's remediation visible when
876
+ // the installed version sits outside each declared window.
877
+ const candidates = fixesOnInstalledBranch.length > 0 ? fixesOnInstalledBranch : fixedVersions;
878
+ if (candidates.length === 0)
824
879
  return null;
825
- return fixedVersions.sort(compareVersions)[0];
880
+ return candidates.sort(compareVersions)[0];
826
881
  }
827
882
  function buildCompletenessDiagnostics(params) {
828
883
  const diagnostics = [];
package/dist/types.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * 1 - findings present (CVE or override) above threshold
6
6
  * 2 - --fix applied but verify() detected the fix did not take
7
7
  * (operationally distinct from 1: "fix ran but did not work")
8
- * 3 - tool error (unhandled exception, unreadable lockfile, etc.)
8
+ * 3 - tool error (unhandled exception, unreadable lockfile, etc.) or incomplete scan (--incomplete-policy=error)
9
9
  */
10
10
  export const EXIT_OK = 0;
11
11
  export const EXIT_FINDINGS = 1;
@@ -0,0 +1,40 @@
1
+ const EPSS_HIGH_EXPLOITATION_THRESHOLD = 0.90;
2
+ export const PRIORITY_SIGNAL_LABELS = {
3
+ fix_now: "⚡ Fix Now",
4
+ fix_soon: "Fix Soon",
5
+ monitor: "Monitor",
6
+ low_priority: "Low Priority",
7
+ };
8
+ // Emoji-free labels for fixed-width table columns (⚡ is 2-wide in terminals but 1 in JS string.length, breaking padEnd alignment)
9
+ export const PRIORITY_SIGNAL_TABLE_LABELS = {
10
+ fix_now: "Fix Now",
11
+ fix_soon: "Fix Soon",
12
+ monitor: "Monitor",
13
+ low_priority: "Low Priority",
14
+ };
15
+ export const PRIORITY_SIGNAL_DESCRIPTIONS = {
16
+ fix_now: "Critical or high severity + top 10% EPSS exploitation likelihood",
17
+ fix_soon: "Critical or high severity, EPSS not top 10%",
18
+ monitor: "Medium or lower severity, EPSS top 10% - watch closely",
19
+ low_priority: "Medium or lower severity, EPSS not top 10%",
20
+ };
21
+ export const PRIORITY_SIGNAL_LEGEND_DESCRIPTIONS = {
22
+ fix_now: "Critical or high severity and EPSS top 10% - high impact, actively exploited class of CVE",
23
+ fix_soon: "Critical or high severity, EPSS not top 10% - high impact but exploitation is less common",
24
+ monitor: "Medium or lower, EPSS top 10% - lower impact but exploitation is active",
25
+ low_priority: "Medium or lower, EPSS not top 10% - address in normal maintenance cycle",
26
+ };
27
+ export function computePrioritySignal(severity, epssScores) {
28
+ if (!epssScores || epssScores.length === 0)
29
+ return null;
30
+ const highImpact = severity === "critical" || severity === "high";
31
+ const maxPercentile = Math.max(...epssScores.map(e => e.percentile));
32
+ const highExploitation = maxPercentile >= EPSS_HIGH_EXPLOITATION_THRESHOLD;
33
+ if (highImpact && highExploitation)
34
+ return "fix_now";
35
+ if (highImpact)
36
+ return "fix_soon";
37
+ if (highExploitation)
38
+ return "monitor";
39
+ return "low_priority";
40
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Builds a Package URL (purl) for an npm package.
3
+ *
4
+ * Scoped names carry a leading `@` which is not legal unencoded in the purl
5
+ * name component, so it is percent-encoded.
6
+ */
7
+ export function buildPurl(name, version) {
8
+ const encodedName = name.startsWith("@") ? `%40${name.slice(1)}` : name;
9
+ return `pkg:npm/${encodedName}@${version}`;
10
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Resolves which SBOM format a run should emit, if any.
3
+ *
4
+ * `--cdx` predates `--sbom` and remains supported, so it is honoured here as
5
+ * well as in argument parsing. Keeping the alias in one place means callers
6
+ * that build options programmatically behave the same as the CLI.
7
+ */
8
+ export function resolveSbomFormat(options) {
9
+ if (options.sbom)
10
+ return options.sbom;
11
+ return options.cdx ? "cyclonedx" : undefined;
12
+ }
@@ -1,27 +1,45 @@
1
1
  export function looksLikeVersion(value) {
2
2
  return /^[0-9]+\.[0-9]+\.[0-9]+(?:[-+][^/]+)?$/.test(value);
3
3
  }
4
+ // Matches a well-formed X.Y.Z version, capturing an optional dot-separated
5
+ // pre-release identifier list and discarding build metadata (semver §11.3/11.4).
6
+ // The pre-release character class matches looksLikeVersion's admission gate
7
+ // above (anything but '/' or '+') rather than a narrower ASCII set, so a
8
+ // pre-release looksLikeVersion accepts can never fail this match and fall
9
+ // through to the flat-token fallback, which would reintroduce the exact
10
+ // precedence bug this file fixes.
11
+ const VERSION_SHAPE = /^(\d+)\.(\d+)\.(\d+)(?:-([^/+]+))?(?:\+.*)?$/;
4
12
  // module-level; safe to grow unbounded — a single scan sees at most
5
13
  // a few hundred unique version strings, and the cache is discarded when the process exits.
6
14
  const parsedVersionCache = new Map();
7
15
  export function getVersionCacheSize() {
8
16
  return parsedVersionCache.size;
9
17
  }
10
- function parseVersionTuple(v) {
18
+ function parseVersion(v) {
11
19
  const cached = parsedVersionCache.get(v);
12
- if (cached)
20
+ if (cached !== undefined)
13
21
  return cached;
14
- const parsed = v.split(/[.+-]/).map(n => {
15
- const num = Number(n);
16
- return Number.isFinite(num) ? num : n;
17
- });
18
- Object.freeze(parsed);
22
+ const match = VERSION_SHAPE.exec(v);
23
+ const parsed = match
24
+ ? {
25
+ release: [Number(match[1]), Number(match[2]), Number(match[3])],
26
+ prerelease: match[4] ? match[4].split(".") : null,
27
+ }
28
+ : null;
19
29
  parsedVersionCache.set(v, parsed);
20
30
  return parsed;
21
31
  }
22
- export function compareVersions(a, b) {
23
- const pa = parseVersionTuple(a);
24
- const pb = parseVersionTuple(b);
32
+ // Legacy flat-tuple comparison, used only when at least one operand doesn't
33
+ // match VERSION_SHAPE, so a malformed input on either side degrades to prior
34
+ // (non-throwing) behavior instead of comparing a semver-parsed shape against
35
+ // an unparsed one (KTD3).
36
+ function compareFallback(a, b) {
37
+ const toTuple = (v) => v.split(/[.+-]/).map(n => {
38
+ const num = Number(n);
39
+ return Number.isFinite(num) ? num : n;
40
+ });
41
+ const pa = toTuple(a);
42
+ const pb = toTuple(b);
25
43
  const len = Math.max(pa.length, pb.length);
26
44
  for (let i = 0; i < len; i++) {
27
45
  const av = pa[i] ?? 0;
@@ -39,6 +57,52 @@ export function compareVersions(a, b) {
39
57
  }
40
58
  return 0;
41
59
  }
60
+ const NUMERIC_IDENTIFIER = /^\d+$/;
61
+ // Compares two dot-separated pre-release identifier lists per semver §11.4:
62
+ // numeric identifiers compare numerically, alphanumeric ones lexically, a
63
+ // numeric identifier always has lower precedence than an alphanumeric one,
64
+ // and (when the shared prefix is equal) a longer list outranks a shorter one.
65
+ function compareIdentifiers(a, b) {
66
+ const aIsNumeric = NUMERIC_IDENTIFIER.test(a);
67
+ const bIsNumeric = NUMERIC_IDENTIFIER.test(b);
68
+ if (aIsNumeric && bIsNumeric)
69
+ return Number(a) - Number(b);
70
+ if (aIsNumeric !== bIsNumeric)
71
+ return aIsNumeric ? -1 : 1;
72
+ return a < b ? -1 : a > b ? 1 : 0;
73
+ }
74
+ // A version with no pre-release outranks one with a pre-release at the same
75
+ // release; between two pre-releases, compare identifiers left to right.
76
+ function comparePrerelease(a, b) {
77
+ if (a === null && b === null)
78
+ return 0;
79
+ if (a === null)
80
+ return 1;
81
+ if (b === null)
82
+ return -1;
83
+ const len = Math.max(a.length, b.length);
84
+ for (let i = 0; i < len; i++) {
85
+ if (i >= a.length)
86
+ return -1;
87
+ if (i >= b.length)
88
+ return 1;
89
+ const cmp = compareIdentifiers(a[i], b[i]);
90
+ if (cmp !== 0)
91
+ return cmp;
92
+ }
93
+ return 0;
94
+ }
95
+ export function compareVersions(a, b) {
96
+ const pa = parseVersion(a);
97
+ const pb = parseVersion(b);
98
+ if (!pa || !pb)
99
+ return compareFallback(a, b);
100
+ for (let i = 0; i < 3; i++) {
101
+ if (pa.release[i] !== pb.release[i])
102
+ return pa.release[i] - pb.release[i];
103
+ }
104
+ return comparePrerelease(pa.prerelease, pb.prerelease);
105
+ }
42
106
  export function parseExactManifestVersion(spec) {
43
107
  const cleaned = spec.trim().replace(/^npm:/, "");
44
108
  if (looksLikeVersion(cleaned))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cve-lite-cli",
3
- "version": "1.32.0",
3
+ "version": "1.34.0",
4
4
  "description": "Developer-friendly CLI for scanning JS/TS projects for dependency vulnerabilities using local lockfiles and OSV",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "better-sqlite3": "^12.8.0",
23
- "fflate": "^0.8.2",
23
+ "fflate": "^0.8.3",
24
24
  "yaml": "^2.7.1",
25
25
  "yarn-lockfile": "^1.1.0"
26
26
  },