magic

package
v1.801.476 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 1 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 magic identifies an image format from the bytes themselves.

This is an ALLOW-LIST, not a detector. It exists because the only safe answer to "what type is this upload?" is one derived from the stored bytes: a filename, a Content-Type part header and a client's word are all attacker-chosen, and a crafted .svg/.html served back under the type its NAME claimed executes script in the viewer's origin. So a caller serves what Type() returns and nothing else — "" means "not a raster image I will render", and the caller's job is then to serve it inert (application/octet-stream + attachment) or refuse it outright.

The four formats here are the ones a browser renders as a picture and cannot be talked into treating as a document. SVG is deliberately absent and must stay absent: it is XML with <script> in it, so it is a program, not a picture.

Deterministic and dependency-free — deliberately NOT net/http.DetectContentType, whose table sniffs HTML/XML and evolves between Go releases, which is exactly the unpinned behavior a security allow-list must not inherit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Type

func Type(data []byte) string

Type returns the canonical MIME type of a recognized raster image, or "" for everything else. The signature checks are length-guarded, so a short or empty input is simply unrecognized rather than a panic.

Types

This section is empty.

Jump to

Keyboard shortcuts

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