Skip to content

Commit 3ebeb6d

Browse files
committed
linting: address gosec G112/G114
GOGC=75 golangci-lint run services/server/server.go:320:27: G114: Use of net/http serve function that has no support for setting timeouts (gosec) return trapClosedConnErr(http.Serve(l, m)) ^ services/server/server.go:340:27: G114: Use of net/http serve function that has no support for setting timeouts (gosec) return trapClosedConnErr(http.Serve(l, m)) ^ cmd/containerd-stress/main.go:238:13: G114: Use of net/http serve function that has no support for setting timeouts (gosec) if err := http.ListenAndServe(c.Metrics, metrics.Handler()); err != nil { ^ Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e6b5311 commit 3ebeb6d

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

cmd/containerd-stress/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ func (c config) newClient() (*containerd.Client, error) {
235235

236236
func serve(c config) error {
237237
go func() {
238-
if err := http.ListenAndServe(c.Metrics, metrics.Handler()); err != nil {
238+
srv := &http.Server{
239+
Addr: c.Metrics,
240+
Handler: metrics.Handler(),
241+
ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout.
242+
}
243+
if err := srv.ListenAndServe(); err != nil {
239244
logrus.WithError(err).Error("listen and serve")
240245
}
241246
}()

services/server/server.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ func (s *Server) ServeTTRPC(l net.Listener) error {
317317
func (s *Server) ServeMetrics(l net.Listener) error {
318318
m := http.NewServeMux()
319319
m.Handle("/v1/metrics", metrics.Handler())
320-
return trapClosedConnErr(http.Serve(l, m))
320+
srv := &http.Server{
321+
Handler: m,
322+
ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout.
323+
}
324+
return trapClosedConnErr(srv.Serve(l))
321325
}
322326

323327
// ServeTCP allows services to serve over tcp
@@ -337,7 +341,11 @@ func (s *Server) ServeDebug(l net.Listener) error {
337341
m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
338342
m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
339343
m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
340-
return trapClosedConnErr(http.Serve(l, m))
344+
srv := &http.Server{
345+
Handler: m,
346+
ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout.
347+
}
348+
return trapClosedConnErr(srv.Serve(l))
341349
}
342350

343351
// Stop the containerd server canceling any open connections

0 commit comments

Comments
 (0)