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:
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.
Define an allow list of extensions. Do not use block list validation.
Define an allow list of MIME types
and validate the Content-Type
HTTP header against this list. Do not use a block list validation.
The following libraries can be used to implement MIME sniffing:
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.
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.
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.
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.
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:
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.
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:
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.
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).
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
Follow file storage best practices, see the section.
Set file size limits and implement upload rate limits, see the section.
Implement protection against path traversal attacks, see the page.
Log successful and unsuccessful file upload attempts and access to files events, see the page.
Implement Content Disarm and Reconstruction (CDR) for , see the section.
Comply with requirements from the page.
Implement a comprehensive input validation for file names, see the page.
Implement a comprehensive input validation for file extensions, see the page.
Note that input data must be normalized before any comparison, validation or processing. If normalization is not done properly, allow list validation can be bypassed easily, check the page.
Avoid uploading files with potentially dangerous extensions, see the section.
Implement to extract the effective MIME type of a file's content.
Avoid uploading files with potentially dangerous MIME types, see the 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 that provides a more in-depth Content-Type
validation and can be used as a workaround for this issue.
library
Function at package
library
wrapper for
Use Universal Unique Identifier (UUID) as a name for uploaded files, see the page.
Enforce a strict Content Security Policy to the service that hosts uploaded files, see the page.
Scan every uploaded file with an anti-malware solution such as .
Comply with the requirements from the page.
Implement protection against Cross-Site Request Forgery (CSRF) attacks for file upload functionality, see the page.
Do not rely on to upload files.
Using chunked transfer encoding is also not recommended, because there is no way to identify a file size. Moreover, there is that abuse differences in parsing requests with Content-Length
and Transfer-Encoding
headers to smuggle HTTP requests.
Implement CDR on images before any further processing, see the section.
Implement CDR on multimedia files before any further processing, see the files section.
Implement CDR on XML and HTML files before any further processing, see the section.
Implement CDR on compressed files before any further processing, see the section.
Implement CDR on Microsoft Office documents and PDFs before any further processing, see the section.
Implement SVG sanitization, see .
Host images on a separate domain, see the section.
Enforce a strict Content Security Policy to that domain, see the page.
Insert random noise into image content, see the page.
The security threats for processing audio and video files are similar to those described in the section.
Implement HTML sanitization, see .
Use a separate domain to store/host uploaded HTML files, see the section.
Enforce strict Content Security Policy to that domain, see the page.
Comply with the requirements from the page for XML files.
Comply with requirements from the page.
Scan Microsoft and PDF documents with an anti-malware solution such as .