sanitize

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 21 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 sanitize is the L1 ingest filter for uploaded editions: cheap structural checks that the bytes match the declared format and aren't a zip bomb or polyglot. It does not strip active content (PDF JavaScript, EPUB scripts) — that's L2, a separate package when we add it.

Validate is called from the upload handler before the file is content- addressed and streamed to storage. It takes a ReadSeeker (multipart.File satisfies this) so EPUB validation can read the zip central directory at the file's tail without buffering the whole upload in memory.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported format")
	ErrFormatMismatch    = errors.New("file content does not match declared format")
	ErrInvalidContent    = errors.New("file content is invalid for declared format")
	ErrZipBomb           = errors.New("archive exceeds safety limits")
	// ErrActiveContent fires when an upload contains scripting or other
	// active payloads we won't carry: scripts in an EPUB, embedded
	// executables, etc. PDFs don't return this — they're silently stripped
	// in Sanitize. Maps to HTTP 400 at the handler.
	ErrActiveContent = errors.New("file contains active content (scripts, executables, etc.)")
)
View Source
var ErrTooComplex = errors.New("file is too large or complex to process")

ErrTooComplex is returned when a file can't be sanitized within the isolation limits: the sanitizer ran out of its memory budget or time. A PDF whose compressed object streams inflate to gigabytes lands here. Maps to HTTP 413 at the handler.

Functions

func DisableIsolation

func DisableIsolation()

DisableIsolation reverts to in-process sanitization. For tests.

func EnableIsolation

func EnableIsolation(cfg IsolationConfig) error

EnableIsolation makes PDF sanitization run in a separate process with the given limits, so a hostile PDF can exhaust only the child's budget, not the server. Call once at startup. Without it, PDFs are sanitized in-process.

func RunChildIfRequested

func RunChildIfRequested()

RunChildIfRequested turns this process into a sanitizer child when the parent started it in child mode, and never returns in that case. Otherwise it returns immediately. Call it first thing in main (and in TestMain of any test binary that enables isolation).

func Sanitize

func Sanitize(format string, rs io.ReadSeeker, size int64) (io.Reader, error)

Sanitize is L2: produce a safe-to-store version of an already-Validated upload. For PDFs we re-serialize with active content (JavaScript actions, embedded files, XFA forms, etc.) stripped; the returned reader holds bytes that differ from the input — the new sha256 is what gets stored. For EPUBs we strict-reject any script-bearing entry and pass through otherwise. For TXTs there's nothing to do — passthrough.

rs must be seeked to 0 (Validate leaves it there on success). On success the returned reader is positioned at 0 and the caller streams it into storage exactly the way it would have streamed the original.

func SanitizeContext

func SanitizeContext(ctx context.Context, format string, rs io.ReadSeeker, size int64) (io.ReadCloser, error)

SanitizeContext is Sanitize for the upload path. It returns a ReadCloser the caller must Close, since the cleaned bytes may live in a temp file. When EnableIsolation has been called, PDFs are sanitized in a child process under its memory, time and concurrency limits; ctx cancels the wait for a slot and the child itself.

func Validate

func Validate(format string, rs io.ReadSeeker, size int64) error

Validate checks that rs's bytes are a well-formed instance of format (case-insensitive — "pdf", "PDF", "Pdf" all accepted). On success rs is left seeked to 0 so the caller can stream it straight into storage. On failure the seek position is undefined.

size is the declared content length, used only as a hint for the EPUB reader; if you don't have it, pass -1 and the validator will Seek to discover it.

Types

type IsolationConfig

type IsolationConfig struct {
	// MemoryLimit is the extra address space, in bytes, the child may map on
	// top of what it starts with. Enforced hard on Linux (RLIMIT_AS) and as a
	// soft GC target elsewhere. 0 means no limit.
	MemoryLimit int64
	// Timeout bounds one sanitization, including time spent waiting for a
	// slot. 0 means no timeout.
	Timeout time.Duration
	// MaxConcurrent caps how many children run at once, so worst-case memory
	// is MaxConcurrent × MemoryLimit. Values < 1 are treated as 1.
	MaxConcurrent int
	// Executable is the binary re-run in child mode. Empty = os.Executable().
	// That binary must call RunChildIfRequested first thing in main (or
	// TestMain).
	Executable string
}

IsolationConfig bounds out-of-process PDF sanitization.

Directories

Path Synopsis
Package sanitizetest holds fixtures for tests of internal/sanitize and its callers.
Package sanitizetest holds fixtures for tests of internal/sanitize and its callers.

Jump to

Keyboard shortcuts

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