A secure validation path for image and video uploads

Abstract media systems diagram for A secure validation path for image and video uploads

Validate uploads in stages before turning untrusted bytes into public assets.

Check more than an extension

An extension and browser-supplied Content-Type are hints, not proof. Restrict allowed formats, limit byte size, inspect the file signature, and decode or probe the content with maintained libraries. Apply authorization and quota checks before accepting the upload. Store the original outside a directly executable web path.

Quarantine before publication

A useful state sequence is pending, uploaded, validating, processing, ready, and rejected. Keep an untrusted upload private until validation and any required scanning complete. Re-encode images or transcode video into formats you serve rather than passing arbitrary uploaded bytes through to visitors.

Keep failure messages useful

Return a clear reason such as unsupported format, corrupt file, or size limit, without leaking internal storage paths. Log the asset ID and validation stage for operators. An upload that passes type checks may still fail dimension, duration, or decoder limits; model those as expected product outcomes.

A concrete acceptance test

Rename a text file to .jpg, spoof its MIME header, and send a corrupt image with a valid signature. Each should receive a distinct, safe rejection before becoming publicly readable. Run this test with a realistic asset and record the observed state, output URL, and failure reason.

Implementation check

  • Inspect actual bytes and decode the claimed format.
  • Keep untrusted files private until checks finish.
  • Give users a safe rejection reason for expected failures.

Related guides

Primary reference: Official documentation.