@vltpkg/registry-client 1.8.1 → 1.9.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.
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) vlt technology, Inc.
1
+ Copyright (c) vlt technology inc.
2
2
 
3
3
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
4
 
package/README.md CHANGED
@@ -88,6 +88,23 @@ A proxy that terminates TLS needs its certificate authority to be
88
88
  trusted. Point `NODE_EXTRA_CA_CERTS` at the CA file and node will
89
89
  trust it for the tunneled connection.
90
90
 
91
+ ## Certificate Authorities
92
+
93
+ Set `npm_config_cafile` (or `NPM_CONFIG_CAFILE`) to the path of a PEM
94
+ file to make registry connections trust only the certificates in that
95
+ file. This is the same variable npm uses for its `cafile` setting.
96
+
97
+ Node's bundled certificate authorities, the system store, and
98
+ `NODE_EXTRA_CA_CERTS` are all ignored for registry connections once
99
+ this is set. If a proxy needs its own CA, put it in the file too.
100
+ Other TLS connections in the process are not affected.
101
+
102
+ A file that cannot be read, or that holds no certificates, is a config
103
+ error.
104
+
105
+ To add a certificate authority and keep the defaults, use
106
+ `NODE_EXTRA_CA_CERTS` instead.
107
+
91
108
  ## Integrity Options
92
109
 
93
110
  An `integrity` option may be specified in a `fetchOptions.context`
package/dist/auth.d.ts CHANGED
@@ -26,6 +26,7 @@ export declare const keychains: Map<string, Keychain<Token>>;
26
26
  */
27
27
  export declare const runtimeTokens: Map<string, Token>;
28
28
  export declare const setRuntimeToken: (registry: string, token: Token) => void;
29
+ export declare const deleteRuntimeToken: (registry: string) => void;
29
30
  export declare const clearRuntimeTokens: () => void;
30
31
  export declare const getKC: (identity: string) => Keychain<Token>;
31
32
  export declare const isToken: (t: any) => t is Token;
package/dist/auth.js CHANGED
@@ -31,6 +31,9 @@ export const runtimeTokens = new Map();
31
31
  export const setRuntimeToken = (registry, token) => {
32
32
  runtimeTokens.set(normalizeRegistryKey(registry), token);
33
33
  };
34
+ export const deleteRuntimeToken = (registry) => {
35
+ runtimeTokens.delete(normalizeRegistryKey(registry));
36
+ };
34
37
  export const clearRuntimeTokens = () => {
35
38
  runtimeTokens.clear();
36
39
  };
@@ -0,0 +1,11 @@
1
+ import type { SecureContext } from 'node:tls';
2
+ /**
3
+ * A TLS context that trusts only the certificates in the PEM bundle
4
+ * named by `npm_config_cafile`, or `undefined` when it is not set.
5
+ *
6
+ * The registry dispatcher gives it to its connections in place of
7
+ * node's default certificate authorities, as npm does with `cafile`.
8
+ * Node's defaults, and every other TLS connection in the process, are
9
+ * left alone.
10
+ */
11
+ export declare const caSecureContext: () => SecureContext | undefined;
@@ -0,0 +1,73 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { X509Certificate } from 'node:crypto';
3
+ import { readFileSync } from 'node:fs';
4
+ import proc from 'node:process';
5
+ import { createSecureContext } from 'node:tls';
6
+ const pemCertificate = /-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g;
7
+ // The last file contents turned into a context. The dispatchers a client
8
+ // builds for each `connectTimeout` share it, so the bundle is parsed
9
+ // once. The file is still read every time, so a bundle replaced at the
10
+ // same path is picked up and a removed one is an error.
11
+ let cached;
12
+ /**
13
+ * The variable npm reads for its `cafile` setting, and its value. npm
14
+ * matches `npm_config_*` names in any case and exports the lowercase
15
+ * spelling to scripts, so that one is read first. An empty value counts
16
+ * as unset.
17
+ */
18
+ const caFileEnv = () => {
19
+ for (const name of ['npm_config_cafile', 'NPM_CONFIG_CAFILE']) {
20
+ const path = proc.env[name];
21
+ if (path)
22
+ return { name, path };
23
+ }
24
+ return undefined;
25
+ };
26
+ /**
27
+ * A TLS context that trusts only the certificates in the PEM bundle
28
+ * named by `npm_config_cafile`, or `undefined` when it is not set.
29
+ *
30
+ * The registry dispatcher gives it to its connections in place of
31
+ * node's default certificate authorities, as npm does with `cafile`.
32
+ * Node's defaults, and every other TLS connection in the process, are
33
+ * left alone.
34
+ */
35
+ export const caSecureContext = () => {
36
+ const env = caFileEnv();
37
+ if (!env)
38
+ return undefined;
39
+ const { name, path } = env;
40
+ let pem;
41
+ try {
42
+ pem = readFileSync(path, 'utf8');
43
+ }
44
+ catch (cause) {
45
+ throw error(`Could not read the CA file in ${name}`, {
46
+ code: 'ECONFIG',
47
+ found: path,
48
+ cause,
49
+ });
50
+ }
51
+ if (cached?.pem === pem)
52
+ return cached.context;
53
+ const certs = pem.match(pemCertificate);
54
+ if (!certs) {
55
+ throw error(`The CA file in ${name} contains no PEM certificates`, {
56
+ code: 'ECONFIG',
57
+ found: path,
58
+ });
59
+ }
60
+ // Node skips a certificate it cannot parse without saying so, which
61
+ // would leave a context that trusts less than the file says.
62
+ for (const cert of certs) {
63
+ try {
64
+ new X509Certificate(cert);
65
+ }
66
+ catch (cause) {
67
+ throw error(`The CA file in ${name} contains an invalid certificate`, { code: 'ECONFIG', found: path, cause });
68
+ }
69
+ }
70
+ const context = createSecureContext({ ca: certs });
71
+ cached = { pem, context };
72
+ return context;
73
+ };
package/dist/oidc.js CHANGED
@@ -1,5 +1,10 @@
1
1
  import { request } from 'undici';
2
- import { setRuntimeToken } from "./auth.js";
2
+ import { deleteRuntimeToken, setRuntimeToken } from "./auth.js";
3
+ import { getDispatcher } from "./proxy.js";
4
+ // one pool for both requests, with the proxy and CA file settings of
5
+ // every other registry request
6
+ let dispatcher;
7
+ const oidcDispatcher = () => (dispatcher ??= getDispatcher({}));
3
8
  const log = (...args) =>
4
9
  // eslint-disable-next-line no-console
5
10
  console.error('[vlt:oidc]', ...args);
@@ -44,6 +49,8 @@ export const oidc = async (opts) => {
44
49
  } /* c8 ignore stop */
45
50
  };
46
51
  const _oidc = async ({ packageName, registry, }) => {
52
+ // a token exchanged for another package must not be reused
53
+ deleteRuntimeToken(registry);
47
54
  const isGitHub = process.env.GITHUB_ACTIONS === 'true';
48
55
  const isGitLab = process.env.GITLAB_CI === 'true';
49
56
  const isCircle = process.env.CIRCLECI === 'true';
@@ -89,6 +96,7 @@ const fetchGitHubIdToken = async (registry) => {
89
96
  log(`Fetching GitHub ID token with audience ${audience}`);
90
97
  try {
91
98
  const res = await request(url, {
99
+ dispatcher: oidcDispatcher(),
92
100
  method: 'GET',
93
101
  headers: {
94
102
  accept: 'application/json',
@@ -123,6 +131,7 @@ const exchangeToken = async (idToken, packageName, registry) => {
123
131
  log(`Exchange URL: ${exchangeUrl.href}`);
124
132
  try {
125
133
  const res = await request(exchangeUrl, {
134
+ dispatcher: oidcDispatcher(),
126
135
  method: 'POST',
127
136
  headers: {
128
137
  'content-type': 'application/json',
package/dist/proxy.d.ts CHANGED
@@ -1,5 +1,13 @@
1
- import type { Dispatcher } from 'undici';
1
+ import type { buildConnector, Dispatcher } from 'undici';
2
2
  import { Agent } from 'undici';
3
+ /**
4
+ * Options for {@link getDispatcher}: those of an {@link Agent}, with
5
+ * `connect` as options for undici's connector rather than a connector of
6
+ * its own, so that the CA file can be added to them.
7
+ */
8
+ export type DispatcherOptions = Omit<Agent.Options, 'connect'> & {
9
+ connect?: buildConnector.BuildOptions;
10
+ };
3
11
  /**
4
12
  * Build the dispatcher that all registry traffic goes through.
5
13
  *
@@ -10,5 +18,10 @@ import { Agent } from 'undici';
10
18
  * `CONNECT`, still dials `no_proxy` origins directly, and receives the
11
19
  * identical options, so timeouts, keepalive, and pooling are the same
12
20
  * either way.
21
+ *
22
+ * If `npm_config_cafile` is set, connections trust only the
23
+ * certificates in that file. The proxy agent reads `connect` for
24
+ * origins it dials directly, `requestTls` for origins it tunnels to, and
25
+ * `proxyTls` for an https proxy, so the context goes on all three.
13
26
  */
14
- export declare const getDispatcher: (options: Agent.Options) => Dispatcher;
27
+ export declare const getDispatcher: (options: DispatcherOptions) => Dispatcher;
package/dist/proxy.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import proc from 'node:process';
2
2
  import { Agent, EnvHttpProxyAgent } from 'undici';
3
+ import { caSecureContext } from "./ca-file.js";
3
4
  // Matches how undici's EnvHttpProxyAgent reads these: lowercase wins
4
5
  // over uppercase, and an empty string counts as unset.
5
6
  const hasProxyEnv = () => !!(proc.env.http_proxy ?? proc.env.HTTP_PROXY) ||
@@ -14,5 +15,21 @@ const hasProxyEnv = () => !!(proc.env.http_proxy ?? proc.env.HTTP_PROXY) ||
14
15
  * `CONNECT`, still dials `no_proxy` origins directly, and receives the
15
16
  * identical options, so timeouts, keepalive, and pooling are the same
16
17
  * either way.
18
+ *
19
+ * If `npm_config_cafile` is set, connections trust only the
20
+ * certificates in that file. The proxy agent reads `connect` for
21
+ * origins it dials directly, `requestTls` for origins it tunnels to, and
22
+ * `proxyTls` for an https proxy, so the context goes on all three.
17
23
  */
18
- export const getDispatcher = (options) => hasProxyEnv() ? new EnvHttpProxyAgent(options) : new Agent(options);
24
+ export const getDispatcher = (options) => {
25
+ const secureContext = caSecureContext();
26
+ const opts = secureContext ?
27
+ {
28
+ ...options,
29
+ connect: { ...options.connect, secureContext },
30
+ requestTls: { secureContext },
31
+ proxyTls: { secureContext },
32
+ }
33
+ : options;
34
+ return hasProxyEnv() ? new EnvHttpProxyAgent(opts) : new Agent(opts);
35
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/registry-client",
3
3
  "description": "Fetch package artifacts and metadata from registries",
4
- "version": "1.8.1",
4
+ "version": "1.9.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,15 +13,15 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/cache": "1.8.1",
17
- "@vltpkg/cache-unzip": "1.8.1",
18
- "@vltpkg/error-cause": "1.8.1",
19
- "@vltpkg/keychain": "1.8.1",
20
- "@vltpkg/output": "1.8.1",
21
- "@vltpkg/types": "1.8.1",
22
- "@vltpkg/url-open": "1.8.1",
23
- "@vltpkg/user-agent": "1.8.1",
24
- "@vltpkg/xdg": "1.8.1",
16
+ "@vltpkg/cache": "1.9.0",
17
+ "@vltpkg/cache-unzip": "1.9.0",
18
+ "@vltpkg/error-cause": "1.9.0",
19
+ "@vltpkg/keychain": "1.9.0",
20
+ "@vltpkg/output": "1.9.0",
21
+ "@vltpkg/types": "1.9.0",
22
+ "@vltpkg/url-open": "1.9.0",
23
+ "@vltpkg/user-agent": "1.9.0",
24
+ "@vltpkg/xdg": "1.9.0",
25
25
  "cache-control-parser": "^2.0.6",
26
26
  "undici": "^7.16.0"
27
27
  },