启用严格远端TLS证书校验
移除远端HTTPS校验中的跳过证书验证配置,使用受信任根证书和域名SNI完成校验,并补充指定地址连接测试。
This commit is contained in:
parent
1164d502ca
commit
5ef8e54dcd
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user