Skip to content

Socket: add servername to TlsUpgradeOptions #9009

Closed

Description

@eugene1g

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. With rejectUnauthorized: true the handshake fails with ERR_TLS_CERT_ALTNAME_INVALID: Host: localhost. is not in the cert's altnames even when the certificate covers the real host.

TlsUpgradeOptions doesn't support providing a hostname:

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:

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:

* **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 fromDuplex gets it for free; otherwise the same one-line forward.
  • Optionally checkServerIdentity could be added alongside for certificate pinning; not required for this request.

What alternatives have you considered?

  • rejectUnauthorized: false on STARTTLS: disables verification entirely, unacceptable for a mail relay.
  • Owning the socket with node:net + node:tls directly, as @effect/sql-pg does for SSLRequest: works, but then I have to manage the entire Socket reader/writer/CloseEvent lifecycle and duplicate the upgrade plumbing in every protocol client.
  • Spreading an undeclared servername into the options object: compiles with a cast but is silently dropped by the adapter.

Activity

  1. added
    enhancement
    on Oct 11, 2026
  2. added a commit that references this issue on Oct 11, 2026
    6eb2f14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions