Securing File Uploads: Validation and Sanitization
File upload functionality is one of the most common attack vectors in web applications. Learn how to validate, sanitize, and securely handle uploaded files.
Key Takeaways
- File uploads can be exploited in multiple ways: executing uploaded scripts (web shells), overwriting system files via path traversal, denial of service through massive files, and delivering malware to other users who download the files.
- ### File Storage Best Practices Never store uploaded files in the web server's document root — this prevents direct execution.
- ### Client-Side Validation (First Line, Not Security) Validate file type and size on the client side for user experience (instant feedback), but never rely on it for security.
- ### Client-Side Processing Advantage Browser-based tools that process files locally have an inherent security advantage — uploaded files never reach a server.
Password Generator
Generate strong, random passwords
The File Upload Threat Model
File uploads can be exploited in multiple ways: executing uploaded scripts (web shells), overwriting system files via path traversal, denial of service through massive files, and delivering malware to other users who download the files.
Client-Side Validation (First Line, Not Security)
Validate file type and size on the client side for user experience (instant feedback), but never rely on it for security. Client-side checks are trivially bypassed. They prevent honest mistakes, not attacks.
Server-Side Validation
Check the file's magic bytes (first few bytes that identify the format), not just the extension. A file named photo.jpg might actually be a PHP script. Validate file size against a reasonable maximum. Check image dimensions to prevent decompression bombs (a tiny file that expands to gigapixels). Scan for known malware signatures if accepting files from untrusted users.
File Storage Best Practices
Never store uploaded files in the web server's document root — this prevents direct execution. Store files outside the web root and serve them through a controller that sets appropriate Content-Type and Content-Disposition headers. Use randomized filenames to prevent enumeration and path traversal attacks. Preserve the original filename in metadata only.
Content-Type Headers
When serving uploaded files, set the Content-Type header based on your own validation, not the user-provided MIME type. Add Content-Disposition: attachment for file downloads to prevent the browser from rendering potentially dangerous content inline. Set X-Content-Type-Options: nosniff to prevent the browser from guessing the content type.
Client-Side Processing Advantage
Browser-based tools that process files locally have an inherent security advantage — uploaded files never reach a server. The files exist only in the browser's sandboxed memory during processing. This eliminates server-side file upload vulnerabilities entirely. Communicate this privacy benefit to users as a trust signal.
Outils associés
Guides associés
How to Check if Your Password Has Been Compromised
Data breaches expose millions of passwords regularly. Learn how to check whether your credentials have been leaked without risking further exposure, using k-anonymity-based services and local hash comparison.
Password Managers Compared: Features That Matter
A password manager is the single most impactful security tool for most people. This comparison covers the key features to evaluate when choosing a password manager for personal or team use.
How to Strip EXIF Metadata From Photos for Privacy
Photos contain hidden metadata including GPS coordinates, device info, and timestamps. Before sharing photos online, learn how to remove this data to protect your privacy and prevent location tracking.
Encryption Best Practices for Personal Data
Encryption protects your data from unauthorized access, whether stored on your devices or transmitted over the internet. This guide covers practical encryption strategies for personal data protection.
Troubleshooting SSL/TLS Certificate Errors
SSL/TLS certificate errors prevent secure connections and scare away visitors. This guide explains common certificate warnings, their causes, and step-by-step fixes for website operators and visitors.