filemagic

package
v1.0.126 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package filemagic does magic-byte detection for archive identification and polyglot detection — examining the first bytes of a response body to catch archives disguised as safe types and Content-Type/content mismatches. It is a self-contained leaf (stdlib only) extracted from package main per ADR-0002.

This complements extension-based blocking — magic bytes can't be faked by renaming a file or manipulating the URL path. (The glue that consults the file blocker, IsBlockedArchive, stays in package main since it needs the fileBlocker singleton.)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckVsContentType

func CheckVsContentType(data []byte, contentType string) string

CheckVsContentType detects polyglot files by comparing the Content-Type header against the actual content's magic bytes. Returns a non-empty reason when a mismatch indicates a potentially disguised file (e.g., a PE served as image/png).

Types

type Sig

type Sig struct {
	Offset  int    // byte offset where the magic starts
	Magic   []byte // expected bytes at that offset
	Type    string // detected file type (e.g., "PE", "ZIP")
	Ext     string // canonical extension (e.g., ".exe", ".zip")
	Archive bool   // true if this is an archive/container format
}

Sig describes a file type signature (magic bytes).

func Detect

func Detect(data []byte) *Sig

Detect examines the first bytes of data and returns the detected file signature, or nil if no known signature matches.

Jump to

Keyboard shortcuts

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