security

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package security provides common security limits and validation utilities for all JPEG family decoders.

Index

Constants

View Source
const (
	// MaxOutputSize is the maximum allowed decoded image size in bytes (512 MB).
	MaxOutputSize = 512 << 20

	// MaxImageWidth is the maximum allowed image width in pixels.
	MaxImageWidth = 65535

	// MaxImageHeight is the maximum allowed image height in pixels.
	MaxImageHeight = 65535

	// MaxPixelCount is the maximum number of pixels allowed (256 megapixels).
	MaxPixelCount = 256 << 20

	// MaxImagePixels is an alias for MaxPixelCount for compatibility.
	MaxImagePixels = MaxPixelCount

	// MaxIterations is the maximum iterations allowed per loop (1 million).
	MaxIterations = 1 << 20

	// MaxTileCount is the maximum number of tiles in tiled images.
	MaxTileCount = 65535

	// MaxDecompLevels is the maximum wavelet decomposition levels (JPEG 2000).
	MaxDecompLevels = 32

	// MaxChannels is the maximum number of color channels.
	MaxChannels = 256

	// MaxBitDepth is the maximum bits per sample.
	MaxBitDepth = 32

	// MaxMarkerLength is the maximum length for a single marker segment.
	MaxMarkerLength = 65535

	// MaxSegmentSize is an alias for MaxMarkerLength for compatibility.
	MaxSegmentSize = MaxMarkerLength

	// MaxCodeBlockSize is the maximum code-block dimension (JPEG 2000).
	MaxCodeBlockSize = 1024

	// MaxANSStateSize is the maximum ANS state table size (JPEG XL).
	MaxANSStateSize = 1 << 20

	// MaxHDRDynamicRange is the maximum dynamic range in stops for HDR images.
	MaxHDRDynamicRange = 30

	// MaxFloatExponent is the maximum float exponent allowed in HDR data.
	MaxFloatExponent = 127

	// MaxToneMapIterations is the maximum iterations for tone mapping operators.
	MaxToneMapIterations = 1000

	// MaxWaveletLevels is the maximum wavelet decomposition levels for JPEG XS.
	MaxWaveletLevels = 16

	// MaxSubbandSize is the maximum size of a single wavelet subband in bytes (64 MB).
	MaxSubbandSize = 64 << 20

	// MaxLineBufferSize is the maximum line buffer size for low-latency codecs (16 MB).
	MaxLineBufferSize = 16 << 20

	// Max4DDimensions is the maximum dimension size for 4D light field data.
	Max4DDimensions = 4096

	// MaxLightFieldViews is the maximum number of views in a light field image.
	MaxLightFieldViews = 65536

	// MaxPointCloudPoints is the maximum number of points in a point cloud (100 million).
	MaxPointCloudPoints = 100 << 20

	// MaxModelSize is the maximum neural network model size in bytes (256 MB).
	MaxModelSize = 256 << 20

	// MaxInferenceIterations is the maximum iterations during inference.
	MaxInferenceIterations = 10000

	// MaxTensorDimensions is the maximum number of dimensions in a tensor.
	MaxTensorDimensions = 8

	// MaxJUMBFBoxDepth is the maximum nesting depth for JUMBF boxes.
	MaxJUMBFBoxDepth = 32

	// MaxMetadataSize is the maximum metadata segment size in bytes (64 MB).
	MaxMetadataSize = 64 << 20

	// MaxLinkedReferences is the maximum number of linked references (JLINK).
	MaxLinkedReferences = 1024

	// MaxEventsPerFrame is the maximum events per frame in event-based imaging.
	MaxEventsPerFrame = 10 << 20

	// MaxEventStreamSize is the maximum event stream size in bytes (128 MB).
	MaxEventStreamSize = 128 << 20

	// MaxTimestampDelta is the maximum timestamp delta between events in microseconds.
	MaxTimestampDelta = 1 << 30
)

Security limits for all decoders. These constants prevent denial-of-service attacks from malformed images.

Variables

View Source
var (
	// ErrOutputTooLarge indicates the decoded output would exceed MaxOutputSize.
	ErrOutputTooLarge = errors.New("decoded output size exceeds maximum limit")

	// ErrDimensionsTooLarge indicates width or height exceeds maximum limits.
	ErrDimensionsTooLarge = errors.New("image dimensions exceed maximum limit")

	// ErrPixelCountTooLarge indicates total pixels exceed MaxPixelCount.
	ErrPixelCountTooLarge = errors.New("pixel count exceeds maximum limit")

	// ErrTooManyIterations indicates a loop exceeded MaxIterations.
	ErrTooManyIterations = errors.New("exceeded maximum iteration limit")

	// ErrTooManyTiles indicates tile count exceeds MaxTileCount.
	ErrTooManyTiles = errors.New("tile count exceeds maximum limit")

	// ErrInvalidDecompLevel indicates decomposition level exceeds MaxDecompLevels.
	ErrInvalidDecompLevel = errors.New("decomposition level exceeds maximum limit")

	// ErrTruncatedData indicates the input data is incomplete.
	ErrTruncatedData = errors.New("truncated or incomplete data")

	// ErrMalformedMarker indicates a marker segment is malformed.
	ErrMalformedMarker = errors.New("malformed marker segment")

	// ErrInvalidBitDepth indicates bit depth is outside valid range.
	ErrInvalidBitDepth = errors.New("invalid bit depth")

	// ErrZeroDimension indicates width or height is zero.
	ErrZeroDimension = errors.New("zero dimension not allowed")

	// ErrZeroChannels indicates number of channels is zero.
	ErrZeroChannels = errors.New("zero channels not allowed")

	// ErrInvalidCodeblock indicates code-block parameters are invalid.
	ErrInvalidCodeblock = errors.New("invalid code-block parameters")

	// ErrInvalidANSState indicates ANS decoder state is invalid.
	ErrInvalidANSState = errors.New("invalid ANS decoder state")

	// ErrInvalidHDRValue indicates an HDR value exceeds allowed limits.
	ErrInvalidHDRValue = errors.New("HDR value exceeds maximum limit")

	// ErrInvalidWaveletLevel indicates wavelet level exceeds maximum.
	ErrInvalidWaveletLevel = errors.New("wavelet level exceeds maximum limit")

	// ErrSubbandTooLarge indicates a wavelet subband exceeds size limit.
	ErrSubbandTooLarge = errors.New("wavelet subband exceeds maximum size")

	// ErrLineBufferTooLarge indicates line buffer exceeds size limit.
	ErrLineBufferTooLarge = errors.New("line buffer exceeds maximum size")

	// ErrTooManyViews indicates light field view count exceeds limit.
	ErrTooManyViews = errors.New("light field view count exceeds maximum limit")

	// ErrTooManyPoints indicates point cloud point count exceeds limit.
	ErrTooManyPoints = errors.New("point cloud point count exceeds maximum limit")

	// ErrModelTooLarge indicates neural network model exceeds size limit.
	ErrModelTooLarge = errors.New("neural network model exceeds maximum size")

	// ErrTensorDimensionsTooLarge indicates tensor has too many dimensions.
	ErrTensorDimensionsTooLarge = errors.New("tensor dimensions exceed maximum limit")

	// ErrJUMBFBoxTooDeep indicates JUMBF box nesting exceeds limit.
	ErrJUMBFBoxTooDeep = errors.New("JUMBF box nesting exceeds maximum depth")

	// ErrMetadataTooLarge indicates metadata segment exceeds size limit.
	ErrMetadataTooLarge = errors.New("metadata exceeds maximum size")

	// ErrTooManyReferences indicates linked references exceed limit.
	ErrTooManyReferences = errors.New("linked references exceed maximum limit")

	// ErrTooManyEvents indicates event count per frame exceeds limit.
	ErrTooManyEvents = errors.New("events per frame exceed maximum limit")

	// ErrEventStreamTooLarge indicates event stream exceeds size limit.
	ErrEventStreamTooLarge = errors.New("event stream exceeds maximum size")

	// ErrInvalidTimestampDelta indicates timestamp delta exceeds limit.
	ErrInvalidTimestampDelta = errors.New("timestamp delta exceeds maximum limit")
)

Common security errors.

Functions

func CheckBounds

func CheckBounds(index, length int) error

CheckBounds validates array access is within bounds.

func SafeRead

func SafeRead(data []byte, pos, length int) []byte

SafeRead returns bytes at position, or nil if out of bounds.

func SafeSlice

func SafeSlice(data []byte, start, end int) []byte

SafeSlice returns a safely bounded slice, preventing panics. If start or end are out of bounds, they are clamped to valid values.

func Validate4DDimension

func Validate4DDimension(dim int) error

Validate4DDimension checks if a 4D dimension value is within limits.

func ValidateBitDepth

func ValidateBitDepth(bitDepth int) error

ValidateBitDepth checks if bit depth is within valid range.

func ValidateDecompLevel

func ValidateDecompLevel(level int) error

ValidateDecompLevel checks if decomposition level is within limits.

func ValidateDimensions

func ValidateDimensions(width, height int) error

ValidateDimensions checks if image dimensions are within allowed limits.

func ValidateEventStreamSize

func ValidateEventStreamSize(size int64) error

ValidateEventStreamSize checks if event stream size is within limits.

func ValidateEventsPerFrame

func ValidateEventsPerFrame(count int) error

ValidateEventsPerFrame checks if event count per frame is within limits.

func ValidateFloatExponent

func ValidateFloatExponent(exponent int) error

ValidateFloatExponent checks if a float exponent is within limits.

func ValidateHDRDynamicRange

func ValidateHDRDynamicRange(dynamicRange float64) error

ValidateHDRDynamicRange checks if the HDR dynamic range is within limits.

func ValidateInferenceIterations

func ValidateInferenceIterations(iterations int) error

ValidateInferenceIterations checks if inference iteration count is within limits.

func ValidateJUMBFBoxDepth

func ValidateJUMBFBoxDepth(depth int) error

ValidateJUMBFBoxDepth checks if JUMBF box nesting depth is within limits.

func ValidateLightFieldViews

func ValidateLightFieldViews(viewCount int) error

ValidateLightFieldViews checks if light field view count is within limits.

func ValidateLineBufferSize

func ValidateLineBufferSize(size int64) error

ValidateLineBufferSize checks if a line buffer size is within limits.

func ValidateLinkedReferences

func ValidateLinkedReferences(count int) error

ValidateLinkedReferences checks if linked reference count is within limits.

func ValidateMetadataSize

func ValidateMetadataSize(size int64) error

ValidateMetadataSize checks if metadata size is within limits.

func ValidateModelSize

func ValidateModelSize(size int64) error

ValidateModelSize checks if neural network model size is within limits.

func ValidateOutputSize

func ValidateOutputSize(width, height, channels, bytesPerSample int) error

ValidateOutputSize checks if the decoded output size is within limits.

func ValidatePointCloudPoints

func ValidatePointCloudPoints(pointCount int64) error

ValidatePointCloudPoints checks if point cloud point count is within limits.

func ValidateSubbandSize

func ValidateSubbandSize(size int64) error

ValidateSubbandSize checks if a wavelet subband size is within limits.

func ValidateTensorDimensions

func ValidateTensorDimensions(dims int) error

ValidateTensorDimensions checks if tensor dimension count is within limits.

func ValidateTileCount

func ValidateTileCount(count int) error

ValidateTileCount checks if tile count is within limits.

func ValidateTimestampDelta

func ValidateTimestampDelta(delta int64) error

ValidateTimestampDelta checks if timestamp delta is within limits.

func ValidateToneMapIterations

func ValidateToneMapIterations(iterations int) error

ValidateToneMapIterations checks if tone mapping iteration count is within limits.

func ValidateWaveletLevel

func ValidateWaveletLevel(level int) error

ValidateWaveletLevel checks if wavelet level is within limits.

Types

This section is empty.

Jump to

Keyboard shortcuts

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