File Signatures: A Useful Clue, Not Proof That an Upload Is Safe

Editorial illustration.

Originally published 27 June 2023; rewritten and reviewed on 15 October 2026.

An uploaded file arrives named report.pdf, and the browser sends Content-Type: application/pdf. Neither of those values is trustworthy on its own—both come straight from the client and take two seconds to spoof. Reading the opening bytes of the file (its magic number or file signature) catches obvious mismatches, such as someone renaming shell.exe to avatar.jpg.

What it cannot do is prove that the file is safe, complete, or even the exact format you think it is.

Know what the prefix can tell you

The original 2023 version of this post listed fifty file extensions in a giant table. I replaced that inventory with a compact reference below because maintaining a huge magic-byte lookup table creates false confidence: many modern formats share container prefixes and cannot be told apart from the first four bytes alone.

Format or family Typical opening bytes in hexadecimal
JPEG FF D8 FF
PNG 89 50 4E 47 0D 0A 1A 0A
GIF 47 49 46 38 37 61 or 47 49 46 38 39 61
PDF 25 50 44 46 2D
ZIP family Often 50 4B 03 04; other ZIP record forms exist
GZIP 1F 8B
Windows executable family 4D 5A, followed by further structural checks

When debugging an upload issue locally on Linux or macOS, you can inspect those opening bytes directly before writing any code:

xxd -l 16 -g 1 suspect-upload.pdf
file --mime-type -b suspect-upload.pdf

Inspect the container when formats overlap

The biggest trap with magic-byte checks is assuming a 1-to-1 match between a signature and an extension.

.docx, .xlsx, and .pptx are all ZIP archives under the hood. Every single one of them begins with 50 4B 03 04—the exact same bytes as a .zip, .jar, or .apk. If your upload endpoint only allows .docx resumes and checks only the first four bytes, an attacker can upload a .jar or a zip bomb renamed to cv.docx and your header check will wave it right through.

The same overlap happens with RIFF containers: .webp images and .wav audio files both start with 52 49 46 46 (RIFF), and you have to inspect bytes 8–11 (WEBP vs WAVE) to tell them apart. Meanwhile, plain text formats like .csv, .json, .txt, and .svg have no universal magic number at all—and .svg is actually an XML document that can embed <script> tags and trigger stored XSS if you serve it inline from your application domain.

When implementing a header check in C# (for example in an ASP.NET Core upload handler), be careful with short or empty files. A classic bug is allocating a fixed buffer, reading from the stream without checking how many bytes were actually returned, and comparing against uninitialised zero bytes or throwing an unhandled exception:

private static readonly byte[] PdfHeader = [0x25, 0x50, 0x44, 0x46, 0x2D]; // %PDF-

public static async Task<bool> HasPdfHeaderAsync(Stream stream, CancellationToken ct)
{
    if (!stream.CanSeek || stream.Length < PdfHeader.Length)
        return false;

    stream.Position = 0;
    var buffer = new byte[PdfHeader.Length];
    var bytesRead = await stream.ReadAsync(buffer, ct);
    stream.Position = 0;

    return bytesRead == PdfHeader.Length && buffer.AsSpan().SequenceEqual(PdfHeader);
}

Checking stream.Length and bytesRead ensures a 0-byte or 2-byte upload fails cleanly instead of sneaking past or crashing the request.

Make the check one layer

A signature check works best as a cheap, early filter—rejecting obvious garbage before you spend CPU parsing a file. For production uploads, pair it with the rest of the pipeline:

  1. Enforce an allowlist of extensions and strict size limits at both the reverse proxy (Nginx client_max_body_size) and the application layer.
  2. Parse or re-encode untrusted media. For images, running uploads through a library like ImageSharp or SkiaSharp and saving a fresh copy strips trailing payloads and catches malformed headers. For Office documents or archives, validate the internal structure and cap decompression ratios to prevent zip bombs.
  3. Never trust the user’s filename or path. Generate a random UUID filename on the server, store the original display name in the database if needed, and save uploads outside wwwroot or your web root.
  4. Control how files are served back. Serve user uploads from a separate domain or object storage bucket (such as Cloudflare R2), set X-Content-Type-Options: nosniff, and use Content-Disposition: attachment for anything that does not need to render inline.

For a complete server-side pattern in .NET, consult Microsoft’s file-upload guidance. Treat magic bytes as the bouncer at the door, not the entire security team.