Skip to content
Back to blog
Guides

Secure File Upload Handling

How to handle user file uploads without introducing remote code execution, path traversal, or storage abuse vulnerabilities.

S
SecureScout Team· Security Engineering
May 15, 20265 min read

A file upload feature touches several distinct risk categories at once — what gets uploaded, where it's stored, and how it's later served back — each needing its own specific handling.

Validate content, not just filename

An extension check (.jpg, .pdf) validates a filename, not the actual file. An attacker can rename anything to have a trusted-looking extension. Validate actual file content — checking magic bytes or using a proper content-sniffing library — before trusting what type of file you've received.

Don't trust the client-supplied filename

User-supplied filenames can contain path traversal sequences (../../etc/passwd) or unexpected characters. Generate your own filename (e.g. a UUID) for storage, and keep the original filename only as metadata if needed for display, never as part of a filesystem path.

Store uploads outside the web root

Uploaded files should generally not be stored somewhere directly executable or accessible via a predictable URL under your application's own domain. Store them in dedicated storage (object storage, or a directory the web server doesn't execute code from) and serve them through a controlled path.

Serve uploads from a separate origin

If a user can upload an HTML file, an SVG (which can contain embedded scripts), or similar content, and it's later served from your main application's domain, it can execute in that origin's security context — effectively a self-inflicted XSS vector. Serving uploaded content from a separate domain or subdomain isolates this risk.

Enforce size and type limits server-side

Client-side validation is a convenience, not a control — it's trivially bypassed. Enforce file size limits and allowed type restrictions on the server, where they can't be skipped.

Scan for malware where relevant

If uploaded files will be shared with or downloaded by other users, scanning uploads for known malware signatures adds a meaningful layer of protection, particularly for platforms accepting a wide range of file types from untrusted users.

Rate limit upload endpoints

Without limits, an upload endpoint can be abused for storage exhaustion or cost abuse, independent of any content-based vulnerability — treat it like any other resource-intensive endpoint from a rate-limiting perspective.

file-uploadweb-securityinput-validation

Frequently Asked Questions

Is checking a file's extension enough to validate its type?

No. A file extension is just a name — an attacker can rename a malicious executable to end in .jpg. Validate actual file content (e.g. via magic bytes or a content-type sniffing library), not just the filename.

Should uploaded files be served from the same domain as the main application?

Generally no. Serving user-uploaded content from a separate domain (or subdomain) prevents an uploaded file — like malicious HTML or SVG containing a script — from executing in the context of your main application's origin.

Related Articles

S

SecureScout Team

Security Engineering

Learn more →