Merge 3f68b0b0b42901e2d897ad5b7d26c6952d6e8ccb into dcaa4296d111981ffb31ac3eba90bb63e1eb5ab9

This commit is contained in:
water 2026-08-19 23:09:58 -07:00 committed by GitHub
commit 12e65cd9c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 6 deletions

View File

@ -125,3 +125,9 @@ func validate(obj any) error {
}
return Validator.ValidateStruct(obj)
}
// MaxBodySize is the maximum request body size in bytes that BSON and
// Protobuf binders will read. If the request body exceeds this limit,
// the binding returns an error. Defaults to 32 MB.
var MaxBodySize int64 = 32 << 20

View File

@ -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 {

View File

@ -19,11 +19,14 @@ func (protobufBinding) Name() string {
}
func (b protobufBinding) Bind(req *http.Request, obj any) error {
buf, err := io.ReadAll(req.Body)
body, err := io.ReadAll(io.LimitReader(req.Body, MaxBodySize+1))
if err != nil {
return err
}
return b.BindBody(buf, obj)
if int64(len(body)) > MaxBodySize {
return errors.New("request body too large")
}
return b.BindBody(body, obj)
}
func (protobufBinding) BindBody(body []byte, obj any) error {