TLS SNI extension cannot be issued from a server socket
Production Risk
Low — caught immediately at call site; affects only misconfigured TLS code.
Thrown when tlsSocket.setServername() is called on a TLS socket that is operating in server mode. Server Name Indication (SNI) is a client-side extension that tells the server which hostname to use. A server socket cannot set its own SNI because it is the recipient, not the sender, of the extension.
- 1Calling socket.setServername() on the server-side TLS socket in the connection handler
- 2Confusing client and server socket roles in TLS code
Triggered when setServername() is called on a server-side TLS socket.
const tls = require('tls');
const server = tls.createServer(options, (socket) => {
socket.setServername('example.com'); // server socket — throws
});expected output
Error [ERR_TLS_SNI_FROM_SERVER]: Cannot issue SNI from a TLS server-side socket
Fix
Call setServername() only on client-side TLS sockets
WHEN When connecting to a server that requires SNI
const socket = tls.connect({
host: 'example.com',
port: 443,
servername: 'example.com', // set in options, not post-connect
});Why this works
SNI is set during the connect() options; it is sent by the client in the ClientHello, not by the server.
const tls = require('tls');
const server = tls.createServer(options, (socket) => {
socket.setServername('example.com'); // server socket — throws
}); // this triggers ERR_TLS_SNI_FROM_SERVERtry {
// operation that may throw ERR_TLS_SNI_FROM_SERVER
riskyOperation()
} catch (err) {
if (err.code === 'ERR_TLS_SNI_FROM_SERVER') {
console.error('ERR_TLS_SNI_FROM_SERVER:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_tls_sni_from_server(...args) {
// validate args here
return performOperation(...args)
}✕ Call setServername() inside a server connection handler
Server sockets receive SNI from clients; they do not send it.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev