Repository navigation
Description
What is the problem this feature would solve?
Socket.Reader.upgrade has no way to pass a server name. The Node adapter calls tls.connect({ ...tlsOptions, socket }) with neither host nor servername. I'm building an SMTP with opportunistic STARTTLS (but this also impacts PostgreSQL SSLRequest, IMAP/LDAP STARTTLS etc) and the problem is that effect/socket/Socket:
- sends no SNI (so the remote server with several certificates answers with its default one)
- verifies the certificate against Node's fallback name (
localhost) rather than the host it connected to. WithrejectUnauthorized: truethe handshake fails withERR_TLS_CERT_ALTNAME_INVALID: Host: localhost. is not in the cert's altnameseven when the certificate covers the real host.
TlsUpgradeOptions doesn't support providing a hostname:
effect/packages/effect/src/socket/Socket.ts
Lines 163 to 174 in 269a7c8
| export interface TlsUpgradeOptions { | |
| readonly key?: | |
| | Redacted.Redacted<string | Uint8Array> | |
| | ReadonlyArray<Redacted.Redacted<string | Uint8Array>> | |
| | undefined | |
| readonly cert?: string | Uint8Array | ReadonlyArray<string | Uint8Array> | undefined | |
| readonly ca?: string | Uint8Array | ReadonlyArray<string | Uint8Array> | undefined | |
| readonly passphrase?: Redacted.Redacted<string> | undefined | |
| readonly alpnProtocols?: ReadonlyArray<string> | undefined | |
| readonly requestCert?: boolean | undefined | |
| readonly rejectUnauthorized?: boolean | undefined | |
| } |
The Node adapter wraps the raw socket without a name:
effect/packages/platform/node-shared/src/NodeSocket.ts
Lines 348 to 358 in 269a7c8
| const tlsOptions = { | |
| secureContext, | |
| ALPNProtocols: upgradeOptions.alpnProtocols === undefined | |
| ? undefined | |
| : [...upgradeOptions.alpnProtocols], | |
| requestCert: upgradeOptions.requestCert, | |
| rejectUnauthorized: upgradeOptions.rejectUnauthorized | |
| } | |
| tls = isServer | |
| ? new Tls.TLSSocket(raw as Net.Socket, { ...tlsOptions, isServer: true }) | |
| : Tls.connect({ ...tlsOptions, socket: raw as Net.Socket }) |
Implicit TLS is unaffected, because NodeSocket.makeTls passes tls.ConnectionOptions (including servername) straight through:
effect/packages/platform/node-shared/src/NodeSocket.ts
Lines 555 to 571 in 269a7c8
| * **Details** | |
| * | |
| * Accepts the same options as `tls.connect`, so trust anchors (`ca`), client | |
| * certificates (`cert` / `key`), ALPN protocols, and `servername` are set | |
| * there. A failed handshake, including an untrusted or expired peer | |
| * certificate, fails with a `SocketOpenError`. Supports `openTimeout` and | |
| * destroys the underlying socket when the reader scope is finalized. | |
| * | |
| * @stability unstable | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| export const makeTls = ( | |
| options: Tls.ConnectionOptions & { | |
| readonly openTimeout?: Duration.Input | undefined | |
| } | |
| ): Effect.Effect<Socket.Socket> => |
What is the feature you are proposing to solve the problem?
Add one optional field on TlsUpgradeOptions, and forward it for client upgrades:
// packages/effect/src/socket/Socket.ts
export interface TlsUpgradeOptions {
// ...existing fields
/**
* Host name the peer certificate is verified against, also sent as SNI.
* Client upgrades only. When omitted, Node's `tls.connect` default applies.
*/
readonly servername?: string | undefined
}// packages/platform/node-shared/src/NodeSocket.ts, inside `upgrade`
const tlsOptions = {
secureContext,
ALPNProtocols: ...,
requestCert: upgradeOptions.requestCert,
rejectUnauthorized: upgradeOptions.rejectUnauthorized,
servername: upgradeOptions.servername
}
// ignored by `new Tls.TLSSocket(..., { isServer: true })`; used by `Tls.connect(...)`- Backwards compatible: optional and absent today.
- No validation needed: Node ignores IP literals for SNI but still uses the value for
checkServerIdentity(RFC 6066 §3). - A Bun adapter sharing
fromDuplexgets it for free; otherwise the same one-line forward. - Optionally
checkServerIdentitycould be added alongside for certificate pinning; not required for this request.
What alternatives have you considered?
rejectUnauthorized: falseon STARTTLS: disables verification entirely, unacceptable for a mail relay.- Owning the socket with
node:net+node:tlsdirectly, as@effect/sql-pgdoes forSSLRequest: works, but then I have to manage the entireSocketreader/writer/CloseEventlifecycle and duplicate the upgrade plumbing in every protocol client. - Spreading an undeclared
servernameinto the options object: compiles with a cast but is silently dropped by the adapter.
Activity
Socket: add servername to TlsUpgradeOptions