From 2d2ffc160ed60c8e62141fefd7a0b71960e9e77d Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sat, 12 Sep 2026 10:00:03 +0200 Subject: [PATCH] fix: cap body sizes for incoming requests to public services Signed-off-by: Jakob Borg --- cmd/infra/strelaypoolsrv/main.go | 10 ++++++++++ cmd/infra/strelaypoolsrv/stats.go | 4 ++-- cmd/infra/ursrv/serve/prometheus.go | 3 +-- cmd/stdiscosrv/apisrv.go | 9 +++++++++ cmd/stdiscosrv/main.go | 1 + cmd/stdiscosrv/stats.go | 2 ++ 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/infra/strelaypoolsrv/main.go b/cmd/infra/strelaypoolsrv/main.go index bd3604b2c..efc79ebd9 100644 --- a/cmd/infra/strelaypoolsrv/main.go +++ b/cmd/infra/strelaypoolsrv/main.go @@ -8,6 +8,7 @@ import ( "crypto/tls" "crypto/x509" "encoding/json" + "errors" "flag" "fmt" "log" @@ -126,6 +127,7 @@ var ( const ( httpStatusEnhanceYourCalm = 429 + maxJSONBodyBytes = 8 << 10 ) func main() { @@ -249,6 +251,7 @@ func main() { postMux.HandleFunc("/endpoint", withAPIMetrics(handleRegister)) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.Body = http.MaxBytesReader(w, r.Body, maxJSONBodyBytes) switch r.Method { case http.MethodGet, http.MethodHead, http.MethodOptions: getMux.ServeHTTP(w, r) @@ -387,6 +390,13 @@ func handleRegister(w http.ResponseWriter, r *http.Request) { r.Body.Close() if err != nil { + if _, ok := errors.AsType[*http.MaxBytesError](err); ok { + if debug { + log.Println("Payload too large") + } + http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge) + return + } if debug { log.Println("Failed to parse payload") } diff --git a/cmd/infra/strelaypoolsrv/stats.go b/cmd/infra/strelaypoolsrv/stats.go index aeea6b636..e9a9d98fb 100644 --- a/cmd/infra/strelaypoolsrv/stats.go +++ b/cmd/infra/strelaypoolsrv/stats.go @@ -4,6 +4,7 @@ package main import ( "encoding/json" + "io" "net" "net/http" "sync" @@ -170,8 +171,7 @@ func fetchStats(relay *relay) *stats { } var stats stats - - if err := json.NewDecoder(response.Body).Decode(&stats); err != nil { + if err := json.NewDecoder(io.LimitReader(response.Body, maxJSONBodyBytes)).Decode(&stats); err != nil { return nil } return &stats diff --git a/cmd/infra/ursrv/serve/prometheus.go b/cmd/infra/ursrv/serve/prometheus.go index bc42646c6..e71b8b22d 100644 --- a/cmd/infra/ursrv/serve/prometheus.go +++ b/cmd/infra/ursrv/serve/prometheus.go @@ -47,8 +47,7 @@ func newMetricsSet(srv *server) *metricsSet { var initForType func(reflect.Type) initForType = func(t reflect.Type) { - for i := range t.NumField() { - field := t.Field(i) + for field := range t.Fields() { if field.Type.Kind() == reflect.Struct { initForType(field.Type) continue diff --git a/cmd/stdiscosrv/apisrv.go b/cmd/stdiscosrv/apisrv.go index be1714f2a..bd31c005e 100644 --- a/cmd/stdiscosrv/apisrv.go +++ b/cmd/stdiscosrv/apisrv.go @@ -140,6 +140,8 @@ func (s *apiSrv) Serve(ctx context.Context) error { } func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) { + req.Body = http.MaxBytesReader(w, req.Body, httpMaxBodyBytes) + t0 := time.Now() lw := NewLoggingResponseWriter(w) @@ -271,6 +273,13 @@ func (s *apiSrv) handlePOST(remoteAddr *net.TCPAddr, w http.ResponseWriter, req var ann announcement if err := json.NewDecoder(req.Body).Decode(&ann); err != nil { + if _, ok := errors.AsType[*http.MaxBytesError](err); ok { + slog.Debug("Request body too large", "id", reqID, "error", err) + announceRequestsTotal.WithLabelValues("request_too_large").Inc() + w.Header().Set("Retry-After", errorRetryAfterString()) + http.Error(w, "Request Entity Too Large", http.StatusRequestEntityTooLarge) + return + } slog.Debug("Failed to decode request", "id", reqID, "error", err) announceRequestsTotal.WithLabelValues("bad_request").Inc() w.Header().Set("Retry-After", errorRetryAfterString()) diff --git a/cmd/stdiscosrv/main.go b/cmd/stdiscosrv/main.go index aa1f91f04..64b70ab4b 100644 --- a/cmd/stdiscosrv/main.go +++ b/cmd/stdiscosrv/main.go @@ -50,6 +50,7 @@ const ( httpReadTimeout = 5 * time.Second httpWriteTimeout = 5 * time.Second httpMaxHeaderBytes = 1 << 10 + httpMaxBodyBytes = 8 << 10 // Size of the replication outbox channel replicationOutboxSize = 10000 diff --git a/cmd/stdiscosrv/stats.go b/cmd/stdiscosrv/stats.go index 32b962ca5..9fbb30cc9 100644 --- a/cmd/stdiscosrv/stats.go +++ b/cmd/stdiscosrv/stats.go @@ -140,6 +140,7 @@ func init() { apiRequestsTotal.WithLabelValues(http.MethodPost, "204") apiRequestsTotal.WithLabelValues(http.MethodPost, "400") apiRequestsTotal.WithLabelValues(http.MethodPost, "403") + apiRequestsTotal.WithLabelValues(http.MethodPost, "413") lookupRequestsTotal.WithLabelValues("success") lookupRequestsTotal.WithLabelValues("not_found_ever") @@ -147,6 +148,7 @@ func init() { announceRequestsTotal.WithLabelValues("success") announceRequestsTotal.WithLabelValues("bad_request") + announceRequestsTotal.WithLabelValues("request_too_large") announceRequestsTotal.WithLabelValues("no_certificate") replicationSendsTotal.WithLabelValues("success")