cve-lite-cli 1.37.0 → 1.39.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 +20 -1
- package/dist/cli/args.js +121 -104
- package/dist/cli/flags.js +39 -0
- package/dist/cli/help.js +6 -3
- package/dist/cli/overrides.js +25 -19
- package/dist/cli/validate.js +31 -9
- package/dist/index.js +6 -1
- package/dist/output/compact-truncation.js +35 -0
- package/dist/output/formatters.js +10 -2
- package/dist/output/html-reporter.js +24 -12
- package/dist/output/multi-folder-html-reporter.js +26 -7
- 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 +34 -21
- package/dist/output/sarif.js +54 -3
- package/dist/output/scan-json.js +74 -0
- package/dist/output/skipped-override-rules.js +41 -0
- package/dist/output/write-outputs.js +16 -28
- package/dist/overrides/admitted-scan.js +12 -1
- package/dist/overrides/api.js +75 -21
- package/dist/overrides/context-builder.js +49 -6
- package/dist/overrides/detectors/index.js +2 -0
- package/dist/overrides/detectors/oa005-nested-ineffective.js +50 -2
- package/dist/overrides/detectors/oa008-materialized.js +72 -13
- 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/scan/multi-folder-scan.js +65 -51
- package/dist/scan/override-audit.js +49 -0
- package/dist/scan/single-scan.js +109 -33
- package/dist/scanner.js +8 -4
- 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/finding.js +5 -0
- package/dist/utils/node-builtins.js +32 -0
- 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 +37 -19
- package/package.json +1 -1
|
@@ -6,7 +6,10 @@ 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 { isNodeBuiltinSpecifier } from "../utils/node-builtins.js";
|
|
12
|
+
import { getDeclaredPackages } from "./detectors/phantom-utils.js";
|
|
10
13
|
export function buildOverrideContext(projectPath, opts) {
|
|
11
14
|
const pkgJsonPath = join(projectPath, "package.json");
|
|
12
15
|
if (!existsSync(pkgJsonPath)) {
|
|
@@ -18,6 +21,7 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
18
21
|
const packageManager = detectPackageManager(projectPath);
|
|
19
22
|
const readResult = readLockfileNames(projectPath, packageManager);
|
|
20
23
|
const lockfilePackageNames = readResult.kind === "ok" ? readResult.names : new Set();
|
|
24
|
+
const lockfileVersions = readResult.kind === "ok" ? readResult.versions : new Map();
|
|
21
25
|
const nodeModulesExists = existsSync(join(projectPath, "node_modules"));
|
|
22
26
|
const skipped = [];
|
|
23
27
|
if (lockfilePackageNames.size === 0 && overrideEntries.length > 0) {
|
|
@@ -62,7 +66,22 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
62
66
|
const registryDistTags = new Map();
|
|
63
67
|
const admittedVersions = new Map();
|
|
64
68
|
const admittedVulnerable = new Map();
|
|
65
|
-
const
|
|
69
|
+
const importsResult = scanAllImports(projectPath);
|
|
70
|
+
// A built-in module is not a package, so it must not enter a map the phantom
|
|
71
|
+
// rules read as "packages this project imports". Filtering at construction
|
|
72
|
+
// covers PD001, PD002 and OA009's declaration guard in one place, rather than
|
|
73
|
+
// leaving each detector to remember.
|
|
74
|
+
const importedPackageNames = new Map([...importsResult.imports].filter(([specifier]) => !isNodeBuiltinSpecifier(specifier)));
|
|
75
|
+
if (importsResult.truncated) {
|
|
76
|
+
opts.logger.warn(usageScanTruncationWarning());
|
|
77
|
+
}
|
|
78
|
+
// Imports are collected from the whole tree, so on a monorepo root a member's
|
|
79
|
+
// own source is included. The PD detectors need each member's manifest to
|
|
80
|
+
// decide whether an import found under that member is actually declared.
|
|
81
|
+
const workspaceMembers = readWorkspaceMemberManifests(projectPath).map((member) => ({
|
|
82
|
+
dir: member.dir,
|
|
83
|
+
declared: getDeclaredPackages(member.manifest),
|
|
84
|
+
}));
|
|
66
85
|
return {
|
|
67
86
|
projectPath,
|
|
68
87
|
packageJson: parsed,
|
|
@@ -70,14 +89,17 @@ export function buildOverrideContext(projectPath, opts) {
|
|
|
70
89
|
packageManager,
|
|
71
90
|
overrideEntries,
|
|
72
91
|
lockfilePackageNames,
|
|
92
|
+
lockfileVersions,
|
|
73
93
|
installedVersions,
|
|
74
94
|
installedCopies: tree.installedCopies,
|
|
75
95
|
parentDeclarations: tree.parentDeclarations,
|
|
96
|
+
declarationsRootOnly: !nodeModulesExists,
|
|
76
97
|
registryDistTags,
|
|
77
98
|
admittedVersions,
|
|
78
99
|
admittedVulnerable,
|
|
79
100
|
skippedDetectors: skipped,
|
|
80
101
|
importedPackageNames,
|
|
102
|
+
workspaceMembers,
|
|
81
103
|
auditLog: opts.auditLog,
|
|
82
104
|
logger: opts.logger,
|
|
83
105
|
};
|
|
@@ -93,6 +115,27 @@ function detectPackageManager(projectPath) {
|
|
|
93
115
|
return "npm";
|
|
94
116
|
return "unknown";
|
|
95
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Groups resolved lockfile versions by package name. A package can appear at
|
|
120
|
+
* several versions in one tree, so every one is kept: an override selector only
|
|
121
|
+
* has to match one of them to be live.
|
|
122
|
+
*/
|
|
123
|
+
function versionsByName(refs) {
|
|
124
|
+
const byName = new Map();
|
|
125
|
+
for (const ref of refs) {
|
|
126
|
+
if (!ref.version)
|
|
127
|
+
continue;
|
|
128
|
+
const seen = byName.get(ref.name);
|
|
129
|
+
if (seen) {
|
|
130
|
+
if (!seen.includes(ref.version))
|
|
131
|
+
seen.push(ref.version);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
byName.set(ref.name, [ref.version]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return byName;
|
|
138
|
+
}
|
|
96
139
|
function readLockfileNames(projectPath, pm) {
|
|
97
140
|
if (pm === "npm") {
|
|
98
141
|
const lockPath = join(projectPath, "package-lock.json");
|
|
@@ -100,7 +143,7 @@ function readLockfileNames(projectPath, pm) {
|
|
|
100
143
|
return { kind: "absent" };
|
|
101
144
|
try {
|
|
102
145
|
const refs = loadFromPackageLock(lockPath, false);
|
|
103
|
-
return { kind: "ok", names: new Set(refs.map((r) => r.name)) };
|
|
146
|
+
return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
|
|
104
147
|
}
|
|
105
148
|
catch (e) {
|
|
106
149
|
const message = e instanceof Error ? e.message : String(e);
|
|
@@ -113,7 +156,7 @@ function readLockfileNames(projectPath, pm) {
|
|
|
113
156
|
return { kind: "absent" };
|
|
114
157
|
try {
|
|
115
158
|
const refs = loadFromPnpmLock(lockPath, false);
|
|
116
|
-
return { kind: "ok", names: new Set(refs.map((r) => r.name)) };
|
|
159
|
+
return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
|
|
117
160
|
}
|
|
118
161
|
catch (e) {
|
|
119
162
|
const message = e instanceof Error ? e.message : String(e);
|
|
@@ -126,7 +169,7 @@ function readLockfileNames(projectPath, pm) {
|
|
|
126
169
|
return { kind: "absent" };
|
|
127
170
|
try {
|
|
128
171
|
const refs = loadFromYarnLock(lockPath);
|
|
129
|
-
return { kind: "ok", names: new Set(refs.map((r) => r.name)) };
|
|
172
|
+
return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
|
|
130
173
|
}
|
|
131
174
|
catch (e) {
|
|
132
175
|
const message = e instanceof Error ? e.message : String(e);
|
|
@@ -139,7 +182,7 @@ function readLockfileNames(projectPath, pm) {
|
|
|
139
182
|
return { kind: "absent" };
|
|
140
183
|
try {
|
|
141
184
|
const refs = loadFromBunLock(lockPath, false);
|
|
142
|
-
return { kind: "ok", names: new Set(refs.map((r) => r.name)) };
|
|
185
|
+
return { kind: "ok", names: new Set(refs.map((r) => r.name)), versions: versionsByName(refs) };
|
|
143
186
|
}
|
|
144
187
|
catch (e) {
|
|
145
188
|
const message = e instanceof Error ? e.message : String(e);
|
|
@@ -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
|
];
|
|
@@ -60,6 +60,45 @@ export function detect(ctx) {
|
|
|
60
60
|
}
|
|
61
61
|
return findings;
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Splits an override key into the package name and the version selector npm
|
|
65
|
+
* allows after it, so `minimatch@3` and `@scope/pkg@^2` both resolve. A bare
|
|
66
|
+
* name, or a scoped name with no selector, comes back with a null range: the
|
|
67
|
+
* leading `@` of a scope is never a separator.
|
|
68
|
+
*/
|
|
69
|
+
function splitSelector(key) {
|
|
70
|
+
const at = key.lastIndexOf("@");
|
|
71
|
+
if (at <= 0)
|
|
72
|
+
return { name: key, range: null };
|
|
73
|
+
return { name: key.slice(0, at), range: key.slice(at + 1) };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Describes why an override's outer parent cannot be matched in the tree, or
|
|
77
|
+
* null when it can.
|
|
78
|
+
*
|
|
79
|
+
* The key may carry a selector, and npm treats that as a range rather than an
|
|
80
|
+
* exact version, so `minimatch@3` is satisfied by an installed 3.1.2. Comparing
|
|
81
|
+
* the whole key against the set of package names is what made a working
|
|
82
|
+
* override look dead.
|
|
83
|
+
*
|
|
84
|
+
* Every uncertain case resolves to null, meaning no finding. Claiming an
|
|
85
|
+
* override is dead is the expensive direction to be wrong in: it tells someone
|
|
86
|
+
* to delete protection that is working. So an unparseable selector, or a
|
|
87
|
+
* package present with no version data, both stay silent rather than guess.
|
|
88
|
+
*/
|
|
89
|
+
function outerParentProblem(ctx, outerKey) {
|
|
90
|
+
const { name, range } = splitSelector(outerKey);
|
|
91
|
+
if (!ctx.lockfilePackageNames.has(name))
|
|
92
|
+
return "is not in the dependency tree";
|
|
93
|
+
if (!range || !isValidRange(range))
|
|
94
|
+
return null;
|
|
95
|
+
const resolved = ctx.lockfileVersions.get(name) ?? [];
|
|
96
|
+
if (resolved.length === 0)
|
|
97
|
+
return null;
|
|
98
|
+
if (resolved.some((version) => satisfiesRange(version, range)))
|
|
99
|
+
return null;
|
|
100
|
+
return `is in the tree at ${resolved.join(", ")}, and the selector "${range}" matches none of them`;
|
|
101
|
+
}
|
|
63
102
|
function classify(args) {
|
|
64
103
|
const { ctx, outerKey, innerKey, innerValue, entryPath, lookup, form } = args;
|
|
65
104
|
const findingBase = (subId, severity, message, details, action) => ({
|
|
@@ -95,8 +134,9 @@ function classify(args) {
|
|
|
95
134
|
return findingBase("OA005.a", "critical", "Nested override ineffective for this package manager (silently ignored)", formDesc, "remove");
|
|
96
135
|
}
|
|
97
136
|
// .b - outer parent not in resolved tree.
|
|
98
|
-
|
|
99
|
-
|
|
137
|
+
const outerProblem = outerParentProblem(ctx, outerKey);
|
|
138
|
+
if (outerProblem) {
|
|
139
|
+
return findingBase("OA005.b", "high", "Nested override outer parent not in resolved tree", `Outer parent ${outerKey} ${outerProblem}. The nested override "${innerKey}" has no parent to apply to.`, "remove");
|
|
100
140
|
}
|
|
101
141
|
// .c - outer in tree but inner is not declared in parent's manifest deps.
|
|
102
142
|
const parentManifest = lookup(outerKey);
|
|
@@ -125,5 +165,13 @@ function classify(args) {
|
|
|
125
165
|
// override is presumed fine - a hygiene auditor flags proven problems, not maybes.
|
|
126
166
|
if (form === "flat-pnpm")
|
|
127
167
|
return null;
|
|
168
|
+
// Same reasoning for an npm key that carries a version selector. `minimatch@3`
|
|
169
|
+
// is the npm spelling of a scoped override: the selector exists so the pin
|
|
170
|
+
// reaches the minimatch 3.x copy and leaves the others alone. Suggesting a flat
|
|
171
|
+
// form there is the same bad advice, since flattening would apply the pin to
|
|
172
|
+
// every copy in the tree. Only an unselectored key, where the nested and flat
|
|
173
|
+
// forms really are close to equivalent, is worth mentioning.
|
|
174
|
+
if (splitSelector(outerKey).range !== null)
|
|
175
|
+
return null;
|
|
128
176
|
return findingBase("OA005.e", "low", "Nested override could be flattened to top-level", `Nested override ${outerKey}.${innerKey} is valid and effective. A flat top-level "overrides": { "${innerKey}": "${innerValue}" } would apply across the whole tree.`, "suggest");
|
|
129
177
|
}
|
|
@@ -1,14 +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
2
|
import { pluralize } from "../../utils/string.js";
|
|
3
3
|
import { jsonPointer } from "../parsing/json-pointer.js";
|
|
4
4
|
const RULE_ID = "OA008";
|
|
5
5
|
/**
|
|
6
|
-
* OA008 - the override declared a
|
|
7
|
-
* installed copy somewhere in the tree
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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?"
|
|
12
12
|
*
|
|
13
13
|
* Considers every installed copy under node_modules, including nested ones.
|
|
14
14
|
* Skips overrides whose value isn't a clean range/version (floating tags are
|
|
@@ -27,12 +27,13 @@ export function detect(ctx) {
|
|
|
27
27
|
const copies = ctx.installedCopies.get(entry.packageName) ?? [];
|
|
28
28
|
if (copies.length === 0)
|
|
29
29
|
continue;
|
|
30
|
+
const isRangePin = isValidRange(pinValue) && !isConcrete(pinValue);
|
|
30
31
|
const vulnerable = copies.filter(c => {
|
|
31
32
|
const v = looksLikeVersion(c.version);
|
|
32
33
|
if (!v)
|
|
33
34
|
return false;
|
|
34
35
|
// For range pins, check `satisfiesRange`. For exact pins, the floor is the pin itself.
|
|
35
|
-
if (
|
|
36
|
+
if (isRangePin) {
|
|
36
37
|
return !satisfiesRange(c.version, pinValue);
|
|
37
38
|
}
|
|
38
39
|
// Concrete pin: floor IS the pin; below it is vulnerable.
|
|
@@ -49,11 +50,9 @@ export function detect(ctx) {
|
|
|
49
50
|
package: { name: entry.packageName },
|
|
50
51
|
location: { file: "package.json", jsonPath: jsonPointer(entry.path) },
|
|
51
52
|
message: "Override floor not applied - vulnerable copy still on disk",
|
|
52
|
-
details:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
`The override is not effective everywhere - likely a parent declares this dep ` +
|
|
56
|
-
`as exact and wins resolution.`,
|
|
53
|
+
details: isRangePin
|
|
54
|
+
? rangeMismatchDetails(entry.packageName, pinValue, vulnerable.length, distinct)
|
|
55
|
+
: concreteFloorDetails(entry.packageName, pinValue, vulnerable.length, distinct),
|
|
57
56
|
references: ["https://github.com/OWASP/cve-lite-cli/blob/main/docs/rules/OA008.md"],
|
|
58
57
|
});
|
|
59
58
|
}
|
|
@@ -74,3 +73,63 @@ function isCheckableFloor(value) {
|
|
|
74
73
|
function isConcrete(value) {
|
|
75
74
|
return looksLikeVersion(value);
|
|
76
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({
|
|
@@ -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 = "PD002";
|
|
3
3
|
export function detect(ctx) {
|
|
4
4
|
if (ctx.skippedDetectors.some((s) => s.ruleId === RULE_ID))
|
|
@@ -9,13 +9,14 @@ 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; // PD001 owns this
|
|
17
15
|
if (!ctx.lockfilePackageNames.has(pkgName))
|
|
18
16
|
continue; // not in graph
|
|
17
|
+
const files = undeclaredImportFiles(ctx, pkgName, importedIn, declared);
|
|
18
|
+
if (files.length === 0)
|
|
19
|
+
continue;
|
|
19
20
|
const shown = files.slice(0, 3);
|
|
20
21
|
const extra = files.length > 3 ? ` (+${files.length - 3} more)` : "";
|
|
21
22
|
findings.push({
|
|
@@ -10,6 +10,35 @@ export function getDeclaredPackages(packageJson) {
|
|
|
10
10
|
}
|
|
11
11
|
return declared;
|
|
12
12
|
}
|
|
13
|
+
// The importing files for which `pkgName` is genuinely undeclared. A root
|
|
14
|
+
// declaration satisfies every file (root node_modules is on every member's
|
|
15
|
+
// resolution path). Otherwise a file is resolved against the nearest enclosing
|
|
16
|
+
// workspace member: a dependency declared in apps/web/package.json is a real
|
|
17
|
+
// direct dependency for apps/web/src/*, not a phantom. Files outside any member
|
|
18
|
+
// fall back to the root manifest.
|
|
19
|
+
export function undeclaredImportFiles(ctx, pkgName, files, rootDeclared) {
|
|
20
|
+
if (rootDeclared.has(pkgName))
|
|
21
|
+
return [];
|
|
22
|
+
const members = ctx.workspaceMembers ?? [];
|
|
23
|
+
if (members.length === 0)
|
|
24
|
+
return files;
|
|
25
|
+
return files.filter((file) => {
|
|
26
|
+
const owner = owningWorkspaceMember(members, file);
|
|
27
|
+
return !owner || !owner.declared.has(pkgName);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function owningWorkspaceMember(members, file) {
|
|
31
|
+
const normalized = file.replace(/\\/g, "/");
|
|
32
|
+
let best = null;
|
|
33
|
+
for (const member of members) {
|
|
34
|
+
if (!normalized.startsWith(`${member.dir}/`))
|
|
35
|
+
continue;
|
|
36
|
+
// Nested members (packages/a and packages/a/plugins/b): the deepest wins.
|
|
37
|
+
if (!best || member.dir.length > best.dir.length)
|
|
38
|
+
best = member;
|
|
39
|
+
}
|
|
40
|
+
return best;
|
|
41
|
+
}
|
|
13
42
|
export function installCmd(pm) {
|
|
14
43
|
if (pm === "pnpm")
|
|
15
44
|
return "pnpm add";
|