---
title: "Sample stored file bytes before tightening upload validation by magic bytes"
url: "https://www.dariuszparys.com/notes/sample-stored-file-bytes-before-tightening-upload-validation"
author: "Dariusz Parys"
published: 2026-11-16T00:00:00.000Z
description: "A stricter upload check based on file signatures would have rejected every upload from the current client. Reading the first bytes of the newest stored blobs showed that before release."
---

# Sample stored file bytes before tightening upload validation by magic bytes

## Context

A security fix stopped trusting the client-declared Content-Type of image uploads. Instead it read the file signature and rejected anything that wasn't PNG or JPEG. It looked safe. Its evidence came only from the _declared_ type of stored blobs, which were all `application/octet-stream`, and that says nothing about the actual bytes.

## Insight

Reading the first 16 bytes of the newest stored blobs showed what the clients actually send. The current mobile client had switched to a proprietary raw capture format, with a custom ASCII magic and a fixed size, while older uploads were PNG. The new check would have rejected **every** upload from the current client with a 400.

**Before tightening upload validation, range-read the headers of real stored objects.**

```sh
az storage blob download --account-name <account> -c <container> -n <blob> \
  --auth-mode login --start-range 0 --end-range 15 -f head.bin
xxd head.bin
```

Sample the newest objects, not random ones: formats change with client releases.

## Policy that came out of it

- **Raw/capture image types:** accept any bytes, but store and serve them as `application/octet-stream` with `Content-Disposition: attachment`, so they never render.
- **Display image types:** must be PNG or JPEG by signature.
- **Ignore the declared part type.** The bytes alone decide, so `image/jpg`, `image/pjpeg` or a missing type don't reject valid files.
- **Type served responses from the bytes, not the stored type.** Legacy blobs stored with the wrong type then come back correctly.
- **Check existence (404) and duplicates (409) before format (400),** so clients retrying an upload that was stored before the change still get the response they treat as done.
