启用严格远端TLS证书校验

移除远端HTTPS校验中的跳过证书验证配置,使用受信任根证书和域名SNI完成校验,并补充指定地址连接测试。
This commit is contained in:
xuans 2026-10-01 06:46:13 +08:00
parent 1164d502ca
commit 5ef8e54dcd
2 changed files with 29 additions and 3 deletions

View File

@ -153,9 +153,9 @@ func (v RemoteValidator) validateHTTPS(ctx context.Context, endpoint, dialAddres
tlsDialer := &tls.Dialer{
NetDialer: dialer,
Config: &tls.Config{
ServerName: parsed.Hostname(),
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true,
ServerName: parsed.Hostname(),
RootCAs: v.Roots,
MinVersion: tls.VersionTLS12,
},
}
connection, err := tlsDialer.DialContext(ctx, "tcp", address)

View File

@ -13,6 +13,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
@ -81,6 +82,31 @@ func TestRemoteValidatorChecksPresentedCertificate(t *testing.T) {
}
}
func TestRemoteValidatorUsesAddressOverrideWithEndpointSNI(t *testing.T) {
certificateData, privateKeyData, leaf := validationTestCertificate(t, []string{"example.com", "127.0.0.1"})
pair, err := tls.X509KeyPair(certificateData, privateKeyData)
if err != nil {
t.Fatal(err)
}
server := httptest.NewUnstartedServer(nil)
server.TLS = &tls.Config{Certificates: []tls.Certificate{pair}}
server.StartTLS()
defer server.Close()
roots := x509.NewCertPool()
roots.AddCert(leaf)
endpoint := "https://example.com" + strings.TrimPrefix(server.URL, "https://127.0.0.1")
address := server.Listener.Addr().String()
result, err := (RemoteValidator{Roots: roots, Timeout: 5 * time.Second}).ValidateHTTPSAt(
context.Background(), endpoint, address, []string{"example.com"},
)
if err != nil {
t.Fatalf("address override with endpoint SNI should pass: %v", err)
}
if result.ChainLength != 1 || len(result.FingerprintSHA256) != 64 {
t.Fatalf("unexpected remote validation result: %+v", result)
}
}
func validationTestCertificate(t *testing.T, names []string) ([]byte, []byte, *x509.Certificate) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)