cve-lite-cli 1.37.0 → 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 +20 -1
- package/dist/cli/args.js +120 -103
- 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 +17 -2
- package/dist/overrides/detectors/index.js +2 -0
- 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 +108 -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/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 +33 -19
- package/package.json +1 -1
|
@@ -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";
|
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { compareVersions, isPreReleaseVersion } from '../../utils/version.js';
|
|
2
|
+
async function classify(attempt, parse) {
|
|
3
|
+
let res;
|
|
4
|
+
try {
|
|
5
|
+
res = await attempt();
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return { kind: "failed" };
|
|
9
|
+
}
|
|
10
|
+
if (res.status === 404 || res.status === 410)
|
|
11
|
+
return { kind: "absent" };
|
|
12
|
+
if (!res.ok)
|
|
13
|
+
return { kind: "failed" };
|
|
14
|
+
try {
|
|
15
|
+
const value = await parse(res);
|
|
16
|
+
return value === null ? { kind: "absent" } : { kind: "ok", value };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return { kind: "failed" };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
2
22
|
/**
|
|
3
23
|
* Fetch the `dist-tags` map for `pkgName` from the npm registry.
|
|
4
24
|
*
|
|
@@ -10,6 +30,43 @@ import { compareVersions, isPreReleaseVersion } from '../../utils/version.js';
|
|
|
10
30
|
* cache the result on `Context.registryDistTags`. This function is single-shot;
|
|
11
31
|
* caching/batching is the scanner's responsibility.
|
|
12
32
|
*/
|
|
33
|
+
/** Shared request shape for both lookups. */
|
|
34
|
+
async function packumentOutcome(pkgName, opts, parse) {
|
|
35
|
+
const baseUrl = opts.baseUrl ?? 'https://registry.npmjs.org';
|
|
36
|
+
const timeoutMs = opts.timeoutMs ?? 5000;
|
|
37
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
38
|
+
const url = `${baseUrl}/${encodeRegistryPath(pkgName)}`;
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
41
|
+
try {
|
|
42
|
+
return await classify(() => fetchImpl(url, {
|
|
43
|
+
signal: controller.signal,
|
|
44
|
+
headers: {
|
|
45
|
+
Accept: 'application/vnd.npm.install-v1+json',
|
|
46
|
+
'User-Agent': 'override-audit-cli',
|
|
47
|
+
},
|
|
48
|
+
}), parse);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function distTagsOutcome(pkgName, opts) {
|
|
55
|
+
return packumentOutcome(pkgName, opts, async (res) => {
|
|
56
|
+
const body = await res.json();
|
|
57
|
+
return body['dist-tags'] ?? null;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async function versionsOutcome(pkgName, opts) {
|
|
61
|
+
return packumentOutcome(pkgName, opts, async (res) => {
|
|
62
|
+
const body = await res.json();
|
|
63
|
+
if (!body.versions)
|
|
64
|
+
return null;
|
|
65
|
+
return Object.keys(body.versions)
|
|
66
|
+
.filter((v) => !isPreReleaseVersion(v))
|
|
67
|
+
.sort(compareVersions);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
13
70
|
export async function fetchDistTags(pkgName, opts = {}) {
|
|
14
71
|
const baseUrl = opts.baseUrl ?? 'https://registry.npmjs.org';
|
|
15
72
|
const timeoutMs = opts.timeoutMs ?? 5000;
|
|
@@ -42,14 +99,20 @@ export async function fetchDistTags(pkgName, opts = {}) {
|
|
|
42
99
|
* (per-package), so a single 404 doesn't tank the whole batch.
|
|
43
100
|
*/
|
|
44
101
|
export async function fetchDistTagsBatch(pkgNames, opts = {}) {
|
|
45
|
-
const
|
|
102
|
+
const results = new Map();
|
|
103
|
+
const failed = [];
|
|
46
104
|
const unique = Array.from(new Set(pkgNames));
|
|
47
105
|
await Promise.all(unique.map(async (name) => {
|
|
48
|
-
const
|
|
49
|
-
if (
|
|
50
|
-
|
|
106
|
+
const outcome = await distTagsOutcome(name, opts);
|
|
107
|
+
if (outcome.kind === "ok") {
|
|
108
|
+
results.set(name, outcome.value);
|
|
109
|
+
}
|
|
110
|
+
else if (outcome.kind === "failed") {
|
|
111
|
+
failed.push(name);
|
|
112
|
+
}
|
|
113
|
+
// `absent` is deliberately neither: the registry answered and said no such package.
|
|
51
114
|
}));
|
|
52
|
-
return
|
|
115
|
+
return { results, failed };
|
|
53
116
|
}
|
|
54
117
|
/**
|
|
55
118
|
* Fetch every published version of `pkgName`, ascending, excluding prereleases.
|
|
@@ -94,14 +157,20 @@ export async function fetchVersions(pkgName, opts = {}) {
|
|
|
94
157
|
* collapse to an absent entry rather than failing the batch.
|
|
95
158
|
*/
|
|
96
159
|
export async function fetchVersionsBatch(pkgNames, opts = {}) {
|
|
97
|
-
const
|
|
160
|
+
const results = new Map();
|
|
161
|
+
const failed = [];
|
|
98
162
|
const unique = Array.from(new Set(pkgNames));
|
|
99
163
|
await Promise.all(unique.map(async (name) => {
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
|
|
164
|
+
const outcome = await versionsOutcome(name, opts);
|
|
165
|
+
if (outcome.kind === "ok") {
|
|
166
|
+
results.set(name, outcome.value);
|
|
167
|
+
}
|
|
168
|
+
else if (outcome.kind === "failed") {
|
|
169
|
+
failed.push(name);
|
|
170
|
+
}
|
|
171
|
+
// `absent` is deliberately neither, same as the dist-tags batch above.
|
|
103
172
|
}));
|
|
104
|
-
return
|
|
173
|
+
return { results, failed };
|
|
105
174
|
}
|
|
106
175
|
/**
|
|
107
176
|
* Encode a package name for the registry URL.
|