cve-lite-cli 1.39.0 → 1.41.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 (82) hide show
  1. package/README.md +28 -22
  2. package/dist/advisory/local-advisory-source.js +9 -5
  3. package/dist/advisory/local-db.js +91 -13
  4. package/dist/advisory/osv-sync.js +187 -56
  5. package/dist/advisory/staging-directory.js +112 -0
  6. package/dist/cli/advisories-sync.js +16 -2
  7. package/dist/cli/args.js +46 -35
  8. package/dist/cli/config.js +4 -1
  9. package/dist/cli/help.js +5 -3
  10. package/dist/cli/validate.js +23 -1
  11. package/dist/constants.js +15 -28
  12. package/dist/index.js +8 -3
  13. package/dist/osv/severity.js +1 -1
  14. package/dist/output/cyclonedx.js +24 -15
  15. package/dist/output/debug.js +68 -18
  16. package/dist/output/formatters.js +35 -7
  17. package/dist/output/html-reporter.js +68 -12
  18. package/dist/output/multi-folder-html-reporter.js +71 -11
  19. package/dist/output/output-dir.js +193 -7
  20. package/dist/output/override-findings-html.js +3 -6
  21. package/dist/output/override-findings-terminal.js +7 -12
  22. package/dist/output/override-fix-command.js +13 -0
  23. package/dist/output/printers.js +27 -12
  24. package/dist/output/sarif.js +168 -57
  25. package/dist/output/sbom-dependency-edges.js +16 -10
  26. package/dist/output/sbom-file.js +14 -6
  27. package/dist/output/scan-json.js +46 -3
  28. package/dist/output/spdx.js +61 -23
  29. package/dist/output/spinner.js +2 -1
  30. package/dist/output/write-outputs.js +143 -41
  31. package/dist/overrides/context-builder.js +23 -70
  32. package/dist/overrides/parsing/registry.js +1 -1
  33. package/dist/parsers/bun-lock.js +1 -2
  34. package/dist/parsers/index.js +22 -1
  35. package/dist/parsers/npm-lock-graph.js +39 -1
  36. package/dist/parsers/package-lock.js +25 -5
  37. package/dist/parsers/pnpm-lock.js +26 -12
  38. package/dist/parsers/utils.js +5 -3
  39. package/dist/parsers/yarn-lock-graph.js +5 -2
  40. package/dist/parsers/yarn-lock.js +1 -2
  41. package/dist/remediation/fix-commands.js +140 -50
  42. package/dist/remediation/npm-registry.js +56 -7
  43. package/dist/remediation/npm-transitive-graph.js +30 -72
  44. package/dist/remediation/npm-transitive-resolution.js +35 -107
  45. package/dist/remediation/parent-candidate-resolver.js +65 -0
  46. package/dist/remediation/parent-upgrade-blockers.js +51 -0
  47. package/dist/remediation/parent-upgrade.js +11 -116
  48. package/dist/remediation/registry-parent-ranges.js +21 -0
  49. package/dist/remediation/transitive-chain-resolver.js +3 -3
  50. package/dist/scan/completeness.js +17 -11
  51. package/dist/scan/multi-folder-scan.js +154 -59
  52. package/dist/scan/override-audit.js +11 -1
  53. package/dist/scan/single-scan.js +123 -67
  54. package/dist/scanner.js +288 -66
  55. package/dist/skills/content.js +12 -12
  56. package/dist/usage/scanner.js +19 -8
  57. package/dist/utils/array.js +11 -3
  58. package/dist/utils/contextual-signals.js +1 -1
  59. package/dist/utils/debug-bundle.js +118 -0
  60. package/dist/utils/debug-project.js +102 -0
  61. package/dist/utils/debug-redaction.js +117 -0
  62. package/dist/utils/effective-options.js +55 -0
  63. package/dist/utils/file.js +2 -1
  64. package/dist/utils/finding.js +1 -1
  65. package/dist/utils/fix-runner.js +98 -15
  66. package/dist/utils/import-type-queries.js +322 -0
  67. package/dist/utils/network.js +14 -10
  68. package/dist/utils/npm-lockfile.js +12 -0
  69. package/dist/utils/osv-fetch.js +70 -0
  70. package/dist/utils/package-identity.js +43 -0
  71. package/dist/utils/package-manager-source.js +24 -0
  72. package/dist/utils/purl.js +37 -0
  73. package/dist/utils/registry-config.js +129 -0
  74. package/dist/utils/severity.js +8 -0
  75. package/dist/utils/string.js +8 -0
  76. package/dist/utils/validate-url.js +4 -4
  77. package/dist/utils/version.js +47 -8
  78. package/dist/utils/versions/osv-range.js +191 -0
  79. package/dist/utils/versions/pep440.js +246 -0
  80. package/dist/utils/versions/scheme.js +10 -0
  81. package/dist/utils/vuln.js +75 -0
  82. package/package.json +3 -2
@@ -2,6 +2,7 @@ import path from "node:path";
2
2
  import { severityOrder } from "../constants.js";
3
3
  import { compareVersions, looksLikeVersion } from "../utils/version.js";
4
4
  import { getPrimaryParent } from "../utils/finding.js";
5
+ import { isNpmLockSource, packageManagerForSource } from "../utils/package-manager-source.js";
5
6
  import { calculatePathCoverage, formatDependencyPath } from "../utils/path-coverage.js";
6
7
  import { pluralize } from "../utils/string.js";
7
8
  import { buildNpmWorkspaceMap } from "../parsers/package-lock.js";
@@ -10,20 +11,21 @@ import { buildYarnWorkspaceMap } from "../parsers/yarn-lock.js";
10
11
  import { buildBunWorkspaceMap } from "../parsers/bun-lock.js";
11
12
  import { readConfiguredCooldown, cooldownWarningFor } from "./cooldown.js";
12
13
  import { isVerifiedUpgrade } from "./parent-upgrade.js";
14
+ import { installPackageSpec, packageInstanceKey, packageNameKey, registryPackageName } from "../utils/package-identity.js";
13
15
  // Rendered next to a parent-upgrade fix target whose recommendedParentUpgrade
14
16
  // could not be verified end-to-end (parent-upgrade.ts confidence: "unverified").
15
17
  // Shared across terminal and HTML output so the wording stays in sync.
16
18
  export const UNVERIFIED_PARENT_UPGRADE_NOTE = "⚠ unverified - rescan after upgrading to confirm this resolves the finding";
17
19
  export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
18
- const packageManager = inferPackageManager(scanInput);
20
+ const packageManager = packageManagerForSource(scanInput.source);
19
21
  if (!packageManager)
20
22
  return null;
21
23
  const offline = !!options?.offline;
22
24
  let workspaceMap = new Map();
23
25
  if (scanInput.filePath) {
24
26
  try {
25
- if (packageManager === "npm" && (scanInput.source === "package-lock" || scanInput.source === "npm-shrinkwrap")) {
26
- workspaceMap = buildNpmWorkspaceMap(scanInput.filePath);
27
+ if (packageManager === "npm" && isNpmLockSource(scanInput.source)) {
28
+ workspaceMap = buildNpmWorkspaceMap(scanInput.filePath, { includeAliasIdentities: true });
27
29
  }
28
30
  else if (packageManager === "pnpm" && scanInput.source === "pnpm-lock") {
29
31
  workspaceMap = buildPnpmWorkspaceMap(scanInput.filePath);
@@ -87,7 +89,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
87
89
  const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
88
90
  // In offline mode validateDirectFixTargets never runs, so an unset
89
91
  // validatedFirstFixedVersion is "did not validate" rather than "validation
90
- // failed" — fall back to the advisory hint instead of dropping the target.
92
+ // failed" - fall back to the advisory hint instead of dropping the target.
91
93
  const validatedFieldOk = offline || !hasValidatedField || finding.validatedFirstFixedVersion !== null;
92
94
  // A confirmed fix version is a direct answer to "is there a safe upgrade",
93
95
  // where the counter comparison below is only an inference about one. When
@@ -102,7 +104,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
102
104
  finding.validatedTargetScannedVersions > 0 &&
103
105
  finding.validatedTargetKnownVulnerableVersions >= finding.validatedTargetScannedVersions;
104
106
  if (directTarget && allVersionsVulnerable) {
105
- skippedByKey.set(`direct:${finding.pkg.name}@${finding.pkg.version}`, {
107
+ skippedByKey.set(`direct:${packageInstanceKey(finding.pkg)}`, {
106
108
  package: finding.pkg.name,
107
109
  version: finding.pkg.version,
108
110
  relationship: finding.relationship,
@@ -113,9 +115,10 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
113
115
  if (directTarget &&
114
116
  isUpgradeTarget(finding.pkg.version, directTarget) &&
115
117
  validatedFieldOk) {
116
- const pkgWorkspaces = workspaceMap.get(finding.pkg.name)?.filter(w => w !== ".") ?? [];
118
+ const pkgWorkspaces = workspaceMap.get(packageNameKey(finding.pkg))?.filter(w => w !== ".") ?? [];
117
119
  upsertTarget(targetsByKey, finding, {
118
120
  package: finding.pkg.name,
121
+ ...(finding.pkg.registryName ? { registryName: finding.pkg.registryName } : {}),
119
122
  currentVersion: finding.pkg.version,
120
123
  targetVersion: directTarget,
121
124
  scannedVersions: finding.validatedTargetScannedVersions ?? null,
@@ -140,15 +143,16 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
140
143
  });
141
144
  }
142
145
  else if (finding.fixVersionValidationNote) {
143
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
146
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
144
147
  package: finding.pkg.name,
145
148
  version: finding.pkg.version,
146
149
  relationship: finding.relationship,
147
150
  reason: finding.fixVersionValidationNote,
151
+ advisoryHintUnavailable: finding.advisoryHintUnavailable ?? false,
148
152
  });
149
153
  }
150
154
  else if (finding.firstFixedVersion) {
151
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
155
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
152
156
  package: finding.pkg.name,
153
157
  version: finding.pkg.version,
154
158
  relationship: finding.relationship,
@@ -156,7 +160,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
156
160
  });
157
161
  }
158
162
  else {
159
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
163
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
160
164
  package: finding.pkg.name,
161
165
  version: finding.pkg.version,
162
166
  relationship: finding.relationship,
@@ -168,7 +172,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
168
172
  continue;
169
173
  }
170
174
  if (finding.relationship === "unknown") {
171
- skippedByKey.set(`unknown:${finding.pkg.name}@${finding.pkg.version}`, {
175
+ skippedByKey.set(`unknown:${packageInstanceKey(finding.pkg)}`, {
172
176
  package: finding.pkg.name,
173
177
  version: finding.pkg.version,
174
178
  relationship: finding.relationship,
@@ -184,11 +188,28 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
184
188
  currentVersion: finding.recommendedNpmTransitiveRemediation.currentVersion,
185
189
  childTargetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
186
190
  reason: finding.recommendedNpmTransitiveRemediation.reason,
187
- command: buildNpmUpdateCommand(finding.recommendedNpmTransitiveRemediation.package, finding.recommendedNpmTransitiveRemediation.workspaces),
191
+ // Name the package that actually moves. This remediation means the parent
192
+ // already allows a safe child version, so the parent stays where it is and
193
+ // the child is what has to be refreshed. Naming the parent achieves nothing
194
+ // when it is already at its newest allowed version, because npm has no
195
+ // reason to re-resolve its subtree.
196
+ command: buildNpmUpdateCommand(finding.pkg.name, finding.recommendedNpmTransitiveRemediation.workspaces),
188
197
  isDev: devLookup.get(`${finding.recommendedNpmTransitiveRemediation.package}@${finding.recommendedNpmTransitiveRemediation.currentVersion}`) ?? false,
198
+ workspaces: finding.recommendedNpmTransitiveRemediation.workspaces,
189
199
  }));
190
200
  continue;
191
201
  }
202
+ // A parent upgrade that a direct dependency would undo is not a fix: that
203
+ // dependency keeps the vulnerable copy installed (#1457).
204
+ if (finding.relationship === "transitive" && finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length) {
205
+ skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
206
+ package: finding.pkg.name,
207
+ version: finding.pkg.version,
208
+ relationship: finding.relationship,
209
+ reason: formatParentUpgradeBlockedReason(finding),
210
+ });
211
+ continue;
212
+ }
192
213
  if (packageManager === "npm" &&
193
214
  finding.relationship === "transitive" &&
194
215
  finding.recommendedNpmTransitiveRemediation?.kind === "upgrade-parent-to-version" &&
@@ -257,7 +278,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
257
278
  continue;
258
279
  }
259
280
  if (finding.relationship === "transitive" && finding.recommendedParentUpgrade) {
260
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
281
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
261
282
  package: finding.pkg.name,
262
283
  version: finding.pkg.version,
263
284
  relationship: finding.relationship,
@@ -271,14 +292,15 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
271
292
  const lockfileRefreshCommand = packageManager === "pnpm"
272
293
  ? buildPnpmLockfileRefreshCommand(finding.pkg.name, finding.recommendedNpmTransitiveRemediation.workspaces)
273
294
  : packageManager === "yarn"
274
- ? `yarn upgrade ${finding.pkg.name}`
275
- : `bun update ${finding.pkg.name}`;
295
+ ? buildYarnUpgradeCommand(finding.pkg.name, finding.recommendedNpmTransitiveRemediation.workspaces)
296
+ : buildBunUpdateCommand(finding.pkg.name);
276
297
  upsertTarget(targetsByKey, finding, parentUpdateTarget(finding, urgent, {
277
298
  package: finding.pkg.name,
278
299
  currentVersion: finding.pkg.version,
279
300
  childTargetVersion: finding.recommendedNpmTransitiveRemediation.targetChildVersion,
280
- reason: `${finding.pkg.name}@${finding.pkg.version} can be refreshed to ${finding.recommendedNpmTransitiveRemediation.targetChildVersion}+ — no parent upgrade needed.`,
301
+ reason: `${finding.pkg.name}@${finding.pkg.version} can be refreshed to ${finding.recommendedNpmTransitiveRemediation.targetChildVersion}+; no parent upgrade needed.`,
281
302
  command: lockfileRefreshCommand,
303
+ workspaces: finding.recommendedNpmTransitiveRemediation?.workspaces,
282
304
  }));
283
305
  continue;
284
306
  }
@@ -312,7 +334,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
312
334
  const fixClause = fixHint
313
335
  ? ` - check for a release that resolves ${finding.pkg.name} to ${fixHint}+`
314
336
  : "";
315
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
337
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
316
338
  package: finding.pkg.name,
317
339
  version: finding.pkg.version,
318
340
  relationship: finding.relationship,
@@ -320,7 +342,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
320
342
  });
321
343
  continue;
322
344
  }
323
- skippedByKey.set(`${finding.relationship}:${finding.pkg.name}@${finding.pkg.version}`, {
345
+ skippedByKey.set(`${finding.relationship}:${packageInstanceKey(finding.pkg)}`, {
324
346
  package: finding.pkg.name,
325
347
  version: finding.pkg.version,
326
348
  relationship: finding.relationship,
@@ -344,10 +366,27 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
344
366
  });
345
367
  // Some install targets carry an explicit `command` built without workspace
346
368
  // context (e.g. a chain-resolution command). This raw field is exposed in JSON
347
- // and embedded in the HTML report, so scope it to the declaring member too -
348
- // parent-update commands are already workspace-aware and left untouched.
369
+ // and embedded in the HTML report, so scope it to the declaring member too.
370
+ // Parent-update commands are rebuilt from the (possibly merged) workspace list
371
+ // so the printed plan matches what --fix will spawn.
349
372
  for (const target of targets) {
350
- if (target.kind !== "parent-update" && target.command && target.workspaces?.length) {
373
+ if (target.kind === "parent-update") {
374
+ const updatePkg = target.childPackage ?? target.package;
375
+ if (packageManager === "npm") {
376
+ target.command = buildNpmUpdateCommand(updatePkg, target.workspaces);
377
+ }
378
+ else if (packageManager === "pnpm") {
379
+ target.command = buildPnpmLockfileRefreshCommand(updatePkg, target.workspaces);
380
+ }
381
+ else if (packageManager === "yarn") {
382
+ target.command = buildYarnUpgradeCommand(updatePkg, target.workspaces);
383
+ }
384
+ else if (packageManager === "bun") {
385
+ target.command = buildBunUpdateCommand(updatePkg);
386
+ }
387
+ continue;
388
+ }
389
+ if (target.command && target.workspaces?.length) {
351
390
  const scoped = buildWorkspaceInstallCommands([target], packageManager);
352
391
  if (scoped.length > 0)
353
392
  target.command = scoped.join(" && ");
@@ -402,36 +441,43 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
402
441
  }
403
442
  return plan;
404
443
  }
405
- function inferPackageManager(scanInput) {
406
- if (scanInput.source === "package-lock")
407
- return "npm";
408
- if (scanInput.source === "pnpm-lock")
409
- return "pnpm";
410
- if (scanInput.source === "yarn-lock")
411
- return "yarn";
412
- if (scanInput.source === "bun-lock")
413
- return "bun";
414
- return null;
415
- }
416
444
  function buildNpmUpdateCommand(pkg, workspaces) {
417
445
  const nonRoot = (workspaces ?? []).filter(w => w !== ".");
446
+ const pkgQ = shellQuote(pkg);
418
447
  if (nonRoot.length === 1) {
419
- return `npm update --workspace=${nonRoot[0]} ${pkg}`;
448
+ return `npm update --workspace=${shellQuote(nonRoot[0])} ${pkgQ}`;
420
449
  }
421
450
  if (nonRoot.length > 1) {
422
- return `npm update --workspaces ${pkg}`;
451
+ return `npm update --workspaces ${pkgQ}`;
423
452
  }
424
- return `npm update ${pkg}`;
453
+ return `npm update ${pkgQ}`;
425
454
  }
426
455
  function buildPnpmLockfileRefreshCommand(pkg, workspaces) {
456
+ const pkgQ = shellQuote(pkg);
427
457
  if (!workspaces || workspaces.length === 0 || workspaces.length > 1) {
428
- return `pnpm update --recursive --no-save ${pkg}`;
458
+ return `pnpm update --recursive --no-save ${pkgQ}`;
429
459
  }
430
460
  const workspace = workspaces[0];
431
461
  if (workspace === ".") {
432
- return `pnpm update --no-save ${pkg}`;
462
+ return `pnpm update --no-save ${pkgQ}`;
433
463
  }
434
- return `pnpm --filter ./${workspace} update --no-save ${pkg}`;
464
+ return `pnpm --filter ${shellQuote("./" + workspace)} update --no-save ${pkgQ}`;
465
+ }
466
+ function buildYarnUpgradeCommand(pkg, workspaces) {
467
+ const pkgQ = shellQuote(pkg);
468
+ const nonRoot = (workspaces ?? []).filter(w => w !== ".");
469
+ if (nonRoot.length === 0)
470
+ return `yarn upgrade ${pkgQ}`;
471
+ return nonRoot.map(ws => `yarn workspace ${shellQuote(ws)} upgrade ${pkgQ}`).join(" && ");
472
+ }
473
+ function buildBunUpdateCommand(pkg) {
474
+ return `bun update ${shellQuote(pkg)}`;
475
+ }
476
+ /** Quote a token for human-facing plan strings that users may paste into a shell. */
477
+ function shellQuote(token) {
478
+ if (/^[A-Za-z0-9_@%+./:=,-]+$/.test(token))
479
+ return token;
480
+ return "'" + token.replace(/'/g, "'\\''") + "'";
435
481
  }
436
482
  export function commandPrefix(packageManager) {
437
483
  if (packageManager === "npm")
@@ -446,6 +492,10 @@ function devFlag(packageManager) {
446
492
  return packageManager === "bun" ? "--dev" : "-D";
447
493
  }
448
494
  export function findFixTargetForFinding(plan, finding) {
495
+ // A sibling finding may share the parent upgrade, but a direct dependency
496
+ // keeps this copy installed, so no command in the plan fixes this one.
497
+ if (finding.recommendedParentUpgrade && finding.parentUpgradeBlockers?.length)
498
+ return null;
449
499
  const target = plan.targets.find(item => {
450
500
  if (finding.recommendedNpmTransitiveRemediation?.kind === "update-parent-within-range") {
451
501
  const rem = finding.recommendedNpmTransitiveRemediation;
@@ -474,12 +524,32 @@ export function findFixTargetForFinding(plan, finding) {
474
524
  compareVersions(item.targetVersion, finding.recommendedParentUpgrade.targetVersion) >= 0);
475
525
  }
476
526
  if (finding.chainResolution) {
477
- return (item.package === finding.chainResolution.directDep &&
478
- item.targetVersion === finding.chainResolution.targetVersion);
527
+ if (item.package !== finding.chainResolution.directDep)
528
+ return false;
529
+ if (item.targetVersion === finding.chainResolution.targetVersion)
530
+ return true;
531
+ // Mirrors the recommendedParentUpgrade branch above. When several children are
532
+ // cleared by the same parent, upsertTarget keeps the highest parent version, so a
533
+ // child whose chain was verified at a lower version failed an exact match and was
534
+ // left with no command, even though the command already in the plan covers it.
535
+ return (looksLikeVersion(item.targetVersion) &&
536
+ looksLikeVersion(finding.chainResolution.targetVersion) &&
537
+ compareVersions(item.targetVersion, finding.chainResolution.targetVersion) >= 0);
479
538
  }
480
539
  const directTarget = finding.validatedFirstFixedVersion ?? finding.firstFixedVersion;
481
540
  if (item.package !== finding.pkg.name)
482
541
  return false;
542
+ if (registryPackageName({ name: item.package, registryName: item.registryName }) !== registryPackageName(finding.pkg)) {
543
+ return false;
544
+ }
545
+ // A transitive finding is only fixed through a remediation, and each kind of
546
+ // remediation has its own branch above. Falling through to here for one means
547
+ // nothing was resolved for it, so matching it against a direct install of the same
548
+ // package name would claim that installing at the root moves a copy nested under a
549
+ // parent. npm does not guarantee that. A finding with no verified fix belongs in the
550
+ // skipped list with a reason, not holding somebody else's command.
551
+ if (finding.relationship !== "direct")
552
+ return false;
483
553
  if (item.targetVersion === directTarget)
484
554
  return true;
485
555
  // Mirrors the recommendedParentUpgrade branch above: a command targeting a
@@ -513,7 +583,7 @@ export function buildTargetCommand(target, packageManager) {
513
583
  if (target.command)
514
584
  return target.command;
515
585
  const flag = target.isDev ? ` ${devFlag(packageManager)}` : "";
516
- return `${commandPrefix(packageManager)}${flag} ${target.package}@${target.targetVersion}`;
586
+ return `${commandPrefix(packageManager)}${flag} ${shellQuote(installPackageSpec(target))}`;
517
587
  }
518
588
  export function findSuggestedCommandForFinding(plan, finding) {
519
589
  const target = findFixTargetForFinding(plan, finding);
@@ -545,10 +615,16 @@ function packageManagerSourceLabel(scanInput) {
545
615
  // end up pinned onto the parent (`npm install axios@4.0.6`, where 4.0.6 is form-data's
546
616
  // version - #1007). Keep parent-update targets in their own keyspace, per child, so
547
617
  // they only ever merge with a refresh of the same child through the same package.
618
+ // The key carries the vulnerable package's INSTALLED version as well as its name. A tree
619
+ // routinely holds two majors of one package, and keying on the name alone merged them into
620
+ // a single target: the surviving row recorded one version pair, and the other finding was
621
+ // left with no target and no skipped entry, so it disappeared from the plan while having a
622
+ // known fix. The duplicate commands that now result collapse again downstream, because the
623
+ // emitted command list is de-duplicated.
548
624
  function targetKey(finding, target) {
549
625
  return target.kind === "parent-update"
550
- ? `parent-update:${target.package}>${finding.pkg.name}`
551
- : `install:${target.package}`;
626
+ ? `parent-update:${target.package}>${finding.pkg.name}@${finding.pkg.version}`
627
+ : `install:${packageNameKey({ name: target.package, registryName: target.registryName })}`;
552
628
  }
553
629
  // Build a parent-update target whose targetVersion always belongs to `package`.
554
630
  // When the named package is the parent of the finding, the child's version moves
@@ -558,6 +634,10 @@ function targetKey(finding, target) {
558
634
  // the child's destination.
559
635
  function parentUpdateTarget(finding, urgent, args) {
560
636
  const namesParent = args.package !== finding.pkg.name;
637
+ // Keep "." in the list: pnpm treats a sole root workspace as a plain refresh
638
+ // (no --filter / no --recursive). Stripping it here collapsed that case into
639
+ // the empty-workspaces branch and emitted --recursive.
640
+ const workspaces = args.workspaces ?? [];
561
641
  return {
562
642
  package: args.package,
563
643
  currentVersion: args.currentVersion,
@@ -575,6 +655,7 @@ function parentUpdateTarget(finding, urgent, args) {
575
655
  command: args.command,
576
656
  usage: finding.usage ?? null,
577
657
  isDev: args.isDev,
658
+ workspaces: workspaces.length > 0 ? workspaces : undefined,
578
659
  };
579
660
  }
580
661
  /**
@@ -825,16 +906,16 @@ function buildWorkspaceInstallCommands(targets, packageManager) {
825
906
  for (const [targets, isDev] of [[prodTargets, false], [devTargets, true]]) {
826
907
  if (targets.length === 0)
827
908
  continue;
828
- const pkgArgs = targets.map(t => `${t.package}@${t.targetVersion}`).join(" ");
909
+ const pkgArgs = targets.map(t => shellQuote(installPackageSpec(t))).join(" ");
829
910
  const flag = isDev ? ` ${devFlag(packageManager)}` : "";
830
911
  if (packageManager === "npm") {
831
- const wsFlags = workspaces.map(ws => `-w ${ws}`).join(" ");
912
+ const wsFlags = workspaces.map(ws => `-w ${shellQuote(ws)}`).join(" ");
832
913
  commands.push(`npm install${flag}${wsFlags ? " " + wsFlags : ""} ${pkgArgs}`);
833
914
  }
834
915
  else if (packageManager === "pnpm") {
835
916
  // pnpm's --filter selector is a CLI-level flag that precedes the command
836
917
  // (`pnpm --filter ./client add pkg`), not a trailing argument.
837
- const wsFlags = workspaces.map(ws => `--filter ./${ws}`).join(" ");
918
+ const wsFlags = workspaces.map(ws => `--filter ${shellQuote("./" + ws)}`).join(" ");
838
919
  const prefix = wsFlags ? `pnpm ${wsFlags} add` : "pnpm add";
839
920
  commands.push(`${prefix}${flag} ${pkgArgs}`);
840
921
  }
@@ -844,12 +925,12 @@ function buildWorkspaceInstallCommands(targets, packageManager) {
844
925
  }
845
926
  else {
846
927
  for (const ws of workspaces) {
847
- commands.push(`yarn workspace ${ws} add${flag} ${pkgArgs}`);
928
+ commands.push(`yarn workspace ${shellQuote(ws)} add${flag} ${pkgArgs}`);
848
929
  }
849
930
  }
850
931
  }
851
932
  else if (packageManager === "bun") {
852
- const wsFlags = workspaces.map(ws => `--filter ${ws}`).join(" ");
933
+ const wsFlags = workspaces.map(ws => `--filter ${shellQuote(ws)}`).join(" ");
853
934
  commands.push(`bun add${flag}${wsFlags ? " " + wsFlags : ""} ${pkgArgs}`);
854
935
  }
855
936
  else {
@@ -870,7 +951,7 @@ function buildCommandForTargets(targets, packageManager) {
870
951
  const updateCommands = explicitCommands.length > 0
871
952
  ? [...new Set(explicitCommands)]
872
953
  : packageManager === "npm"
873
- ? [`npm update ${parentUpdateTargets.map(target => target.package).join(" ")}`]
954
+ ? [`npm update ${parentUpdateTargets.map(target => shellQuote(target.childPackage ?? target.package)).join(" ")}`]
874
955
  : [];
875
956
  commandParts.push(...updateCommands);
876
957
  }
@@ -883,10 +964,10 @@ function buildCommandForTargets(targets, packageManager) {
883
964
  const prodInstall = installTargets.filter(t => !t.isDev);
884
965
  const devInstall = installTargets.filter(t => t.isDev);
885
966
  if (prodInstall.length > 0) {
886
- commandParts.push(`${commandPrefix(packageManager)} ${prodInstall.map(t => `${t.package}@${t.targetVersion}`).join(" ")}`);
967
+ commandParts.push(`${commandPrefix(packageManager)} ${prodInstall.map(t => shellQuote(installPackageSpec(t))).join(" ")}`);
887
968
  }
888
969
  if (devInstall.length > 0) {
889
- commandParts.push(`${commandPrefix(packageManager)} ${devFlag(packageManager)} ${devInstall.map(t => `${t.package}@${t.targetVersion}`).join(" ")}`);
970
+ commandParts.push(`${commandPrefix(packageManager)} ${devFlag(packageManager)} ${devInstall.map(t => shellQuote(installPackageSpec(t))).join(" ")}`);
890
971
  }
891
972
  }
892
973
  }
@@ -895,3 +976,12 @@ function buildCommandForTargets(targets, packageManager) {
895
976
  function capitalize(value) {
896
977
  return value.charAt(0).toUpperCase() + value.slice(1);
897
978
  }
979
+ // Shared by the plan and the per-finding recommendation, so both say the same thing.
980
+ export function formatParentUpgradeBlockedReason(finding) {
981
+ const upgrade = finding.recommendedParentUpgrade;
982
+ const blockers = (finding.parentUpgradeBlockers ?? [])
983
+ .map(blocker => `${blocker.package}@${blocker.version} requires ${blocker.range}`)
984
+ .join(", ");
985
+ const parent = upgrade ? `${upgrade.package} to ${upgrade.targetVersion}` : "the parent";
986
+ return `Upgrading ${parent} alone leaves ${finding.pkg.name}@${finding.pkg.version} installed: ${blockers}. Those dependencies need upgrading too.`;
987
+ }
@@ -1,4 +1,5 @@
1
1
  import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
2
+ import { registryForPackage } from "../utils/registry-config.js";
2
3
  import { AsyncLocalStorage } from "node:async_hooks";
3
4
  const packumentCache = new Map();
4
5
  const DEFAULT_NPM_REGISTRY_URL = "https://registry.npmjs.org";
@@ -46,8 +47,50 @@ export async function fetchPackument(packageName) {
46
47
  notifyPackumentObserver(result, packageName);
47
48
  return result.packument;
48
49
  }
50
+ let registryConfig = null;
51
+ /**
52
+ * Points package-metadata lookups at the registry the project installs from.
53
+ *
54
+ * Null restores the public registry, which is also the state when a project has no
55
+ * configuration, so an unconfigured scan behaves exactly as it did before.
56
+ *
57
+ * Only metadata lookups move. The npm advisory bulk endpoint deliberately stays on
58
+ * the public registry: a Nexus or Artifactory mirror proxies package metadata but
59
+ * generally does not implement `/-/npm/v1/security/advisories/bulk`, so redirecting
60
+ * it would turn a working advisory source into a 404 on every enterprise scan.
61
+ */
62
+ export function configureNpmRegistry(config) {
63
+ registryConfig = config;
64
+ }
65
+ /**
66
+ * Names the registry a lookup for this package actually went to, for use inside
67
+ * a sentence.
68
+ *
69
+ * A message that says npm when we asked a mirror is a false statement, and it is
70
+ * false in the direction that matters: the actionable fact is that the registry
71
+ * the project installs from is behind, not that public npm is missing something.
72
+ *
73
+ * Resolved per package with the same expression `doFetchPackument` uses, rather
74
+ * than from `registryConfig.registry`, because a scoped package can carry its own
75
+ * registry and the top-level value would then name the wrong one.
76
+ */
77
+ function registryLabelFor(packageName) {
78
+ const resolved = registryConfig
79
+ ? registryForPackage(registryConfig, packageName)
80
+ : DEFAULT_NPM_REGISTRY_URL;
81
+ // Trailing slashes are trimmed before comparing so that both spellings of the
82
+ // public registry are recognised: this module's own constant carries none,
83
+ // while the resolved configuration default carries one.
84
+ const trimmed = resolved.replace(/\/+$/, "");
85
+ if (trimmed === DEFAULT_NPM_REGISTRY_URL.replace(/\/+$/, ""))
86
+ return "npm";
87
+ return trimmed;
88
+ }
49
89
  async function doFetchPackument(packageName) {
50
- const url = `${DEFAULT_NPM_REGISTRY_URL}/${encodeURIComponent(packageName)
90
+ const base = registryConfig
91
+ ? registryForPackage(registryConfig, packageName)
92
+ : DEFAULT_NPM_REGISTRY_URL;
93
+ const url = `${base.replace(/\/+$/, "")}/${encodeURIComponent(packageName)
51
94
  .replace(/%40/g, "@")
52
95
  .replace(/%2F/g, "/")}`;
53
96
  const batchId = registryDebugLog ? nextRegistryBatchId() : "";
@@ -109,7 +152,7 @@ export async function resolvePublishedFixVersion(packageName, fixedVersionHint)
109
152
  if (!packument) {
110
153
  return {
111
154
  resolvedVersion: null,
112
- note: `Fixed-version hint ${fixedVersionHint} could not be verified against the npm registry for ${packageName}.`,
155
+ note: `Fixed-version hint ${fixedVersionHint} could not be verified for ${packageName} against ${registryLabelFor(packageName)}.`,
113
156
  verified: false,
114
157
  candidatesChecked: 0,
115
158
  candidatesKnownVulnerable: 0,
@@ -139,20 +182,26 @@ export async function resolvePublishedFixVersion(packageName, fixedVersionHint)
139
182
  resolvedVersion: nearestPublishedVersion,
140
183
  note: fixedVersionHintIsPreRelease
141
184
  ? `Advisory fixed-version hint ${fixedVersionHint} is a pre-release; using nearest published non-pre-release version ${nearestPublishedVersion}.`
142
- : `Advisory fixed-version hint ${fixedVersionHint} is not published on npm for ${packageName}; using nearest published version ${nearestPublishedVersion}.`,
185
+ : `Advisory fixed-version hint ${fixedVersionHint} was not found for ${packageName} on ${registryLabelFor(packageName)}; using nearest published version ${nearestPublishedVersion}.`,
143
186
  verified: true,
144
187
  candidatesChecked: 0,
145
188
  candidatesKnownVulnerable: 0,
146
189
  candidatesUnknownCoverage: 0,
147
190
  publishedAt: packument.time?.[nearestPublishedVersion] ?? null,
191
+ // Set from the same test that picks the wording above, so the flag and the
192
+ // sentence can never disagree. A pre-release hint is deliberately excluded:
193
+ // that is reported as a pre-release problem rather than an availability one,
194
+ // and it belongs in a different section of the output.
195
+ advisoryHintUnavailable: !fixedVersionHintIsPreRelease,
148
196
  };
149
197
  }
150
198
  return {
151
199
  resolvedVersion: null,
152
200
  note: fixedVersionHintIsPreRelease
153
201
  ? `Advisory fixed-version hint ${fixedVersionHint} is a pre-release, and no published non-pre-release version >= ${fixedVersionHint} was found for ${packageName}.`
154
- : `Advisory fixed-version hint ${fixedVersionHint} is not published on npm for ${packageName}, and no published version >= ${fixedVersionHint} was found.`,
202
+ : `Advisory fixed-version hint ${fixedVersionHint} was not found for ${packageName} on ${registryLabelFor(packageName)}, and no version >= ${fixedVersionHint} was found there either.`,
155
203
  verified: true,
204
+ advisoryHintUnavailable: !fixedVersionHintIsPreRelease,
156
205
  candidatesChecked: 0,
157
206
  candidatesKnownVulnerable: 0,
158
207
  candidatesUnknownCoverage: 0,
@@ -195,7 +244,7 @@ async function resolveFromAdvisoryRanges(packageName, installedVersion, vulnerab
195
244
  if (!packument) {
196
245
  return {
197
246
  resolvedVersion: null,
198
- note: `Published versions for ${packageName} could not be verified against the npm registry.`,
247
+ note: `Published versions for ${packageName} could not be verified against ${registryLabelFor(packageName)}.`,
199
248
  verified: false,
200
249
  candidatesChecked: 0,
201
250
  candidatesKnownVulnerable: 0,
@@ -211,7 +260,7 @@ async function resolveFromAdvisoryRanges(packageName, installedVersion, vulnerab
211
260
  if (candidates.length === 0) {
212
261
  return {
213
262
  resolvedVersion: null,
214
- note: `No published versions above ${installedVersion} were found for ${packageName}.`,
263
+ note: `No versions above ${installedVersion} were found for ${packageName} on ${registryLabelFor(packageName)}.`,
215
264
  verified: true,
216
265
  candidatesChecked: 0,
217
266
  candidatesKnownVulnerable: 0,
@@ -269,7 +318,7 @@ async function resolveFromAdvisoryRanges(packageName, installedVersion, vulnerab
269
318
  }
270
319
  return {
271
320
  resolvedVersion: null,
272
- note: `No known non-vulnerable published version was found above ${installedVersion} for ${packageName}.`,
321
+ note: `No known non-vulnerable version was found above ${installedVersion} for ${packageName} on ${registryLabelFor(packageName)}.`,
273
322
  verified: true,
274
323
  candidatesChecked,
275
324
  candidatesKnownVulnerable,