File Upload
Last updated
Last updated
This page contains recommendations for the implementation of secure file upload functionality.
Process files with well-known and up-to-date libraries and frameworks.
Implement a comprehensive validation of each uploaded file, which must include at least:
Follow file storage best practices, see the File storage section.
Set file size limits and implement upload rate limits, see the Size and rate limits section.
Implement protection against path traversal attacks, see the Vulnerability Mitigation: Path Traversal page.
Log successful and unsuccessful file upload attempts and access to files events, see the Logging and Monitoring page.
Implement Content Disarm and Reconstruction (CDR) for potentially dangerous file types, see the Content Disarm and Reconstruction (CDR) section.
Comply with requirements from the Error and Exception Handling page.
Implement a comprehensive input validation for file names, see the Input Validation page.
Enforce a limit to file name. For example, require the length of a file name to be between 1
and 64
characters.
Define an allow-list of characters for file names. The allow list must consist of alphanumeric characters, hyphens, spaces, and period characters.
Implement a comprehensive input validation for file extensions, see the Input Validation page.
Define an allow list of extensions. Do not use block list validation.
Avoid uploading files with potentially dangerous extensions, see the Dangerous file type section.
Define an allow list of MIME types
and validate the Content-Type
HTTP header against this list. Do not use a block list validation.
Implement MIME Sniffing to extract the effective MIME type of a file's content.
Avoid uploading files with potentially dangerous MIME types, see the Dangerous file types section.
The Content-Type
header can be easily manipulated by a user and spoofed to bypass Content-Type
verifications. Therefore, it can not be fully trusted. However, there is a content sniffing algorithm known as MIME Sniffing that provides a more in-depth Content-Type
validation and can be used as a workaround for this issue.
The following libraries can be used to implement MIME sniffing:
libmagic library
Use Universal Unique Identifier (UUID) as a name for uploaded files, see the Cryptography: Universal Unique Identifier (UUID) page.
Store uploaded files outside of the webroot folder.
Store uploaded files separate from application content and code. For example, use a separate volume or a third-party storage.
Define an appropriate directory structure accordingly to your file system.
Set up file storage and uploaded files with the minimum necessary set of permissions according to the purpose of use.
Reset permissions for all uploaded files.
Make sure the storage is encrypted at rest.
Make sure there is a backup strategy for the storage.
Make sure there is storage capacity monitoring.
If file execution is needed, the file must be scanned with an anti-malware solution and executed only within a separate and sandboxed environment.
Use third-party services to manage file-uploading functionality.
Use a separate domain/service to store/host uploaded files.
Enforce a strict Content Security Policy to the service that hosts uploaded files, see the Content Security Policy (CSP) page.
Scan every uploaded file with an anti-malware solution such as VirusTotal.
If files need to be modified by an application (on the user's update request as an example), set up versioning on files.
Allow file uploads only for authenticated users.
Validate a user has the necessary permissions to upload files.
Comply with the requirements from the Authorization page.
Implement protection against Cross-Site Request Forgery (CSRF) attacks for file upload functionality, see the Vulnerability Mitigation: Cross-Site Request Forgery (CSRF) page.
Allow access to uploaded files only for registered users.
Do not expose uploaded files to anonymous users.
Set a limit on the size of the uploaded file and validate that the file size does not exceed the specified limit before handling it.
Use the Content-Length
header to identify a file size.
Do not rely on user-controlled parameters such as form values to identify a file size.
Do not rely on chunked transfer encoding to upload files.
Limit the number of uploaded files per each request.
Limit the amount of file upload requests (single or multiple upload file requests) a single user can perform within a reasonable fixed period of time according to an application and user needs.
Many dangerous file types can be used to attack an application and pose risks for the entity that receives and stores such files and users that access them. Content Disarm and Reconstruction (CDR) techniques allow you to mitigate these risks.
Implement CDR on images before any further processing, see the CDR Images section.
Implement CDR on multimedia files before any further processing, see the CDR Multimedia files section.
Implement CDR on XML and HTML files before any further processing, see the CDR Content and Markup Languages section.
Implement CDR on compressed files before any further processing, see the CDR Compressed files section.
Implement CDR on Microsoft Office documents and PDFs before any further processing, see the CDR Documents section.
Use a third-party solution to perform CDR on known dangerous files before any further processing.
Image uploading may lead to different security threats like the following ones:
Embedding malicious code into metadata.
Cross-Site Scripting or XML external entity (XXE) injection through SVG images.
Memory leaks due to image error processing.
Remote code execution due to improper image processing.
Create an allow list of image formats and work only with images of these formats.
Avoid processing SVG
files.
If it is necessary to work with SVG
files implement extra protection layers to reduce the blast radius:
Implement SVG sanitization, see DOMPurify.
Host images on a separate domain, see the File storage section.
Enforce a strict Content Security Policy to that domain, see the Content Security Policy (CSP) page.
Use well-known and up-to-date libraries to process images.
Process images (cropping, resizing, etc.) in a sandboxed environment.
Remove metadata from images such as EXIF metadata.
Insert random noise into image content, see the Cryptography: Random Generators page.
Multimedia file uploading may lead to different security threats like the following ones:
Memory leaks due to multimedia file error processing.
Remote code execution due to improper multimedia file processing.
Server-side Request Forgery due to improper multimedia file processing.
Use well-known and up-to-date libraries to process audio and video files.
Process audio and video files in a sandboxed environment.
XML and HTML file uploading may lead to different security threats like the following ones:
Arbitrary JavaScript execution via malicious HTML.
Arbitrary JavaScript execution via malicious CSS.
XML External Entity injection via malicious XML.
Avoid processing HTML
files.
If it is necessary to work with HTML
files implement extra protection layers to reduce the blast radius:
Implement HTML sanitization, see DOMPurify.
Use a separate domain to store/host uploaded HTML files, see the File storage section.
Enforce strict Content Security Policy to that domain, see the Content Security Policy (CSP) page.
Comply with the requirements from the Vulnerability Mitigation: XML External Entity (XXE) page for XML files.
Compressed file uploading may lead to different security threats like the following ones:
Denial of service due to unpacking a Zip bomb file.
Path traversal during unpacking compressed files leads to overwriting existing files.
Accessing restricted files due to improper handling of symbolic links in compressed files.
Verify decompress output size before unpacking files.
Remove symbolic links from the unpacked files.
Reset permissions for all unpacked files.
Make sure unpacking does not overwrite existing files.
Validate each unpacked file according to the requirements on this page.
Comply with requirements from the Vulnerability Mitigation: Path Traversal page.
Microsoft and PDF document uploading may lead to different security threats like the following ones:
Execution of arbitrary JavaScript embedded into a PDF document.
Embedding malicious files to a PDF document.
Remote code execution due to improper PDF processing.
Malicious macros, Flash, OLE objects or HTA Handlers in Microsoft documents (Word, Excel or PowerPoint).
Scan Microsoft and PDF documents with an anti-malware solution such as VirusTotal.
Processing some types of files is potentially dangerous because it can easily lead to a vulnerability. It is recommended to avoid processing such files and block the upload of any files with the appropriate extensions. If it is necessary to accept any of these files, it is highly recommended to handle such files in a sandbox, implement extra layer protections (like Content Security Policy) and perform Content Disarm and Reconstruction (CDR) to reduce the blast radius.
Below you can find a list of potentially dangerous files. This list is not exhaustive.
Extensions
.config
, .ini
, .htaccess
, .htpasswd
, .xml