fix: cap body sizes for incoming requests to public services

Signed-off-by: Jakob Borg <[email protected]>
This commit is contained in:
Jakob Borg committed 2026-10-18 11:31:39 +02:00
1 parent ab91974876
commit 2d2ffc160e
6 files changed
+25 -4

No files matched your search

+10
View File
@@ -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")
}
+2 -2
View File
@@ -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
+1 -2
View File
@@ -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
+9
View File
@@ -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())
+1
View File
@@ -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
+2
View File
@@ -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")