Files
op-packages/xray-core/patches/100-restore-allow-insecure.patch
T

155 lines
5.2 KiB
Diff

From: coolsnowwolf <coolsnowwolf@gmail.com>
Subject: [PATCH] tls: restore explicit allowInsecure compatibility
Restore the opt-in TLS option for legacy proxy certificates, including
Hysteria2 servers without SANs. Keep certificate verification enabled by
default and continue enforcing explicit certificate pins and names.
Signed-off-by: coolsnowwolf <coolsnowwolf@gmail.com>
--- a/infra/conf/transport_security.go
+++ b/infra/conf/transport_security.go
@@ -359,9 +359,7 @@
config.RejectUnknownSni = c.RejectUnknownSNI
config.MasterKeyLog = c.MasterKeyLog
- if c.AllowInsecure {
- return nil, errors.PrintRemovedFeatureError(`"allowInsecure"`, `"pinnedPeerCertSha256"(pcs) and "verifyPeerCertByName"(vcn)`)
- }
+ config.AllowInsecure = c.AllowInsecure
if c.PinnedPeerCertSha256 != "" {
for v := range strings.SplitSeq(c.PinnedPeerCertSha256, ",") {
v = strings.TrimSpace(v)
--- a/infra/conf/tls_allow_insecure_test.go
+++ b/infra/conf/tls_allow_insecure_test.go
@@ -0,0 +1,70 @@
+package conf_test
+
+import (
+ "crypto/sha256"
+ "crypto/tls"
+ "encoding/hex"
+ "encoding/json"
+ "io"
+ "log"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/xtls/xray-core/infra/conf"
+ xraytls "github.com/xtls/xray-core/transport/internet/tls"
+ "google.golang.org/protobuf/proto"
+)
+
+func TestTLSAllowInsecure(t *testing.T) {
+ server := httptest.NewUnstartedServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
+ server.Config.ErrorLog = log.New(io.Discard, "", 0)
+ server.StartTLS()
+ defer server.Close()
+ hash := sha256.Sum256(server.Certificate().Raw)
+ pin := hex.EncodeToString(hash[:])
+ for _, tc := range []struct {
+ name string
+ options string
+ wantOK bool
+ }{
+ {"default rejects untrusted certificate", `{}`, false},
+ {"false rejects untrusted certificate", `{"allowInsecure":false}`, false},
+ {"true accepts untrusted certificate", `{"allowInsecure":true}`, true},
+ {"valid pin", `{"pinnedPeerCertSha256":"` + pin + `"}`, true},
+ {"true with valid pin", `{"allowInsecure":true,"pinnedPeerCertSha256":"` + pin + `"}`, true},
+ {"true preserves invalid pin rejection", `{"allowInsecure":true,"pinnedPeerCertSha256":"` + strings.Repeat("00", 32) + `"}`, false},
+ {"true preserves explicit name verification", `{"allowInsecure":true,"verifyPeerCertByName":"wrong.invalid"}`, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ var input conf.TLSConfig
+ if err := json.Unmarshal([]byte(tc.options), &input); err != nil {
+ t.Fatal(err)
+ }
+ input.ServerName = "example.com"
+ built, err := input.Build()
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Exercise the protobuf path used by the core, not just the JSON loader.
+ data, err := proto.Marshal(built)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var config xraytls.Config
+ if err := proto.Unmarshal(data, &config); err != nil {
+ t.Fatal(err)
+ }
+ conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 3 * time.Second}, "tcp", server.Listener.Addr().String(), config.GetTLSConfig())
+ if conn != nil {
+ conn.Close()
+ }
+ if (err == nil) != tc.wantOK {
+ t.Fatalf("handshake error = %v, want success %v", err, tc.wantOK)
+ }
+ })
+ }
+}
--- a/transport/internet/tls/config.proto
+++ b/transport/internet/tls/config.proto
@@ -38,7 +38,8 @@
}
message Config {
- // Number 1 was assigned and used by an legacy option.
+ // Allow unverified certificates only when explicitly enabled by the client.
+ bool allow_insecure = 1;
// List of certificates to be served on server.
repeated Certificate certificate = 2;
--- a/transport/internet/tls/config.pb.go
+++ b/transport/internet/tls/config.pb.go
@@ -178,6 +178,8 @@
type Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
+ // Allow unverified certificates only when explicitly enabled by the client.
+ AllowInsecure bool `protobuf:"varint,1,opt,name=allow_insecure,json=allowInsecure,proto3" json:"allow_insecure,omitempty"`
// List of certificates to be served on server.
Certificate []*Certificate `protobuf:"bytes,2,rep,name=certificate,proto3" json:"certificate,omitempty"`
// Override server name.
@@ -241,6 +243,13 @@
return file_transport_internet_tls_config_proto_rawDescGZIP(), []int{1}
}
+func (x *Config) GetAllowInsecure() bool {
+ if x != nil {
+ return x.AllowInsecure
+ }
+ return false
+}
+
func (x *Config) GetCertificate() []*Certificate {
if x != nil {
return x.Certificate
@@ -385,8 +394,9 @@
"\x05Usage\x12\x10\n" +
"\fENCIPHERMENT\x10\x00\x12\x14\n" +
"\x10AUTHORITY_VERIFY\x10\x01\x12\x13\n" +
- "\x0fAUTHORITY_ISSUE\x10\x02\"\xca\x06\n" +
- "\x06Config\x12J\n" +
+ "\x0fAUTHORITY_ISSUE\x10\x02\"\xf1\x06\n" +
+ "\x06Config\x12%\n" +
+ "\x0eallow_insecure\x18\x01 \x01(\bR\rallowInsecure\x12J\n" +
"\vcertificate\x18\x02 \x03(\v2(.xray.transport.internet.tls.CertificateR\vcertificate\x12\x1f\n" +
"\vserver_name\x18\x03 \x01(\tR\n" +
"serverName\x12#\n" +
--- a/transport/internet/tls/config.go
+++ b/transport/internet/tls/config.go
@@ -385,6 +385,7 @@
PinnedPeerCertSha256: c.PinnedPeerCertSha256,
}
config := &tls.Config{
+ InsecureSkipVerify: c.AllowInsecure,
Rand: randCarrier,
ClientSessionCache: globalSessionCache,
RootCAs: root,