Skip to content

Commit 9995f8c

Browse files
Copilotgaby
andauthored
🐛 bug: defer TLS handler setup until client CA validation succeeds
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/c36b8ed0-2ea6-43fd-9fc1-8b2e5ff74bf1 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
1 parent 7170b88 commit 9995f8c

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

listen.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func (app *App) Listen(addr string, config ...ListenConfig) error {
187187

188188
// Configure TLS
189189
var tlsConfig *tls.Config
190+
var tlsHandler *TLSHandler
190191
if cfg.TLSConfig != nil {
191192
tlsConfig = cfg.TLSConfig.Clone()
192193
} else {
@@ -199,7 +200,7 @@ func (app *App) Listen(addr string, config ...ListenConfig) error {
199200
return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %w", cfg.CertFile, cfg.CertKeyFile, err)
200201
}
201202

202-
tlsHandler := &TLSHandler{}
203+
tlsHandler = &TLSHandler{}
203204
tlsConfig = &tls.Config{ //nolint:gosec // This is a user input
204205
MinVersion: cfg.TLSMinVersion,
205206
Certificates: []tls.Certificate{
@@ -208,8 +209,6 @@ func (app *App) Listen(addr string, config ...ListenConfig) error {
208209
GetCertificate: tlsHandler.GetClientInfo,
209210
}
210211

211-
// Attach the tlsHandler to the config
212-
app.SetTLSHandler(tlsHandler)
213212
case cfg.AutoCertManager != nil:
214213
tlsConfig = &tls.Config{ //nolint:gosec // This is a user input
215214
MinVersion: cfg.TLSMinVersion,
@@ -223,6 +222,11 @@ func (app *App) Listen(addr string, config ...ListenConfig) error {
223222
if err := applyClientCert(tlsConfig, cfg.CertClientFile); err != nil {
224223
return err
225224
}
225+
226+
if tlsHandler != nil {
227+
// Attach the tlsHandler to the config
228+
app.SetTLSHandler(tlsHandler)
229+
}
226230
}
227231

228232
if tlsConfig != nil && cfg.TLSConfigFunc != nil {

listen_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,16 @@ func Test_Listen_AutoCert_WithClientCertFile(t *testing.T) {
514514
t.Parallel()
515515

516516
app := New()
517+
done := make(chan struct{})
518+
defer close(done)
519+
520+
go func() {
521+
select {
522+
case <-done:
523+
case <-time.After(time.Second):
524+
assert.NoError(t, app.Shutdown())
525+
}
526+
}()
517527

518528
err := app.Listen(":0", ListenConfig{
519529
CertClientFile: tc.clientCAPath,
@@ -527,6 +537,27 @@ func Test_Listen_AutoCert_WithClientCertFile(t *testing.T) {
527537
}
528538
}
529539

540+
func Test_Listen_ClientCertErrorDoesNotSetTLSHandler(t *testing.T) {
541+
t.Parallel()
542+
543+
invalidClientCAPath := filepath.Join(t.TempDir(), "client-ca.pem")
544+
require.NoError(t, os.WriteFile(invalidClientCAPath, []byte("not a pem"), 0o600))
545+
546+
app := New()
547+
548+
err := app.Listen(":0", ListenConfig{
549+
CertFile: "./.github/testdata/ssl.pem",
550+
CertKeyFile: "./.github/testdata/ssl.key",
551+
CertClientFile: invalidClientCAPath,
552+
})
553+
require.ErrorContains(t, err, filepath.Base(invalidClientCAPath))
554+
555+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
556+
defer app.ReleaseCtx(c)
557+
558+
require.Nil(t, c.ClientHelloInfo())
559+
}
560+
530561
// go test -run Test_Listen_ListenerAddrFunc
531562
func Test_Listen_ListenerAddrFunc(t *testing.T) {
532563
var network string

0 commit comments

Comments
 (0)