cve-lite-cli 1.41.4 → 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.
Files changed (2) hide show
  1. package/dist/usage/scanner.js +345 -45
  2. package/package.json +1 -1
@@ -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
- // Matches:
35
- // import { foo } from 'pkg'
36
- // import * as foo from "pkg"
37
- // export { foo } from 'pkg'
38
- // import 'pkg'
39
- // require('pkg')
40
- // await import('pkg')
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 === '"' || ch === "`") {
123
- const quote = ch;
124
- prev = quote;
292
+ if (ch === "'" || ch === '"') {
293
+ prev = ch;
125
294
  prevWord = "";
126
- out += ch;
127
- i++;
128
- while (i < n) {
129
- const c = source[i];
130
- out += c;
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 === quote)
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 = /^(?:import|export)\s+([\s\S]*?)\s+from\s/.exec(statement)?.[1]?.trim();
427
+ const clause = extractImportClause(statement);
193
428
  if (!clause)
194
429
  return false;
195
- if (/^type\s+[\w${*]/.test(clause))
430
+ if (/^type(?![\w$])\s*[\w${*]/.test(clause))
196
431
  return true;
197
- const braced = /^\{([\s\S]*)\}$/.exec(clause);
198
- if (!braced)
432
+ if (!clause.startsWith("{") || !clause.endsWith("}"))
199
433
  return false;
200
- const specifiers = braced[1].split(",").map(s => s.trim()).filter(Boolean);
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 paths = [];
208
- const source = stripComments(content);
458
+ function collectRuntimeImportPaths(content, extension) {
459
+ const { source, nonCode } = prepareImportSource(content, extension);
460
+ const extracted = [];
209
461
  let typeQueries;
210
- for (const match of source.matchAll(IMPORT_REQUIRE_REGEX)) {
211
- if (match[4]) {
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(match.index)) {
485
+ if (typeQueries.has(start)) {
214
486
  continue;
215
487
  }
216
488
  }
217
- if (match[1] && isTypeOnlyImportStatement(match[0]))
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 importPath = match[1] || match[2] || match[3] || match[4];
220
- if (importPath)
221
- paths.push(importPath);
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
- return paths;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cve-lite-cli",
3
- "version": "1.41.4",
3
+ "version": "1.41.5",
4
4
  "description": "Developer-friendly CLI for scanning JS/TS projects for dependency vulnerabilities using local lockfiles and OSV",
5
5
  "type": "module",
6
6
  "bin": {