Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
security: cap HTTP request body size on untrusted endpoints
Antonio Ojea9a82f2894129
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex e1093241..13a0d6dd 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -54,6 +54,9 @@ const (EnrollRateLimit = 10EnrollBurst = 20JWTVerificationTimeout = 10 * time.Second+ // maxRequestBodyBytes caps request bodies read into memory to guard+ // against memory-exhaustion from oversized payloads.+ maxRequestBodyBytes = 1 << 20 // 1 MiB)// Server implements the SAM Control Plane web app.@@ -341,6 +344,7 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -516,6 +520,7 @@ func (s *Server) HandleRefresh(w http.ResponseWriter, r *http.Request) {return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -728,6 +733,7 @@ func (s *Server) HandleRouterLease(w http.ResponseWriter, r *http.Request) {return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -885,6 +891,7 @@ func (s *Server) HandlePolicies(w http.ResponseWriter, r *http.Request) {if !s.checkAdminAuth(w, r) {return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -986,6 +993,7 @@ func (s *Server) HandleEnroll(w http.ResponseWriter, r *http.Request) {return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -1297,6 +1305,7 @@ func (s *Server) HandleAdminBootstrapTokens(w http.ResponseWriter, r *http.RequeDescription string `json:"description"`}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)if err := json.NewDecoder(r.Body).Decode(&req); err != nil {http.Error(w, "Invalid JSON body", http.StatusBadRequest)return@@ -1503,6 +1512,7 @@ func (s *Server) HandleAdminRevoke(w http.ResponseWriter, r *http.Request) {}ctx := r.Context()+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusBadRequest)@@ -1677,6 +1687,7 @@ func (s *Server) HandleUserBootstrapTokens(w http.ResponseWriter, r *http.RequesDescription string `json:"description"`}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)if err := json.NewDecoder(r.Body).Decode(&req); err != nil {http.Error(w, "Invalid JSON body", http.StatusBadRequest)returndiff --git a/internal/node/sidecar.go b/internal/node/sidecar.goindex 66dd12a0..61bf4e01 100644--- a/internal/node/sidecar.go+++ b/internal/node/sidecar.go@@ -228,12 +228,17 @@ type ServiceRequest struct {ServiceName string `json:"service_name"`}+// maxRequestBodyBytes caps request bodies read into memory to guard+// against memory-exhaustion from oversized payloads.+const maxRequestBodyBytes = 1 << 20 // 1 MiB+func handleRegisterService(node *SamNode, w http.ResponseWriter, r *http.Request) {if r.Method != http.MethodPost {http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)return}+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read request body", http.StatusInternalServerError)@@ -281,6 +286,7 @@ func handleUnregisterService(node *SamNode, w http.ResponseWriter, r *http.Reque}var req api.ServiceInfo+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)if err := json.NewDecoder(r.Body).Decode(&req); err != nil {http.Error(w, "Invalid request body", http.StatusBadRequest)returndiff --git a/internal/node/stdio_bridge.go b/internal/node/stdio_bridge.goindex 2c2b1337..1e0a12db 100644--- a/internal/node/stdio_bridge.go+++ b/internal/node/stdio_bridge.go@@ -128,6 +128,7 @@ func (b *StdioBridge) ServeHTTP(w http.ResponseWriter, r *http.Request) {}}case http.MethodPost:+ r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)body, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "Failed to read body", http.StatusInternalServerError)