Articles / Go

  • go
  • crypto-tls
  • tls
  • servername
  • x509
  • debugging

Why a Go TLS Client Connects Despite the Wrong ServerName

You set ServerName to something obviously wrong, ran the client, and it connected without a murmur. Either TLS verification is broken, or ServerName does not mean what you assumed. It is nearly always the second, and occasionally the first.

ServerName is the name Go verifies the certificate against, not the place it connects to. The address you dial and the name you verify are two separate inputs.

What ServerName actually does

In tls.Config, ServerName does two jobs:

  • It is sent in the ClientHello as the SNI extension (unless it is an IP address), so the server can pick a certificate.
  • It is the hostname the client checks against the certificate's subject alternative names.

It plays no part in where the TCP connection goes. That comes from the address you pass to the dialler. So this is perfectly legal:

package main

import (
	"crypto/tls"
	"fmt"
	"log"
)

func main() {
	// Dial one address, but verify as another name.
	conn, err := tls.Dial("tcp", "203.0.113.10:443", &tls.Config{
		ServerName: "example.com",
	})
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	fmt.Println("verified as example.com:", conn.ConnectionState().PeerCertificates[0].Subject)
}

If whatever answers on that IP presents a valid certificate for example.com, the handshake succeeds. That is how you test a new origin before DNS moves, and it is the same trick as curl --resolve. Nothing is wrong; you just told Go which name to expect.

The "wrong" name that is not wrong

Often the name you consider wrong is covered by the certificate anyway. A wildcard for *.example.com matches anything.example.com. A certificate with many SANs (common on shared hosting and CDNs) matches all of them.

So ServerName: "staging.example.com" against the production server succeeds if the production certificate lists it. The check is "does this certificate cover this name", not "is this the server I meant".

If you rely on ServerName to pin a client to one service, a certificate shared with other services will not stop a different backend answering, as long as it has a valid certificate for that name.

Three settings that make the name irrelevant

If you set a name that the certificate genuinely does not cover and still connect, one of these is almost certainly the cause.

  1. InsecureSkipVerify: true. This turns off chain and hostname verification entirely. The name is sent as SNI and then ignored.
  2. InsecureSkipVerify plus a VerifyPeerCertificate callback that only checks the chain. People do this to "do it properly", then forget that the callback is now the only check and has to verify the hostname itself.
  3. A VerifyConnection callback that returns nil early, or that only looks at the first certificate.

Grep your codebase for InsecureSkipVerify first. It turns up in test helpers that quietly became production code more often than anyone admits.

Quick detour: what if ServerName is empty?

With tls.Dial, an empty ServerName is filled in from the host part of the address, which is why most code never sets it. Through http.Transport, the host of the request URL is used the same way.

If you use tls.Client on a connection you dialled yourself, nothing is filled in. With verification on, the handshake then fails with an error saying either ServerName or InsecureSkipVerify must be specified. Go refuses to guess. That refusal is why a lot of people reach for InsecureSkipVerify in the first place, and it is the wrong fix: set the name instead.

Setting one name for a whole http.Client

A fixed ServerName in Transport.TLSClientConfig applies to every host that transport talks to. Redirect to another host and Go will still verify against your fixed name. That is occasionally what you want and often a surprise.

For the "connect here, verify as there" case, leave ServerName alone and override the dial instead:

package main

import (
	"context"
	"log"
	"net"
	"net/http"
)

func main() {
	d := &net.Dialer{}
	client := &http.Client{
		Transport: &http.Transport{
			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
				// The URL says example.com; the bytes go to the test origin.
				return d.DialContext(ctx, network, "203.0.113.10:443")
			},
		},
	}
	resp, err := client.Get("https://example.com/")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	log.Println(resp.Status)
}

Here SNI, the Host header and certificate verification all use example.com from the URL, and only the dial is redirected. Everything stays consistent, so no ServerName override is needed.

Checking what you actually verified

Do not trust the absence of an error as proof. Ask the connection:

st := conn.ConnectionState()
cert := st.PeerCertificates[0]
fmt.Println("SANs:", cert.DNSNames)
fmt.Println("verified chains:", len(st.VerifiedChains))
fmt.Println("verify name:", cert.VerifyHostname("example.com"))

VerifiedChains is empty when verification was skipped, which is a quick tell for InsecureSkipVerify. VerifyHostname returns nil only if the leaf covers the name. Printing the SANs often settles the argument by itself: the "wrong" name is simply in the list.

One caveat: VerifyHostname on the leaf alone tells you about the name, not about trust. Trust comes from the chain, so check both.