mirror of
https://github.com/coredns/coredns.git
synced 2026-01-06 00:41:31 -05:00
fix(lint): address G114 gosec findings in ready, pprof, and health plugins (#7798)
Replace http.Serve() with http.Server{} configured with timeouts to
address G114 gosec findings (HTTP server without timeouts). This
prevents potential slowloris attacks and resource exhaustion.
Changes:
- Add ReadTimeout, WriteTimeout, IdleTimeout (5s each) to HTTP servers
- Use srv.Shutdown(ctx) for graceful shutdown instead of ln.Close()
- Follow existing pattern from plugin/metrics
Fixes part of #7793
Signed-off-by: Azeez Syed <syedazeez337@gmail.com>
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
pp "net/http/pprof"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/reuseport"
|
||||
)
|
||||
@@ -15,9 +17,12 @@ type handler struct {
|
||||
addr string
|
||||
rateBloc int
|
||||
ln net.Listener
|
||||
srv *http.Server
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
const shutdownTimeout = 5 * time.Second
|
||||
|
||||
func (h *handler) Startup() error {
|
||||
// Reloading the plugin without changing the listening address results
|
||||
// in an error unless we reuse the port because Startup is called for
|
||||
@@ -42,16 +47,25 @@ func (h *handler) Startup() error {
|
||||
|
||||
runtime.SetBlockProfileRate(h.rateBloc)
|
||||
|
||||
go func() {
|
||||
// #nosec G114 -- TODO
|
||||
http.Serve(h.ln, h.mux)
|
||||
}()
|
||||
h.srv = &http.Server{
|
||||
Handler: h.mux,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second,
|
||||
IdleTimeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
go func() { h.srv.Serve(h.ln) }()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *handler) Shutdown() error {
|
||||
if h.ln != nil {
|
||||
return h.ln.Close()
|
||||
if h.srv != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cancel()
|
||||
if err := h.srv.Shutdown(ctx); err != nil {
|
||||
log.Infof("Failed to stop pprof http server: %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user