decodebudget

package
v1.54.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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.

func (JSON) Validate

func (b JSON) Validate(data []byte) error

Validate accepts exactly one complete JSON value, including scalar roots.

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

func (b Proto) Unmarshal(data []byte, msg proto.Message) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL