Skip to content

Commit 424b29d

Browse files
authored
fix: 60s read header timeout on http servers (#358)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent c20bcf8 commit 424b29d

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

cmd/cardano-node-api/main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,9 +18,11 @@ import (
1818
"flag"
1919
"fmt"
2020
"net/http"
21-
_ "net/http/pprof"
2221
"os"
22+
"time"
2323

24+
// #nosec G108
25+
_ "net/http/pprof"
2426
_ "go.uber.org/automaxprocs"
2527

2628
"github.com/blinklabs-io/cardano-node-api/internal/api"
@@ -66,7 +68,11 @@ func main() {
6668
}
6769
}
6870

69-
logger.Info("starting cardano-node-api version", "version", version.GetVersionString())
71+
logger.Info(
72+
"starting cardano-node-api version",
73+
"version",
74+
version.GetVersionString(),
75+
)
7076

7177
// Start debug listener
7278
if cfg.Debug.ListenPort > 0 {
@@ -76,14 +82,15 @@ func main() {
7682
cfg.Debug.ListenPort,
7783
))
7884
go func() {
79-
err := http.ListenAndServe(
80-
fmt.Sprintf(
85+
debugger := &http.Server{
86+
Addr: fmt.Sprintf(
8187
"%s:%d",
8288
cfg.Debug.ListenAddress,
8389
cfg.Debug.ListenPort,
8490
),
85-
nil,
86-
)
91+
ReadHeaderTimeout: 60 * time.Second,
92+
}
93+
err := debugger.ListenAndServe()
8794
if err != nil {
8895
logger.Error("failed to start debug listener:", "error", err)
8996
os.Exit(1)

internal/utxorpc/api.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package utxorpc
1717
import (
1818
"fmt"
1919
"net/http"
20+
"time"
2021

2122
"connectrpc.com/connect"
2223
"connectrpc.com/grpchealth"
@@ -104,23 +105,30 @@ func Start(cfg *config.Config) error {
104105
),
105106
)
106107
if cfg.Tls.CertFilePath != "" && cfg.Tls.KeyFilePath != "" {
107-
err := http.ListenAndServeTLS(
108-
fmt.Sprintf(
108+
server := &http.Server{
109+
Addr: fmt.Sprintf(
109110
"%s:%d",
110111
cfg.Utxorpc.ListenAddress,
111112
cfg.Utxorpc.ListenPort,
112113
),
114+
Handler: mux,
115+
ReadHeaderTimeout: 60 * time.Second,
116+
}
117+
return server.ListenAndServeTLS(
113118
cfg.Tls.CertFilePath,
114119
cfg.Tls.KeyFilePath,
115-
mux,
116120
)
117-
return err
118121
} else {
119-
err := http.ListenAndServe(
120-
fmt.Sprintf("%s:%d", cfg.Utxorpc.ListenAddress, cfg.Utxorpc.ListenPort),
122+
server := &http.Server{
123+
Addr: fmt.Sprintf(
124+
"%s:%d",
125+
cfg.Utxorpc.ListenAddress,
126+
cfg.Utxorpc.ListenPort,
127+
),
121128
// Use h2c so we can serve HTTP/2 without TLS
122-
h2c.NewHandler(mux, &http2.Server{}),
123-
)
124-
return err
129+
Handler: h2c.NewHandler(mux, &http2.Server{}),
130+
ReadHeaderTimeout: 60 * time.Second,
131+
}
132+
return server.ListenAndServe()
125133
}
126134
}

0 commit comments

Comments
 (0)