{"record":{"id":"0753f7f34996ccec","repo":"denoland/deno","slug":"err-tls-sni-from-server","errorCode":"ERR_TLS_SNI_FROM_SERVER","errorMessage":"Cannot issue SNI from a TLS server-side socket","messagePattern":"Cannot issue SNI from a TLS server-side socket","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_tls_wrap.js","lineNumber":1077,"sourceCode":"    tcpHandle.readStart();\n  }\n\n  // Kick-start the TLS readable side. During start(), the handshake cycle\n  // may have received and processed a close_notify (peer called end() before\n  // we set up event listeners). The decrypted EOF is buffered in pending_eof\n  // because inner.onread wasn't set yet. Call readStart() directly on the\n  // TLSWrap to install onread and flush any pending data/EOF.\n  if (this._handle) {\n    this._handle.readStart();\n  }\n};\n\nTLSSocket.prototype.setServername = function (name) {\n  if (typeof name !== \"string\") {\n    throw new ERR_INVALID_ARG_TYPE(\"name\", \"string\", name);\n  }\n  if (this._tlsOptions?.isServer) {\n    throw new ERR_TLS_SNI_FROM_SERVER();\n  }\n  this._handle?.setServername(name);\n};\n\n// Format prefixes for the synthetic session buffers we emit from\n// onConnectSecure when rustls handles resumption internally. The encoded\n// payload is `${servername ?? host ?? \"\"}:${port ?? \"\"}` -- see the\n// matching emit sites in onConnectSecure.\nconst SYNTHETIC_SESSION_PREFIXES = [\n  \"deno-tls12-session:\",\n  \"deno-tls13-session-ticket-1:\",\n  \"deno-tls13-session-ticket-2:\",\n  \"deno-tls13-dummy-session:\",\n];\n\nfunction syntheticSessionMatches(buf, options) {\n  if (!buf || !options) return false;\n  const sessionKey = `${options.servername ?? options.host ?? \"\"}:${","sourceCodeStart":1059,"sourceCodeEnd":1095,"githubUrl":"https://github.com/denoland/deno/blob/f7822238cab635a3a19f99f493f675fa81a7f9d8/ext/node/polyfills/_tls_wrap.js#L1059-L1095","documentation":"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.","triggerScenarios":"A tls.Server 'secureConnection'/'connection' handler calling conn.setServername(host); new tls.TLSSocket(rawSock, { isServer: true }) followed by setServername on that socket.","commonSituations":"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.","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"],"exampleFix":"// before\nserver.on('secureConnection', (sock) => {\n  sock.setServername('example.com'); // ERR_TLS_SNI_FROM_SERVER\n});\n\n// after\nconst server = tls.createServer({\n  SNICallback: (servername, cb) => cb(null, contexts[servername] ?? defaultCtx),\n}, handler);","handlingStrategy":"validation","validationCode":"// setServername is client-only; check the socket's role before calling.\nconst isClientSocket = (s: tls.TLSSocket) => !(s as any)._tlsOptions?.isServer;\nif (isClientSocket(sock)) {\n  sock.setServername(host);\n}","typeGuard":null,"tryCatchPattern":"try {\n  sock.setServername(host);\n} catch (err) {\n  if ((err as NodeJS.ErrnoException).code === 'ERR_TLS_SNI_FROM_SERVER') {\n    // server-side socket: nothing to do, SNI arrives from the client\n  } else throw err;\n}","preventionTips":["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"],"tags":["tls","sni","node-compat","server"],"backgroundTag":"tls-sni-misuse","analyzedSha":"f7822238cab635a3a19f99f493f675fa81a7f9d8","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}