cve-lite-cli 1.41.0 → 1.41.2

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.
@@ -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
  }
@@ -58,6 +58,7 @@ function addDependencyNamesFromManifest(target, manifest, prodOnly) {
58
58
  const sections = [
59
59
  isRecord(manifest.dependencies) ? manifest.dependencies : undefined,
60
60
  isRecord(manifest.optionalDependencies) ? manifest.optionalDependencies : undefined,
61
+ isRecord(manifest.peerDependencies) ? manifest.peerDependencies : undefined,
61
62
  ];
62
63
  if (!prodOnly) {
63
64
  sections.push(isRecord(manifest.devDependencies) ? manifest.devDependencies : undefined);
@@ -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.2",
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": {