archiveio

package
v1.105.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package archiveio detects and walks the archive containers Chainloop accepts as material values (zip, tar, tar.gz), enforcing the guards that keep a hostile archive from exhausting the host: entry-count and uncompressed-size limits, and rejection of paths that escape the extraction root.

It is a leaf package so that both the material crafters (which validate an archive from a path on disk) and the policy-input projections (which aggregate an archive already held in memory as material bytes) can share one implementation of those guards. Duplicating them would risk the two paths disagreeing about what is safe.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooManyEntries is returned when an archive has more qualifying entries
	// than the configured maximum.
	ErrTooManyEntries = errors.New("archive exceeds the maximum number of entries")
	// ErrArchiveTooLarge is returned when the running uncompressed size of an
	// archive exceeds the configured maximum.
	ErrArchiveTooLarge = errors.New("archive exceeds the maximum uncompressed size")
	// ErrUnsafeEntry is returned when an archive entry's path is absolute or escapes the extraction root.
	ErrUnsafeEntry = errors.New("unsafe entry path in archive")
)

Functions

func WalkBytes

func WalkBytes(data []byte, format Format, limits Limits, yield func(name string, r io.Reader) error) error

WalkBytes is WalkPath for an archive already held in memory. It applies the same guards, so a projection reading recorded material bytes cannot be tricked by an archive a crafter would have rejected.

func WalkPath

func WalkPath(p string, format Format, limits Limits, yield func(name string, r io.Reader) error) error

WalkPath calls yield for every regular file in the archive at p, enforcing limits and skipping directories, symlinks, hardlinks, empty entries, and path-traversal entries.

Types

type Format

type Format int

Format identifies a supported archive container.

const (
	None Format = iota
	Zip
	Tar
	TarGz
)

func DetectBytes

func DetectBytes(data []byte) Format

DetectBytes reports the container format of an in-memory blob from its magic bytes alone, returning None when the bytes are not a supported archive. It is the counterpart to DetectPath for callers that already hold the content, such as a policy-input projection working from recorded material bytes.

func DetectFile

func DetectFile(p string) (Format, error)

DetectFile reports the container format of the file at p from its content alone, ignoring its name. A non-archive returns (None, nil).

Prefer this over DetectPath when the same content will later be detected from bytes with DetectBytes and the two decisions must agree — for example a crafter validating a value that a policy-input projection will re-read from the recorded material. DetectPath trusts the filename first, so a name can disagree with the content: a zip carrying a prepended stub still opens (its central directory is at the end) but no longer starts with the zip magic, so DetectPath calls it an archive and DetectBytes does not. Detecting from content on both sides makes such a value fail loudly at craft time instead of being accepted and then silently failing to project.

func DetectPath

func DetectPath(p string) (Format, error)

DetectPath reports whether path is a supported archive and, if so, its format. Detection is by extension first; for files whose extension does not match, magic bytes are used as a backstop so renamed archives are still caught. A non-archive returns (None, nil).

type Limits

type Limits struct {
	MaxEntries   int
	MaxTotalSize int64
}

Limits bounds archive expansion to guard against zip bombs.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the safe defaults: 10000 entries and 1 GiB total uncompressed size.

Jump to

Keyboard shortcuts

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