From c1775621079f03f3ad11d3b43e3506e6099d046a Mon Sep 17 00:00:00 2001 From: water <672684719@qq.com> Date: Wed, 29 Jul 2026 11:24:37 +0800 Subject: [PATCH] fix: limit request body size in BSON binder --- binding/bson.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/binding/bson.go b/binding/bson.go index 464890f0..37736d06 100644 --- a/binding/bson.go +++ b/binding/bson.go @@ -5,6 +5,7 @@ package binding import ( + "errors" "io" "net/http" @@ -18,11 +19,14 @@ func (bsonBinding) Name() string { } func (b bsonBinding) Bind(req *http.Request, obj any) error { - buf, err := io.ReadAll(req.Body) - if err == nil { - err = b.BindBody(buf, obj) + body, err := io.ReadAll(io.LimitReader(req.Body, MaxBodySize+1)) + if err != nil { + return err } - return err + if int64(len(body)) > MaxBodySize { + return errors.New("request body too large") + } + return b.BindBody(body, obj) } func (bsonBinding) BindBody(body []byte, obj any) error {