Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigate potential Slowloris attacks by setting ReadHeaderTimeout in all http.Server instances #6534

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmd/cainjector/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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.CAInjectorConfiguration, ctx context.Context) error {
ctx = logf.NewContext(ctx, logf.Log, "cainjector")
log := logf.FromContext(ctx)
Expand Down Expand Up @@ -97,7 +108,8 @@ func Run(opts *config.CAInjectorConfiguration, ctx context.Context) error {
profiling.Install(profilerMux)
log.V(logf.InfoLevel).Info("running go profiler on", "address", opts.PprofAddress)
server := &http.Server{
Handler: profilerMux,
Handler: profilerMux,
ReadHeaderTimeout: defaultReadHeaderTimeout, // Mitigation for G112: Potential slowloris attack
}

mgr.Add(runnableNoLeaderElectionFunc(func(ctx context.Context) error {
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