cve-lite-cli 1.41.3 → 1.41.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/output/printers.js
CHANGED
|
@@ -965,7 +965,7 @@ function colorFixSectionTitle(severity, title) {
|
|
|
965
965
|
return chalk.green(title);
|
|
966
966
|
return chalk.gray(title);
|
|
967
967
|
}
|
|
968
|
-
function formatFixCommandSummary(plan) {
|
|
968
|
+
export function formatFixCommandSummary(plan) {
|
|
969
969
|
if (!plan)
|
|
970
970
|
return "";
|
|
971
971
|
const packageCount = plan.targets.length;
|
|
@@ -12,7 +12,7 @@ import { buildYarnWorkspaceMap } from "../parsers/yarn-lock.js";
|
|
|
12
12
|
import { buildBunWorkspaceMap } from "../parsers/bun-lock.js";
|
|
13
13
|
import { readConfiguredCooldown, cooldownWarningFor } from "./cooldown.js";
|
|
14
14
|
import { isVerifiedUpgrade } from "./parent-upgrade.js";
|
|
15
|
-
import { installPackageSpec, packageInstanceKey, packageNameKey, registryPackageName } from "../utils/package-identity.js";
|
|
15
|
+
import { commandPackageName, installPackageSpec, isOptionShapedName, packageInstanceKey, packageNameKey, registryPackageName } from "../utils/package-identity.js";
|
|
16
16
|
// Rendered next to a parent-upgrade fix target whose recommendedParentUpgrade
|
|
17
17
|
// could not be verified end-to-end (parent-upgrade.ts confidence: "unverified").
|
|
18
18
|
// Shared across terminal and HTML output so the wording stays in sync.
|
|
@@ -117,7 +117,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
117
117
|
isUpgradeTarget(finding.pkg.version, directTarget) &&
|
|
118
118
|
validatedFieldOk) {
|
|
119
119
|
const pkgWorkspaces = workspaceMap.get(packageNameKey(finding.pkg))?.filter(w => w !== ".") ?? [];
|
|
120
|
-
upsertTarget(targetsByKey, finding, {
|
|
120
|
+
upsertTarget(targetsByKey, skippedByKey, finding, {
|
|
121
121
|
package: finding.pkg.name,
|
|
122
122
|
...(finding.pkg.registryName ? { registryName: finding.pkg.registryName } : {}),
|
|
123
123
|
currentVersion: finding.pkg.version,
|
|
@@ -184,7 +184,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
184
184
|
if (packageManager === "npm" &&
|
|
185
185
|
finding.relationship === "transitive" &&
|
|
186
186
|
finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
|
|
187
|
-
upsertTarget(targetsByKey, finding, parentUpdateTarget(finding, urgent, {
|
|
187
|
+
upsertTarget(targetsByKey, skippedByKey, finding, parentUpdateTarget(finding, urgent, {
|
|
188
188
|
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
189
189
|
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
190
190
|
childTargetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
@@ -221,7 +221,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
221
221
|
? finding.chainResolution
|
|
222
222
|
: null;
|
|
223
223
|
const npmParentUpgradeWorkspaces = workspaceMap.get(finding.recommendedNpmTransitiveRemediation.package)?.filter(w => w !== ".") ?? [];
|
|
224
|
-
upsertTarget(targetsByKey, finding, {
|
|
224
|
+
upsertTarget(targetsByKey, skippedByKey, finding, {
|
|
225
225
|
package: finding.recommendedNpmTransitiveRemediation.package,
|
|
226
226
|
currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
|
|
227
227
|
targetVersion: finding.recommendedNpmTransitiveRemediation.targetVersion,
|
|
@@ -253,7 +253,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
253
253
|
? finding.chainResolution
|
|
254
254
|
: null;
|
|
255
255
|
const parentUpgradeWorkspaces = workspaceMap.get(finding.recommendedParentUpgrade.package)?.filter(w => w !== ".") ?? [];
|
|
256
|
-
upsertTarget(targetsByKey, finding, {
|
|
256
|
+
upsertTarget(targetsByKey, skippedByKey, finding, {
|
|
257
257
|
package: finding.recommendedParentUpgrade.package,
|
|
258
258
|
currentVersion: finding.recommendedParentUpgrade.currentVersion,
|
|
259
259
|
targetVersion: finding.recommendedParentUpgrade.targetVersion,
|
|
@@ -295,7 +295,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
295
295
|
: packageManager === "yarn"
|
|
296
296
|
? buildYarnUpgradeCommand(finding.pkg.name, finding.recommendedNpmTransitiveRemediation.workspaces)
|
|
297
297
|
: buildBunUpdateCommand(finding.pkg.name);
|
|
298
|
-
upsertTarget(targetsByKey, finding, parentUpdateTarget(finding, urgent, {
|
|
298
|
+
upsertTarget(targetsByKey, skippedByKey, finding, parentUpdateTarget(finding, urgent, {
|
|
299
299
|
package: finding.pkg.name,
|
|
300
300
|
currentVersion: finding.pkg.version,
|
|
301
301
|
childTargetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
|
|
@@ -311,7 +311,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
|
|
|
311
311
|
const isDev = devLookup.get(`${finding.chainResolution.directDep}@${finding.chainResolution.directDepCurrentVersion}`) ?? false;
|
|
312
312
|
const coverage = calculatePathCoverage(finding.dependencyPaths, [finding.chainResolution.directDep]);
|
|
313
313
|
const chainWorkspaces = workspaceMap.get(finding.chainResolution.directDep)?.filter(w => w !== ".") ?? [];
|
|
314
|
-
upsertTarget(targetsByKey, finding, {
|
|
314
|
+
upsertTarget(targetsByKey, skippedByKey, finding, {
|
|
315
315
|
package: finding.chainResolution.directDep,
|
|
316
316
|
currentVersion: finding.chainResolution.directDepCurrentVersion,
|
|
317
317
|
targetVersion: finding.chainResolution.targetVersion,
|
|
@@ -718,7 +718,18 @@ function withholdShellUnsafeTargets(sortedTargets, findings, skippedByKey, subfo
|
|
|
718
718
|
}
|
|
719
719
|
return kept;
|
|
720
720
|
}
|
|
721
|
-
function upsertTarget(targetsByKey, finding, next) {
|
|
721
|
+
function upsertTarget(targetsByKey, skippedByKey, finding, next) {
|
|
722
|
+
const operand = commandPackageName(next);
|
|
723
|
+
if (isOptionShapedName(operand)) {
|
|
724
|
+
skippedByKey.set(`option-shaped:${packageInstanceKey(finding.pkg)}`, {
|
|
725
|
+
package: finding.pkg.name,
|
|
726
|
+
version: finding.pkg.version,
|
|
727
|
+
relationship: finding.relationship,
|
|
728
|
+
reason: `Package name "${operand}" looks like a command-line option, so no fix command is offered.`,
|
|
729
|
+
optionShapedName: true,
|
|
730
|
+
});
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
722
733
|
const key = targetKey(finding, next);
|
|
723
734
|
const existing = targetsByKey.get(key);
|
|
724
735
|
if (!existing) {
|
package/dist/scan/single-scan.js
CHANGED
|
@@ -314,6 +314,7 @@ export async function handleSingleFolderScan(params) {
|
|
|
314
314
|
parentUpgradeCount: fixResult?.parentUpgradeCount ?? 0,
|
|
315
315
|
breakingUpgradeCount: fixResult?.breakingUpgradeCount ?? 0,
|
|
316
316
|
noFixCount: fixResult?.noFixCount ?? 0,
|
|
317
|
+
optionShapedNameCount: fixResult?.optionShapedNameCount ?? 0,
|
|
317
318
|
},
|
|
318
319
|
};
|
|
319
320
|
writeFixResultJson(fixResultJson, projectPath);
|
package/dist/usage/scanner.js
CHANGED
|
@@ -3,6 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { EXCLUDED_DIRS } from "../constants.js";
|
|
4
4
|
import { importTypeQueryOffsets } from "../utils/import-type-queries.js";
|
|
5
5
|
const ALLOWED_EXTS = new Set([".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"]);
|
|
6
|
+
const JSX_EXTS = new Set([".js", ".jsx", ".tsx"]);
|
|
6
7
|
const TYPESCRIPT_DECLARATION_FILE = /\.d\.[cm]?ts$/;
|
|
7
8
|
export const MAX_FILES_TO_SCAN = 5000;
|
|
8
9
|
/**
|
|
@@ -31,14 +32,13 @@ const ALL_IMPORTS_EXCLUDED_DIRS = new Set([
|
|
|
31
32
|
"test",
|
|
32
33
|
"tests",
|
|
33
34
|
]);
|
|
34
|
-
//
|
|
35
|
-
// import
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
// import 'pkg'
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
const IMPORT_REQUIRE_REGEX = /(?:(?:import|export)\s+[\w$\s{},*]+\s+from\s+['"]([^'"]+)['"]|import\s+['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\)|import\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
35
|
+
// Side-effect imports, `require(...)`, and dynamic `import(...)`. Kept separate
|
|
36
|
+
// from `import/export ... from` clauses so every quantifier is bounded by a
|
|
37
|
+
// disjoint delimiter and cannot backtrack superlinearly.
|
|
38
|
+
const DIRECT_IMPORT_OR_CALL_REGEX = /(?:import\b\s*['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\)|import\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
39
|
+
// Tail of `import ... from 'pkg'` / `export ... from 'pkg'`; the preceding clause
|
|
40
|
+
// is scanned backwards in linear time so missing `from` never backtracks.
|
|
41
|
+
const FROM_SPECIFIER_REGEX = /(?<![\w$])from\s*['"]([^'"]+)['"]/g;
|
|
42
42
|
// A `/` opens a regex literal only where an expression may begin. After a value
|
|
43
43
|
// - an identifier, a number, `)`, `]` or a closing backtick - it is division.
|
|
44
44
|
// These keywords are the cases where a `/` directly after a word is still a
|
|
@@ -92,7 +92,76 @@ function isWordChar(code, ch) {
|
|
|
92
92
|
code === 0x24 // $
|
|
93
93
|
);
|
|
94
94
|
}
|
|
95
|
+
const JSX_OPEN = /<([$\p{ID_Start}][$\p{ID_Continue}:.-]*(?=[\s/<>])|(?=>))/uy;
|
|
96
|
+
const TYPE_PARAMETER_TRIVIA = /(?:\s+|\/\*[^]*?\*\/|\/\/[^\r\n\u2028\u2029]*)*/y;
|
|
97
|
+
const TYPE_PARAMETER_EXTENDS = /extends(?![$\u200c\u200d\p{ID_Continue}])/uy;
|
|
98
|
+
const TYPE_PARAMETER_NAME = /[$\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/uy;
|
|
99
|
+
const PARAMETER_TOKEN = /\(|\)|"(?:\\[^]|[^"\\])*"|'(?:\\[^]|[^'\\])*'|`(?:\\[^]|[^`\\])*`|[^\s()'"`/]+|\//y;
|
|
100
|
+
function skipTypeParameterTrivia(source, start) {
|
|
101
|
+
TYPE_PARAMETER_TRIVIA.lastIndex = start;
|
|
102
|
+
TYPE_PARAMETER_TRIVIA.test(source);
|
|
103
|
+
return TYPE_PARAMETER_TRIVIA.lastIndex;
|
|
104
|
+
}
|
|
105
|
+
function hasArrowAfterParameters(source, start) {
|
|
106
|
+
// A bare <T> can start a function type or a JSX tag whose text begins with "(".
|
|
107
|
+
if (source[start] !== "(")
|
|
108
|
+
return false;
|
|
109
|
+
let depth = 1;
|
|
110
|
+
let i = start + 1;
|
|
111
|
+
while (i < source.length) {
|
|
112
|
+
PARAMETER_TOKEN.lastIndex = skipTypeParameterTrivia(source, i);
|
|
113
|
+
const token = PARAMETER_TOKEN.exec(source);
|
|
114
|
+
if (!token)
|
|
115
|
+
return false;
|
|
116
|
+
i = PARAMETER_TOKEN.lastIndex;
|
|
117
|
+
if (token[0] === "(")
|
|
118
|
+
depth++;
|
|
119
|
+
if (token[0] === ")" && --depth === 0) {
|
|
120
|
+
return source.startsWith("=>", skipTypeParameterTrivia(source, i));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
function looksLikeTypeParameters(source, start, firstName) {
|
|
126
|
+
if (firstName === "const") {
|
|
127
|
+
TYPE_PARAMETER_NAME.lastIndex = skipTypeParameterTrivia(source, start);
|
|
128
|
+
if (!TYPE_PARAMETER_NAME.test(source))
|
|
129
|
+
return false;
|
|
130
|
+
start = TYPE_PARAMETER_NAME.lastIndex;
|
|
131
|
+
}
|
|
132
|
+
const next = skipTypeParameterTrivia(source, start);
|
|
133
|
+
const token = source[next];
|
|
134
|
+
if (token === "," || token === "=")
|
|
135
|
+
return true;
|
|
136
|
+
if (token === ">")
|
|
137
|
+
return hasArrowAfterParameters(source, skipTypeParameterTrivia(source, next + 1));
|
|
138
|
+
TYPE_PARAMETER_EXTENDS.lastIndex = next;
|
|
139
|
+
if (!TYPE_PARAMETER_EXTENDS.test(source))
|
|
140
|
+
return false;
|
|
141
|
+
// JSX can use `extends` as a boolean or assigned attribute.
|
|
142
|
+
const constraint = source[skipTypeParameterTrivia(source, TYPE_PARAMETER_EXTENDS.lastIndex)];
|
|
143
|
+
return constraint !== undefined && constraint !== "=" && constraint !== ">" && constraint !== "/";
|
|
144
|
+
}
|
|
145
|
+
function jsxOpeningEnd(source, start, checkTypeParameters) {
|
|
146
|
+
JSX_OPEN.lastIndex = start;
|
|
147
|
+
const opening = JSX_OPEN.exec(source);
|
|
148
|
+
if (!opening)
|
|
149
|
+
return start;
|
|
150
|
+
const end = JSX_OPEN.lastIndex;
|
|
151
|
+
const name = opening[1];
|
|
152
|
+
// Member, namespaced, and hyphenated JSX names cannot be type parameters.
|
|
153
|
+
if (checkTypeParameters && name && !/[.:-]/.test(name)
|
|
154
|
+
&& looksLikeTypeParameters(source, end, name))
|
|
155
|
+
return start;
|
|
156
|
+
return end;
|
|
157
|
+
}
|
|
95
158
|
export function stripComments(source) {
|
|
159
|
+
return prepareImportSource(source).source;
|
|
160
|
+
}
|
|
161
|
+
function prepareImportSource(source, extension = "") {
|
|
162
|
+
const nonCode = [];
|
|
163
|
+
const contexts = [];
|
|
164
|
+
const jsx = JSX_EXTS.has(extension);
|
|
96
165
|
let out = "";
|
|
97
166
|
let i = 0;
|
|
98
167
|
const n = source.length;
|
|
@@ -100,9 +169,110 @@ export function stripComments(source) {
|
|
|
100
169
|
// ending at it. Both ignore comment and string bodies.
|
|
101
170
|
let prev = "";
|
|
102
171
|
let prevWord = "";
|
|
172
|
+
function emitNonCode(end, kind) {
|
|
173
|
+
const start = out.length;
|
|
174
|
+
out += source.slice(i, end);
|
|
175
|
+
i = end;
|
|
176
|
+
const previous = nonCode[nonCode.length - 1];
|
|
177
|
+
if (kind === "jsx" && previous?.kind === kind && previous.end === start) {
|
|
178
|
+
previous.end = out.length;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
nonCode.push({ start, end: out.length, kind });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function startExpression() {
|
|
185
|
+
contexts.push({ kind: "expression", braces: 1 });
|
|
186
|
+
out += "{";
|
|
187
|
+
i++;
|
|
188
|
+
prev = "{";
|
|
189
|
+
prevWord = "";
|
|
190
|
+
}
|
|
103
191
|
while (i < n) {
|
|
104
192
|
const ch = source[i];
|
|
105
193
|
const next = source[i + 1];
|
|
194
|
+
const context = contexts[contexts.length - 1];
|
|
195
|
+
if (context?.kind === "jsx") {
|
|
196
|
+
if (ch === "{" && context.mode !== "closing" && context.typeDepth === 0) {
|
|
197
|
+
startExpression();
|
|
198
|
+
}
|
|
199
|
+
else if (context.mode === "text") {
|
|
200
|
+
if (ch === "<" && next === "/") {
|
|
201
|
+
context.mode = "closing";
|
|
202
|
+
emitNonCode(i + 2, "jsx");
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
const end = ch === "<" ? jsxOpeningEnd(source, i, false) : i;
|
|
206
|
+
if (end > i) {
|
|
207
|
+
contexts.push({ kind: "jsx", mode: "tag", typeDepth: 0 });
|
|
208
|
+
emitNonCode(end, "jsx");
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
let end = i + 1;
|
|
212
|
+
while (end < n && source[end] !== "<" && source[end] !== "{")
|
|
213
|
+
end++;
|
|
214
|
+
emitNonCode(end, "jsx");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (context.typeDepth === 0 && (ch === ">" || ch === "/" && next === ">")) {
|
|
219
|
+
emitNonCode(i + (ch === "/" ? 2 : 1), "jsx");
|
|
220
|
+
if (ch === "/" || context.mode === "closing") {
|
|
221
|
+
contexts.pop();
|
|
222
|
+
prev = "]";
|
|
223
|
+
prevWord = "";
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
context.mode = "text";
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else if (ch === "'" || ch === '"') {
|
|
230
|
+
const end = source.indexOf(ch, i + 1);
|
|
231
|
+
emitNonCode(end === -1 ? n : end + 1, "jsx");
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
if (ch === "<")
|
|
235
|
+
context.typeDepth++;
|
|
236
|
+
if (ch === ">" && source[i - 1] !== "=")
|
|
237
|
+
context.typeDepth--;
|
|
238
|
+
emitNonCode(i + 1, "jsx");
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (context?.kind === "template" || ch === "`") {
|
|
243
|
+
const continuing = context?.kind === "template";
|
|
244
|
+
if (!continuing)
|
|
245
|
+
contexts.push({ kind: "template" });
|
|
246
|
+
let end = continuing ? i : i + 1;
|
|
247
|
+
while (end < n && source[end] !== "`" && !(source[end] === "$" && source[end + 1] === "{")) {
|
|
248
|
+
end += source[end] === "\\" && end + 1 < n ? 2 : 1;
|
|
249
|
+
}
|
|
250
|
+
const interpolation = source[end] === "$";
|
|
251
|
+
emitNonCode(Math.min(end + 1, n), "template");
|
|
252
|
+
if (interpolation) {
|
|
253
|
+
startExpression();
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
contexts.pop();
|
|
257
|
+
prev = "`";
|
|
258
|
+
prevWord = "";
|
|
259
|
+
}
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (jsx && ch === "<" && prev !== "<" && regexCanStartAfter(prev, prevWord)) {
|
|
263
|
+
const end = jsxOpeningEnd(source, i, extension === ".tsx");
|
|
264
|
+
if (end > i) {
|
|
265
|
+
contexts.push({ kind: "jsx", mode: "tag", typeDepth: 0 });
|
|
266
|
+
emitNonCode(end, "jsx");
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (context?.kind === "expression") {
|
|
271
|
+
if (ch === "{")
|
|
272
|
+
context.braces++;
|
|
273
|
+
if (ch === "}" && --context.braces === 0)
|
|
274
|
+
contexts.pop();
|
|
275
|
+
}
|
|
106
276
|
if (ch === "/" && next === "/") {
|
|
107
277
|
while (i < n && source[i] !== "\n")
|
|
108
278
|
i++;
|
|
@@ -119,28 +289,20 @@ export function stripComments(source) {
|
|
|
119
289
|
out += " ";
|
|
120
290
|
continue;
|
|
121
291
|
}
|
|
122
|
-
if (ch === "'" || ch === '"'
|
|
123
|
-
|
|
124
|
-
prev = quote;
|
|
292
|
+
if (ch === "'" || ch === '"') {
|
|
293
|
+
prev = ch;
|
|
125
294
|
prevWord = "";
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
i++;
|
|
132
|
-
if (c === "\\" && i < n) {
|
|
133
|
-
out += source[i];
|
|
134
|
-
i++;
|
|
135
|
-
continue;
|
|
295
|
+
let end = i + 1;
|
|
296
|
+
while (end < n) {
|
|
297
|
+
const c = source[end++];
|
|
298
|
+
if (c === "\\" && end < n) {
|
|
299
|
+
end++;
|
|
136
300
|
}
|
|
137
|
-
if (c ===
|
|
138
|
-
break;
|
|
139
|
-
// An unterminated ' or " string ends at the line break; template
|
|
140
|
-
// literals may span lines.
|
|
141
|
-
if (c === "\n" && quote !== "`")
|
|
301
|
+
else if (c === ch || c === "\n") {
|
|
142
302
|
break;
|
|
303
|
+
}
|
|
143
304
|
}
|
|
305
|
+
emitNonCode(end, "string");
|
|
144
306
|
continue;
|
|
145
307
|
}
|
|
146
308
|
if (ch === "/" && regexCanStartAfter(prev, prevWord)) {
|
|
@@ -181,7 +343,80 @@ export function stripComments(source) {
|
|
|
181
343
|
out += ch;
|
|
182
344
|
i++;
|
|
183
345
|
}
|
|
184
|
-
return out;
|
|
346
|
+
return { source: out, nonCode };
|
|
347
|
+
}
|
|
348
|
+
function isImportClauseChar(code, ch) {
|
|
349
|
+
return (isWordChar(code, ch) ||
|
|
350
|
+
isWhitespaceChar(code, ch) ||
|
|
351
|
+
code === 0x2c || // ,
|
|
352
|
+
code === 0x7b || // {
|
|
353
|
+
code === 0x7d || // }
|
|
354
|
+
code === 0x2a // *
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
function extractImportExportStatement(source, fromIndex, matchEnd) {
|
|
358
|
+
let i = fromIndex - 1;
|
|
359
|
+
while (i >= 0 && isImportClauseChar(source.charCodeAt(i), source[i])) {
|
|
360
|
+
i--;
|
|
361
|
+
}
|
|
362
|
+
const sliceStart = i + 1;
|
|
363
|
+
const clauseEnd = fromIndex;
|
|
364
|
+
let candidateStart = -1;
|
|
365
|
+
let candidateBodyStart = -1;
|
|
366
|
+
let pos = sliceStart;
|
|
367
|
+
while (pos < clauseEnd) {
|
|
368
|
+
const ch = source[pos];
|
|
369
|
+
const code = source.charCodeAt(pos);
|
|
370
|
+
if (isWordChar(code, ch)) {
|
|
371
|
+
const wordStart = pos;
|
|
372
|
+
pos++;
|
|
373
|
+
while (pos < clauseEnd && isWordChar(source.charCodeAt(pos), source[pos])) {
|
|
374
|
+
pos++;
|
|
375
|
+
}
|
|
376
|
+
const word = source.slice(wordStart, pos);
|
|
377
|
+
if (word === "import" || word === "export") {
|
|
378
|
+
if (pos === clauseEnd ||
|
|
379
|
+
isWhitespaceChar(source.charCodeAt(pos), source[pos]) ||
|
|
380
|
+
source[pos] === "{" ||
|
|
381
|
+
source[pos] === "*") {
|
|
382
|
+
candidateStart = wordStart;
|
|
383
|
+
let bodyStart = pos;
|
|
384
|
+
while (bodyStart < clauseEnd &&
|
|
385
|
+
isWhitespaceChar(source.charCodeAt(bodyStart), source[bodyStart])) {
|
|
386
|
+
bodyStart++;
|
|
387
|
+
}
|
|
388
|
+
candidateBodyStart = bodyStart;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
pos++;
|
|
394
|
+
}
|
|
395
|
+
if (candidateStart === -1 || candidateBodyStart >= clauseEnd) {
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
start: candidateStart,
|
|
400
|
+
statement: source.slice(candidateStart, matchEnd),
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function extractImportClause(statement) {
|
|
404
|
+
if (!statement.startsWith("import") && !statement.startsWith("export")) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const keywordEnd = 6;
|
|
408
|
+
if (keywordEnd >= statement.length) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const ch = statement[keywordEnd];
|
|
412
|
+
if (!isWhitespaceChar(statement.charCodeAt(keywordEnd), ch) && ch !== "{" && ch !== "*") {
|
|
413
|
+
return null;
|
|
414
|
+
}
|
|
415
|
+
const fromMatch = /(?<![\w$])from\s*['"]/.exec(statement);
|
|
416
|
+
if (!fromMatch)
|
|
417
|
+
return null;
|
|
418
|
+
const clause = statement.slice(keywordEnd, fromMatch.index).trim();
|
|
419
|
+
return clause.length > 0 ? clause : null;
|
|
185
420
|
}
|
|
186
421
|
// `import type { X } from 'pkg'`, `export type { X } from 'pkg'`, and
|
|
187
422
|
// `import { type A, type B } from 'pkg'` are erased by the TypeScript compiler
|
|
@@ -189,38 +424,103 @@ export function stripComments(source) {
|
|
|
189
424
|
// `type` (`import type from 'pkg'`) or a mixed list (`import { type A, b }`)
|
|
190
425
|
// still loads the module and is kept.
|
|
191
426
|
export function isTypeOnlyImportStatement(statement) {
|
|
192
|
-
const clause =
|
|
427
|
+
const clause = extractImportClause(statement);
|
|
193
428
|
if (!clause)
|
|
194
429
|
return false;
|
|
195
|
-
if (/^type\s
|
|
430
|
+
if (/^type(?![\w$])\s*[\w${*]/.test(clause))
|
|
196
431
|
return true;
|
|
197
|
-
|
|
198
|
-
if (!braced)
|
|
432
|
+
if (!clause.startsWith("{") || !clause.endsWith("}"))
|
|
199
433
|
return false;
|
|
200
|
-
const specifiers =
|
|
434
|
+
const specifiers = clause.slice(1, -1).split(",").map(s => s.trim()).filter(Boolean);
|
|
201
435
|
return specifiers.length > 0 && specifiers.every(s => /^type\s+\S/.test(s));
|
|
202
436
|
}
|
|
437
|
+
function findNonCodeRange(nonCode, offset) {
|
|
438
|
+
let low = 0;
|
|
439
|
+
let high = nonCode.length - 1;
|
|
440
|
+
while (low <= high) {
|
|
441
|
+
const mid = (low + high) >> 1;
|
|
442
|
+
const range = nonCode[mid];
|
|
443
|
+
if (offset < range.start) {
|
|
444
|
+
high = mid - 1;
|
|
445
|
+
}
|
|
446
|
+
else if (offset >= range.end) {
|
|
447
|
+
low = mid + 1;
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
return range;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
203
455
|
// Every module specifier that `content` would actually load at runtime:
|
|
204
456
|
// static imports and re-exports, side-effect imports, `require()` and dynamic
|
|
205
457
|
// `import()`, minus anything inside a comment or in a type-only import.
|
|
206
|
-
function collectRuntimeImportPaths(content) {
|
|
207
|
-
const
|
|
208
|
-
const
|
|
458
|
+
function collectRuntimeImportPaths(content, extension) {
|
|
459
|
+
const { source, nonCode } = prepareImportSource(content, extension);
|
|
460
|
+
const extracted = [];
|
|
209
461
|
let typeQueries;
|
|
210
|
-
|
|
211
|
-
|
|
462
|
+
DIRECT_IMPORT_OR_CALL_REGEX.lastIndex = 0;
|
|
463
|
+
let match;
|
|
464
|
+
while ((match = DIRECT_IMPORT_OR_CALL_REGEX.exec(source)) !== null) {
|
|
465
|
+
const start = match.index;
|
|
466
|
+
const matchEnd = start + match[0].length;
|
|
467
|
+
const importPath = match[1] || match[2] || match[3];
|
|
468
|
+
if (!importPath)
|
|
469
|
+
continue;
|
|
470
|
+
const range = findNonCodeRange(nonCode, start);
|
|
471
|
+
const inNonCode = range !== undefined;
|
|
472
|
+
const templateSnippet = inNonCode &&
|
|
473
|
+
range.kind === "template" &&
|
|
474
|
+
matchEnd <= range.end &&
|
|
475
|
+
source[start - 1] !== "'" &&
|
|
476
|
+
source[start - 1] !== '"' &&
|
|
477
|
+
importPath === importPath.trim() &&
|
|
478
|
+
!/[\r\n]/.test(importPath);
|
|
479
|
+
if (inNonCode && !templateSnippet) {
|
|
480
|
+
DIRECT_IMPORT_OR_CALL_REGEX.lastIndex = start + 1;
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (match[3]) {
|
|
212
484
|
typeQueries ??= importTypeQueryOffsets(source);
|
|
213
|
-
if (typeQueries.has(
|
|
485
|
+
if (typeQueries.has(start)) {
|
|
214
486
|
continue;
|
|
215
487
|
}
|
|
216
488
|
}
|
|
217
|
-
|
|
489
|
+
extracted.push({ index: start, path: importPath });
|
|
490
|
+
}
|
|
491
|
+
FROM_SPECIFIER_REGEX.lastIndex = 0;
|
|
492
|
+
while ((match = FROM_SPECIFIER_REGEX.exec(source)) !== null) {
|
|
493
|
+
const fromIndex = match.index;
|
|
494
|
+
const importPath = match[1];
|
|
495
|
+
if (!importPath)
|
|
218
496
|
continue;
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
497
|
+
const matchEnd = fromIndex + match[0].length;
|
|
498
|
+
const statementInfo = extractImportExportStatement(source, fromIndex, matchEnd);
|
|
499
|
+
if (!statementInfo) {
|
|
500
|
+
FROM_SPECIFIER_REGEX.lastIndex = fromIndex + 1;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
const keywordStart = statementInfo.start;
|
|
504
|
+
const range = findNonCodeRange(nonCode, keywordStart);
|
|
505
|
+
const inNonCode = range !== undefined;
|
|
506
|
+
const templateSnippet = inNonCode &&
|
|
507
|
+
range.kind === "template" &&
|
|
508
|
+
matchEnd <= range.end &&
|
|
509
|
+
source[keywordStart - 1] !== "'" &&
|
|
510
|
+
source[keywordStart - 1] !== '"' &&
|
|
511
|
+
importPath === importPath.trim() &&
|
|
512
|
+
!/[\r\n]/.test(importPath);
|
|
513
|
+
if (inNonCode && !templateSnippet) {
|
|
514
|
+
FROM_SPECIFIER_REGEX.lastIndex = fromIndex + 1;
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (isTypeOnlyImportStatement(statementInfo.statement)) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
extracted.push({ index: keywordStart, path: importPath });
|
|
222
521
|
}
|
|
223
|
-
|
|
522
|
+
extracted.sort((a, b) => a.index - b.index);
|
|
523
|
+
return extracted.map(entry => entry.path);
|
|
224
524
|
}
|
|
225
525
|
function getBareModuleName(importPath) {
|
|
226
526
|
if (importPath.startsWith(".") || importPath.startsWith("/")) {
|
|
@@ -294,7 +594,7 @@ export function scanProjectForPackageUsage(projectPath, packagesToLookFor) {
|
|
|
294
594
|
if (!hasPotentialMatch)
|
|
295
595
|
return;
|
|
296
596
|
const foundPackages = new Set();
|
|
297
|
-
for (const importPath of collectRuntimeImportPaths(content)) {
|
|
597
|
+
for (const importPath of collectRuntimeImportPaths(content, path.extname(filePath))) {
|
|
298
598
|
const bare = getBareModuleName(importPath);
|
|
299
599
|
if (bare && packagesToLookFor.has(bare)) {
|
|
300
600
|
foundPackages.add(bare);
|
|
@@ -356,7 +656,7 @@ export function scanAllImports(projectPath) {
|
|
|
356
656
|
if (!content.includes("import") && !content.includes("require") && !content.includes("export"))
|
|
357
657
|
return;
|
|
358
658
|
const relPath = path.relative(projectPath, filePath);
|
|
359
|
-
for (const importPath of collectRuntimeImportPaths(content)) {
|
|
659
|
+
for (const importPath of collectRuntimeImportPaths(content, path.extname(filePath))) {
|
|
360
660
|
const bare = getBareModuleName(importPath);
|
|
361
661
|
if (!bare)
|
|
362
662
|
continue;
|
package/dist/utils/fix-runner.js
CHANGED
|
@@ -5,7 +5,7 @@ import { createSpinner } from "../output/spinner.js";
|
|
|
5
5
|
import { chalk } from "./chalk.js";
|
|
6
6
|
import { pluralize } from "./string.js";
|
|
7
7
|
import { isBreakingUpgrade } from "./version.js";
|
|
8
|
-
import { installPackageSpec } from "./package-identity.js";
|
|
8
|
+
import { installPackageSpec, isOptionShapedName } from "./package-identity.js";
|
|
9
9
|
import { formatParentUpdateChildNote } from "../remediation/parent-update-child-note.js";
|
|
10
10
|
export async function runInstallCommand(command, args, cwd, debugLog) {
|
|
11
11
|
const commandLine = [command, ...args].join(" ");
|
|
@@ -53,19 +53,21 @@ export function categorizeUnappliedTargets(plan) {
|
|
|
53
53
|
return {
|
|
54
54
|
parentUpgradeCount,
|
|
55
55
|
breakingUpgradeCount,
|
|
56
|
-
noFixCount: plan.skipped.length,
|
|
56
|
+
noFixCount: plan.skipped.filter(skip => !skip.optionShapedName).length,
|
|
57
|
+
optionShapedNameCount: plan.skipped.filter(skip => skip.optionShapedName).length,
|
|
57
58
|
};
|
|
58
59
|
}
|
|
59
60
|
const EMPTY_UNAPPLIED = {
|
|
60
61
|
parentUpgradeCount: 0,
|
|
61
62
|
breakingUpgradeCount: 0,
|
|
62
63
|
noFixCount: 0,
|
|
64
|
+
optionShapedNameCount: 0,
|
|
63
65
|
};
|
|
64
66
|
// Build argv lists for --fix without round-tripping through a shell string.
|
|
65
67
|
// Workspace paths and package tokens are passed as whole argv elements to spawn
|
|
66
68
|
// (no shell), so a directory name containing `&&` cannot become a second executable.
|
|
67
69
|
// Human-facing plan strings still come from buildTargetCommand.
|
|
68
|
-
function buildApplyCommands(packageManager, target) {
|
|
70
|
+
export function buildApplyCommands(packageManager, target) {
|
|
69
71
|
if (target.kind === "parent-update") {
|
|
70
72
|
return buildParentUpdateApplyArgv(packageManager, target);
|
|
71
73
|
}
|
|
@@ -74,6 +76,8 @@ function buildApplyCommands(packageManager, target) {
|
|
|
74
76
|
function buildInstallApplyArgv(packageManager, target) {
|
|
75
77
|
const workspaces = (target.workspaces ?? []).filter(w => w !== ".");
|
|
76
78
|
const pkgArg = installPackageSpec(target);
|
|
79
|
+
if (isOptionShapedName(pkgArg))
|
|
80
|
+
return [];
|
|
77
81
|
const isDev = Boolean(target.isDev);
|
|
78
82
|
if (packageManager === "npm") {
|
|
79
83
|
const args = ["install"];
|
|
@@ -126,6 +130,8 @@ function buildParentUpdateApplyArgv(packageManager, target) {
|
|
|
126
130
|
// Always build from structured fields. Never tokenize target.command: plan
|
|
127
131
|
// strings may be shell-quoted for display, and must not be re-parsed into argv.
|
|
128
132
|
const updatePkg = target.childPackage ?? target.package;
|
|
133
|
+
if (isOptionShapedName(updatePkg))
|
|
134
|
+
return [];
|
|
129
135
|
const workspaces = target.workspaces ?? [];
|
|
130
136
|
const nonRoot = workspaces.filter(w => w !== ".");
|
|
131
137
|
if (packageManager === "npm") {
|
|
@@ -178,8 +184,10 @@ export async function applyFixesIfRequested(params) {
|
|
|
178
184
|
// refreshes (parent-update). Parent upgrades (breaking or otherwise) still
|
|
179
185
|
// need review and are left for the user.
|
|
180
186
|
const applyTargets = params.plan.targets.filter(target => target.kind === "direct" || target.kind === "parent-update");
|
|
187
|
+
const packageManager = params.plan.packageManager;
|
|
188
|
+
const runnableTargets = applyTargets.filter(target => buildApplyCommands(packageManager, target).length > 0);
|
|
181
189
|
const unapplied = categorizeUnappliedTargets(params.plan);
|
|
182
|
-
if (
|
|
190
|
+
if (runnableTargets.length === 0) {
|
|
183
191
|
return {
|
|
184
192
|
appliedFixCount: 0,
|
|
185
193
|
appliedWithinRangeRefreshCount: 0,
|
|
@@ -189,7 +197,7 @@ export async function applyFixesIfRequested(params) {
|
|
|
189
197
|
};
|
|
190
198
|
}
|
|
191
199
|
const commandLabel = commandLabelForPackageManager(params.plan.packageManager);
|
|
192
|
-
const total =
|
|
200
|
+
const total = runnableTargets.length;
|
|
193
201
|
const spinner = createSpinner(`Applying safe package fixes with ${commandLabel}... 0/${total}`, params.options);
|
|
194
202
|
const fixHint = `\nHint: Check that \`${params.plan.packageManager}\` is available and you have write access to node_modules.\nIf running in CI, ensure the install step has already run before cve-lite --fix.`;
|
|
195
203
|
// parent-update is keyed per child, so two children behind one parent produce
|
|
@@ -197,8 +205,8 @@ export async function applyFixesIfRequested(params) {
|
|
|
197
205
|
// parents that refresh the same child can still share `npm update <child>`.
|
|
198
206
|
// Spawn each command once; the displayed plan already dedupes the same way.
|
|
199
207
|
const seenCommands = new Set();
|
|
200
|
-
for (let i = 0; i <
|
|
201
|
-
const target =
|
|
208
|
+
for (let i = 0; i < runnableTargets.length; i++) {
|
|
209
|
+
const target = runnableTargets[i];
|
|
202
210
|
for (const commandParts of buildApplyCommands(params.plan.packageManager, target)) {
|
|
203
211
|
const commandLine = commandParts.join(" ");
|
|
204
212
|
if (seenCommands.has(commandLine))
|
|
@@ -216,12 +224,12 @@ export async function applyFixesIfRequested(params) {
|
|
|
216
224
|
}
|
|
217
225
|
}
|
|
218
226
|
}
|
|
219
|
-
spinner.succeed(`Applied ${
|
|
227
|
+
spinner.succeed(`Applied ${runnableTargets.length} package ${pluralize(runnableTargets.length, "fix", "fixes")} with ${commandLabel}`);
|
|
220
228
|
return {
|
|
221
|
-
appliedFixCount:
|
|
222
|
-
appliedWithinRangeRefreshCount:
|
|
229
|
+
appliedFixCount: runnableTargets.length,
|
|
230
|
+
appliedWithinRangeRefreshCount: runnableTargets.filter(target => target.kind === "parent-update").length,
|
|
223
231
|
...unapplied,
|
|
224
|
-
applied:
|
|
232
|
+
applied: runnableTargets.map(target => ({
|
|
225
233
|
package: target.package,
|
|
226
234
|
from: target.currentVersion ?? "unknown",
|
|
227
235
|
to: target.targetVersion,
|
|
@@ -253,7 +261,7 @@ export function printFixModeSummary(params) {
|
|
|
253
261
|
? ` (incl. ${result.appliedWithinRangeRefreshCount} within-range ${pluralize(result.appliedWithinRangeRefreshCount, "refresh", "refreshes")})`
|
|
254
262
|
: "";
|
|
255
263
|
console.log(`- Applied fixes: ${chalk.green(String(result.appliedFixCount))}${chalk.gray(refreshNote)}`);
|
|
256
|
-
const notAutoApplied = result.parentUpgradeCount + result.noFixCount;
|
|
264
|
+
const notAutoApplied = result.parentUpgradeCount + result.noFixCount + (result.optionShapedNameCount ?? 0);
|
|
257
265
|
console.log(`- Not auto-applied: ${chalk.yellow(String(notAutoApplied))}`);
|
|
258
266
|
if (result.parentUpgradeCount > 0) {
|
|
259
267
|
const breakingNote = result.breakingUpgradeCount > 0
|
|
@@ -264,6 +272,10 @@ export function printFixModeSummary(params) {
|
|
|
264
272
|
if (result.noFixCount > 0) {
|
|
265
273
|
console.log(` - No fix available: ${chalk.yellow(String(result.noFixCount))}`);
|
|
266
274
|
}
|
|
275
|
+
const optionShapedNameCount = result.optionShapedNameCount ?? 0;
|
|
276
|
+
if (optionShapedNameCount > 0) {
|
|
277
|
+
console.log(` - Package name looks like an option (no command offered): ${chalk.yellow(String(optionShapedNameCount))}`);
|
|
278
|
+
}
|
|
267
279
|
console.log(`- Findings before fix: ${chalk.white(String(params.findingsBeforeFix))}`);
|
|
268
280
|
console.log(`- Remaining findings after fix: ${chalk.white(String(params.findingsAfterFix))}`);
|
|
269
281
|
console.log(`- Remaining severity mix: critical ${chalk.redBright(String(params.remainingBySeverity.critical))}, high ${chalk.red(String(params.remainingBySeverity.high))}, medium ${chalk.yellow(String(params.remainingBySeverity.medium))}, low ${chalk.blueBright(String(params.remainingBySeverity.low))}, unknown ${chalk.magenta(String(params.remainingBySeverity.unknown))}`);
|
|
@@ -26,6 +26,21 @@ export function installPackageSpec(target) {
|
|
|
26
26
|
const version = target.registryName ? `${NPM_ALIAS_PROTOCOL}${target.registryName}@${target.targetVersion}` : target.targetVersion;
|
|
27
27
|
return `${target.package}@${version}`;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* A package name beginning with "-" would be read as an option by the package
|
|
31
|
+
* manager (npm never allows such a name), so fix commands that would name one
|
|
32
|
+
* are suppressed rather than printed or run.
|
|
33
|
+
*/
|
|
34
|
+
export function isOptionShapedName(name) {
|
|
35
|
+
return name.startsWith("-");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The package name a fix command would name. For a parent-update the command
|
|
39
|
+
* names the child being refreshed; otherwise it names the package itself.
|
|
40
|
+
*/
|
|
41
|
+
export function commandPackageName(target) {
|
|
42
|
+
return target.kind === "parent-update" ? (target.childPackage ?? target.package) : target.package;
|
|
43
|
+
}
|
|
29
44
|
/** npm lockfile v1 stores an alias's resolved name and version in `version`. */
|
|
30
45
|
export function parseNpmAliasVersion(value) {
|
|
31
46
|
if (typeof value !== "string" || !value.startsWith(NPM_ALIAS_PROTOCOL)) {
|