Skip to content

Commit

Permalink
Merge pull request #6538 from wallrj/backport-6534-to-release-1.13
Browse files Browse the repository at this point in the history
[release-1.13] Mitigate potential Slowloris attacks by setting ReadHeaderTimeout in all http.Server instances
  • Loading branch information
jetstack-bot committed Dec 7, 2023
2 parents d1e2d25 + d080cec commit 876e386
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
14 changes: 13 additions & 1 deletion cmd/cainjector/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ import (
"github.com/cert-manager/cert-manager/pkg/util/profiling"
)

const (
// This is intended to mitigate "slowloris" attacks by limiting the time a
// deliberately slow client can spend sending HTTP headers.
// This default value is copied from:
// * kubernetes api-server:
// https://github.com/kubernetes/kubernetes/blob/9e028b40b9e970142191259effe796b3dab39828/staging/src/k8s.io/apiserver/pkg/server/secure_serving.go#L165-L173
// * controller-runtime:
// https://github.com/kubernetes-sigs/controller-runtime/blob/1ea2be573f7887a9fbd766e9a921c5af344da6eb/pkg/internal/httpserver/server.go#L14
defaultReadHeaderTimeout = 32 * time.Second
)

// InjectorControllerOptions is a struct having injector controller options values
type InjectorControllerOptions struct {
Logging *logs.Options
Expand Down Expand Up @@ -235,7 +246,8 @@ func (o InjectorControllerOptions) RunInjectorController(ctx context.Context) er
profiling.Install(profilerMux)
o.log.V(logf.InfoLevel).Info("running go profiler on", "address", o.PprofAddr)
server := &http.Server{
Handler: profilerMux,
Handler: profilerMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}
g.Go(func() error {
<-gctx.Done()
Expand Down
14 changes: 13 additions & 1 deletion cmd/controller/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ import (
"github.com/cert-manager/cert-manager/pkg/util/profiling"
)

const (
// This is intended to mitigate "slowloris" attacks by limiting the time a
// deliberately slow client can spend sending HTTP headers.
// This default value is copied from:
// * kubernetes api-server:
// https://github.com/kubernetes/kubernetes/blob/9e028b40b9e970142191259effe796b3dab39828/staging/src/k8s.io/apiserver/pkg/server/secure_serving.go#L165-L173
// * controller-runtime:
// https://github.com/kubernetes-sigs/controller-runtime/blob/1ea2be573f7887a9fbd766e9a921c5af344da6eb/pkg/internal/httpserver/server.go#L14
defaultReadHeaderTimeout = 32 * time.Second
)

func Run(opts *config.ControllerConfiguration, stopCh <-chan struct{}) error {
rootCtx, cancelContext := context.WithCancel(cmdutil.ContextWithStopCh(context.Background(), stopCh))
defer cancelContext()
Expand Down Expand Up @@ -107,7 +118,8 @@ func Run(opts *config.ControllerConfiguration, stopCh <-chan struct{}) error {
// Add pprof endpoints to this mux
profiling.Install(profilerMux)
profilerServer := &http.Server{
Handler: profilerMux,
Handler: profilerMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}

g.Go(func() error {
Expand Down
17 changes: 15 additions & 2 deletions pkg/issuer/acme/http/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ import (
"net/http"
"path"
"strings"
"time"

"github.com/go-logr/logr"
)

const (
// This is intended to mitigate "slowloris" attacks by limiting the time a
// deliberately slow client can spend sending HTTP headers.
// This default value is copied from:
// * kubernetes api-server:
// https://github.com/kubernetes/kubernetes/blob/9e028b40b9e970142191259effe796b3dab39828/staging/src/k8s.io/apiserver/pkg/server/secure_serving.go#L165-L173
// * controller-runtime:
// https://github.com/kubernetes-sigs/controller-runtime/blob/1ea2be573f7887a9fbd766e9a921c5af344da6eb/pkg/internal/httpserver/server.go#L14
defaultReadHeaderTimeout = 32 * time.Second
)

type HTTP01Solver struct {
ListenPort int

Expand Down Expand Up @@ -91,8 +103,9 @@ func (h *HTTP01Solver) Listen(log logr.Logger) error {
})

h.Server = http.Server{
Addr: fmt.Sprintf(":%d", h.ListenPort),
Handler: handler,
Addr: fmt.Sprintf(":%d", h.ListenPort),
Handler: handler,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}

return h.Server.ListenAndServe()
Expand Down
20 changes: 17 additions & 3 deletions pkg/webhook/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ import (
servertls "github.com/cert-manager/cert-manager/pkg/webhook/server/tls"
)

const (
// This is intended to mitigate "slowloris" attacks by limiting the time a
// deliberately slow client can spend sending HTTP headers.
// This default value is copied from:
// * kubernetes api-server:
// https://github.com/kubernetes/kubernetes/blob/9e028b40b9e970142191259effe796b3dab39828/staging/src/k8s.io/apiserver/pkg/server/secure_serving.go#L165-L173
// * controller-runtime:
// https://github.com/kubernetes-sigs/controller-runtime/blob/1ea2be573f7887a9fbd766e9a921c5af344da6eb/pkg/internal/httpserver/server.go#L14
defaultReadHeaderTimeout = 32 * time.Second
)

var (
// defaultScheme is used to encode and decode the AdmissionReview and
// ConversionReview resources submitted to the webhook server.
Expand Down Expand Up @@ -135,7 +146,8 @@ func (s *Server) Run(ctx context.Context) error {
healthMux.HandleFunc("/livez", s.handleLivez)
s.log.V(logf.InfoLevel).Info("listening for insecure healthz connections", "address", s.HealthzAddr)
server := &http.Server{
Handler: healthMux,
Handler: healthMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}
g.Go(func() error {
<-gctx.Done()
Expand Down Expand Up @@ -168,7 +180,8 @@ func (s *Server) Run(ctx context.Context) error {
profiling.Install(profilerMux)
s.log.V(logf.InfoLevel).Info("running go profiler on", "address", s.PprofAddr)
server := &http.Server{
Handler: profilerMux,
Handler: profilerMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}
g.Go(func() error {
<-gctx.Done()
Expand Down Expand Up @@ -228,7 +241,8 @@ func (s *Server) Run(ctx context.Context) error {
serverMux.HandleFunc("/mutate", s.handle(s.mutate))
serverMux.HandleFunc("/convert", s.handle(s.convert))
server := &http.Server{
Handler: serverMux,
Handler: serverMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}
g.Go(func() error {
<-gctx.Done()
Expand Down

0 comments on commit 876e386

Please sign in to comment.