infer
A small Rust crate that infers a file's true type from its magic-number byte signature, not its extension
Repository Health
Technical Analysis
infer determines a file’s real type by inspecting the first bytes of its content against a built-in table of magic-number signatures, covering images, video, audio, archives, documents, fonts, and executables. Because it reads the byte signature rather than trusting a filename extension or user-supplied MIME type, it is commonly used to validate uploaded files server-side where an attacker could otherwise rename a malicious file to look like an innocuous one.
The crate supports both a std mode (reading from a File or byte slice, including container formats like CFB via the cfb dependency) and a no_std/alloc mode for embedded or constrained environments, and exposes a small, extensible Matcher type so applications can register custom signatures for formats infer doesn’t recognize out of the box.
What You Get
- Magic-number-based type detection covering images, video, audio, archives, documents, fonts, and executables
- get() / get_from_path() functions returning a Type with extension and MIME type
- is_*() convenience predicates (is_image, is_video, is_archive, etc.) for common category checks
- Custom Matcher registration for detecting formats not covered by the built-in signature table
- no_std + alloc compatibility for use in embedded or WASM contexts
Common Use Cases
- Validating uploaded file content server-side so an attacker can’t bypass extension-based checks by renaming a malicious file
- Sniffing the real type of a file whose extension is missing, wrong, or untrusted
- Routing files to type-specific processing pipelines (image resizing, video transcoding, document parsing) based on detected type rather than extension
- Adding detection for a proprietary or uncommon binary format via a custom Matcher
Under The Hood
Architecture - The crate centers on a Type enum returned by get()/get_from_path(), backed by a matchers/ directory splitting signature-matching logic by category (image, video, audio, archive, doc, font, app, text, odf, book), each exposing small matcher functions checked in priority order against the byte buffer; map.rs wires these matchers to their corresponding extension/MIME-type metadata.
Tech Stack - Pure Rust, no_std-compatible by default with std/alloc as opt-in Cargo features; the only optional dependency is cfb (Compound File Binary format support, e.g. for legacy MS Office formats), pulled in only under the std feature so the no_std build stays dependency-free.
Code Quality - The tests/ and testdata/ directories contain real sample files per supported format used to assert correct detection, and the matcher functions are small, single-purpose, and easy to audit individually; the crate has maintained a steady, if modest, contribution and release cadence.
API Design - The primary entry points (infer::get(buf), infer::get_from_path(path), and category predicates like infer::is_image(buf)) require no configuration and return a simple Option<Type> or bool, making the common case a one-line call; extending detection with a custom Matcher is a small amount of additional code, keeping the API approachable for both the simple and the extensible use case.
Used by 6 apps in this directory
GitButler
Developer Tools · Devops · AI Development
Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.
Hoppscotch
Developer Tools
A lightweight, offline-capable API development ecosystem for testing HTTP, GraphQL, WebSocket, MQTT, and SSE endpoints across web, desktop, and CLI.
Jan
AI Assistants
Run LLMs 100% locally with full privacy, or connect to cloud AI — your machine, your data, your control.
Lemmy
Community · Social Media
Federated, self-hosted Reddit alternative with full community ownership and no corporate control.
Meetily
Productivity · AI Assistants
Privacy-first AI meeting assistant that transcribes and summarizes your meetings entirely on your local machine — no cloud, no data leakage.
Stalwart
Collaboration
All-in-one secure mail and collaboration server covering IMAP, JMAP, SMTP, CalDAV, CardDAV, and WebDAV in a single memory-safe Rust binary.