cve-lite-cli 1.23.1 → 1.25.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 +56 -0
- package/dist/audit-log/events.js +3 -0
- package/dist/audit-log/handle.js +20 -0
- package/dist/audit-log/index.js +16 -0
- package/dist/audit-log/ndjson-writer.js +28 -0
- package/dist/cli/args.js +71 -3
- package/dist/cli/fix-overrides-hook.js +64 -0
- package/dist/cli/help.js +22 -0
- package/dist/cli/overrides.js +67 -0
- package/dist/index.js +270 -12
- package/dist/osv/cache.js +23 -3
- package/dist/output/formatters.js +23 -4
- package/dist/output/html-reporter.js +34 -4
- package/dist/output/override-findings-html.js +57 -0
- package/dist/output/override-findings-json.js +3 -0
- package/dist/output/override-findings-sarif.js +52 -0
- package/dist/output/override-findings-terminal.js +37 -0
- package/dist/output/printers.js +25 -8
- package/dist/output/sarif.js +77 -8
- package/dist/output/write-outputs.js +7 -3
- package/dist/overrides/api.js +79 -0
- package/dist/overrides/composite.js +23 -0
- package/dist/overrides/context-builder.js +150 -0
- package/dist/overrides/context.js +3 -0
- package/dist/overrides/detectors/index.js +23 -0
- package/dist/overrides/detectors/oa001-orphaned-target.js +36 -0
- package/dist/overrides/detectors/oa002-floating-tag.js +40 -0
- package/dist/overrides/detectors/oa003-wrong-section.js +48 -0
- package/dist/overrides/detectors/oa004-surpassed-pin.js +49 -0
- package/dist/overrides/detectors/oa005-nested-ineffective.js +128 -0
- package/dist/overrides/detectors/oa006-coupled-platform-binary.js +170 -0
- package/dist/overrides/detectors/oa007-frozen-latest.js +69 -0
- package/dist/overrides/detectors/oa008-materialized.js +75 -0
- package/dist/overrides/detectors/platform-binary.js +37 -0
- package/dist/overrides/fixer.js +199 -0
- package/dist/overrides/index.js +5 -0
- package/dist/overrides/parsing/installed-tree.js +164 -0
- package/dist/overrides/parsing/json-pointer.js +8 -0
- package/dist/overrides/parsing/node-modules.js +19 -0
- package/dist/overrides/parsing/package-json.js +139 -0
- package/dist/overrides/parsing/registry.js +66 -0
- package/dist/overrides/types.js +2 -0
- package/dist/parsers/yarn-lock.js +2 -2
- package/dist/remediation/fix-commands.js +110 -29
- package/dist/remediation/npm-registry.js +30 -0
- package/dist/remediation/npm-transitive-resolution.js +2 -2
- package/dist/remediation/transitive-chain-resolver.js +100 -0
- package/dist/scan/multi-folder-scan.js +26 -0
- package/dist/scanner.js +61 -5
- package/dist/types.js +13 -1
- package/dist/utils/baseline.js +16 -0
- package/dist/utils/create-pr.js +19 -4
- package/dist/utils/string.js +34 -0
- package/dist/utils/version.js +115 -0
- package/package.json +4 -2
package/dist/scanner.js
CHANGED
|
@@ -9,9 +9,10 @@ import { LocalAdvisorySource } from "./advisory/local-advisory-source.js";
|
|
|
9
9
|
import { LocalAdvisoryDatabase } from "./advisory/local-db.js";
|
|
10
10
|
import { ADVISORY_DB_STALE_AFTER_MS, getDefaultAdvisoryDbPath } from "./advisory/osv-sync.js";
|
|
11
11
|
import { resolveRecommendedParentUpgrade } from "./remediation/parent-upgrade.js";
|
|
12
|
-
import { configureNpmRegistryDebug, fetchPackument, resolveLowestKnownNonVulnerableVersion, resolvePublishedFixVersion, } from "./remediation/npm-registry.js";
|
|
12
|
+
import { configureNpmRegistryDebug, fetchPackument, resolveLowestKnownNonVulnerableVersion, resolvePublishedFixVersion, resolveVersionPublishDate, } from "./remediation/npm-registry.js";
|
|
13
13
|
import { countBySeverity } from "./utils/severity.js";
|
|
14
14
|
import { resolveNpmTransitiveRemediation, resolveTransitiveRemediationViaRegistry } from "./remediation/npm-transitive-resolution.js";
|
|
15
|
+
import { resolveChainFix } from "./remediation/transitive-chain-resolver.js";
|
|
15
16
|
import { loadNpmLockGraph } from "./parsers/npm-lock-graph.js";
|
|
16
17
|
import { buildPnpmWorkspaceMap } from "./parsers/pnpm-lock.js";
|
|
17
18
|
import { buildNpmWorkspaceMap } from "./parsers/package-lock.js";
|
|
@@ -45,6 +46,17 @@ export function createAdvisorySource(options) {
|
|
|
45
46
|
function getPackageCacheKey(pkg) {
|
|
46
47
|
return `${pkg.ecosystem}:${pkg.name}@${pkg.version}`;
|
|
47
48
|
}
|
|
49
|
+
function inferPackageManagerForChain(source) {
|
|
50
|
+
if (source === "package-lock" || source === "npm-shrinkwrap")
|
|
51
|
+
return "npm";
|
|
52
|
+
if (source === "pnpm-lock")
|
|
53
|
+
return "pnpm";
|
|
54
|
+
if (source === "yarn-lock")
|
|
55
|
+
return "yarn";
|
|
56
|
+
if (source === "bun-lock")
|
|
57
|
+
return "bun";
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
48
60
|
export async function scanPackages(packages, batchSize, options, context, debugLog) {
|
|
49
61
|
const log = debugLog ?? (() => { });
|
|
50
62
|
const sourceContext = createAdvisorySource({
|
|
@@ -222,6 +234,7 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
222
234
|
firstFixedVersion,
|
|
223
235
|
validatedFirstFixedVersion: null,
|
|
224
236
|
fixVersionValidationNote: null,
|
|
237
|
+
fixVersionPublishedAt: null,
|
|
225
238
|
validatedTargetScannedVersions: null,
|
|
226
239
|
validatedTargetKnownVulnerableVersions: null,
|
|
227
240
|
recommendedParentUpgrade: undefined,
|
|
@@ -308,9 +321,12 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
308
321
|
};
|
|
309
322
|
try {
|
|
310
323
|
if (!offline) {
|
|
311
|
-
await validateDirectFixTargets(findings, (finding) => {
|
|
324
|
+
const npmVersionCacheUpdated = await validateDirectFixTargets(findings, (finding) => {
|
|
312
325
|
updateAnalysisProgress("validating fix target for", `${finding.pkg.name}@${finding.pkg.version}`);
|
|
313
|
-
}, log);
|
|
326
|
+
}, { cache, noCache: options.noCache, debugLog: log });
|
|
327
|
+
if (npmVersionCacheUpdated) {
|
|
328
|
+
saveCache(cache, cacheDirOverride, options.debug ? log : undefined);
|
|
329
|
+
}
|
|
314
330
|
}
|
|
315
331
|
// Pre-warm packument cache for all packages needed in the remediation loop.
|
|
316
332
|
// This converts N sequential registry round-trips into one concurrent burst,
|
|
@@ -413,6 +429,21 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
413
429
|
}
|
|
414
430
|
}
|
|
415
431
|
analysisSpinner.succeed("Analyzed vulnerability findings");
|
|
432
|
+
// Chain fix resolution: pre-compute validated chain for transitive findings
|
|
433
|
+
const chainPm = inferPackageManagerForChain(context?.scanSource);
|
|
434
|
+
if (chainPm) {
|
|
435
|
+
const installedVersions = new Map(packages.map(p => [p.name, p.version]));
|
|
436
|
+
await Promise.all(findings
|
|
437
|
+
.filter(f => f.relationship === "transitive" && f.dependencyPaths?.length)
|
|
438
|
+
.map(async (finding) => {
|
|
439
|
+
try {
|
|
440
|
+
finding.chainResolution = await resolveChainFix(finding, installedVersions, chainPm, { offline });
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
finding.chainResolution = null;
|
|
444
|
+
}
|
|
445
|
+
}));
|
|
446
|
+
}
|
|
416
447
|
}
|
|
417
448
|
catch (error) {
|
|
418
449
|
analysisSpinner.fail("Failed while analyzing vulnerability findings");
|
|
@@ -420,7 +451,14 @@ export async function scanPackages(packages, batchSize, options, context, debugL
|
|
|
420
451
|
}
|
|
421
452
|
}
|
|
422
453
|
else if (!offline) {
|
|
423
|
-
await validateDirectFixTargets(findings, undefined,
|
|
454
|
+
const npmVersionCacheUpdated = await validateDirectFixTargets(findings, undefined, {
|
|
455
|
+
cache,
|
|
456
|
+
noCache: options.noCache,
|
|
457
|
+
debugLog: log,
|
|
458
|
+
});
|
|
459
|
+
if (npmVersionCacheUpdated) {
|
|
460
|
+
saveCache(cache, cacheDirOverride, options.debug ? log : undefined);
|
|
461
|
+
}
|
|
424
462
|
}
|
|
425
463
|
const severityCounts = countBySeverity(findings);
|
|
426
464
|
log("Findings classified", {
|
|
@@ -473,8 +511,10 @@ function createNpmTransitiveGraphFromLockfile(filePath, log, packageCount) {
|
|
|
473
511
|
return null;
|
|
474
512
|
}
|
|
475
513
|
}
|
|
476
|
-
async function validateDirectFixTargets(findings, onFinding,
|
|
514
|
+
async function validateDirectFixTargets(findings, onFinding, options) {
|
|
477
515
|
const directCandidates = findings.filter(finding => finding.vulnerabilities.length > 0);
|
|
516
|
+
let npmVersionCacheUpdated = false;
|
|
517
|
+
const debugLog = options?.debugLog;
|
|
478
518
|
for (const finding of directCandidates) {
|
|
479
519
|
onFinding?.(finding);
|
|
480
520
|
const logFixValidation = () => {
|
|
@@ -493,6 +533,7 @@ async function validateDirectFixTargets(findings, onFinding, debugLog) {
|
|
|
493
533
|
finding.validatedTargetKnownVulnerableVersions = lowestKnownResolution.candidatesKnownVulnerable;
|
|
494
534
|
if (!fixedVersionHint || fixedVersionHint === lowestKnownResolution.resolvedVersion) {
|
|
495
535
|
finding.fixVersionValidationNote = null;
|
|
536
|
+
npmVersionCacheUpdated = await attachFixVersionPublishDate(finding, options) || npmVersionCacheUpdated;
|
|
496
537
|
logFixValidation();
|
|
497
538
|
continue;
|
|
498
539
|
}
|
|
@@ -500,11 +541,13 @@ async function validateDirectFixTargets(findings, onFinding, debugLog) {
|
|
|
500
541
|
if (hintResolution.resolvedVersion === lowestKnownResolution.resolvedVersion &&
|
|
501
542
|
hintResolution.note) {
|
|
502
543
|
finding.fixVersionValidationNote = hintResolution.note;
|
|
544
|
+
npmVersionCacheUpdated = await attachFixVersionPublishDate(finding, options) || npmVersionCacheUpdated;
|
|
503
545
|
logFixValidation();
|
|
504
546
|
continue;
|
|
505
547
|
}
|
|
506
548
|
finding.fixVersionValidationNote =
|
|
507
549
|
`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}.`;
|
|
550
|
+
npmVersionCacheUpdated = await attachFixVersionPublishDate(finding, options) || npmVersionCacheUpdated;
|
|
508
551
|
logFixValidation();
|
|
509
552
|
continue;
|
|
510
553
|
}
|
|
@@ -513,6 +556,7 @@ async function validateDirectFixTargets(findings, onFinding, debugLog) {
|
|
|
513
556
|
finding.fixVersionValidationNote = lowestKnownResolution.note;
|
|
514
557
|
finding.validatedTargetScannedVersions = null;
|
|
515
558
|
finding.validatedTargetKnownVulnerableVersions = null;
|
|
559
|
+
finding.fixVersionPublishedAt = null;
|
|
516
560
|
logFixValidation();
|
|
517
561
|
continue;
|
|
518
562
|
}
|
|
@@ -521,8 +565,20 @@ async function validateDirectFixTargets(findings, onFinding, debugLog) {
|
|
|
521
565
|
finding.fixVersionValidationNote = resolution.note ?? lowestKnownResolution.note;
|
|
522
566
|
finding.validatedTargetScannedVersions = null;
|
|
523
567
|
finding.validatedTargetKnownVulnerableVersions = null;
|
|
568
|
+
npmVersionCacheUpdated = await attachFixVersionPublishDate(finding, options) || npmVersionCacheUpdated;
|
|
524
569
|
logFixValidation();
|
|
525
570
|
}
|
|
571
|
+
return npmVersionCacheUpdated;
|
|
572
|
+
}
|
|
573
|
+
async function attachFixVersionPublishDate(finding, options) {
|
|
574
|
+
const version = finding.validatedFirstFixedVersion;
|
|
575
|
+
if (!version) {
|
|
576
|
+
finding.fixVersionPublishedAt = null;
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
const { publishedAt, cacheWritten } = await resolveVersionPublishDate(finding.pkg.name, version, options);
|
|
580
|
+
finding.fixVersionPublishedAt = publishedAt;
|
|
581
|
+
return cacheWritten;
|
|
526
582
|
}
|
|
527
583
|
function isAdvisoryDbStale(metadata) {
|
|
528
584
|
if (!metadata.lastSyncAt) {
|
package/dist/types.js
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Exit codes used by the CLI.
|
|
3
|
+
*
|
|
4
|
+
* 0 - no findings above --fail-on threshold
|
|
5
|
+
* 1 - findings present (CVE or override) above threshold
|
|
6
|
+
* 2 - --fix applied but verify() detected the fix did not take
|
|
7
|
+
* (operationally distinct from 1: "fix ran but did not work")
|
|
8
|
+
* 3 - tool error (unhandled exception, unreadable lockfile, etc.)
|
|
9
|
+
*/
|
|
10
|
+
export const EXIT_OK = 0;
|
|
11
|
+
export const EXIT_FINDINGS = 1;
|
|
12
|
+
export const EXIT_VERIFY_FAILED = 2;
|
|
13
|
+
export const EXIT_ERROR = 3;
|
package/dist/utils/baseline.js
CHANGED
|
@@ -32,6 +32,22 @@ export function writeBaseline(projectRoot, findings) {
|
|
|
32
32
|
};
|
|
33
33
|
fs.writeFileSync(baselinePath(projectRoot), JSON.stringify(baseline, null, 2) + "\n", "utf8");
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Decide what `--ratchet` should do for this scan.
|
|
37
|
+
*
|
|
38
|
+
* - No baseline yet -> "save": record the current findings as the baseline.
|
|
39
|
+
* - Baseline exists -> "gate": never overwrite; report findings not already in
|
|
40
|
+
* the baseline so the caller can fail on them. This is the "if baseline exists,
|
|
41
|
+
* only fail on new findings" half of --ratchet. Re-saving here would let a newly
|
|
42
|
+
* introduced vulnerability be silently absorbed into a fresh baseline, so a
|
|
43
|
+
* ratcheted CI job could never fail on a regression.
|
|
44
|
+
*/
|
|
45
|
+
export function ratchetOutcome(baseline, findings) {
|
|
46
|
+
if (!baseline)
|
|
47
|
+
return { action: "save" };
|
|
48
|
+
const { newFindings, suppressedCount } = filterNewFindings(findings, baseline);
|
|
49
|
+
return { action: "gate", newFindings, suppressedCount };
|
|
50
|
+
}
|
|
35
51
|
export function filterNewFindings(findings, baseline) {
|
|
36
52
|
const baselineMap = new Map();
|
|
37
53
|
for (const entry of baseline.findings) {
|
package/dist/utils/create-pr.js
CHANGED
|
@@ -22,11 +22,17 @@ export function defaultFixBranchName(date = new Date()) {
|
|
|
22
22
|
const day = String(date.getUTCDate()).padStart(2, "0");
|
|
23
23
|
return `cve-lite/fix-${year}-${month}-${day}`;
|
|
24
24
|
}
|
|
25
|
-
export function buildPullRequestTitle(appliedCount, packageNames) {
|
|
25
|
+
export function buildPullRequestTitle(appliedCount, packageNames, overrideFixCount = 0) {
|
|
26
|
+
if (appliedCount === 0 && overrideFixCount > 0) {
|
|
27
|
+
return `[CVE-Lite-CLI] chore: ${overrideFixCount} override hygiene ${pluralize(overrideFixCount, "fix", "fixes")}`;
|
|
28
|
+
}
|
|
26
29
|
const pkgList = packageNames.length <= 3
|
|
27
30
|
? packageNames.join(", ")
|
|
28
31
|
: `${packageNames.slice(0, 2).join(", ")} +${packageNames.length - 2} more`;
|
|
29
|
-
|
|
32
|
+
const base = `[CVE-Lite-CLI] fix: upgrade ${pkgList} (${appliedCount} ${pluralize(appliedCount, "vulnerability", "vulnerabilities")} resolved)`;
|
|
33
|
+
return overrideFixCount > 0
|
|
34
|
+
? `${base} + ${overrideFixCount} override hygiene ${pluralize(overrideFixCount, "fix", "fixes")}`
|
|
35
|
+
: base;
|
|
30
36
|
}
|
|
31
37
|
export function collectAdvisoryIdsForPackage(findings, packageName) {
|
|
32
38
|
const ids = new Set();
|
|
@@ -45,6 +51,7 @@ export function collectAdvisoryIdsForPackage(findings, packageName) {
|
|
|
45
51
|
return [...ids].sort();
|
|
46
52
|
}
|
|
47
53
|
export function buildPullRequestBody(params) {
|
|
54
|
+
const overrideFixCount = params.overrideFixCount ?? 0;
|
|
48
55
|
const lines = [
|
|
49
56
|
"## Summary",
|
|
50
57
|
"",
|
|
@@ -53,12 +60,18 @@ export function buildPullRequestBody(params) {
|
|
|
53
60
|
"## Fixed packages",
|
|
54
61
|
"",
|
|
55
62
|
];
|
|
63
|
+
if (params.fixResult.applied.length === 0) {
|
|
64
|
+
lines.push("- _No direct CVE fixes in this PR._");
|
|
65
|
+
}
|
|
56
66
|
for (const item of params.fixResult.applied) {
|
|
57
67
|
const advisoryIds = collectAdvisoryIdsForPackage(params.findingsBeforeFix, item.package);
|
|
58
68
|
const advisoryText = advisoryIds.length > 0 ? advisoryIds.join(", ") : "n/a";
|
|
59
69
|
lines.push(`- **${item.package}**: \`${item.from}\` → \`${item.to}\``);
|
|
60
70
|
lines.push(` - Advisories: ${advisoryText}`);
|
|
61
71
|
}
|
|
72
|
+
if (overrideFixCount > 0) {
|
|
73
|
+
lines.push("", "## Override hygiene", "", `- Applied **${overrideFixCount}** override hygiene ${pluralize(overrideFixCount, "fix", "fixes")} to \`package.json\` (e.g. removing orphaned or ineffective overrides). Each change is constrained to existing overrides; no new override key is ever introduced.`);
|
|
74
|
+
}
|
|
62
75
|
lines.push("", "## Scan results", "", `- Findings before fix: **${params.findingsBeforeFix.length}**`, `- Findings after fix: **${params.findingsAfterFix.length}**`, "", "## Notes", "", "- Only validated **direct** dependency upgrades are included in this PR.", "- Transitive vulnerabilities may still require parent package upgrades or upstream releases.");
|
|
63
76
|
return lines.join("\n");
|
|
64
77
|
}
|
|
@@ -151,7 +164,8 @@ function extractPullRequestUrl(output) {
|
|
|
151
164
|
}
|
|
152
165
|
export async function createPullRequestForFixes(params) {
|
|
153
166
|
const baseBranchName = defaultFixBranchName();
|
|
154
|
-
|
|
167
|
+
const overrideFixCount = params.overrideFixCount ?? 0;
|
|
168
|
+
if (params.fixResult.appliedFixCount === 0 && overrideFixCount === 0) {
|
|
155
169
|
return {
|
|
156
170
|
branchName: baseBranchName,
|
|
157
171
|
prUrl: null,
|
|
@@ -170,11 +184,12 @@ export async function createPullRequestForFixes(params) {
|
|
|
170
184
|
};
|
|
171
185
|
}
|
|
172
186
|
const packageNames = params.fixResult.applied.map(a => a.package);
|
|
173
|
-
const title = buildPullRequestTitle(params.fixResult.appliedFixCount, packageNames);
|
|
187
|
+
const title = buildPullRequestTitle(params.fixResult.appliedFixCount, packageNames, overrideFixCount);
|
|
174
188
|
const body = buildPullRequestBody({
|
|
175
189
|
fixResult: params.fixResult,
|
|
176
190
|
findingsBeforeFix: params.findingsBeforeFix,
|
|
177
191
|
findingsAfterFix: params.findingsAfterFix,
|
|
192
|
+
overrideFixCount,
|
|
178
193
|
});
|
|
179
194
|
await stageDependencyFilesOnly(params.projectPath);
|
|
180
195
|
if (!(await hasStagedDependencyChanges(params.projectPath))) {
|
package/dist/utils/string.js
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
1
|
export function pluralize(count, singular, plural = `${singular}s`) {
|
|
2
2
|
return count === 1 ? singular : plural;
|
|
3
3
|
}
|
|
4
|
+
/** Known top-level cve-lite commands, for typo suggestions on a mistyped first argument. */
|
|
5
|
+
const KNOWN_COMMANDS = ["advisories", "install-skill", "config", "overrides"];
|
|
6
|
+
/**
|
|
7
|
+
* Suggest the nearest known command for a mistyped first argument. Returns
|
|
8
|
+
* undefined when nothing is within edit distance 2 (e.g. a real typo'd path
|
|
9
|
+
* rather than a typo'd command).
|
|
10
|
+
*/
|
|
11
|
+
export function nearestCommand(arg) {
|
|
12
|
+
let best;
|
|
13
|
+
let bestDist = 3;
|
|
14
|
+
for (const c of KNOWN_COMMANDS) {
|
|
15
|
+
const d = levenshtein(arg.toLowerCase(), c);
|
|
16
|
+
if (d < bestDist) {
|
|
17
|
+
bestDist = d;
|
|
18
|
+
best = c;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return best;
|
|
22
|
+
}
|
|
23
|
+
export function levenshtein(a, b) {
|
|
24
|
+
const m = a.length;
|
|
25
|
+
const n = b.length;
|
|
26
|
+
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
|
27
|
+
for (let i = 0; i <= m; i++)
|
|
28
|
+
dp[i][0] = i;
|
|
29
|
+
for (let j = 0; j <= n; j++)
|
|
30
|
+
dp[0][j] = j;
|
|
31
|
+
for (let i = 1; i <= m; i++) {
|
|
32
|
+
for (let j = 1; j <= n; j++) {
|
|
33
|
+
dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return dp[m][n];
|
|
37
|
+
}
|
package/dist/utils/version.js
CHANGED
|
@@ -45,3 +45,118 @@ export function normalizeRawVersion(value) {
|
|
|
45
45
|
return cleaned;
|
|
46
46
|
return null;
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Extract the leading integer (major) from a version string. Returns null
|
|
50
|
+
* if the string does not start with a digit-only run.
|
|
51
|
+
*/
|
|
52
|
+
export function majorVersion(value) {
|
|
53
|
+
if (!value)
|
|
54
|
+
return null;
|
|
55
|
+
const m = value.match(/^(\d+)/);
|
|
56
|
+
if (!m)
|
|
57
|
+
return null;
|
|
58
|
+
return Number(m[1]);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Coerce a partial or range-prefixed version into a concrete X.Y.Z form.
|
|
62
|
+
* Strips leading range operators (`^`, `~`, `>=`, `<=`, `>`, `<`, `=`), pads
|
|
63
|
+
* missing minor/patch with zeros. Returns null if the input cannot be
|
|
64
|
+
* interpreted as a version.
|
|
65
|
+
*
|
|
66
|
+
* Examples:
|
|
67
|
+
* "1" -> "1.0.0"
|
|
68
|
+
* "1.2" -> "1.2.0"
|
|
69
|
+
* "^1.2.3" -> "1.2.3"
|
|
70
|
+
* ">=2.0" -> "2.0.0"
|
|
71
|
+
* "latest" -> null
|
|
72
|
+
*/
|
|
73
|
+
export function coerceVersion(input) {
|
|
74
|
+
if (!input)
|
|
75
|
+
return null;
|
|
76
|
+
const stripped = input.trim().replace(/^[\^~]|^[<>]=?|^=/, "").trim();
|
|
77
|
+
const m = stripped.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:[-+][^\s]+)?/);
|
|
78
|
+
if (!m)
|
|
79
|
+
return null;
|
|
80
|
+
const [, major, minor = "0", patch = "0"] = m;
|
|
81
|
+
return `${major}.${minor}.${patch}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Does the string parse as a version range cve-lite recognises?
|
|
85
|
+
* Supports plain versions and the operators `^`, `~`, `>=`, `<=`, `>`, `<`, `=`.
|
|
86
|
+
* Does not support OR (`||`) or hyphen ranges (`1.0.0 - 2.0.0`) - if those
|
|
87
|
+
* surface in real projects, extend later.
|
|
88
|
+
*/
|
|
89
|
+
export function isValidRange(input) {
|
|
90
|
+
if (!input)
|
|
91
|
+
return false;
|
|
92
|
+
const trimmed = input.trim();
|
|
93
|
+
if (!/^(?:\^|~|>=|<=|>|<|=)?\s*\d+(?:\.\d+)?(?:\.\d+)?(?:[-+][^\s]+)?$/.test(trimmed)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return coerceVersion(trimmed) !== null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Does `version` satisfy `range`? Minimal subset of semver semantics covering
|
|
100
|
+
* what the OA detectors need: exact, `^`, `~`, `>=`, `<=`, `>`, `<`, `=`.
|
|
101
|
+
*
|
|
102
|
+
* Pre-releases follow semver convention: a pre-release version (e.g.
|
|
103
|
+
* "1.2.3-beta") satisfies a range only if the range explicitly mentions a
|
|
104
|
+
* pre-release component at the same major.minor.patch boundary.
|
|
105
|
+
*/
|
|
106
|
+
export function satisfiesRange(version, range) {
|
|
107
|
+
const v = coerceVersion(version);
|
|
108
|
+
if (!v)
|
|
109
|
+
return false;
|
|
110
|
+
const trimmedRange = range.trim();
|
|
111
|
+
// Exact match (no operator)
|
|
112
|
+
if (/^\d/.test(trimmedRange)) {
|
|
113
|
+
return compareVersions(v, coerceVersion(trimmedRange) ?? "") === 0;
|
|
114
|
+
}
|
|
115
|
+
// `=` operator
|
|
116
|
+
if (trimmedRange.startsWith("=")) {
|
|
117
|
+
const target = coerceVersion(trimmedRange.slice(1));
|
|
118
|
+
return target !== null && compareVersions(v, target) === 0;
|
|
119
|
+
}
|
|
120
|
+
// `^` caret: same major, >= specified, < next major
|
|
121
|
+
if (trimmedRange.startsWith("^")) {
|
|
122
|
+
const target = coerceVersion(trimmedRange.slice(1));
|
|
123
|
+
if (!target)
|
|
124
|
+
return false;
|
|
125
|
+
if (compareVersions(v, target) < 0)
|
|
126
|
+
return false;
|
|
127
|
+
const tMajor = majorVersion(target);
|
|
128
|
+
const vMajor = majorVersion(v);
|
|
129
|
+
if (tMajor === null || vMajor === null)
|
|
130
|
+
return false;
|
|
131
|
+
return vMajor === tMajor;
|
|
132
|
+
}
|
|
133
|
+
// `~` tilde: same major.minor, >= specified
|
|
134
|
+
if (trimmedRange.startsWith("~")) {
|
|
135
|
+
const target = coerceVersion(trimmedRange.slice(1));
|
|
136
|
+
if (!target)
|
|
137
|
+
return false;
|
|
138
|
+
if (compareVersions(v, target) < 0)
|
|
139
|
+
return false;
|
|
140
|
+
const [vMaj, vMin] = v.split(".").map(Number);
|
|
141
|
+
const [tMaj, tMin] = target.split(".").map(Number);
|
|
142
|
+
return vMaj === tMaj && vMin === tMin;
|
|
143
|
+
}
|
|
144
|
+
// Comparators
|
|
145
|
+
for (const op of [">=", "<=", ">", "<"]) {
|
|
146
|
+
if (trimmedRange.startsWith(op)) {
|
|
147
|
+
const target = coerceVersion(trimmedRange.slice(op.length));
|
|
148
|
+
if (!target)
|
|
149
|
+
return false;
|
|
150
|
+
const cmp = compareVersions(v, target);
|
|
151
|
+
if (op === ">=")
|
|
152
|
+
return cmp >= 0;
|
|
153
|
+
if (op === "<=")
|
|
154
|
+
return cmp <= 0;
|
|
155
|
+
if (op === ">")
|
|
156
|
+
return cmp > 0;
|
|
157
|
+
if (op === "<")
|
|
158
|
+
return cmp < 0;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cve-lite-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.25.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": {
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
"build": "tsc -p tsconfig.json",
|
|
11
11
|
"dev": "tsx src/index.ts .",
|
|
12
12
|
"start": "node dist/index.js .",
|
|
13
|
+
"lint:tests": "node scripts/check-test-hygiene.mjs",
|
|
14
|
+
"pretest": "node scripts/check-test-hygiene.mjs",
|
|
13
15
|
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --passWithNoTests",
|
|
14
16
|
"test:watch": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --watch --passWithNoTests"
|
|
15
17
|
},
|
|
@@ -23,7 +25,7 @@
|
|
|
23
25
|
"@types/better-sqlite3": "^7.6.13",
|
|
24
26
|
"@types/jest": "^30.0.0",
|
|
25
27
|
"@types/node": "^24.0.0",
|
|
26
|
-
"jest": "^30.
|
|
28
|
+
"jest": "^30.4.1",
|
|
27
29
|
"ts-jest": "^29.4.9",
|
|
28
30
|
"tsx": "^4.22.0",
|
|
29
31
|
"typescript": "^5.8.2"
|