Skip to content

Commit b370484

Browse files
authored
Merge pull request #247 from SenseUnit/fix_mtls_go1.27.0
Add workaround to fix mTLS auth on early versions of go1.27
2 parents 6254663 + 5c83472 commit b370484

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

auth/cert.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"crypto/tls"
78
"encoding/hex"
89
"errors"
910
"fmt"
@@ -16,9 +17,10 @@ import (
1617
"time"
1718

1819
clog "github.com/SenseUnit/dumbproxy/log"
19-
"github.com/hashicorp/go-multierror"
20+
"github.com/SenseUnit/dumbproxy/tlsutil"
2021

2122
us "github.com/Snawoot/uniqueslice"
23+
"github.com/hashicorp/go-multierror"
2224
)
2325

2426
type serialNumberSetFile struct {
@@ -94,10 +96,26 @@ func (auth *CertAuth) handleReject(ctx context.Context, wr http.ResponseWriter,
9496
}
9597

9698
func (auth *CertAuth) Validate(ctx context.Context, wr http.ResponseWriter, req *http.Request) (string, bool) {
97-
if req.TLS == nil || len(req.TLS.VerifiedChains) < 1 || len(req.TLS.VerifiedChains[0]) < 1 {
99+
cs := req.TLS
100+
if cs == nil {
101+
auth.logger.Debug("resorting to workaround for go bug #81384")
102+
// could be issue https://go.dev/issue/81384
103+
conn, ok := tlsutil.ConnFromContext(ctx)
104+
if ok {
105+
if cstater, ok := conn.(interface{ ConnectionState() tls.ConnectionState }); ok {
106+
cs = new(tls.ConnectionState)
107+
*cs = cstater.ConnectionState()
108+
} else {
109+
auth.logger.Debug("...and conn does not support required interface!")
110+
}
111+
} else {
112+
auth.logger.Debug("...and conn was not recovered from context!")
113+
}
114+
}
115+
if cs == nil || len(cs.VerifiedChains) < 1 || len(cs.VerifiedChains[0]) < 1 {
98116
return auth.handleReject(ctx, wr, req)
99117
}
100-
eeCert := req.TLS.VerifiedChains[0][0]
118+
eeCert := cs.VerifiedChains[0][0]
101119
if auth.blacklist.Load().file.Has(eeCert.SerialNumber) {
102120
return auth.handleReject(ctx, wr, req)
103121
}

auth/tlscookie.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ func (auth *TLSCookieAuth) addLearned(sessionID tlsutil.TLSSessionID) {
8787
}
8888

8989
func (auth *TLSCookieAuth) Validate(ctx context.Context, wr http.ResponseWriter, req *http.Request) (string, bool) {
90-
sessionID, ok := tlsutil.TLSSessionIDFromContext(ctx)
90+
conn, ok := tlsutil.ConnFromContext(ctx)
9191
if !ok {
92-
auth.logger.Debug("tlscookie: no session extracted for %s", req.RemoteAddr)
92+
auth.logger.Debug("tlscookie: no conn found in context for %s", req.RemoteAddr)
93+
return auth.handleReject(ctx, wr, req)
94+
}
95+
sessionID, ok := tlsutil.GetTLSSessionID(conn)
96+
if !ok {
97+
auth.logger.Debug("tlscookie: no session ID recovered for %s", req.RemoteAddr)
9398
return auth.handleReject(ctx, wr, req)
9499
}
95100
if auth.hiddenDomain != "" {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ func run() int {
963963
return stopContext
964964
},
965965
ConnContext: func(ctx context.Context, conn net.Conn) context.Context {
966-
return tlsutil.TLSSessionIDToContext(ctx, conn)
966+
return tlsutil.ConnToContext(ctx, conn)
967967
},
968968
}
969969
if args.disableHTTP2 {

tlsutil/session.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ func GetTLSSessionID(conn net.Conn) (TLSSessionID, bool) {
6363
return getTLSSessionID(tagger)
6464
}
6565

66-
func TLSSessionIDToContext(ctx context.Context, conn net.Conn) context.Context {
66+
func ConnToContext(ctx context.Context, conn net.Conn) context.Context {
6767
return context.WithValue(ctx, connKey{}, conn)
6868
}
6969

70-
func TLSSessionIDFromContext(ctx context.Context) (TLSSessionID, bool) {
70+
func ConnFromContext(ctx context.Context) (net.Conn, bool) {
7171
val := ctx.Value(connKey{})
7272
conn, ok := val.(net.Conn)
73-
if !ok {
74-
return TLSSessionID{}, false
75-
}
76-
return GetTLSSessionID(conn)
73+
return conn, ok
7774
}
7875

7976
func EnableTLSCookies(cfg *tls.Config, logger *clog.CondLogger) *tls.Config {

0 commit comments

Comments
 (0)