Documentation
¶
Overview ¶
Package decodebudget bounds untrusted input before decoding. Proto checks protobuf and protojson input by field count, nesting depth and estimated decoded allocation; JSON checks plain JSON by token count and depth. It depends only on the protobuf runtime so callers need not import utils.
Index ¶
- Variables
- type JSON
- type Proto
- func (b Proto) Estimate(data []byte, desc protoreflect.MessageDescriptor) (int, error)
- func (b Proto) EstimateProtoJSON(data []byte, desc protoreflect.MessageDescriptor) (int, error)
- func (b Proto) Unmarshal(data []byte, msg proto.Message) error
- func (b Proto) Validate(data []byte, desc protoreflect.MessageDescriptor) error
- func (b Proto) ValidateProtoJSON(data []byte, desc protoreflect.MessageDescriptor) error
Constants ¶
This section is empty.
Variables ¶
var ErrMessageComplexity = errors.New("message exceeds structural complexity limit")
ErrMessageComplexity indicates that a message exceeds its structural budget.
Functions ¶
This section is empty.
Types ¶
type JSON ¶
type JSON struct {
// MaxTokens includes scalar values, object keys, and opening/closing delimiters.
MaxTokens int
// MaxDepth counts nested arrays/objects, including a root container at depth one.
MaxDepth int
}
JSON limits JSON structure before a downstream decoder constructs collections. Callers must separately limit the serialized byte size. Both limits must be positive; the zero value does not disable validation.
type Proto ¶
type Proto struct {
// MaxFields counts every wire-field occurrence and each packed scalar value,
// including occurrences of singular and unknown fields.
MaxFields int
// MaxDepth counts nested messages, including the root message at depth one.
MaxDepth int
// MaxDecodedBytes bounds the estimated bytes the decoder allocates: the Go
// struct of each message, copied strings and bytes, oneof wrappers, and
// slice and map growth. Zero leaves it unchecked.
MaxDecodedBytes int
}
Proto limits allocation-driving protobuf structure before decoding. Callers must separately limit the serialized byte size. MaxFields and MaxDepth must be positive; the zero value does not disable validation.
func (Proto) Estimate ¶
func (b Proto) Estimate(data []byte, desc protoreflect.MessageDescriptor) (int, error)
Estimate validates data like Validate and returns the estimated bytes the binary decoder allocates for it, including the root message, decoding into desc's generated type, or into a dynamicpb message when desc has none.
func (Proto) EstimateProtoJSON ¶
func (b Proto) EstimateProtoJSON(data []byte, desc protoreflect.MessageDescriptor) (int, error)
EstimateProtoJSON validates protojson input like ValidateProtoJSON and returns the estimated bytes protojson allocates decoding it. Malformed or mistyped input is left for the decoder to reject.
func (Proto) Unmarshal ¶
Unmarshal validates the budget before decoding into a non-nil mutable message. It discards unknown fields, uses MaxDepth as the decoder recursion limit, and retains the binary decoder's required-field checks. Preflight rejection leaves the destination unchanged; other decode errors follow normal protobuf semantics. A destination without a generated fast path, such as a dynamicpb message, is estimated as one.
func (Proto) Validate ¶
func (b Proto) Validate(data []byte, desc protoreflect.MessageDescriptor) error
Validate checks data against the message descriptor without constructing the decoded message. Known extensions use protoregistry.GlobalTypes, matching the default protobuf decoder. Legacy protobuf groups are deliberately unsupported.
func (Proto) ValidateProtoJSON ¶
func (b Proto) ValidateProtoJSON(data []byte, desc protoreflect.MessageDescriptor) error
ValidateProtoJSON checks protojson input against the budget in protobuf terms: an object key or array element is a field, and nesting is message depth.