Merge 6cfbe5a1e8cce8c6f09cdc592f42147e1ae3052a into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
water 2026-10-12 01:21:27 +07:00 committed by GitHub
commit 9b843b0ec1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 3 deletions

View File

@ -26,6 +26,11 @@ const (
MIMEBSON = "application/bson"
)
// DefaultMaxBodySize is the maximum request body size (in bytes) that the BSON,
// Protobuf, and Plain binders will read. Requests with a body larger than this
// limit return an error. Set to 0 to disable the limit.
const DefaultMaxBodySize = 32 << 20 // 32 MB
// Binding describes the interface which needs to be implemented for binding the
// data present in the request such as JSON request body, query parameters or
// the form POST.

View File

@ -18,8 +18,11 @@ func (bsonBinding) Name() string {
}
func (b bsonBinding) Bind(req *http.Request, obj any) error {
buf, err := io.ReadAll(req.Body)
buf, err := io.ReadAll(io.LimitReader(req.Body, DefaultMaxBodySize+1))
if err == nil {
if len(buf) > DefaultMaxBodySize {
return http.ErrBodyReadAfterClose
}
err = b.BindBody(buf, obj)
}
return err

View File

@ -16,10 +16,13 @@ func (plainBinding) Name() string {
}
func (plainBinding) Bind(req *http.Request, obj any) error {
all, err := io.ReadAll(req.Body)
all, err := io.ReadAll(io.LimitReader(req.Body, DefaultMaxBodySize+1))
if err != nil {
return err
}
if len(all) > DefaultMaxBodySize {
return http.ErrBodyReadAfterClose
}
return decodePlain(all, obj)
}

View File

@ -19,10 +19,13 @@ func (protobufBinding) Name() string {
}
func (b protobufBinding) Bind(req *http.Request, obj any) error {
buf, err := io.ReadAll(req.Body)
buf, err := io.ReadAll(io.LimitReader(req.Body, DefaultMaxBodySize+1))
if err != nil {
return err
}
if len(buf) > DefaultMaxBodySize {
return http.ErrBodyReadAfterClose
}
return b.BindBody(buf, obj)
}