Skip to content

Commit 415045c

Browse files
Copilotshueybubbles
andcommitted
Skip hostname validation for all encryption modes when certificate is provided
Co-authored-by: shueybubbles <[email protected]>
1 parent fac055e commit 415045c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

msdsn/conn_str.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ func parseTLS(params map[string]string, host string) (Encryption, *tls.Config, e
280280
skipHostnameValidation := false
281281
if encrypt == "strict" {
282282
trustServerCert = false
283-
// When a certificate is provided with strict encryption, skip hostname validation
284-
// The certificate itself will still be validated against the provided CA
285-
if len(certificate) > 0 {
286-
skipHostnameValidation = true
287-
}
283+
}
284+
// When a certificate is provided with any encryption mode (strict, true/required, mandatory),
285+
// skip hostname validation. The certificate itself will still be validated against the provided CA
286+
if len(certificate) > 0 {
287+
skipHostnameValidation = true
288288
}
289289
tlsConfig, err := SetupTLS(certificate, trustServerCert, host, tlsMin, skipHostnameValidation)
290290
if err != nil {

msdsn/conn_str_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,17 @@ QSkVdAJg8mHKYGNZ6pIYMFr7RoBLGqMnKLPMYn3VqFvMccPx7A0hKQFJBR/qV8lh
379379
f0kGHKQEAFYGJLqJdK4KsGQDKLfZr9fqvXCCAA==
380380
-----END CERTIFICATE-----`
381381

382-
pemfile, _ := os.CreateTemp("", "*.pem")
382+
pemfile, err := os.CreateTemp("", "*.pem")
383+
if err != nil {
384+
t.Fatalf("failed to create temporary certificate file: %v", err)
385+
}
383386
defer os.Remove(pemfile.Name())
384-
pemfile.WriteString(pemCert)
385-
pemfile.Close()
387+
if _, err := pemfile.WriteString(pemCert); err != nil {
388+
t.Fatalf("failed to write certificate to file: %v", err)
389+
}
390+
if err := pemfile.Close(); err != nil {
391+
t.Fatalf("failed to close certificate file: %v", err)
392+
}
386393

387394
// Test 1: encrypt=strict with certificate should skip hostname validation
388395
connStr := "server=differenthostname;encrypt=strict;certificate=" + pemfile.Name()
@@ -402,11 +409,13 @@ f0kGHKQEAFYGJLqJdK4KsGQDKLfZr9fqvXCCAA==
402409
assert.NotNil(t, config2.TLSConfig, "Expected TLSConfig to be set")
403410
assert.False(t, config2.TLSConfig.InsecureSkipVerify, "Expected InsecureSkipVerify to be false when no certificate is provided")
404411

405-
// Test 3: encrypt=required with certificate should still validate hostname
412+
// Test 3: encrypt=required with certificate should also skip hostname validation
406413
connStr3 := "server=somehost;encrypt=true;certificate=" + pemfile.Name()
407414
config3, err := Parse(connStr3)
408415
assert.Nil(t, err, "Expected no error parsing connection string")
409416
assert.Equal(t, Encryption(EncryptionRequired), config3.Encryption, "Expected EncryptionRequired")
410417
assert.NotNil(t, config3.TLSConfig, "Expected TLSConfig to be set")
411-
assert.False(t, config3.TLSConfig.InsecureSkipVerify, "Expected InsecureSkipVerify to be false for encrypt=true")
418+
// When a certificate is provided, hostname validation is skipped for any encryption mode
419+
assert.False(t, config3.TLSConfig.InsecureSkipVerify, "Expected InsecureSkipVerify to be false")
420+
assert.NotNil(t, config3.TLSConfig.VerifyConnection, "Expected VerifyConnection callback to be set for encrypt=true with certificate")
412421
}

0 commit comments

Comments
 (0)