gin/binding/bson.go
elix3r 98bc3d1347 fix(binding): add optional size limit for BSON and Protobuf bodies
BSON and Protobuf binders used io.ReadAll with no cap. Add
binding.MaxBodyBytes (default 0, unlimited) and a shared reader so
apps can bound those binders without changing historical defaults.
Oversized bodies return *http.MaxBytesError, which MustBindWith
already maps to HTTP 413.

Fixes #4759

Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
2026-08-27 07:30:16 +05:30

30 lines
572 B
Go

// Copyright 2025 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package binding
import (
"net/http"
"go.mongodb.org/mongo-driver/v2/bson"
)
type bsonBinding struct{}
func (bsonBinding) Name() string {
return "bson"
}
func (b bsonBinding) Bind(req *http.Request, obj any) error {
buf, err := readBody(req.Body)
if err == nil {
err = b.BindBody(buf, obj)
}
return err
}
func (bsonBinding) BindBody(body []byte, obj any) error {
return bson.Unmarshal(body, obj)
}