denoland/deno · error · Error
ERR_TLS_SNI_FROM_SERVER
ERR_TLS_SNI_FROM_SERVER
Error message
Cannot issue SNI from a TLS server-side socket
What it means
setServername() sends the TLS SNI extension, which per RFC 6066 is client-to-server only: the client names the host it wants, the server cannot send one. Calling setServername() on a server-side TLSSocket (accepted from a tls.Server or created with isServer: true) throws ERR_TLS_SNI_FROM_SERVER immediately.
Solutions
- Remove setServername calls from server-side handlers — the server receives SNI from clients
- Serve multiple hostnames on the server via SNICallback or server.addContext(servername, ctx)
- Branch on socket role if code is shared: only call setServername on client sockets created with isServer falsy
Example fix
// before
server.on('secureConnection', (sock) => {
sock.setServername('example.com'); // ERR_TLS_SNI_FROM_SERVER
});
// after
const server = tls.createServer({
SNICallback: (servername, cb) => cb(null, contexts[servername] ?? defaultCtx),
}, handler); Defensive patterns
Strategy: validation
Validate before calling
// setServername is client-only; check the socket's role before calling.
const isClientSocket = (s: tls.TLSSocket) => !(s as any)._tlsOptions?.isServer;
if (isClientSocket(sock)) {
sock.setServername(host);
} Try / catch
try {
sock.setServername(host);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ERR_TLS_SNI_FROM_SERVER') {
// server-side socket: nothing to do, SNI arrives from the client
} else throw err;
} Prevention
- Keep client and server socket-handling code in separate functions
- Use SNICallback/addContext on servers to serve multiple names
- Never reuse client handshake snippets inside 'secureConnection' handlers
When it happens
Trigger: A tls.Server 'secureConnection'/'connection' handler calling conn.setServername(host); new tls.TLSSocket(rawSock, { isServer: true }) followed by setServername on that socket.
Common situations: Copy-pasting client handshake code into a server connection handler; generic socket middleware that sets SNI on every connection regardless of role; proxy code shared between upstream and downstream paths.
Understand the failure class
- SSL/TLS and certificate errors — how TLS handshakes and certificate validation fail.
Related errors
- ERR_TLS_REQUIRED_SERVER_NAME
- ERR_INVALID_ARG_TYPE
- ERR_INVALID_ARG_VALUE
- ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS
- Session ticket keys must be a 48-byte buffer
AI-assisted analysis of denoland/deno@f7822238ca (2026-08-20).
Data as JSON: /api/errors/0753f7f34996ccec.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/_tls_wrap.js:1077
tcpHandle.readStart();
}
// Kick-start the TLS readable side. During start(), the handshake cycle
// may have received and processed a close_notify (peer called end() before
// we set up event listeners). The decrypted EOF is buffered in pending_eof
// because inner.onread wasn't set yet. Call readStart() directly on the
// TLSWrap to install onread and flush any pending data/EOF.
if (this._handle) {
this._handle.readStart();
}
};
TLSSocket.prototype.setServername = function (name) {
if (typeof name !== "string") {
throw new ERR_INVALID_ARG_TYPE("name", "string", name);
}
if (this._tlsOptions?.isServer) {
throw new ERR_TLS_SNI_FROM_SERVER();
}
this._handle?.setServername(name);
};
// Format prefixes for the synthetic session buffers we emit from
// onConnectSecure when rustls handles resumption internally. The encoded
// payload is `${servername ?? host ?? ""}:${port ?? ""}` -- see the
// matching emit sites in onConnectSecure.
const SYNTHETIC_SESSION_PREFIXES = [
"deno-tls12-session:",
"deno-tls13-session-ticket-1:",
"deno-tls13-session-ticket-2:",
"deno-tls13-dummy-session:",
];
function syntheticSessionMatches(buf, options) {
if (!buf || !options) return false;
const sessionKey = `${options.servername ?? options.host ?? ""}:${View on GitHub (pinned to f7822238ca)