@bastani/atomic-storage-postgres 0.10.2-alpha.1 → 0.10.3-alpha.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.10.3-alpha.1] - 2026-11-21
6
+
7
+ ### Fixed
8
+
9
+ - A certificate that Node.js refuses as `UNSPECIFIED`, its code for a reason outside its own table, such as a root CA whose key usage leaves out `keyCertSign`, now fails the open at once with the reason (`UNSPECIFIED: key usage does not include certificate signing`), as in the standalone binary, instead of being retried as an unreachable server. OpenSSL's own failures reported as `UNSPECIFIED`, and errors that only share the code, are still retried. A refused certificate now fails at once on every connection the adapter opens, including the reconnect after a refused cloud token and the connection that creates the schema. When an `https://` proxy's own certificate is refused, the error, cause chain included, no longer carries the database server's wording and `sslrootcert` fix: it names the proxy and says to add the CA that issued its certificate to `NODE_EXTRA_CA_CERTS` ([#6112](https://github.com/bastani-inc/atomic/issues/6112))
10
+
11
+ ## [0.10.2] - 2026-11-20
12
+
13
+ ### Fixed
14
+
15
+ - A server certificate that TLS verification refuses with `INVALID_PURPOSE` (its CA is not marked as a CA, or is limited to client authentication) or `PATH_LENGTH_EXCEEDED` (its chain has more intermediate CAs than its root allows) now fails the open at once, naming the reason and the `sslrootcert` fix, instead of being retried five times as an unreachable server. Several other certificate verification failures that were retried, such as `CERT_REVOKED`, now also fail at once. This applies directly, through a proxy, and to an https proxy's own certificate ([#6074](https://github.com/bastani-inc/atomic/issues/6074)).
16
+
5
17
  ## [0.10.2-alpha.1] - 2026-11-20
6
18
 
7
19
  ### Fixed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bastani/atomic-storage-postgres",
3
- "version": "0.10.2-alpha.1",
3
+ "version": "0.10.3-alpha.1",
4
4
  "description": "PostgreSQL storage adapter for Atomic sessions and Intercom.",
5
5
  "contributors": [
6
6
  "Norin Lavaee",
package/postgres.js CHANGED
@@ -14,12 +14,12 @@ import { createCloudPasswordProvider } from "./auth.js";
14
14
  import { createPostgresListener } from "./listen.js";
15
15
  import {
16
16
  isLocalTarget,
17
+ isRefusedCertificate,
17
18
  OpenedSocket,
18
19
  ProxySettingError,
19
20
  ProxyTunnelError,
20
21
  postgresTransport,
21
22
  socketOpener,
22
- TLS_VERIFICATION_CODES,
23
23
  } from "./proxy.js";
24
24
 
25
25
  /**
@@ -69,7 +69,8 @@ const MINIMUM_SERVER_VERSION = 140000;
69
69
 
70
70
  /**
71
71
  * The permanent failure a connection refused by TLS verification is (#5218), with the reason and the fix: a host name
72
- * the certificate names, or the CA in `sslrootcert`. Undefined for any other failure.
72
+ * the certificate names, or the CA in `sslrootcert`. Undefined for any other failure, and for an https proxy's own
73
+ * certificate, which its tunnel failure names with the proxy's fix (#6112).
73
74
  * @param {unknown} cause
74
75
  * @returns {StorageError | undefined}
75
76
  */
@@ -77,8 +78,9 @@ function tlsVerificationFailure(cause) {
77
78
  /** @type {unknown} */
78
79
  let current = cause;
79
80
  for (let depth = 0; depth < MAX_CAUSE_DEPTH && typeof current === "object" && current !== null; depth++) {
80
- const code = /** @type {{ code?: unknown }} */ (current).code;
81
- if (typeof code === "string" && TLS_VERIFICATION_CODES.has(code)) {
81
+ if (current instanceof ProxyTunnelError) return undefined;
82
+ const { code, message } = /** @type {{ code?: unknown, message?: unknown }} */ (current);
83
+ if (typeof code === "string" && isRefusedCertificate(code, typeof message === "string" ? message.trim() : "")) {
82
84
  const fix =
83
85
  code === "ERR_TLS_CERT_ALTNAME_INVALID" || code === "HOSTNAME_MISMATCH"
84
86
  ? "Connect by a host name the certificate names, or, for an address it does not name such as a private IP address, add sslmode=verify-ca with sslrootcert=<CA file> to the URL"
@@ -93,6 +95,22 @@ function tlsVerificationFailure(cause) {
93
95
  return undefined;
94
96
  }
95
97
 
98
+ /**
99
+ * What a connection attempt that failed with `cause` throws, wherever the adapter opens one (the pool, the reconnect
100
+ * after a refused token, the schema's own connection): a certificate TLS verification refused, or a tunnel a proxy
101
+ * refused, fails at once; anything else is a connection that could not be made for now (#6112).
102
+ * @param {unknown} cause
103
+ * @returns {Error}
104
+ */
105
+ function connectFailure(cause) {
106
+ const tls = tlsVerificationFailure(cause);
107
+ if (tls !== undefined) return tls;
108
+ // A proxy that refused the tunnel refuses it again: permanent, never retried as unreachable (#5473).
109
+ const tunnel = proxyTunnelFailure(cause);
110
+ if (tunnel?.permanent === true) return new StorageError(tunnel.message, { cause: /** @type {Error} */ (cause) });
111
+ return new PostgresConnectionFailure("connect", /** @type {StorageFailureCause} */ (cause));
112
+ }
113
+
96
114
  /**
97
115
  * The tunnel a proxy did not open along `cause`'s chain (#5473), or undefined when the failure is not a proxy's.
98
116
  * @param {unknown} cause
@@ -838,15 +856,10 @@ export async function openPostgresDatabase(options) {
838
856
  try {
839
857
  return await pool.connect();
840
858
  } catch (retried) {
841
- throw new PostgresConnectionFailure("connect", /** @type {StorageFailureCause} */ (retried));
859
+ throw connectFailure(retried);
842
860
  }
843
861
  }
844
- const tls = tlsVerificationFailure(cause);
845
- if (tls !== undefined) throw tls;
846
- // A proxy that refused the tunnel refuses it again: permanent, never retried as unreachable (#5473).
847
- const tunnel = proxyTunnelFailure(cause);
848
- if (tunnel?.permanent === true) throw new StorageError(tunnel.message, { cause: /** @type {Error} */ (cause) });
849
- throw new PostgresConnectionFailure("connect", /** @type {StorageFailureCause} */ (cause));
862
+ throw connectFailure(cause);
850
863
  }
851
864
  };
852
865
 
@@ -1006,9 +1019,7 @@ export async function openPostgresDatabase(options) {
1006
1019
  client.connect((/** @type {Error | undefined} */ error) => (error ? reject(error) : resolve(undefined))),
1007
1020
  );
1008
1021
  } catch (cause) {
1009
- const tunnel = proxyTunnelFailure(cause);
1010
- if (tunnel?.permanent === true) throw new StorageError(tunnel.message, { cause: /** @type {Error} */ (cause) });
1011
- throw new PostgresConnectionFailure("connect", /** @type {StorageFailureCause} */ (cause));
1022
+ throw connectFailure(cause);
1012
1023
  }
1013
1024
  try {
1014
1025
  await watched(
package/proxy.d.ts CHANGED
@@ -21,6 +21,18 @@ export const PROXY_ANSWER_TIMEOUT_MS: 8000;
21
21
  */
22
22
  export const TLS_VERIFICATION_CODES: ReadonlySet<string>;
23
23
 
24
+ /**
25
+ * The reasons, lowercase, OpenSSL and BoringSSL give for a refused certificate that Node and Bun report as
26
+ * `UNSPECIFIED`, a verification result outside their table.
27
+ */
28
+ export const UNSPECIFIED_CERTIFICATE_REASONS: readonly string[];
29
+
30
+ /**
31
+ * Whether TLS verification refused a certificate with `code`, whose message is `reason`: a code in
32
+ * `TLS_VERIFICATION_CODES`, or `UNSPECIFIED` with a reason in `UNSPECIFIED_CERTIFICATE_REASONS`.
33
+ */
34
+ export function isRefusedCertificate(code: string, reason: string): boolean;
35
+
24
36
  /**
25
37
  * Whether a `NO_PROXY` value exempts `host` on `port`, as Atomic's HTTPS clients decide it: entries separated by commas
26
38
  * or white space; a host matches itself and its subdomains, `*.name` only subdomains, `*` every host; an entry with a
package/proxy.js CHANGED
@@ -57,6 +57,94 @@ export const TLS_VERIFICATION_CODES = new Set([
57
57
  "HOSTNAME_MISMATCH",
58
58
  ]);
59
59
 
60
+ /**
61
+ * Node and Bun report a verification result outside their table as `UNSPECIFIED`, with OpenSSL's reason for it, such as
62
+ * "key usage does not include certificate signing" for a root that may not sign certificates (#6112). These are those
63
+ * reasons that describe the certificate, lowercase, from OpenSSL 3 and BoringSSL. `UNSPECIFIED` with any other message,
64
+ * such as OpenSSL's own failures or an error that did not come from TLS at all, is not a refused certificate. Atomic's
65
+ * model requests hold the same list (`packages/ai/src/utils/tls-certificate.ts`).
66
+ * @type {readonly string[]}
67
+ */
68
+ export const UNSPECIFIED_CERTIFICATE_REASONS = [
69
+ "authority and issuer serial number mismatch",
70
+ "authority and subject key identifier mismatch",
71
+ "authority key identifier marked critical",
72
+ "basic constraints of ca cert not marked critical",
73
+ "ca cert does not include key usage extension",
74
+ "ca certificate key too weak",
75
+ "ca signature digest algorithm too weak",
76
+ "cannot find certificate signature algorithm",
77
+ "cert info signature and signature algorithm mismatch",
78
+ "certificate public key has explicit ecc parameters",
79
+ "certificate transparency required, but no valid scts found",
80
+ "crl path validation error",
81
+ "different crl scope",
82
+ "ee certificate key too weak",
83
+ "email address mismatch",
84
+ "empty subject alternative name extension",
85
+ "excluded subtree violation",
86
+ "invalid non-ca certificate (has ca markings)",
87
+ "invalid or inconsistent certificate extension",
88
+ "invalid or inconsistent certificate policy extension",
89
+ "ip address mismatch",
90
+ "issuer certificate doesn't have a public key",
91
+ "issuer has name constraints but leaf has no sans",
92
+ "issuer name empty",
93
+ "key usage does not include certificate signing",
94
+ "key usage does not include crl signing",
95
+ "key usage does not include digital signature",
96
+ "key usage keycertsign invalid for non-ca cert",
97
+ "missing authority key identifier",
98
+ "missing subject key identifier",
99
+ "name constraints minimum and maximum not supported",
100
+ "no explicit policy",
101
+ "no matching dane tlsa records",
102
+ "ocsp unknown cert",
103
+ "ocsp verification failed",
104
+ "ocsp verification needed",
105
+ "path length given without key usage keycertsign",
106
+ "path length invalid for non-ca cert",
107
+ "path loop",
108
+ "permitted subtree violation",
109
+ "proxy certificates not allowed, please set the appropriate flag",
110
+ "proxy path length constraint exceeded",
111
+ "proxy subject name violation",
112
+ "raw public key untrusted, no trusted keys configured",
113
+ "rfc 3779 resource not subset of parent's resources",
114
+ "subject empty and subject alt name extension not critical",
115
+ "subject issuer mismatch",
116
+ "subject key identifier marked critical",
117
+ "subject name empty",
118
+ "subject signature algorithm and issuer public key algorithm mismatch",
119
+ "suite b: cannot sign p-384 with p-256",
120
+ "suite b: certificate version invalid",
121
+ "suite b: curve not allowed for this los",
122
+ "suite b: invalid ecc curve",
123
+ "suite b: invalid public key algorithm",
124
+ "suite b: invalid signature algorithm",
125
+ "unable to get crl issuer certificate",
126
+ "unhandled critical crl extension",
127
+ "unhandled critical extension",
128
+ "unsupported extension feature",
129
+ "unsupported name constraint type",
130
+ "unsupported or invalid name constraint syntax",
131
+ "unsupported or invalid name syntax",
132
+ "using cert extension requires at least x509v3",
133
+ ];
134
+
135
+ /**
136
+ * Whether TLS verification refused a certificate with `code`, whose message is `reason`: a code in
137
+ * `TLS_VERIFICATION_CODES`, or `UNSPECIFIED` with a reason in `UNSPECIFIED_CERTIFICATE_REASONS`.
138
+ * @param {string} code
139
+ * @param {string} reason
140
+ */
141
+ export function isRefusedCertificate(code, reason) {
142
+ if (TLS_VERIFICATION_CODES.has(code)) return true;
143
+ if (code !== "UNSPECIFIED") return false;
144
+ const text = reason.toLowerCase();
145
+ return UNSPECIFIED_CERTIFICATE_REASONS.some((known) => text.startsWith(known));
146
+ }
147
+
60
148
  /**
61
149
  * @typedef {{ readonly host: string, readonly port: number }} ProxyTarget
62
150
  * @typedef {{ readonly [name: string]: string | undefined }} ProxyEnvironment
@@ -299,10 +387,15 @@ function reasonText(/** @type {string} */ text) {
299
387
  return text.replace(/[^\x20-\x7e]/g, "").trim().slice(0, MAX_REASON_LENGTH);
300
388
  }
301
389
 
302
- /** The code of a TLS verification failure along `cause`, or undefined. */
390
+ /**
391
+ * How a failure names the certificate TLS verification refused with `cause`, or undefined: its code, and for
392
+ * `UNSPECIFIED`, which says nothing on its own, the reason too.
393
+ */
303
394
  function tlsVerificationCode(/** @type {unknown} */ cause) {
304
- const code = /** @type {{ code?: unknown }} */ (cause ?? {}).code;
305
- return typeof code === "string" && TLS_VERIFICATION_CODES.has(code) ? code : undefined;
395
+ const { code, message } = /** @type {{ code?: unknown, message?: unknown }} */ (cause ?? {});
396
+ const reason = typeof message === "string" ? message.trim() : "";
397
+ if (typeof code !== "string" || !isRefusedCertificate(code, reason)) return undefined;
398
+ return code === "UNSPECIFIED" && reason !== "" ? `${code}: ${reason}` : code;
306
399
  }
307
400
 
308
401
  /**
@@ -373,10 +466,11 @@ export function openProxyTunnel(proxy, target, options = {}) {
373
466
  });
374
467
  socket.on("error", (/** @type {Error} */ cause) => {
375
468
  const tls = tlsVerificationCode(cause);
469
+ // The proxy's certificate is checked against the CAs Node.js trusts, never the server's `sslrootcert` (#6112).
376
470
  if (tls !== undefined)
377
471
  return fail(
378
472
  "proxy-unreachable",
379
- `the proxy ${proxy.label} showed a TLS certificate that was not accepted (${tls}), so no tunnel to ${authority} was opened`,
473
+ `the proxy ${proxy.label} showed a TLS certificate that was not accepted (${tls}), so no tunnel to ${authority} was opened. The proxy's certificate is checked against the CAs Node.js trusts: if a private or corporate CA issued it, add that CA to NODE_EXTRA_CA_CERTS`,
380
474
  true,
381
475
  { cause },
382
476
  );