cve-lite-cli 1.41.0 → 1.41.1

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.
@@ -1,13 +1,11 @@
1
1
  import { OVERRIDES_COMMAND } from "../constants.js";
2
- // The folder a fix command targets, relative to the scanned project root
3
- // ("packages/a"), or empty for the root itself. Never pass a cwd-relative or
4
- // absolute path: the HTML report is opened from an unknown directory, so only a
5
- // project-relative target stays runnable when it is copied out.
2
+ import { quoteShellPath } from "../utils/shell-quote.js";
3
+ // HTML callers pass a project-relative path, and terminal callers pass a
4
+ // cwd-relative path. An empty target or "." needs no positional argument.
6
5
  export function overrideCommandTargetSuffix(commandTarget) {
7
- const normalized = commandTarget?.replace(/\\/g, "/");
8
- return normalized && normalized !== "." ? ` ${normalized}` : "";
6
+ return commandTarget && commandTarget !== "." ? ` ${quoteShellPath(commandTarget)}` : "";
9
7
  }
10
8
  export function buildOverrideFixCommand(runnableCommand, commandTarget) {
11
9
  const suffix = overrideCommandTargetSuffix(commandTarget);
12
- return runnableCommand.replace(OVERRIDES_COMMAND, `cve-lite${suffix} overrides`);
10
+ return runnableCommand.replace(OVERRIDES_COMMAND, () => `cve-lite${suffix} overrides`);
13
11
  }
@@ -1,3 +1,4 @@
1
+ import { fetchRegistryMetadata } from '../../utils/registry-fetch.js';
1
2
  import { compareVersions, isPreReleaseVersion } from '../../utils/version.js';
2
3
  async function classify(attempt, parse) {
3
4
  let res;
@@ -19,33 +20,21 @@ async function classify(attempt, parse) {
19
20
  return { kind: "failed" };
20
21
  }
21
22
  }
22
- /**
23
- * Fetch the `dist-tags` map for `pkgName` from the npm registry.
24
- *
25
- * Returns null on any failure (network error, non-200, parse error, timeout).
26
- * The caller surfaces a `skippedDetectors` note when this returns null for a
27
- * rule that depends on registry data.
28
- *
29
- * Per scan, callers should batch-fetch all needed packages in parallel and
30
- * cache the result on `Context.registryDistTags`. This function is single-shot;
31
- * caching/batching is the scanner's responsibility.
32
- */
33
23
  /** Shared request shape for both lookups. */
34
24
  async function packumentOutcome(pkgName, opts, parse) {
35
25
  const baseUrl = opts.baseUrl ?? 'https://registry.npmjs.org';
36
26
  const timeoutMs = opts.timeoutMs ?? 5000;
37
- const fetchImpl = opts.fetchImpl ?? fetch;
38
- const url = `${baseUrl}/${encodeRegistryPath(pkgName)}`;
27
+ const url = `${baseUrl.replace(/\/+$/, "")}/${encodeRegistryPath(pkgName)}`;
39
28
  const controller = new AbortController();
40
29
  const timer = setTimeout(() => controller.abort(), timeoutMs);
41
30
  try {
42
- return await classify(() => fetchImpl(url, {
31
+ return await classify(() => fetchRegistryMetadata(baseUrl, url, opts.allowPrivate ?? false, {
43
32
  signal: controller.signal,
44
33
  headers: {
45
34
  Accept: 'application/vnd.npm.install-v1+json',
46
35
  'User-Agent': 'override-audit-cli',
47
36
  },
48
- }), parse);
37
+ }, opts.fetchImpl), parse);
49
38
  }
50
39
  finally {
51
40
  clearTimeout(timer);
@@ -67,32 +56,20 @@ async function versionsOutcome(pkgName, opts) {
67
56
  .sort(compareVersions);
68
57
  });
69
58
  }
59
+ /**
60
+ * Fetch the `dist-tags` map for `pkgName` from the npm registry.
61
+ *
62
+ * Returns null on any failure (network error, non-200, parse error, timeout).
63
+ * The caller surfaces a `skippedDetectors` note when this returns null for a
64
+ * rule that depends on registry data.
65
+ *
66
+ * Per scan, callers should batch-fetch all needed packages in parallel and
67
+ * cache the result on `Context.registryDistTags`. This function is single-shot;
68
+ * caching/batching is the scanner's responsibility.
69
+ */
70
70
  export async function fetchDistTags(pkgName, opts = {}) {
71
- const baseUrl = opts.baseUrl ?? 'https://registry.npmjs.org';
72
- const timeoutMs = opts.timeoutMs ?? 5000;
73
- const fetchImpl = opts.fetchImpl ?? fetch;
74
- const url = `${baseUrl}/${encodeRegistryPath(pkgName)}`;
75
- const controller = new AbortController();
76
- const timer = setTimeout(() => controller.abort(), timeoutMs);
77
- try {
78
- const res = await fetchImpl(url, {
79
- signal: controller.signal,
80
- headers: {
81
- Accept: 'application/vnd.npm.install-v1+json',
82
- 'User-Agent': 'override-audit-cli',
83
- },
84
- });
85
- if (!res.ok)
86
- return null;
87
- const body = await res.json();
88
- return body['dist-tags'] ?? null;
89
- }
90
- catch {
91
- return null;
92
- }
93
- finally {
94
- clearTimeout(timer);
95
- }
71
+ const outcome = await distTagsOutcome(pkgName, opts);
72
+ return outcome.kind === "ok" ? outcome.value : null;
96
73
  }
97
74
  /**
98
75
  * Fetch dist-tags for many packages concurrently. Failures collapse to null
@@ -122,35 +99,8 @@ export async function fetchDistTagsBatch(pkgNames, opts = {}) {
122
99
  * Returns null on any failure, matching `fetchDistTags`.
123
100
  */
124
101
  export async function fetchVersions(pkgName, opts = {}) {
125
- const baseUrl = opts.baseUrl ?? 'https://registry.npmjs.org';
126
- const timeoutMs = opts.timeoutMs ?? 5000;
127
- const fetchImpl = opts.fetchImpl ?? fetch;
128
- const url = `${baseUrl}/${encodeRegistryPath(pkgName)}`;
129
- const controller = new AbortController();
130
- const timer = setTimeout(() => controller.abort(), timeoutMs);
131
- try {
132
- const res = await fetchImpl(url, {
133
- signal: controller.signal,
134
- headers: {
135
- Accept: 'application/vnd.npm.install-v1+json',
136
- 'User-Agent': 'override-audit-cli',
137
- },
138
- });
139
- if (!res.ok)
140
- return null;
141
- const body = await res.json();
142
- if (!body.versions)
143
- return null;
144
- return Object.keys(body.versions)
145
- .filter((v) => !isPreReleaseVersion(v))
146
- .sort(compareVersions);
147
- }
148
- catch {
149
- return null;
150
- }
151
- finally {
152
- clearTimeout(timer);
153
- }
102
+ const outcome = await versionsOutcome(pkgName, opts);
103
+ return outcome.kind === "ok" ? outcome.value : null;
154
104
  }
155
105
  /**
156
106
  * Fetch version lists for many packages concurrently. Per-package failures
@@ -1,6 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import { parse as parseYarnLock } from "yarn-lockfile";
3
- import { isYarnBerry, parseBerryDependencies, parseYarnPackageKey } from "./yarn-lock.js";
3
+ import { isYarnBerry, parseBerryDependencies, parseYarnClassicSelectorKey, parseYarnPackageKey } from "./yarn-lock.js";
4
4
  const EMPTY = Object.freeze([]);
5
5
  /**
6
6
  * Builds the index for a Berry (v2+) lockfile.
@@ -51,7 +51,17 @@ function buildBerryIndex(content) {
51
51
  }
52
52
  graph.set(canonicalKey, [...children]);
53
53
  }
54
- return { selectorToKey, graph };
54
+ return { selectorToKey, graph, registryNameByKey: new Map() };
55
+ }
56
+ function classicCanonicalKey(selectorKey, version) {
57
+ const identity = parseYarnClassicSelectorKey(selectorKey);
58
+ if (!identity)
59
+ return null;
60
+ return {
61
+ key: `${identity.name}@${version}`,
62
+ selectors: identity.selectors,
63
+ ...(identity.registryName ? { registryName: identity.registryName } : {}),
64
+ };
55
65
  }
56
66
  /**
57
67
  * Builds the index for a classic (v1) lockfile, which the `yarn-lockfile`
@@ -60,28 +70,27 @@ function buildBerryIndex(content) {
60
70
  function buildClassicIndex(parsed) {
61
71
  const selectorToKey = new Map();
62
72
  const graph = new Map();
73
+ const registryNameByKey = new Map();
63
74
  const entries = Object.entries(parsed?.object ?? {});
64
75
  for (const [selectorKey, meta] of entries) {
65
76
  const version = meta?.version;
66
77
  if (!version)
67
78
  continue;
68
- const selectors = String(selectorKey).split(",").map(part => part.trim()).filter(Boolean);
69
- const atIndex = selectors[0]?.lastIndexOf("@") ?? -1;
70
- if (atIndex <= 0)
79
+ const entry = classicCanonicalKey(selectorKey, version);
80
+ if (!entry)
71
81
  continue;
72
- const canonicalKey = `${selectors[0].slice(0, atIndex)}@${version}`;
73
- for (const selector of selectors)
74
- selectorToKey.set(selector, canonicalKey);
82
+ if (entry.registryName)
83
+ registryNameByKey.set(entry.key, entry.registryName);
84
+ for (const selector of entry.selectors)
85
+ selectorToKey.set(selector, entry.key);
75
86
  }
76
87
  for (const [selectorKey, meta] of entries) {
77
88
  const version = meta?.version;
78
89
  if (!version)
79
90
  continue;
80
- const selectors = String(selectorKey).split(",").map(part => part.trim()).filter(Boolean);
81
- const atIndex = selectors[0]?.lastIndexOf("@") ?? -1;
82
- if (atIndex <= 0)
91
+ const entry = classicCanonicalKey(selectorKey, version);
92
+ if (!entry)
83
93
  continue;
84
- const canonicalKey = `${selectors[0].slice(0, atIndex)}@${version}`;
85
94
  const children = new Set();
86
95
  // An optional dependency is installed whenever it can be, so its parent
87
96
  // constrains the installed copy as much as a regular one does.
@@ -94,9 +103,9 @@ function buildClassicIndex(parsed) {
94
103
  children.add(childKey);
95
104
  }
96
105
  }
97
- graph.set(canonicalKey, [...children]);
106
+ graph.set(entry.key, [...children]);
98
107
  }
99
- return { selectorToKey, graph };
108
+ return { selectorToKey, graph, registryNameByKey };
100
109
  }
101
110
  /**
102
111
  * Builds the complete child-to-parents map for a `yarn.lock`, covering both the
@@ -145,7 +154,14 @@ export function loadYarnLockGraph(filePath) {
145
154
  parentsFor: nodeId => parentsByKey.get(nodeId) ?? EMPTY,
146
155
  getNode: nodeId => {
147
156
  const parsedKey = parseYarnPackageKey(nodeId);
148
- return parsedKey ? { name: parsedKey.name, version: parsedKey.version } : null;
157
+ if (!parsedKey)
158
+ return null;
159
+ const registryName = index.registryNameByKey.get(nodeId);
160
+ return {
161
+ name: parsedKey.name,
162
+ version: parsedKey.version,
163
+ ...(registryName ? { registryName } : {}),
164
+ };
149
165
  },
150
166
  };
151
167
  }
@@ -1,6 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { parse as parseYarnLock } from "yarn-lockfile";
4
+ import { npmAliasRegistryName, packageInstanceKey } from "../utils/package-identity.js";
4
5
  import { normalizeRawVersion } from "../utils/version.js";
5
6
  import { upsertPackage, MAX_PATHS_PER_PACKAGE } from "./utils.js";
6
7
  const MAX_PATH_DEPTH = 10;
@@ -144,6 +145,75 @@ export function parseYarnPackageKey(key) {
144
145
  return null;
145
146
  return { key, name, version };
146
147
  }
148
+ /**
149
+ * Yarn v1 records an alias as `<alias>@npm:<package>@<range>`.
150
+ *
151
+ * `yarn 1.22` quotes that key. A scoped target keeps the leading `@` after the
152
+ * protocol (`frames@npm:@babel/code-frame@7.23.5`), and a scoped alias keeps
153
+ * its own (`@my/frames@npm:@babel/code-frame@7.23.5`). The range in the key is
154
+ * what was requested. The installed version is the lockfile `version` field,
155
+ * so this only returns names.
156
+ *
157
+ * A normal key (`lodash@4.17.20`, `@babel/code-frame@^7.23.4`) has no `@npm:`
158
+ * marker. Splitting those on the final `@` is still the package name. Doing
159
+ * that to an alias key yields `util-lib@npm:lodash`, which is not a package
160
+ * OSV knows about.
161
+ */
162
+ export function parseYarnClassicDescriptor(selector) {
163
+ const marker = "@npm:";
164
+ const markerAt = selector.indexOf(marker);
165
+ if (markerAt > 0) {
166
+ const name = selector.slice(0, markerAt);
167
+ const target = selector.slice(markerAt + marker.length);
168
+ if (!name || !target)
169
+ return null;
170
+ // One definition of an alias target, shared with npm lockfiles: a later `@`
171
+ // starts the requested range, and the leading `@` of `@scope/pkg` does not.
172
+ const registryName = npmAliasRegistryName("npm:" + target);
173
+ if (!registryName)
174
+ return null;
175
+ // `lodash@npm:lodash@4.17.20` is the same package under its own name.
176
+ if (registryName === name)
177
+ return { name };
178
+ return { name, registryName };
179
+ }
180
+ const atIndex = selector.lastIndexOf("@");
181
+ if (atIndex <= 0)
182
+ return null;
183
+ const name = selector.slice(0, atIndex);
184
+ if (!name)
185
+ return null;
186
+ return { name };
187
+ }
188
+ /** Every descriptor in one Yarn v1 entry, and the package the first one names. */
189
+ export function parseYarnClassicSelectorKey(selectorKey) {
190
+ const selectors = String(selectorKey).split(",").map(part => part.trim()).filter(Boolean);
191
+ const identity = selectors[0] ? parseYarnClassicDescriptor(selectors[0]) : null;
192
+ if (!identity)
193
+ return null;
194
+ return {
195
+ name: identity.name,
196
+ ...(identity.registryName ? { registryName: identity.registryName } : {}),
197
+ selectors,
198
+ };
199
+ }
200
+ function readYarnClassicEntry(selectorKey, meta) {
201
+ const version = meta?.version;
202
+ if (typeof version !== "string" || version.length === 0)
203
+ return null;
204
+ const identity = parseYarnClassicSelectorKey(selectorKey);
205
+ if (!identity)
206
+ return null;
207
+ return {
208
+ ...identity,
209
+ version,
210
+ instanceKey: packageInstanceKey({
211
+ name: identity.name,
212
+ registryName: identity.registryName,
213
+ version,
214
+ }),
215
+ };
216
+ }
147
217
  function collectYarnPaths(seeds, graph, map, taggedPathsByKey) {
148
218
  const queue = seeds.map(seed => ({ key: seed.key, path: seed.path }));
149
219
  const visitedStates = new Set();
@@ -153,7 +223,12 @@ function collectYarnPaths(seeds, graph, map, taggedPathsByKey) {
153
223
  const ref = parseYarnPackageKey(current.key);
154
224
  if (!ref)
155
225
  continue;
156
- const nextPath = [...current.path, ref.name];
226
+ // An aliased entry's canonical key is `alias\0registry@version`. The path
227
+ // shows the name that was installed, which is the part before the NUL.
228
+ const installName = ref.name.split("\0")[0];
229
+ if (!installName)
230
+ continue;
231
+ const nextPath = [...current.path, installName];
157
232
  const stateKey = `${ref.key}>${nextPath.join(">")}`;
158
233
  if (visitedStates.has(stateKey))
159
234
  continue;
@@ -302,32 +377,25 @@ function loadFromYarnClassicLock(parsed, filePath) {
302
377
  const graph = new Map();
303
378
  const selectorToKey = new Map();
304
379
  for (const [selectorKey, meta] of Object.entries(parsed.object)) {
305
- const version = meta?.version;
306
- if (!version)
380
+ const entry = readYarnClassicEntry(selectorKey, meta);
381
+ if (!entry)
307
382
  continue;
308
- const selectors = String(selectorKey).split(",").map(selector => selector.trim()).filter(Boolean);
309
- const firstSelector = selectors[0];
310
- const atIndex = firstSelector.lastIndexOf("@");
311
- if (atIndex <= 0)
312
- continue;
313
- const name = firstSelector.slice(0, atIndex);
314
- const canonicalKey = `${name}@${version}`;
315
- for (const selector of selectors) {
316
- selectorToKey.set(selector, canonicalKey);
383
+ for (const selector of entry.selectors) {
384
+ selectorToKey.set(selector, entry.instanceKey);
317
385
  }
318
- upsertPackage(map, { name, version, ecosystem: "npm", paths: [], resolvedUrl: meta?.resolved });
386
+ upsertPackage(map, {
387
+ name: entry.name,
388
+ version: entry.version,
389
+ ecosystem: "npm",
390
+ paths: [],
391
+ resolvedUrl: meta?.resolved,
392
+ ...(entry.registryName ? { registryName: entry.registryName } : {}),
393
+ });
319
394
  }
320
395
  for (const [selectorKey, meta] of Object.entries(parsed.object)) {
321
- const version = meta?.version;
322
- if (!version)
323
- continue;
324
- const selectors = String(selectorKey).split(",").map(selector => selector.trim()).filter(Boolean);
325
- const firstSelector = selectors[0];
326
- const atIndex = firstSelector.lastIndexOf("@");
327
- if (atIndex <= 0)
396
+ const entry = readYarnClassicEntry(selectorKey, meta);
397
+ if (!entry)
328
398
  continue;
329
- const name = firstSelector.slice(0, atIndex);
330
- const canonicalKey = `${name}@${version}`;
331
399
  const depKeys = new Set();
332
400
  const deps = meta?.dependencies;
333
401
  if (deps && typeof deps === "object") {
@@ -337,7 +405,7 @@ function loadFromYarnClassicLock(parsed, filePath) {
337
405
  depKeys.add(childKey);
338
406
  }
339
407
  }
340
- graph.set(canonicalKey, [...depKeys]);
408
+ graph.set(entry.instanceKey, [...depKeys]);
341
409
  }
342
410
  const taggedPathsByKey = new Map();
343
411
  const prodFallbackKeys = new Set();
@@ -5,6 +5,7 @@ import { getPrimaryParent } from "../utils/finding.js";
5
5
  import { isNpmLockSource, packageManagerForSource } from "../utils/package-manager-source.js";
6
6
  import { calculatePathCoverage, formatDependencyPath } from "../utils/path-coverage.js";
7
7
  import { pluralize } from "../utils/string.js";
8
+ import { quoteShellPath, shellQuote } from "../utils/shell-quote.js";
8
9
  import { buildNpmWorkspaceMap } from "../parsers/package-lock.js";
9
10
  import { buildPnpmWorkspaceMap } from "../parsers/pnpm-lock.js";
10
11
  import { buildYarnWorkspaceMap } from "../parsers/yarn-lock.js";
@@ -432,7 +433,7 @@ export function buildSuggestedFixCommandPlan(findings, scanInput, options) {
432
433
  return true;
433
434
  }).length;
434
435
  if (options?.subfolder) {
435
- const prefix = `cd ${options.subfolder} && `;
436
+ const prefix = `cd ${quoteShellPath(options.subfolder)} && `;
436
437
  if (plan.command)
437
438
  plan.command = prefix + plan.command;
438
439
  for (const section of plan.sections) {
@@ -473,12 +474,6 @@ function buildYarnUpgradeCommand(pkg, workspaces) {
473
474
  function buildBunUpdateCommand(pkg) {
474
475
  return `bun update ${shellQuote(pkg)}`;
475
476
  }
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, "'\\''") + "'";
481
- }
482
477
  export function commandPrefix(packageManager) {
483
478
  if (packageManager === "npm")
484
479
  return "npm install";
@@ -1,5 +1,6 @@
1
1
  import { compareVersions, isPreReleaseVersion, looksLikeVersion } from "../utils/version.js";
2
2
  import { registryForPackage } from "../utils/registry-config.js";
3
+ import { fetchRegistryMetadata, RegistryNetworkPolicyError } from "../utils/registry-fetch.js";
3
4
  import { AsyncLocalStorage } from "node:async_hooks";
4
5
  const packumentCache = new Map();
5
6
  const DEFAULT_NPM_REGISTRY_URL = "https://registry.npmjs.org";
@@ -24,6 +25,8 @@ function notifyPackumentObserver(result, packageName) {
24
25
  const observer = observerStorage.getStore();
25
26
  if (result.status === "transient-failure") {
26
27
  observer?.onTransientFailure?.(packageName);
28
+ if (result.policyBlocked)
29
+ observer?.onPolicyBlocked?.();
27
30
  }
28
31
  else {
29
32
  observer?.onResolved?.(packageName);
@@ -61,6 +64,7 @@ let registryConfig = null;
61
64
  */
62
65
  export function configureNpmRegistry(config) {
63
66
  registryConfig = config;
67
+ clearPackumentCache();
64
68
  }
65
69
  /**
66
70
  * Names the registry a lookup for this package actually went to, for use inside
@@ -99,7 +103,7 @@ async function doFetchPackument(packageName) {
99
103
  registryDebugLog("Registry fetch", { batchId, package: packageName, url });
100
104
  }
101
105
  try {
102
- const response = await fetch(url);
106
+ const response = await fetchRegistryMetadata(base, url, !!registryConfig?.allowPrivate);
103
107
  if (!response.ok) {
104
108
  if (registryDebugLog) {
105
109
  registryDebugLog("Registry response", {
@@ -138,7 +142,11 @@ async function doFetchPackument(packageName) {
138
142
  : String(error),
139
143
  });
140
144
  }
141
- return { status: "transient-failure", packument: null };
145
+ return {
146
+ status: "transient-failure",
147
+ packument: null,
148
+ policyBlocked: error instanceof RegistryNetworkPolicyError,
149
+ };
142
150
  }
143
151
  }
144
152
  export async function packageVersionExists(packageName, version) {
@@ -33,6 +33,10 @@ export async function runOverrideAudit(params) {
33
33
  })
34
34
  : null;
35
35
  try {
36
+ const registry = resolveRegistryConfig({
37
+ projectPath: params.projectPath,
38
+ packageManager: packageManagerForSource(params.scanSource),
39
+ });
36
40
  const result = await audit(ctx, {
37
41
  checkNetwork: params.checkNetwork,
38
42
  advisorySource: advisory?.advisorySource,
@@ -45,10 +49,8 @@ export async function runOverrideAudit(params) {
45
49
  // the scanner's module state, because the audit runs before the scan configures it.
46
50
  registry: {
47
51
  fetchImpl: params.fetchImpl,
48
- baseUrl: resolveRegistryConfig({
49
- projectPath: params.projectPath,
50
- packageManager: packageManagerForSource(params.scanSource),
51
- }).registry,
52
+ baseUrl: registry.registry,
53
+ allowPrivate: registry.allowPrivate,
52
54
  },
53
55
  });
54
56
  return { findings: result.findings, skipped: result.skippedDetectors };
package/dist/scanner.js CHANGED
@@ -525,12 +525,14 @@ export async function scanPackages(packages, batchSize, options, context, debugL
525
525
  }
526
526
  const lockfileWorkspaceMap = pnpmWorkspaceMap ?? bunWorkspaceMap ?? null;
527
527
  const packumentFailures = new Set();
528
+ let registryPolicyBlocked = false;
528
529
  const remediationFailures = new Map();
529
530
  let chainResolutionFailureCount = 0;
530
531
  let directCount = 0;
531
532
  let transitiveCount = 0;
532
533
  await runWithObserver({
533
534
  onTransientFailure(name) { packumentFailures.add(name); },
535
+ onPolicyBlocked() { registryPolicyBlocked = true; },
534
536
  }, async () => {
535
537
  const npmRemediationCache = new Map();
536
538
  const parentUpgradeCache = new Map();
@@ -723,6 +725,10 @@ export async function scanPackages(packages, batchSize, options, context, debugL
723
725
  }
724
726
  }
725
727
  });
728
+ if (registryPolicyBlocked && registryConfig.sourceFile) {
729
+ const source = path.basename(registryConfig.sourceFile);
730
+ console.warn(`Warning: The registry from ${source} was blocked because repository settings cannot use plain HTTP or private addresses. To use it, set NPM_CONFIG_REGISTRY for the scan, or on Windows remove the registry from the project's config and set it in your user-level config (~/.npmrc or ~/.yarnrc.yml).`);
731
+ }
726
732
  const severityCounts = countBySeverity(findings);
727
733
  log("Findings classified", {
728
734
  total: findings.length,
@@ -0,0 +1,44 @@
1
+ import dns from "node:dns";
2
+ import { isIP } from "node:net";
3
+ import { isPrivateOrReservedIp, stripIpv6Brackets } from "./validate-url.js";
4
+ export function createCheckedAddressPolicy(options) {
5
+ function checkAddresses(addresses) {
6
+ if (!addresses.length || addresses.some(({ address }) => !isIP(address))) {
7
+ throw new Error(options.invalidAddressMessage);
8
+ }
9
+ if (!options.allowPrivate && addresses.some(({ address }) => isPrivateOrReservedIp(address))) {
10
+ throw options.privateAddressError();
11
+ }
12
+ }
13
+ // Validate all DNS answers and return only those same addresses to the socket.
14
+ const lookup = (hostname, requestOptions, callback) => {
15
+ dns.lookup(hostname, { all: true }, (error, addresses) => {
16
+ if (error) {
17
+ callback(error, "", 0);
18
+ return;
19
+ }
20
+ try {
21
+ checkAddresses(addresses);
22
+ const family = requestOptions.family;
23
+ const candidates = family ? addresses.filter(address => address.family === family) : addresses;
24
+ if (!candidates.length)
25
+ throw new Error(options.missingFamilyMessage);
26
+ if (requestOptions.all)
27
+ callback(null, candidates);
28
+ else
29
+ callback(null, candidates[0].address, candidates[0].family);
30
+ }
31
+ catch (failure) {
32
+ callback(failure instanceof Error ? failure : new Error(String(failure)), "", 0);
33
+ }
34
+ });
35
+ };
36
+ function checkLiteral(hostname) {
37
+ // Node bypasses lookup callbacks for IP literals.
38
+ const address = stripIpv6Brackets(hostname);
39
+ const family = isIP(address);
40
+ if (family)
41
+ checkAddresses([{ address, family }]);
42
+ }
43
+ return { lookup, checkLiteral };
44
+ }
@@ -197,6 +197,7 @@ export function createCertAwareFetch(caCertPath, lookup) {
197
197
  headers,
198
198
  agent: getAgent(),
199
199
  lookup,
200
+ signal: init?.signal ?? undefined,
200
201
  };
201
202
  const req = https.request(reqOptions, res => {
202
203
  const chunks = [];
@@ -1,45 +1,7 @@
1
- import dns from "node:dns";
2
1
  import { FLAG_ALLOW_PRIVATE_OSV_URL, FLAG_OSV_URL } from "../cli/flags.js";
3
- import { isIP } from "node:net";
2
+ import { createCheckedAddressPolicy } from "./checked-address-policy.js";
4
3
  import { createCertAwareFetch } from "./network.js";
5
- import { isOfficialOsv, isPrivateOrReservedIp, stripIpv6Brackets } from "./validate-url.js";
6
- function rejectPrivateAddresses(addresses, allowPrivate) {
7
- if (addresses.length === 0 || addresses.some(address => !isIP(address.address))) {
8
- throw new Error(`${FLAG_OSV_URL} did not resolve to a valid IP address`);
9
- }
10
- if (!allowPrivate && addresses.some(address => isPrivateOrReservedIp(address.address))) {
11
- throw new Error(`${FLAG_OSV_URL} resolves to a private/reserved IP at connection time. ` +
12
- `Use ${FLAG_ALLOW_PRIVATE_OSV_URL} for internal mirrors.`);
13
- }
14
- }
15
- /** Return only the addresses that were checked, without another resolver call. */
16
- function createOsvLookup(allowPrivate) {
17
- return (hostname, options, callback) => {
18
- dns.lookup(hostname, { all: true }, (error, addresses) => {
19
- if (error) {
20
- callback(error, "", 0);
21
- return;
22
- }
23
- try {
24
- rejectPrivateAddresses(addresses, allowPrivate);
25
- const family = options.family;
26
- const candidates = family ? addresses.filter(address => address.family === family) : addresses;
27
- if (candidates.length === 0) {
28
- throw new Error(`${FLAG_OSV_URL} has no address for the requested IP family`);
29
- }
30
- if (options.all) {
31
- callback(null, candidates);
32
- }
33
- else {
34
- callback(null, candidates[0].address, candidates[0].family);
35
- }
36
- }
37
- catch (failure) {
38
- callback(failure, "", 0);
39
- }
40
- });
41
- };
42
- }
4
+ import { isOfficialOsv } from "./validate-url.js";
43
5
  /** Protect only the configured OSV origin; registry and other fetches keep their transport. */
44
6
  export function createOsvFetch(osvUrl, allowPrivate, caCertPath) {
45
7
  const fallback = caCertPath ? createCertAwareFetch(caCertPath) : fetch;
@@ -47,7 +9,14 @@ export function createOsvFetch(osvUrl, allowPrivate, caCertPath) {
47
9
  return fallback;
48
10
  }
49
11
  // Parse lazily: validateOptions owns invalid-URL diagnostics before any request.
50
- const protectedFetch = createCertAwareFetch(caCertPath, createOsvLookup(allowPrivate));
12
+ const addressPolicy = createCheckedAddressPolicy({
13
+ allowPrivate,
14
+ invalidAddressMessage: `${FLAG_OSV_URL} did not resolve to a valid IP address`,
15
+ privateAddressError: () => new Error(`${FLAG_OSV_URL} resolves to a private/reserved IP at connection time. ` +
16
+ `Use ${FLAG_ALLOW_PRIVATE_OSV_URL} for internal mirrors.`),
17
+ missingFamilyMessage: `${FLAG_OSV_URL} has no address for the requested IP family`,
18
+ });
19
+ const protectedFetch = createCertAwareFetch(caCertPath, addressPolicy.lookup);
51
20
  return async (input, init) => {
52
21
  const target = new URL(osvUrl);
53
22
  const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url);
@@ -57,14 +26,9 @@ export function createOsvFetch(osvUrl, allowPrivate, caCertPath) {
57
26
  if (url.protocol !== "https:") {
58
27
  throw new Error(`${FLAG_OSV_URL} requires https:// scheme`);
59
28
  }
60
- // Node bypasses lookup callbacks for literals, so enforce the same policy here.
61
- const hostname = stripIpv6Brackets(url.hostname);
62
- const family = isIP(hostname);
63
- if (family) {
64
- rejectPrivateAddresses([{ address: hostname, family }], allowPrivate);
65
- }
29
+ addressPolicy.checkLiteral(url.hostname);
66
30
  // The HTTPS transport never follows redirects. It retains the hostname for
67
31
  // Host, SNI and certificate validation, and validates DNS for each new socket.
68
- return protectedFetch(input, init);
32
+ return protectedFetch(url, init);
69
33
  };
70
34
  }
@@ -92,11 +92,17 @@ function readIfPresent(file) {
92
92
  export function resolveRegistryConfig(args) {
93
93
  const env = args.env ?? process.env;
94
94
  const homeDir = args.homeDir ?? os.homedir();
95
+ const config = args.packageManager ? MANAGER_CONFIG[args.packageManager] : undefined;
95
96
  const envRegistry = env.NPM_CONFIG_REGISTRY || env.npm_config_registry;
96
97
  if (envRegistry) {
97
- return { registry: envRegistry, scopes: {}, sourceFile: "NPM_CONFIG_REGISTRY" };
98
+ const explicit = Boolean(env.NPM_CONFIG_REGISTRY) && (args.platform ?? process.platform) !== "win32";
99
+ return {
100
+ registry: envRegistry,
101
+ scopes: {},
102
+ sourceFile: env.NPM_CONFIG_REGISTRY ? "NPM_CONFIG_REGISTRY" : "npm_config_registry",
103
+ allowPrivate: explicit || isUserRegistry(envRegistry, config, homeDir, args.projectPath),
104
+ };
98
105
  }
99
- const config = args.packageManager ? MANAGER_CONFIG[args.packageManager] : undefined;
100
106
  if (!config)
101
107
  return { registry: NPM_PUBLIC_REGISTRY, scopes: {}, sourceFile: null };
102
108
  for (const dir of [args.projectPath, homeDir]) {
@@ -113,10 +119,42 @@ export function resolveRegistryConfig(args) {
113
119
  registry: parsed.registry || NPM_PUBLIC_REGISTRY,
114
120
  scopes: parsed.scopes,
115
121
  sourceFile: file,
122
+ // A project scanned at home (or a home config linked into the project)
123
+ // must not promote repository-owned settings to user authorization.
124
+ allowPrivate: dir === homeDir && !isInsideProject(file, args.projectPath),
116
125
  };
117
126
  }
118
127
  return { registry: NPM_PUBLIC_REGISTRY, scopes: {}, sourceFile: null };
119
128
  }
129
+ /**
130
+ * npm and npx export every setting they read, including a project's own `.npmrc`, to
131
+ * child processes as lowercase `npm_config_*` variables. So a lowercase registry
132
+ * variable may be repository-owned, and only earns trust when it is the public
133
+ * registry or the one in the user's own config. Windows environment names ignore
134
+ * case, so there even the uppercase spelling cannot be told apart.
135
+ */
136
+ function isUserRegistry(registry, config, homeDir, projectPath) {
137
+ const target = normaliseRegistry(registry);
138
+ if (target === normaliseRegistry(NPM_PUBLIC_REGISTRY))
139
+ return true;
140
+ if (!config)
141
+ return false;
142
+ const file = path.join(homeDir, config.file);
143
+ const contents = readIfPresent(file);
144
+ if (contents === null || isInsideProject(file, projectPath))
145
+ return false;
146
+ const home = (config.format === "ini" ? fromIni(contents) : fromYaml(contents)).registry;
147
+ return Boolean(home) && normaliseRegistry(home) === target;
148
+ }
149
+ function normaliseRegistry(registry) {
150
+ try {
151
+ const url = new URL(registry);
152
+ return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
153
+ }
154
+ catch {
155
+ return registry;
156
+ }
157
+ }
120
158
  export { packageManagerForSource } from "./package-manager-source.js";
121
159
  /** The registry a single package resolves through, honouring its scope. */
122
160
  export function registryForPackage(config, packageName) {
@@ -127,3 +165,12 @@ export function registryForPackage(config, packageName) {
127
165
  }
128
166
  return config.registry;
129
167
  }
168
+ function isInsideProject(file, projectPath) {
169
+ try {
170
+ const relative = path.relative(fs.realpathSync(projectPath), fs.realpathSync(file));
171
+ return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative));
172
+ }
173
+ catch {
174
+ return true;
175
+ } // Uncertain ownership must not authorize private access.
176
+ }
@@ -0,0 +1,42 @@
1
+ import { NPM_PUBLIC_REGISTRY } from "../constants.js";
2
+ import { createCheckedAddressPolicy } from "./checked-address-policy.js";
3
+ import { createCertAwareFetch } from "./network.js";
4
+ export class RegistryNetworkPolicyError extends Error {
5
+ }
6
+ const publicRegistryPolicy = createCheckedAddressPolicy({
7
+ allowPrivate: false,
8
+ invalidAddressMessage: "Repository registry resolves to a private/reserved or invalid address",
9
+ privateAddressError: () => new RegistryNetworkPolicyError("Repository registry resolves to a private/reserved address"),
10
+ missingFamilyMessage: "Registry has no address for the requested IP family",
11
+ });
12
+ /** Repository settings select metadata sources, not permission to access internal services. */
13
+ export async function fetchRegistryMetadata(base, requestUrl, allowPrivate, init, fetchImpl) {
14
+ const registry = new URL(base);
15
+ if (registry.username || registry.password || registry.search || registry.hash || base.includes("?") || base.includes("#")) {
16
+ throw new Error("Registry URL must not contain credentials, a query or a fragment");
17
+ }
18
+ if (registry.protocol === "http:" && !allowPrivate) {
19
+ throw new RegistryNetworkPolicyError("Repository registry requires HTTPS");
20
+ }
21
+ if (registry.protocol !== "https:" && registry.protocol !== "http:") {
22
+ throw new Error("Registry URL must use HTTP or HTTPS");
23
+ }
24
+ const target = new URL(requestUrl);
25
+ if (target.origin !== registry.origin)
26
+ throw new Error("Registry request changed origin");
27
+ const ca = process.env.NODE_EXTRA_CA_CERTS;
28
+ const official = registry.origin === new URL(NPM_PUBLIC_REGISTRY).origin;
29
+ let transport;
30
+ if (allowPrivate || official) {
31
+ transport = fetchImpl ?? (ca && registry.protocol === "https:" ? createCertAwareFetch(ca) : fetch);
32
+ }
33
+ else {
34
+ publicRegistryPolicy.checkLiteral(target.hostname);
35
+ transport = createCertAwareFetch(ca, publicRegistryPolicy.lookup);
36
+ }
37
+ // Use the canonical URL so alternate HTTPS spellings still use the checked transport.
38
+ const response = await transport(target.href, { ...init, redirect: "error" });
39
+ if (response.status >= 300 && response.status < 400)
40
+ throw new Error("Registry redirects are not permitted");
41
+ return response;
42
+ }
@@ -0,0 +1,12 @@
1
+ import path from "node:path";
2
+ /** Quote a token for a command the user may paste into a POSIX shell. */
3
+ export function shellQuote(token) {
4
+ if (/^[A-Za-z0-9_@%+./:=,-]+$/.test(token))
5
+ return token;
6
+ return "'" + token.replace(/'/g, "'\\''") + "'";
7
+ }
8
+ export function quoteShellPath(directory) {
9
+ // Preserve literal backslashes in POSIX names and keep ordinary Windows paths unquoted.
10
+ const normalized = path.sep === "\\" ? directory.replace(/\\/g, "/") : directory;
11
+ return shellQuote(normalized.startsWith("-") ? `./${normalized}` : normalized);
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cve-lite-cli",
3
- "version": "1.41.0",
3
+ "version": "1.41.1",
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": {