cve-lite-cli 1.41.4 → 1.42.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.
Files changed (73) hide show
  1. package/README.md +22 -19
  2. package/dist/advisory/detail-resolution.js +62 -0
  3. package/dist/advisory/local-db.js +90 -15
  4. package/dist/advisory/osv-advisory-source.js +113 -62
  5. package/dist/advisory/osv-sync.js +18 -16
  6. package/dist/advisory/sqlite.js +50 -0
  7. package/dist/audit-log/ndjson-writer.js +6 -3
  8. package/dist/audit-log/open-error.js +22 -0
  9. package/dist/cli/args.js +145 -87
  10. package/dist/cli/flags.js +2 -0
  11. package/dist/cli/help.js +6 -2
  12. package/dist/cli/overrides.js +5 -2
  13. package/dist/cli/validate.js +45 -5
  14. package/dist/constants.js +10 -0
  15. package/dist/index.js +15 -6
  16. package/dist/licenses/license-check.js +17 -4
  17. package/dist/osv/cache.js +7 -13
  18. package/dist/output/cyclonedx.js +57 -18
  19. package/dist/output/finding-display.js +8 -1
  20. package/dist/output/formatters.js +52 -11
  21. package/dist/output/html-reporter.js +40 -8
  22. package/dist/output/override-findings-terminal.js +21 -13
  23. package/dist/output/printers.js +185 -76
  24. package/dist/output/sarif.js +182 -16
  25. package/dist/output/spdx.js +15 -1
  26. package/dist/output/write-outputs.js +29 -20
  27. package/dist/overrides/context-builder.js +1 -7
  28. package/dist/overrides/detectors/oa005-nested-ineffective.js +6 -1
  29. package/dist/overrides/detectors/oa008-materialized.js +33 -10
  30. package/dist/overrides/detectors/oa009-stale-floor.js +2 -2
  31. package/dist/overrides/detectors/oa010-vulnerable-floor.js +19 -13
  32. package/dist/overrides/detectors/phantom-utils.js +2 -10
  33. package/dist/overrides/fixer.js +13 -2
  34. package/dist/overrides/index.js +1 -0
  35. package/dist/overrides/types.js +15 -1
  36. package/dist/parsers/bun-lock.js +6 -0
  37. package/dist/parsers/index.js +177 -40
  38. package/dist/parsers/multi-package.js +4 -0
  39. package/dist/parsers/package-lock.js +6 -2
  40. package/dist/parsers/pnpm-lock.js +4 -7
  41. package/dist/parsers/requirements-txt.js +185 -0
  42. package/dist/parsers/utils.js +11 -6
  43. package/dist/parsers/yarn-lock.js +1 -5
  44. package/dist/remediation/fix-commands.js +60 -36
  45. package/dist/remediation/parent-update-child-note.js +3 -2
  46. package/dist/scan/completeness.js +57 -4
  47. package/dist/scan/multi-folder-scan.js +27 -13
  48. package/dist/scan/single-scan.js +118 -37
  49. package/dist/scanner.js +59 -36
  50. package/dist/types.js +1 -1
  51. package/dist/usage/scanner.js +345 -45
  52. package/dist/utils/array.js +5 -0
  53. package/dist/utils/baseline.js +8 -2
  54. package/dist/utils/chalk.js +1 -3
  55. package/dist/utils/create-pr.js +2 -2
  56. package/dist/utils/debug-bundle.js +37 -22
  57. package/dist/utils/debug-project.js +6 -5
  58. package/dist/utils/debug-redaction.js +1 -1
  59. package/dist/utils/effective-options.js +5 -1
  60. package/dist/utils/finding.js +4 -0
  61. package/dist/utils/fix-runner.js +110 -36
  62. package/dist/utils/import-type-queries.js +5 -1
  63. package/dist/utils/install-command.js +6 -0
  64. package/dist/utils/malicious.js +11 -0
  65. package/dist/utils/package-identity.js +12 -2
  66. package/dist/utils/path-coverage.js +1 -5
  67. package/dist/utils/sanitize.js +59 -0
  68. package/dist/utils/sbom-format.js +17 -8
  69. package/dist/utils/severity.js +61 -7
  70. package/dist/utils/url.js +175 -0
  71. package/dist/utils/version.js +44 -0
  72. package/dist/utils/vuln.js +11 -14
  73. package/package.json +2 -4
@@ -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;
@@ -20,6 +20,11 @@ export function uniquePathArrays(paths) {
20
20
  }
21
21
  return output;
22
22
  }
23
+ export function pathsEqual(left, right) {
24
+ if (left.length !== right.length)
25
+ return false;
26
+ return left.every((value, index) => value === right[index]);
27
+ }
23
28
  export async function runWithConcurrency(items, limit, fn, options) {
24
29
  const results = new Array(items.length);
25
30
  const queue = items.map((item, index) => ({ item, index }));
@@ -2,8 +2,14 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  const BASELINE_DIR = ".cve-lite";
4
4
  const BASELINE_FILE = "baseline.json";
5
+ function baselineDir(projectRoot) {
6
+ const root = fs.existsSync(projectRoot) && fs.statSync(projectRoot).isFile()
7
+ ? path.dirname(projectRoot)
8
+ : projectRoot;
9
+ return path.join(root, BASELINE_DIR);
10
+ }
5
11
  function baselinePath(projectRoot) {
6
- return path.join(projectRoot, BASELINE_DIR, BASELINE_FILE);
12
+ return path.join(baselineDir(projectRoot), BASELINE_FILE);
7
13
  }
8
14
  export function readBaseline(projectRoot) {
9
15
  const filePath = baselinePath(projectRoot);
@@ -17,7 +23,7 @@ export function readBaseline(projectRoot) {
17
23
  }
18
24
  }
19
25
  export function writeBaseline(projectRoot, findings) {
20
- const dir = path.join(projectRoot, BASELINE_DIR);
26
+ const dir = baselineDir(projectRoot);
21
27
  if (!fs.existsSync(dir)) {
22
28
  fs.mkdirSync(dir, { recursive: true });
23
29
  }
@@ -44,6 +44,4 @@ export const chalk = {
44
44
  white: (text) => paint(ansi.white, text),
45
45
  whiteBright: (text) => paint(ansi.whiteBright, text)
46
46
  };
47
- export function stripAnsi(text) {
48
- return text.replace(/\x1b\[[0-9;]*m/g, "");
49
- }
47
+ export { stripAnsi } from "./sanitize.js";
@@ -84,8 +84,8 @@ export function buildPullRequestBody(params) {
84
84
  const advisoryPackage = item.childPackage ?? item.package;
85
85
  const advisoryIds = collectAdvisoryIdsForPackage(params.findingsBeforeFix, advisoryPackage);
86
86
  const advisoryText = advisoryIds.length > 0 ? advisoryIds.join(", ") : "n/a";
87
- const childNote = formatParentUpdateChildNote(item);
88
- const versionLine = `- **${item.package}**: \`${item.from}\` → \`${item.to}\``;
87
+ const childNote = formatParentUpdateChildNote(item, { codeSpan: true });
88
+ const versionLine = `- **\`${item.package}\`**: \`${item.from}\` → \`${item.to}\``;
89
89
  lines.push(childNote ? `${versionLine} (${childNote})` : versionLine);
90
90
  lines.push(` - Advisories: ${advisoryText}`);
91
91
  }
@@ -1,26 +1,34 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { getCliVersion } from "./version-info.js";
4
- import { summarizeEffectiveOptions } from "./effective-options.js";
4
+ import { resolveSearchDepth, summarizeEffectiveOptions } from "./effective-options.js";
5
5
  import { createBundleRedactor } from "./debug-redaction.js";
6
- import { findNpmLockfile } from "./npm-lockfile.js";
6
+ import { PACKAGE_MANAGER_BY_LOCKFILE } from "../constants.js";
7
+ import { selectScanLockfile } from "../parsers/index.js";
8
+ import { isMultiFolderScan } from "../parsers/multi-package.js";
7
9
  import { buildProjectJson as projectMetadata } from "./debug-project.js";
8
10
  export { redactDeep, redactPath } from "./debug-redaction.js";
9
- function detectPackageManager(projectPath) {
10
- if (fs.existsSync(path.join(projectPath, "pnpm-lock.yaml")))
11
- return "pnpm";
12
- if (fs.existsSync(path.join(projectPath, "bun.lockb")) || fs.existsSync(path.join(projectPath, "bun.lock")))
13
- return "bun";
14
- if (fs.existsSync(path.join(projectPath, "yarn.lock")))
15
- return "yarn";
16
- if (findNpmLockfile(projectPath))
17
- return "npm";
18
- return "unknown";
11
+ /** A multi-folder run reads several lockfiles, so no single one describes it. */
12
+ const MULTIPLE_LOCKFILES = "multiple";
13
+ /**
14
+ * The lockfile this run's scan reads, chosen by the scan's own rules, so the
15
+ * bundle describes that file rather than whatever sits at the scan root (#1514).
16
+ */
17
+ function scanLockfile(options, projectPath) {
18
+ const depth = resolveSearchDepth(options);
19
+ if (isMultiFolderScan(projectPath, depth))
20
+ return MULTIPLE_LOCKFILES;
21
+ return selectScanLockfile(projectPath, depth);
19
22
  }
20
- function readLockfileVersion(projectPath) {
21
- const lockPath = findNpmLockfile(projectPath);
22
- if (lockPath === null)
23
- return null;
23
+ function packageManagerFor(lockfile, projectPath) {
24
+ if (lockfile === MULTIPLE_LOCKFILES)
25
+ return MULTIPLE_LOCKFILES;
26
+ if (lockfile !== null)
27
+ return PACKAGE_MANAGER_BY_LOCKFILE[lockfile.name];
28
+ // bun.lockb is not scanned, but naming the manager still helps a report.
29
+ return fs.existsSync(path.join(projectPath, "bun.lockb")) ? "bun" : "unknown";
30
+ }
31
+ function readLockfileVersion(lockPath) {
24
32
  try {
25
33
  const parsed = JSON.parse(fs.readFileSync(lockPath, "utf8"));
26
34
  return typeof parsed.lockfileVersion === "number" ? parsed.lockfileVersion : null;
@@ -29,15 +37,21 @@ function readLockfileVersion(projectPath) {
29
37
  return null;
30
38
  }
31
39
  }
32
- export function buildEnvironmentJson(options, projectPath) {
33
- const packageManager = detectPackageManager(projectPath);
40
+ /** The npm lockfile the scan reads, or null when the scan reads another manager's. */
41
+ function npmLockfilePath(lockfile) {
42
+ if (lockfile === null || lockfile === MULTIPLE_LOCKFILES)
43
+ return null;
44
+ return PACKAGE_MANAGER_BY_LOCKFILE[lockfile.name] === "npm" ? lockfile.filePath : null;
45
+ }
46
+ export function buildEnvironmentJson(options, projectPath, lockfile = scanLockfile(options, projectPath)) {
47
+ const npmLockfile = npmLockfilePath(lockfile);
34
48
  return {
35
49
  cliVersion: getCliVersion(),
36
50
  nodeVersion: process.version,
37
51
  platform: process.platform,
38
52
  arch: process.arch,
39
- packageManager,
40
- lockfileVersion: packageManager === "npm" ? readLockfileVersion(projectPath) : null,
53
+ packageManager: packageManagerFor(lockfile, projectPath),
54
+ lockfileVersion: npmLockfile === null ? null : readLockfileVersion(npmLockfile),
41
55
  nodeModulesPresent: fs.existsSync(path.join(projectPath, "node_modules")),
42
56
  effectiveFlags: summarizeEffectiveOptions(options),
43
57
  osvEndpoint: {
@@ -79,8 +93,9 @@ export function createDebugBundle(events, options, projectPath, scanJsonPath) {
79
93
  }
80
94
  catch { /* An unavailable scan artifact does not prevent a diagnostic bundle. */ }
81
95
  }
82
- const environment = buildEnvironmentJson(options, projectPath);
83
- const project = projectMetadata(events, projectPath);
96
+ const lockfile = scanLockfile(options, projectPath);
97
+ const environment = buildEnvironmentJson(options, projectPath, lockfile);
98
+ const project = projectMetadata(events, projectPath, npmLockfilePath(lockfile));
84
99
  const redact = createBundleRedactor(projectPath, [events, options, environment, project, scan]);
85
100
  const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
86
101
  // A unique directory prevents a same-second run from inheriting scan.json
@@ -39,9 +39,8 @@ function readWorkspaceGlobs(projectPath) {
39
39
  return null;
40
40
  }
41
41
  }
42
- /** Count package entries explicitly marked as links in npm's active lockfile. */
43
- function readNpmLinkEntryCount(projectPath) {
44
- const lockPath = findNpmLockfile(projectPath);
42
+ /** Count package entries explicitly marked as links in the npm lockfile the scan reads. */
43
+ function readNpmLinkEntryCount(lockPath) {
45
44
  if (lockPath === null)
46
45
  return null;
47
46
  try {
@@ -58,13 +57,15 @@ function readNpmLinkEntryCount(projectPath) {
58
57
  * Build project metadata from debug events and project declaration files.
59
58
  * Workspace member names/counts are intentionally never inferred from lockfile maps.
60
59
  */
61
- export function buildProjectJson(events, projectPath) {
60
+ export function buildProjectJson(events, projectPath,
61
+ /** The npm lockfile the scan reads, or null when it reads another manager's. */
62
+ npmLockfile = findNpmLockfile(projectPath)) {
62
63
  const project = {};
63
64
  const skippedRules = [];
64
65
  const workspaceGlobs = readWorkspaceGlobs(projectPath);
65
66
  if (workspaceGlobs !== null)
66
67
  project.workspaceGlobs = workspaceGlobs;
67
- const linkEntryCount = readNpmLinkEntryCount(projectPath);
68
+ const linkEntryCount = readNpmLinkEntryCount(npmLockfile);
68
69
  if (linkEntryCount !== null)
69
70
  project.linkEntryCount = linkEntryCount;
70
71
  for (const event of events) {
@@ -1,7 +1,7 @@
1
1
  import os from "node:os";
2
2
  import path from "node:path";
3
+ import { SECRET_KEY } from "./url.js";
3
4
  const URL_PATTERN = /[a-z][a-z\d+.-]*:\/\/[^\s<>"'`]+/gi;
4
- const SECRET_KEY = /^(?:password|passwd|_authToken|_auth|authToken|token|authorization|credentials?|apiKey|secret|accessToken|refreshToken)$/i;
5
5
  const PRIVATE_VALUE_KEY = /^(?:cacheDir|offlineDb|output|caCert|nodeExtraCaCerts|auditLog|report|osvUrl|proxy|httpsProxy|httpProxy)$/i;
6
6
  /** One context for every file: hostnames and credentials also occur bare in errors. */
7
7
  export function createBundleRedactor(projectPath, inputs) {