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: 3 Imported by: 0

Documentation

Overview

Package security provides security validation for JPEG 2000 parsing

Index

Constants

View Source
const (
	// MaxImageWidth is the maximum image width
	MaxImageWidth  = 65535
	MaxImageHeight = 65535
	MaxImageArea   = 268435456 // 256 megapixels

	// MaxComponents is the maximum number of components
	MaxComponents = 16384

	// MaxTileWidth is the maximum tile width
	MaxTileWidth  = 65535
	MaxTileHeight = 65535
	MaxTiles      = 65535

	// MaxDecompositionLevels is the maximum number of decomposition levels
	MaxDecompositionLevels = 32
	MaxResolutionLevels    = 33

	// MaxCodeBlockSize is the maximum code block size (Width * Height)
	MaxCodeBlockSize = 4096
	MaxCodeBlockDim  = 64 // Max dimension in either direction

	// MaxPrecinctSize is the maximum precinct size
	MaxPrecinctSize = 32768

	// MaxQualityLayers is the maximum number of quality layers
	MaxQualityLayers = 16384

	// MaxSegmentLength is the maximum segment length (64 MB)
	MaxSegmentLength = 67108864

	// MaxBoxNestingDepth is the maximum box nesting depth to prevent infinite loops
	MaxBoxNestingDepth = 32
	MaxMarkerSegments  = 65535
	MaxIterations      = 1000000
)

Security limits to prevent resource exhaustion attacks

Variables

View Source
var (
	ErrImageTooLarge       = errors.New("image dimensions exceed maximum allowed")
	ErrTooManyComponents   = errors.New("number of components exceeds maximum")
	ErrTooManyTiles        = errors.New("number of tiles exceeds maximum")
	ErrTileTooLarge        = errors.New("tile dimensions exceed maximum")
	ErrTooManyLayers       = errors.New("number of quality layers exceeds maximum")
	ErrCodeBlockTooLarge   = errors.New("code block size exceeds maximum")
	ErrPrecinctTooLarge    = errors.New("precinct size exceeds maximum")
	ErrTooManyDecompLevels = errors.New("decomposition levels exceed maximum")
	ErrSegmentTooLarge     = errors.New("segment length exceeds maximum")
	ErrIntegerOverflow     = errors.New("integer overflow detected")
	ErrNestingTooDeep      = errors.New("box nesting depth exceeds maximum")
	ErrInvalidDimensions   = errors.New("invalid dimensions (zero or negative)")
	ErrInvalidBitDepth     = errors.New("invalid bit depth")
)

Errors

Functions

func SafeAdd

func SafeAdd(a, b int) (int, bool)

SafeAdd performs addition with overflow checking

func SafeMultiply

func SafeMultiply(a, b int) (int, bool)

SafeMultiply performs multiplication with overflow checking

func SafeSubtract

func SafeSubtract(a, b int) (int, bool)

SafeSubtract performs subtraction with overflow checking

func ValidateCOD

func ValidateCOD(decompositionLevels, codeBlockWidth, codeBlockHeight int) error

ValidateCOD validates COD marker parameters

func ValidateSIZ

func ValidateSIZ(width, height, tileWidth, tileHeight, xOffset, yOffset, components int) error

ValidateSIZ validates SIZ marker parameters

Types

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator provides security validation for JPEG 2000 parsing

func NewValidator

func NewValidator() *Validator

NewValidator creates a new security validator

func (*Validator) CheckIteration

func (v *Validator) CheckIteration() error

CheckIteration guards against infinite loops

func (*Validator) CountSegment

func (v *Validator) CountSegment() error

CountSegment tracks number of marker segments parsed

func (*Validator) EnterBox

func (v *Validator) EnterBox() error

EnterBox tracks box nesting depth

func (*Validator) ExitBox

func (v *Validator) ExitBox()

ExitBox decrements box nesting depth

func (*Validator) Reset

func (v *Validator) Reset()

Reset resets all validator state

func (*Validator) ResetIteration

func (v *Validator) ResetIteration()

ResetIteration resets the iteration counter

func (*Validator) SetMaxMemory

func (v *Validator) SetMaxMemory(max uint64)

SetMaxMemory sets the maximum memory allocation limit

func (*Validator) TrackPixels

func (v *Validator) TrackPixels(width, height, components int) error

TrackPixels tracks total pixels for memory accounting

func (*Validator) ValidateAllocation

func (v *Validator) ValidateAllocation(size uint64) error

ValidateAllocation checks if a memory allocation is safe

func (*Validator) ValidateBitDepth

func (v *Validator) ValidateBitDepth(depth int) error

ValidateBitDepth validates component bit depth

func (*Validator) ValidateCodeBlockSize

func (v *Validator) ValidateCodeBlockSize(width, height int) error

ValidateCodeBlockSize validates code block dimensions

func (*Validator) ValidateDecompositionLevels

func (v *Validator) ValidateDecompositionLevels(levels int) error

ValidateDecompositionLevels validates the number of decomposition levels

func (*Validator) ValidateImageSize

func (v *Validator) ValidateImageSize(width, height int) error

ValidateImageSize validates image dimensions

func (*Validator) ValidateNumComponents

func (v *Validator) ValidateNumComponents(n int) error

ValidateNumComponents validates the number of components

func (*Validator) ValidateNumLayers

func (v *Validator) ValidateNumLayers(layers int) error

ValidateNumLayers validates the number of quality layers

func (*Validator) ValidateNumTiles

func (v *Validator) ValidateNumTiles(n int) error

ValidateNumTiles validates the number of tiles

func (*Validator) ValidatePrecinctSize

func (v *Validator) ValidatePrecinctSize(width, height int) error

ValidatePrecinctSize validates precinct dimensions

func (*Validator) ValidateSegmentLength

func (v *Validator) ValidateSegmentLength(length int) error

ValidateSegmentLength validates marker segment length

func (*Validator) ValidateTileSize

func (v *Validator) ValidateTileSize(width, height int) error

ValidateTileSize validates tile dimensions

Jump to

Keyboard shortcuts

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