@@ -43,12 +43,14 @@ func setupTLSCommonName(config *tls.Config, pem []byte) error {
4343
4444 // Build intermediates pool from the peer certificates (excluding the first one which is the server cert)
4545 intermediates := x509 .NewCertPool ()
46- for i := 1 ; i < len (rawCerts ); i ++ {
47- intermediateCert , err := x509 .ParseCertificate (rawCerts [i ])
48- if err != nil {
49- return fmt .Errorf ("failed to parse intermediate certificate: %w" , err )
46+ if len (rawCerts ) > 1 {
47+ for i := 1 ; i < len (rawCerts ); i ++ {
48+ intermediateCert , err := x509 .ParseCertificate (rawCerts [i ])
49+ if err != nil {
50+ return fmt .Errorf ("failed to parse intermediate certificate: %w" , err )
51+ }
52+ intermediates .AddCert (intermediateCert )
5053 }
51- intermediates .AddCert (intermediateCert )
5254 }
5355
5456 // Verify the certificate chain against the provided root CA
@@ -64,9 +66,10 @@ func setupTLSCommonName(config *tls.Config, pem []byte) error {
6466
6567// setupTLSCertificateOnly validates the certificate chain without checking the hostname
6668func setupTLSCertificateOnly (config * tls.Config , pem []byte ) error {
67- // Skip hostname validation by setting ServerName to empty string
68- // The certificate chain will still be verified against RootCAs (set later in SetupTLS)
69- // This is the secure way to skip hostname validation without using InsecureSkipVerify
69+ // Skip hostname validation by setting ServerName to empty string.
70+ // When ServerName is empty, Go's TLS implementation will skip hostname verification
71+ // but still verify the certificate chain against the RootCAs (configured in SetupTLS after this function returns).
72+ // This is the secure way to skip hostname validation without using InsecureSkipVerify.
7073 config .ServerName = ""
7174 return nil
7275}
0 commit comments