cve-lite-cli 1.36.1 → 1.38.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 +25 -3
- package/dist/advisory/local-db.js +22 -3
- package/dist/audit-log/events.js +14 -0
- package/dist/cli/args.js +125 -102
- package/dist/cli/flags.js +39 -0
- package/dist/cli/help.js +8 -3
- package/dist/cli/overrides.js +25 -19
- package/dist/cli/validate.js +34 -9
- package/dist/index.js +6 -1
- package/dist/output/compact-truncation.js +35 -0
- package/dist/output/cyclonedx.js +6 -3
- package/dist/output/formatters.js +10 -2
- package/dist/output/html-reporter.js +31 -15
- package/dist/output/multi-folder-html-reporter.js +26 -7
- package/dist/output/output-dir.js +20 -0
- package/dist/output/override-findings-html.js +24 -3
- package/dist/output/override-findings-json.js +5 -2
- package/dist/output/override-findings-sarif.js +1 -0
- package/dist/output/override-findings-terminal.js +21 -1
- package/dist/output/printers.js +52 -25
- package/dist/output/sarif.js +63 -6
- package/dist/output/sbom-file.js +6 -4
- package/dist/output/scan-json.js +74 -0
- package/dist/output/skipped-override-rules.js +41 -0
- package/dist/output/spdx.js +2 -2
- package/dist/output/write-outputs.js +22 -32
- package/dist/overrides/admitted-scan.js +12 -1
- package/dist/overrides/api.js +75 -21
- package/dist/overrides/context-builder.js +17 -2
- package/dist/overrides/detectors/index.js +2 -0
- package/dist/overrides/detectors/oa008-materialized.js +74 -14
- package/dist/overrides/detectors/oa009-stale-floor.js +12 -15
- package/dist/overrides/detectors/oa010-vulnerable-floor.js +5 -1
- package/dist/overrides/detectors/oa011-exceeds-range.js +128 -0
- package/dist/overrides/detectors/pd001-override-only-phantom.js +5 -4
- package/dist/overrides/detectors/pd002-transitive-only-phantom.js +5 -4
- package/dist/overrides/detectors/phantom-utils.js +29 -0
- package/dist/overrides/parsing/registry.js +79 -10
- package/dist/remediation/fix-commands.js +89 -31
- package/dist/remediation/npm-registry.js +9 -1
- package/dist/remediation/parent-update-child-note.js +11 -0
- package/dist/scan/multi-folder-scan.js +68 -53
- package/dist/scan/override-audit.js +49 -0
- package/dist/scan/single-scan.js +115 -44
- package/dist/scanner.js +35 -11
- package/dist/skills/content.js +4 -2
- package/dist/usage/scanner.js +219 -28
- package/dist/utils/contextual-signals.js +193 -0
- package/dist/utils/create-pr.js +21 -2
- package/dist/utils/finding.js +5 -0
- package/dist/utils/fix-runner.js +15 -2
- package/dist/utils/package-json.js +66 -14
- package/dist/utils/severity.js +22 -0
- package/dist/utils/update-check.js +2 -1
- package/dist/utils/version.js +81 -86
- package/package.json +3 -1
- package/scripts/pr-scan-delta.mjs +7 -4
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { chalk } from "../utils/chalk.js";
|
|
4
|
-
import { serializeLicenseFindings } from "./license-findings-json.js";
|
|
5
|
-
import { serializeFinding } from "./formatters.js";
|
|
6
4
|
import { writeSarifReport, deriveLockfileUri } from "./sarif.js";
|
|
7
5
|
import { writeCycloneDxReport } from "./cyclonedx.js";
|
|
8
6
|
import { writeSpdxReport } from "./spdx.js";
|
|
9
7
|
import { resolveDependencyEdges } from "./sbom-dependency-edges.js";
|
|
10
8
|
import { resolveSbomFormat } from "../utils/sbom-format.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
9
|
+
import { buildScanJson } from "./scan-json.js";
|
|
10
|
+
import { resolveOutputDir, displayOutputPath } from "./output-dir.js";
|
|
13
11
|
function readProjectMeta(projectPath) {
|
|
14
12
|
try {
|
|
15
13
|
const pkgPath = path.join(projectPath, "package.json");
|
|
@@ -29,40 +27,32 @@ function readProjectMeta(projectPath) {
|
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
export async function writeOutputs(options, scanState, scanInput, projectPath) {
|
|
30
|
+
const outputDir = resolveOutputDir(options.output);
|
|
32
31
|
if (options.json) {
|
|
33
32
|
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
34
33
|
const jsonFilename = `cve-lite-scan-${ts}.json`;
|
|
35
|
-
const jsonOutputPath = path.join(
|
|
36
|
-
const payload = {
|
|
34
|
+
const jsonOutputPath = path.join(outputDir, jsonFilename);
|
|
35
|
+
const payload = buildScanJson({
|
|
37
36
|
projectPath,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
packageCount: scanInput.packages.length,
|
|
41
|
-
findingCount: scanState.sorted.length,
|
|
42
|
-
status: scanState.completeness.complete ? "ok" : "partial",
|
|
43
|
-
complete: scanState.completeness.complete,
|
|
44
|
-
diagnostics: scanState.completeness.diagnostics,
|
|
37
|
+
scanInput,
|
|
38
|
+
findings: scanState.sorted,
|
|
45
39
|
suggestedFixCommands: scanState.suggestedFixCommands,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
: {};
|
|
59
|
-
const combined = { ...payload, ...overridePayload, ...maintenancePayload, ...licensePayload };
|
|
60
|
-
fs.writeFileSync(jsonOutputPath, JSON.stringify(combined, null, 2));
|
|
61
|
-
console.log(`${chalk.gray("JSON saved to")} ${chalk.cyan(jsonFilename)}`);
|
|
40
|
+
coverage: scanState.coverage,
|
|
41
|
+
completeness: scanState.completeness,
|
|
42
|
+
overrideFindings: scanState.overrideFindings,
|
|
43
|
+
// Only passed when override checking ran, so `[]` means "ran, nothing skipped" and
|
|
44
|
+
// absent means "never audited". An unconditional `[]` collapses those two.
|
|
45
|
+
overrideSkipped: scanState.overrideSkipped,
|
|
46
|
+
maintenanceFindings: scanState.maintenanceFindings,
|
|
47
|
+
licenseFindings: scanState.licenseFindings,
|
|
48
|
+
licenseLimitedToDirectOnly: scanState.licenseLimitedToDirectOnly,
|
|
49
|
+
});
|
|
50
|
+
fs.writeFileSync(jsonOutputPath, JSON.stringify(payload, null, 2));
|
|
51
|
+
console.log(`${chalk.gray("JSON saved to")} ${chalk.cyan(displayOutputPath(jsonOutputPath))}`);
|
|
62
52
|
}
|
|
63
53
|
if (options.sarif) {
|
|
64
54
|
const lockfileUri = deriveLockfileUri(scanInput);
|
|
65
|
-
const sarifFilename = writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, scanState.overrideFindings, scanState.maintenanceFindings, scanState.licenseFindings);
|
|
55
|
+
const sarifFilename = writeSarifReport(scanState.sorted, lockfileUri, scanState.suggestedFixCommands, scanState.overrideFindings, scanState.maintenanceFindings, scanState.licenseFindings, outputDir, scanState.overrideSkipped);
|
|
66
56
|
console.log(`${chalk.gray("SARIF report written to")} ${chalk.cyan(sarifFilename)}`);
|
|
67
57
|
}
|
|
68
58
|
const sbomFormat = resolveSbomFormat(options);
|
|
@@ -74,11 +64,11 @@ export async function writeOutputs(options, scanState, scanInput, projectPath) {
|
|
|
74
64
|
// Resolved from the lockfile graph where available. Empty for pnpm,
|
|
75
65
|
// Yarn and Bun, which falls back to path-derived edges.
|
|
76
66
|
dependencyEdges: resolveDependencyEdges(scanInput, scanState.allPackages),
|
|
77
|
-
});
|
|
67
|
+
}, outputDir);
|
|
78
68
|
console.log(`${chalk.gray("SPDX SBOM written to")} ${chalk.cyan(spdxFilename)}`);
|
|
79
69
|
}
|
|
80
70
|
else {
|
|
81
|
-
const cdxFilename = writeCycloneDxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta);
|
|
71
|
+
const cdxFilename = writeCycloneDxReport(scanState.allPackages, scanState.sorted, scanState.suggestedFixCommands, projectMeta, outputDir);
|
|
82
72
|
console.log(`${chalk.gray("CycloneDX BOM written to")} ${chalk.cyan(cdxFilename)}`);
|
|
83
73
|
}
|
|
84
74
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { admittedKey } from "./context.js";
|
|
2
2
|
import { fetchVersionsBatch } from "./parsing/registry.js";
|
|
3
|
+
import { registryFailureReason } from "../output/skipped-override-rules.js";
|
|
3
4
|
import { looksLikeVersion, satisfiesRange, compareVersions } from "../utils/version.js";
|
|
4
5
|
import { inferSeverity } from "../osv/severity.js";
|
|
5
6
|
import { chunk } from "../utils/array.js";
|
|
@@ -49,7 +50,17 @@ export async function populateAdmittedVersions(ctx, opts) {
|
|
|
49
50
|
const entries = rangeEntries(ctx);
|
|
50
51
|
if (entries.length === 0)
|
|
51
52
|
return;
|
|
52
|
-
const versionsByName = await fetchVersionsBatch(entries.map((e) => e.name), opts.registry ?? {});
|
|
53
|
+
const { results: versionsByName, failed: versionFetchFailures } = await fetchVersionsBatch(entries.map((e) => e.name), opts.registry ?? {});
|
|
54
|
+
// Same reasoning as OA007 in api.ts: a total registry failure leaves this map empty and
|
|
55
|
+
// every entry below would `continue` past in silence, so OA010 would report nothing and
|
|
56
|
+
// look clean. Only a total failure is recorded, since a skip entry suppresses the rule.
|
|
57
|
+
if (versionsByName.size === 0 && versionFetchFailures.length > 0) {
|
|
58
|
+
ctx.skippedDetectors.push({
|
|
59
|
+
ruleId: "OA010",
|
|
60
|
+
reason: registryFailureReason(versionFetchFailures.length, "floored"),
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
53
64
|
const refs = [];
|
|
54
65
|
// A (name, version) pair can be admitted by more than one override range, so
|
|
55
66
|
// track which keys each pair belongs to and query the pair only once.
|
package/dist/overrides/api.js
CHANGED
|
@@ -1,32 +1,85 @@
|
|
|
1
1
|
import { populateAdmittedVersions } from "./admitted-scan.js";
|
|
2
2
|
import { ALL_DETECTORS, VERIFY_DETECTORS } from "./detectors/index.js";
|
|
3
3
|
import { applyComposite } from "./composite.js";
|
|
4
|
+
import { FLAG_CHECK_NETWORK } from "../cli/flags.js";
|
|
5
|
+
import { registryFailureReason } from "../output/skipped-override-rules.js";
|
|
4
6
|
import { fetchDistTagsBatch } from "./parsing/registry.js";
|
|
7
|
+
/**
|
|
8
|
+
* Records the rules that cannot run under the given options, before any detector runs.
|
|
9
|
+
* Detectors self-suppress by matching their own id against this list, so these entries
|
|
10
|
+
* are load-bearing rather than cosmetic.
|
|
11
|
+
*
|
|
12
|
+
* Nothing is recorded when the manifest declares no overrides. There is no audit to be
|
|
13
|
+
* incomplete, so warning about it would be both noisy and untrue, and it would fire on
|
|
14
|
+
* every plain `--check-overrides` run. Same guard the lockfile and node_modules skips in
|
|
15
|
+
* context-builder already use.
|
|
16
|
+
*/
|
|
17
|
+
function recordNetworkSkips(ctx, opts) {
|
|
18
|
+
if (ctx.overrideEntries.length === 0)
|
|
19
|
+
return;
|
|
20
|
+
const record = (ruleId, reason) => {
|
|
21
|
+
// audit() can be called more than once against one context; do not stack duplicates.
|
|
22
|
+
if (ctx.skippedDetectors.some((s) => s.ruleId === ruleId && s.reason === reason))
|
|
23
|
+
return;
|
|
24
|
+
ctx.skippedDetectors.push({ ruleId, reason });
|
|
25
|
+
};
|
|
26
|
+
if (!opts.checkNetwork) {
|
|
27
|
+
// Neither network-backed rule can run without the opt-in. Recorded rather than left
|
|
28
|
+
// to no-op on empty data: a scan that cannot run a rule has to say so instead of
|
|
29
|
+
// reporting a clean bill of health (#1222).
|
|
30
|
+
record("OA007", `requires ${FLAG_CHECK_NETWORK}`);
|
|
31
|
+
record("OA010", `requires ${FLAG_CHECK_NETWORK}`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (!opts.advisorySource) {
|
|
35
|
+
// Distinct from the flag being absent: the caller opted in but wired no advisory
|
|
36
|
+
// source, which is ours to fix rather than the user's.
|
|
37
|
+
record("OA010", "no advisory source was available");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* OA007's registry work, in its own function the way OA010's lives in
|
|
42
|
+
* populateAdmittedVersions. Never throws: a failure is recorded and the audit continues.
|
|
43
|
+
*/
|
|
44
|
+
async function populateRegistryDistTags(ctx, opts) {
|
|
45
|
+
if (!opts.checkNetwork)
|
|
46
|
+
return;
|
|
47
|
+
const stringOverrideNames = ctx.overrideEntries
|
|
48
|
+
.filter((e) => typeof e.value === "string")
|
|
49
|
+
.map((e) => e.packageName);
|
|
50
|
+
if (stringOverrideNames.length === 0)
|
|
51
|
+
return;
|
|
52
|
+
try {
|
|
53
|
+
const { results, failed } = await fetchDistTagsBatch(stringOverrideNames, opts.registry ?? {});
|
|
54
|
+
for (const [name, tags] of results) {
|
|
55
|
+
ctx.registryDistTags.set(name, tags);
|
|
56
|
+
}
|
|
57
|
+
// Every lookup failed, so OA007 has no data at all. Without this the rule returns no
|
|
58
|
+
// findings and the scan reports a clean bill of health, which is exactly what happens
|
|
59
|
+
// behind a TLS proxy. Recorded only on a TOTAL failure, because a skip entry makes the
|
|
60
|
+
// detector suppress itself and a partial result is still worth reporting. The partial
|
|
61
|
+
// case is a known gap tracked separately.
|
|
62
|
+
if (results.size === 0 && failed.length > 0) {
|
|
63
|
+
ctx.skippedDetectors.push({
|
|
64
|
+
ruleId: "OA007",
|
|
65
|
+
reason: registryFailureReason(failed.length, "overridden"),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
ctx.skippedDetectors.push({
|
|
71
|
+
ruleId: "OA007",
|
|
72
|
+
reason: `registry calls failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
5
76
|
/**
|
|
6
77
|
* Full audit: run all 8 detectors over the project, apply composite passes,
|
|
7
78
|
* emit oa.detected per finding.
|
|
8
79
|
*/
|
|
9
80
|
export async function audit(ctx, opts) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const stringOverrideNames = ctx.overrideEntries
|
|
13
|
-
.filter((e) => typeof e.value === "string")
|
|
14
|
-
.map((e) => e.packageName);
|
|
15
|
-
if (stringOverrideNames.length > 0) {
|
|
16
|
-
try {
|
|
17
|
-
const fetched = await fetchDistTagsBatch(stringOverrideNames, opts.registry ?? {});
|
|
18
|
-
for (const [name, tags] of fetched) {
|
|
19
|
-
ctx.registryDistTags.set(name, tags);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
catch (err) {
|
|
23
|
-
ctx.skippedDetectors.push({
|
|
24
|
-
ruleId: "OA007",
|
|
25
|
-
reason: `registry calls failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
81
|
+
recordNetworkSkips(ctx, opts);
|
|
82
|
+
await populateRegistryDistTags(ctx, opts);
|
|
30
83
|
// OA010 network fetch - needs both the opt-in and an injected advisory source.
|
|
31
84
|
if (opts.checkNetwork && opts.advisorySource) {
|
|
32
85
|
try {
|
|
@@ -59,7 +112,8 @@ export async function audit(ctx, opts) {
|
|
|
59
112
|
location: { file: f.location.file, jsonPath: f.location.jsonPath },
|
|
60
113
|
});
|
|
61
114
|
}
|
|
62
|
-
|
|
115
|
+
// Copied, so a caller holding the result cannot mutate the context.
|
|
116
|
+
return { findings, skippedDetectors: [...ctx.skippedDetectors] };
|
|
63
117
|
}
|
|
64
118
|
/**
|
|
65
119
|
* Post-fix verification: run OA001 and OA008 only, scoped to the just-touched
|
|
@@ -6,7 +6,9 @@ import { loadFromPackageLock } from "../parsers/package-lock.js";
|
|
|
6
6
|
import { loadFromPnpmLock } from "../parsers/pnpm-lock.js";
|
|
7
7
|
import { loadFromYarnLock } from "../parsers/yarn-lock.js";
|
|
8
8
|
import { loadFromBunLock } from "../parsers/bun-lock.js";
|
|
9
|
-
import { scanAllImports } from "../usage/scanner.js";
|
|
9
|
+
import { scanAllImports, usageScanTruncationWarning } from "../usage/scanner.js";
|
|
10
|
+
import { readWorkspaceMemberManifests } from "../utils/package-json.js";
|
|
11
|
+
import { getDeclaredPackages } from "./detectors/phantom-utils.js";
|
|
10
12
|
export function buildOverrideContext(projectPath, opts) {
|
|
11
13
|
const pkgJsonPath = join(projectPath, "package.json");
|
|
12
14
|
if (!existsSync(pkgJsonPath)) {
|
|
@@ -62,7 +64,18 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
62
64
|
const registryDistTags = new Map();
|
|
63
65
|
const admittedVersions = new Map();
|
|
64
66
|
const admittedVulnerable = new Map();
|
|
65
|
-
const
|
|
67
|
+
const importsResult = scanAllImports(projectPath);
|
|
68
|
+
const importedPackageNames = importsResult.imports;
|
|
69
|
+
if (importsResult.truncated) {
|
|
70
|
+
opts.logger.warn(usageScanTruncationWarning());
|
|
71
|
+
}
|
|
72
|
+
// Imports are collected from the whole tree, so on a monorepo root a member's
|
|
73
|
+
// own source is included. The PD detectors need each member's manifest to
|
|
74
|
+
// decide whether an import found under that member is actually declared.
|
|
75
|
+
const workspaceMembers = readWorkspaceMemberManifests(projectPath).map((member) => ({
|
|
76
|
+
dir: member.dir,
|
|
77
|
+
declared: getDeclaredPackages(member.manifest),
|
|
78
|
+
}));
|
|
66
79
|
return {
|
|
67
80
|
projectPath,
|
|
68
81
|
packageJson: parsed,
|
|
@@ -73,11 +86,13 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
73
86
|
installedVersions,
|
|
74
87
|
installedCopies: tree.installedCopies,
|
|
75
88
|
parentDeclarations: tree.parentDeclarations,
|
|
89
|
+
declarationsRootOnly: !nodeModulesExists,
|
|
76
90
|
registryDistTags,
|
|
77
91
|
admittedVersions,
|
|
78
92
|
admittedVulnerable,
|
|
79
93
|
skippedDetectors: skipped,
|
|
80
94
|
importedPackageNames,
|
|
95
|
+
workspaceMembers,
|
|
81
96
|
auditLog: opts.auditLog,
|
|
82
97
|
logger: opts.logger,
|
|
83
98
|
};
|
|
@@ -8,6 +8,7 @@ import { detect as detectOA007 } from "./oa007-frozen-latest.js";
|
|
|
8
8
|
import { detect as detectOA008 } from "./oa008-materialized.js";
|
|
9
9
|
import { detect as detectOA009 } from "./oa009-stale-floor.js";
|
|
10
10
|
import { detect as detectOA010 } from "./oa010-vulnerable-floor.js";
|
|
11
|
+
import { detect as detectOA011 } from "./oa011-exceeds-range.js";
|
|
11
12
|
import { detect as detectPD001 } from "./pd001-override-only-phantom.js";
|
|
12
13
|
import { detect as detectPD002 } from "./pd002-transitive-only-phantom.js";
|
|
13
14
|
export const ALL_DETECTORS = [
|
|
@@ -21,6 +22,7 @@ export const ALL_DETECTORS = [
|
|
|
21
22
|
{ ruleId: "OA008", detect: detectOA008 },
|
|
22
23
|
{ ruleId: "OA009", detect: detectOA009 },
|
|
23
24
|
{ ruleId: "OA010", detect: detectOA010 },
|
|
25
|
+
{ ruleId: "OA011", detect: detectOA011 },
|
|
24
26
|
{ ruleId: "PD001", detect: detectPD001 },
|
|
25
27
|
{ ruleId: "PD002", detect: detectPD002 },
|
|
26
28
|
];
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { satisfiesRange, coerceVersion, compareVersions, looksLikeVersion, isValidRange } from "../../utils/version.js";
|
|
1
|
+
import { satisfiesRange, coerceVersion, compareVersions, looksLikeVersion, isValidRange, positionAgainstRange, } from "../../utils/version.js";
|
|
2
|
+
import { pluralize } from "../../utils/string.js";
|
|
2
3
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
3
4
|
const RULE_ID = "OA008";
|
|
4
5
|
/**
|
|
5
|
-
* OA008 - the override declared a
|
|
6
|
-
* installed copy somewhere in the tree
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* OA008 - the override declared a range or exact-version floor, but an
|
|
7
|
+
* installed copy somewhere in the tree does not satisfy it. For a concrete
|
|
8
|
+
* pin, only copies below the floor are flagged. For a range, every copy outside
|
|
9
|
+
* the range is flagged, including versions above its upper bound. This is the
|
|
10
|
+
* post-install verification rule: "did the constraint I asked for actually get
|
|
11
|
+
* applied everywhere?"
|
|
11
12
|
*
|
|
12
13
|
* Considers every installed copy under node_modules, including nested ones.
|
|
13
14
|
* Skips overrides whose value isn't a clean range/version (floating tags are
|
|
@@ -26,12 +27,13 @@ export function detect(ctx) {
|
|
|
26
27
|
const copies = ctx.installedCopies.get(entry.packageName) ?? [];
|
|
27
28
|
if (copies.length === 0)
|
|
28
29
|
continue;
|
|
30
|
+
const isRangePin = isValidRange(pinValue) && !isConcrete(pinValue);
|
|
29
31
|
const vulnerable = copies.filter(c => {
|
|
30
32
|
const v = looksLikeVersion(c.version);
|
|
31
33
|
if (!v)
|
|
32
34
|
return false;
|
|
33
35
|
// For range pins, check `satisfiesRange`. For exact pins, the floor is the pin itself.
|
|
34
|
-
if (
|
|
36
|
+
if (isRangePin) {
|
|
35
37
|
return !satisfiesRange(c.version, pinValue);
|
|
36
38
|
}
|
|
37
39
|
// Concrete pin: floor IS the pin; below it is vulnerable.
|
|
@@ -41,18 +43,16 @@ export function detect(ctx) {
|
|
|
41
43
|
if (vulnerable.length === 0)
|
|
42
44
|
continue;
|
|
43
45
|
const versions = vulnerable.map(c => c.version);
|
|
44
|
-
const distinct = Array.from(new Set(versions)).sort();
|
|
46
|
+
const distinct = Array.from(new Set(versions)).sort(compareVersions);
|
|
45
47
|
findings.push({
|
|
46
48
|
ruleId: RULE_ID,
|
|
47
49
|
severity: "critical",
|
|
48
50
|
package: { name: entry.packageName },
|
|
49
51
|
location: { file: "package.json", jsonPath: jsonPointer(entry.path) },
|
|
50
52
|
message: "Override floor not applied - vulnerable copy still on disk",
|
|
51
|
-
details:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
`The override is not effective everywhere - likely a parent declares this dep ` +
|
|
55
|
-
`as exact and wins resolution.`,
|
|
53
|
+
details: isRangePin
|
|
54
|
+
? rangeMismatchDetails(entry.packageName, pinValue, vulnerable.length, distinct)
|
|
55
|
+
: concreteFloorDetails(entry.packageName, pinValue, vulnerable.length, distinct),
|
|
56
56
|
references: ["https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/OA008.md"],
|
|
57
57
|
});
|
|
58
58
|
}
|
|
@@ -73,3 +73,63 @@ function isCheckableFloor(value) {
|
|
|
73
73
|
function isConcrete(value) {
|
|
74
74
|
return looksLikeVersion(value);
|
|
75
75
|
}
|
|
76
|
+
const CAUSE_HINT = "The override is not effective everywhere - likely a parent declares this dep " +
|
|
77
|
+
"as exact and wins resolution.";
|
|
78
|
+
const NO_CAUSE_HINT = "The override is not effective everywhere.";
|
|
79
|
+
const POSITION_LABEL = {
|
|
80
|
+
below: "below the range",
|
|
81
|
+
above: "above the range",
|
|
82
|
+
outside: "outside the accepted intervals",
|
|
83
|
+
};
|
|
84
|
+
/** Shared opening clause: which override, and which copies contradict it. */
|
|
85
|
+
function copiesClause(packageName, pinValue, copyCount, versions) {
|
|
86
|
+
return (`Override demands ${packageName} ${pinValue}, but ${copyCount} installed ` +
|
|
87
|
+
`${pluralize(copyCount, "copy", "copies")} (${versions.join(", ")}) `);
|
|
88
|
+
}
|
|
89
|
+
function concreteFloorDetails(packageName, pinValue, copyCount, versions) {
|
|
90
|
+
return (copiesClause(packageName, pinValue, copyCount, versions) +
|
|
91
|
+
`${copyCount === 1 ? "is" : "are"} below the floor. ` +
|
|
92
|
+
CAUSE_HINT);
|
|
93
|
+
}
|
|
94
|
+
function rangeMismatchDetails(packageName, pinValue, copyCount, versions) {
|
|
95
|
+
const below = [];
|
|
96
|
+
const above = [];
|
|
97
|
+
const outside = [];
|
|
98
|
+
for (const version of versions) {
|
|
99
|
+
const position = positionAgainstRange(version, pinValue);
|
|
100
|
+
if (position === "below")
|
|
101
|
+
below.push(version);
|
|
102
|
+
else if (position === "above")
|
|
103
|
+
above.push(version);
|
|
104
|
+
else
|
|
105
|
+
outside.push(version);
|
|
106
|
+
}
|
|
107
|
+
// The explanation belongs to the DIRECTION of the mismatch, not the shape of
|
|
108
|
+
// the pin: a copy below the floor is the OA006-style resolution loss the hint
|
|
109
|
+
// describes, whether the pin was concrete or a range. Copies only above or
|
|
110
|
+
// outside the range have a different cause, so they get no hint (#1190).
|
|
111
|
+
const cause = below.length > 0 ? CAUSE_HINT : NO_CAUSE_HINT;
|
|
112
|
+
const populated = [
|
|
113
|
+
["below", below],
|
|
114
|
+
["above", above],
|
|
115
|
+
["outside", outside],
|
|
116
|
+
].filter(([, group]) => group.length > 0);
|
|
117
|
+
// One bucket needs no partition list: it would just reprint the versions the
|
|
118
|
+
// opening clause already named.
|
|
119
|
+
if (populated.length === 1) {
|
|
120
|
+
const [position] = populated[0];
|
|
121
|
+
return (copiesClause(packageName, pinValue, copyCount, versions) +
|
|
122
|
+
`${copyCount === 1 ? "is" : "are"} ${POSITION_LABEL[position]}. ` +
|
|
123
|
+
cause);
|
|
124
|
+
}
|
|
125
|
+
const partitions = populated
|
|
126
|
+
.map(([position, group]) => {
|
|
127
|
+
const label = POSITION_LABEL[position];
|
|
128
|
+
const capitalizedLabel = label.charAt(0).toUpperCase() + label.slice(1);
|
|
129
|
+
return `${capitalizedLabel}: ${group.join(", ")}.`;
|
|
130
|
+
})
|
|
131
|
+
.join(" ");
|
|
132
|
+
return (copiesClause(packageName, pinValue, copyCount, versions) +
|
|
133
|
+
`do not satisfy the range. ` +
|
|
134
|
+
`${partitions} ${cause}`);
|
|
135
|
+
}
|
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
2
2
|
import { looksLikeVersion, coerceVersion, compareVersions } from "../../utils/version.js";
|
|
3
3
|
import { OVERRIDES_COMMAND } from "../../constants.js";
|
|
4
|
+
import { getDeclaredPackages, undeclaredImportFiles } from "./phantom-utils.js";
|
|
4
5
|
const RULE_ID = "OA009";
|
|
5
6
|
function majorOf(version) {
|
|
6
7
|
return parseInt(version.split(".")[0] ?? "0", 10);
|
|
7
8
|
}
|
|
8
|
-
function getDeclaredPackages(packageJson) {
|
|
9
|
-
const declared = new Set();
|
|
10
|
-
for (const section of ["dependencies", "devDependencies"]) {
|
|
11
|
-
const deps = packageJson[section];
|
|
12
|
-
if (deps && typeof deps === "object" && !Array.isArray(deps)) {
|
|
13
|
-
for (const name of Object.keys(deps)) {
|
|
14
|
-
declared.add(name);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return declared;
|
|
19
|
-
}
|
|
20
9
|
export function detect(ctx) {
|
|
21
10
|
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
22
11
|
return [];
|
|
23
12
|
const findings = [];
|
|
13
|
+
// Computed once: the same for every override entry, and PD001/PD002 do the same.
|
|
14
|
+
const rootDeclared = getDeclaredPackages(ctx.packageJson);
|
|
24
15
|
for (const entry of ctx.overrideEntries) {
|
|
25
16
|
if (typeof entry.value !== "string")
|
|
26
17
|
continue;
|
|
@@ -53,9 +44,15 @@ export function detect(ctx) {
|
|
|
53
44
|
// Safety guard: if this package is imported in source but not declared in
|
|
54
45
|
// dependencies, the override is the only thing making it available. Removing
|
|
55
46
|
// it would create a phantom import (PD001). OA009 must not fire.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
//
|
|
48
|
+
// This must answer the declaration question the same way PD001 does, via the
|
|
49
|
+
// shared undeclaredImportFiles, or the two rules disagree on a workspace: a
|
|
50
|
+
// package declared in apps/web/package.json but not at the root makes PD001
|
|
51
|
+
// correctly fall silent while OA009 keeps suppressing itself, and a genuinely
|
|
52
|
+
// redundant floor goes unreported by either (#1118).
|
|
53
|
+
const importedIn = ctx.importedPackageNames.get(entry.packageName);
|
|
54
|
+
if (importedIn && importedIn.length > 0) {
|
|
55
|
+
if (undeclaredImportFiles(ctx, entry.packageName, importedIn, rootDeclared).length > 0)
|
|
59
56
|
continue;
|
|
60
57
|
}
|
|
61
58
|
findings.push({
|
|
@@ -2,6 +2,7 @@ import { admittedKey } from "../context.js";
|
|
|
2
2
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
3
3
|
import { looksLikeVersion, compareVersions, majorVersion } from "../../utils/version.js";
|
|
4
4
|
import { OVERRIDES_COMMAND } from "../../constants.js";
|
|
5
|
+
import { FLAG_CHECK_NETWORK } from "../../cli/flags.js";
|
|
5
6
|
const RULE_ID = "OA010";
|
|
6
7
|
/**
|
|
7
8
|
* Severity ordering for picking the worst advisory in an admitted set. "unknown"
|
|
@@ -115,7 +116,10 @@ export function detect(ctx) {
|
|
|
115
116
|
fix: {
|
|
116
117
|
type: "rfc6902",
|
|
117
118
|
patch: [{ op: "replace", path: jsonPointer(entry.path), value: rewritten }],
|
|
118
|
-
|
|
119
|
+
// --check-network is part of the command because without it this very rule does
|
|
120
|
+
// not run, so the command would answer "OA010 did not run" for a finding OA010
|
|
121
|
+
// just reported.
|
|
122
|
+
runnableCommand: `${OVERRIDES_COMMAND} --fix --rule ${RULE_ID} ${FLAG_CHECK_NETWORK}`,
|
|
119
123
|
},
|
|
120
124
|
});
|
|
121
125
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
2
|
+
import { looksLikeVersion, coerceVersion, majorVersion } from "../../utils/version.js";
|
|
3
|
+
const RULE_ID = "OA011";
|
|
4
|
+
/**
|
|
5
|
+
* Highest major version a `^X`, bare `>=X`, or compound `>=X <Y` range can
|
|
6
|
+
* resolve to. `^` and a `<` ceiling both bound the range to a single major
|
|
7
|
+
* (or, when the ceiling isn't a clean major boundary like `<5.2.0`, to the
|
|
8
|
+
* ceiling's own major - 5.1.99 is still reachable). A bare `>=X` has no
|
|
9
|
+
* ceiling at all, so it can resolve into any future major: Infinity.
|
|
10
|
+
* A concrete version (exact pin) is bounded to its own major.
|
|
11
|
+
* Returns null for any shape this rule doesn't understand - callers treat
|
|
12
|
+
* that as "can't prove anything", the same conservative stance OA009 takes.
|
|
13
|
+
*/
|
|
14
|
+
function reachableMaxMajor(value) {
|
|
15
|
+
const trimmed = value.trim();
|
|
16
|
+
if (looksLikeVersion(trimmed))
|
|
17
|
+
return majorVersion(trimmed);
|
|
18
|
+
const tokens = trimmed.split(/\s+/).filter(Boolean);
|
|
19
|
+
if (tokens.length === 1 && tokens[0].startsWith("^")) {
|
|
20
|
+
const floor = coerceVersion(tokens[0].slice(1));
|
|
21
|
+
return floor ? majorVersion(floor) : null;
|
|
22
|
+
}
|
|
23
|
+
if (tokens.length === 1 && tokens[0].startsWith(">=")) {
|
|
24
|
+
const floor = coerceVersion(tokens[0].slice(2));
|
|
25
|
+
return floor ? Number.POSITIVE_INFINITY : null;
|
|
26
|
+
}
|
|
27
|
+
if (tokens.length === 2 &&
|
|
28
|
+
tokens[0].startsWith(">=") &&
|
|
29
|
+
tokens[1].startsWith("<") &&
|
|
30
|
+
!tokens[1].startsWith("<=")) {
|
|
31
|
+
const ceiling = coerceVersion(tokens[1].slice(1));
|
|
32
|
+
if (!ceiling)
|
|
33
|
+
return null;
|
|
34
|
+
const [ceilingMajor, ceilingMinor, ceilingPatch] = ceiling.split(".").map(Number);
|
|
35
|
+
return ceilingMinor === 0 && ceilingPatch === 0 ? ceilingMajor - 1 : ceilingMajor;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
/** Floor of a `^X`, `>=X`, or `>=X <Y` range - for the finding message only. */
|
|
40
|
+
function floorMajorOf(value) {
|
|
41
|
+
const trimmed = value.trim();
|
|
42
|
+
if (looksLikeVersion(trimmed))
|
|
43
|
+
return majorVersion(trimmed);
|
|
44
|
+
const tokens = trimmed.split(/\s+/).filter(Boolean);
|
|
45
|
+
const floorToken = tokens.find((t) => t.startsWith("^") || t.startsWith(">="));
|
|
46
|
+
if (!floorToken)
|
|
47
|
+
return null;
|
|
48
|
+
const raw = floorToken.startsWith("^") ? floorToken.slice(1) : floorToken.slice(2);
|
|
49
|
+
const floor = coerceVersion(raw);
|
|
50
|
+
return floor ? majorVersion(floor) : null;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* OA011 - the inverse of OA009. OA009 asks whether a floor is already met
|
|
54
|
+
* by every parent declaration (redundant, safe to remove). This asks
|
|
55
|
+
* whether the floor can resolve *above* what every parent declaration
|
|
56
|
+
* permits - a range with no ceiling, or a ceiling above every parent's own,
|
|
57
|
+
* lets npm/pnpm resolve into a major none of them ever declared support for.
|
|
58
|
+
*
|
|
59
|
+
* Scoped to the same range shapes OA009 and OA010 already handle: `^X`,
|
|
60
|
+
* bare `>=X`, and compound `>=X <Y`. Anything else (tilde, exact pins,
|
|
61
|
+
* dist-tags, workspace:/file:/link: protocols) is out of scope, the same
|
|
62
|
+
* boundary OA009 draws.
|
|
63
|
+
*/
|
|
64
|
+
export function detect(ctx) {
|
|
65
|
+
const rootOnly = ctx.declarationsRootOnly === true;
|
|
66
|
+
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
67
|
+
return [];
|
|
68
|
+
const findings = [];
|
|
69
|
+
for (const entry of ctx.overrideEntries) {
|
|
70
|
+
if (typeof entry.value !== "string")
|
|
71
|
+
continue;
|
|
72
|
+
const pin = entry.value.trim();
|
|
73
|
+
if (!pin.startsWith(">=") && !pin.startsWith("^"))
|
|
74
|
+
continue;
|
|
75
|
+
const overrideMax = reachableMaxMajor(pin);
|
|
76
|
+
if (overrideMax === null)
|
|
77
|
+
continue;
|
|
78
|
+
const parents = ctx.parentDeclarations.get(entry.packageName) ?? [];
|
|
79
|
+
if (parents.length === 0)
|
|
80
|
+
continue;
|
|
81
|
+
const parentMaxes = parents.map((p) => reachableMaxMajor(p.declaredValue));
|
|
82
|
+
// Conservative: a declaration we can't reduce to a major might well
|
|
83
|
+
// overlap the override's range, so one unparseable parent silences the
|
|
84
|
+
// whole finding rather than risk a false positive.
|
|
85
|
+
if (parentMaxes.some((m) => m === null))
|
|
86
|
+
continue;
|
|
87
|
+
const highestParentMax = Math.max(...parentMaxes);
|
|
88
|
+
if (overrideMax <= highestParentMax)
|
|
89
|
+
continue;
|
|
90
|
+
const floorMajor = floorMajorOf(pin);
|
|
91
|
+
const overrideDescription = overrideMax === Number.POSITIVE_INFINITY
|
|
92
|
+
? `major ${floorMajor ?? "?"} and every major above it`
|
|
93
|
+
: `up to major ${overrideMax}`;
|
|
94
|
+
const parentList = parents
|
|
95
|
+
.map((p) => `${p.parentName}@${p.declaredValue}`)
|
|
96
|
+
.join(", ");
|
|
97
|
+
findings.push({
|
|
98
|
+
ruleId: RULE_ID,
|
|
99
|
+
severity: "medium",
|
|
100
|
+
package: { name: entry.packageName },
|
|
101
|
+
location: { file: "package.json", jsonPath: jsonPointer(entry.path) },
|
|
102
|
+
// Without node_modules the declaration map holds the root manifest alone, so
|
|
103
|
+
// the complete-set wording would assert something this scan never checked and
|
|
104
|
+
// the remedy would be derived from one manifest. Both are scoped down instead.
|
|
105
|
+
message: rootOnly
|
|
106
|
+
? "Override floor exceeds the range declared in the root manifest"
|
|
107
|
+
: "Override floor exceeds every declaring package's range",
|
|
108
|
+
details: rootOnly
|
|
109
|
+
? `${entry.packageName} is overridden to ${pin}, which admits ${overrideDescription}. ` +
|
|
110
|
+
`The root manifest declares it (${parentList}) capping out at major ${highestParentMax}, ` +
|
|
111
|
+
`so a resolution above that is not something this project's own manifest permits - what ` +
|
|
112
|
+
`gets installed and what gets published as its requirements would disagree. This scan ran ` +
|
|
113
|
+
`without node_modules, so packages that declare it transitively were not visible and may ` +
|
|
114
|
+
`permit a higher major; re-run after an install to check them. Lower the override to stay ` +
|
|
115
|
+
`within major ${highestParentMax}, or raise the declaration if the newer major is intended.`
|
|
116
|
+
: `${entry.packageName} is overridden to ${pin}, which admits ${overrideDescription}. ` +
|
|
117
|
+
`Every package that declares it (${parentList}) caps out at major ${highestParentMax}, ` +
|
|
118
|
+
`so a resolution above that is not something any consumer's manifest actually permits - ` +
|
|
119
|
+
`what gets installed and what gets published as this project's own requirements would ` +
|
|
120
|
+
`disagree. Lower the override to stay within major ${highestParentMax}, or raise the ` +
|
|
121
|
+
`declaring package(s) if the newer major is intended.`,
|
|
122
|
+
references: [
|
|
123
|
+
"https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/OA011.md",
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return findings;
|
|
128
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getDeclaredPackages, installCmd } from "./phantom-utils.js";
|
|
1
|
+
import { getDeclaredPackages, installCmd, undeclaredImportFiles } from "./phantom-utils.js";
|
|
2
2
|
const RULE_ID = "PD001";
|
|
3
3
|
export function detect(ctx) {
|
|
4
4
|
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
@@ -9,11 +9,12 @@ export function detect(ctx) {
|
|
|
9
9
|
const overrideNames = new Set(ctx.overrideEntries.map((e) => e.packageName));
|
|
10
10
|
const cmd = installCmd(ctx.packageManager);
|
|
11
11
|
const findings = [];
|
|
12
|
-
for (const [pkgName,
|
|
13
|
-
if (declared.has(pkgName))
|
|
14
|
-
continue;
|
|
12
|
+
for (const [pkgName, importedIn] of ctx.importedPackageNames) {
|
|
15
13
|
if (!overrideNames.has(pkgName))
|
|
16
14
|
continue;
|
|
15
|
+
const files = undeclaredImportFiles(ctx, pkgName, importedIn, declared);
|
|
16
|
+
if (files.length === 0)
|
|
17
|
+
continue;
|
|
17
18
|
const shown = files.slice(0, 3);
|
|
18
19
|
const extra = files.length > 3 ? ` (+${files.length - 3} more)` : "";
|
|
19
20
|
findings.push({
|