(Note · · 6 min read)
Accepting file uploads without handing over your server
Every upload feature accepts arbitrary bytes from an unknown person and writes them somewhere your application can reach. Most of the defences people add — checking the extension, trusting the content type — are checks on values the uploader chose, which makes them decoration.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Nothing the client tells you is evidence
The filename, the extension and the declared content type all arrive from the client and can all say anything. A file called photo.jpg is not a photo because it ends in jpg; it is whatever its bytes are.
Determine the type from the content itself, and accept only an explicit list of types you intend to support. A deny-list is a list of the attacks you have thought of; an allow-list is a list of what your product actually needs.
Never store under the name you were given
A supplied filename can contain path segments that climb out of your upload directory, characters that mean something to a shell, or a name that collides with an existing file. Generate your own identifier for storage and keep the original name as a display label only, separate from the path.
Resolve the final path and confirm it sits inside the directory you intended before writing. That one check removes an entire class of traversal bugs.
Serve uploads from somewhere that cannot execute
The serious failure is an uploaded file being executed by the server. Keep uploads outside the application's served directories entirely, ideally in object storage on a different hostname, and serve them with a content type you decided rather than one derived from the file.
Send them with a disposition that prompts download rather than rendering where that suits, and set headers that stop browsers guessing a type you did not intend. A hostname separate from the application also limits what a file rendered in a browser can reach.
Bound the size before you accept the bytes
Enforce a maximum at the point of receiving, not after the file is in memory or on disk, and set it at the proxy as well as in the application. Otherwise a single upload decides how much memory your process uses.
Compressed formats need their own limit on what they expand to. An archive or image that is small on disk and enormous once decoded is a standard way to exhaust a server, and the size check you did on the upload says nothing about it.
Process away from the request, carefully
Resizing, converting and extracting text are slow and are performed by libraries parsing untrusted input, so do them in a background job rather than in the request, with a timeout and a memory limit. A malformed file should fail one job, not take down an instance.
Treat anything extracted from the file as untrusted too. Text pulled out of a document and shown back to a user is user input with extra steps, and if it is fed to a model it becomes a channel for instructions you did not write.
Decide who may read it
An unguessable URL is not access control. If an upload is private, authorise each request for it, or issue short-lived signed links rather than permanent ones, so a URL shared once does not grant access forever.
Common questions
Is checking the file extension enough to validate an upload?
No. The filename, extension and declared content type all come from the client and can say anything. Determine the type from the file's own bytes and accept only an explicit allow-list of types your product needs.
Should uploaded files keep their original filename?
Not for storage. A supplied name can contain path segments that escape the upload directory or characters meaningful to a shell. Generate your own storage identifier, keep the original as a display label, and confirm the resolved path sits inside the intended directory.
Where should uploaded files be stored?
Outside any directory the application serves, ideally in object storage on a separate hostname, served with a content type you chose rather than one derived from the file. The serious failure is an uploaded file being executed by the server.
Why limit what a compressed file expands to?
Because a small archive or image can decode to something enormous, exhausting memory or disk. The size check performed on the upload says nothing about its expanded size, so set a separate limit and process files in a bounded background job.