By submitting your personal information, you agree to receive emails regarding relevant products and special offers from TechTarget and its partners. You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

Ask a question

The ability to upload files on a website is a common feature, often used to enable users or customers to upload documents and images. While this is useful in many situations, the security implications of hosting a file-upload facility are significant. Here are some file upload security best practices.

Malicious file uploadsAn ordinary user may use the facility to upload the type of files expected. However, an attacker could take advantage of the facility with malicious file uploads.

There are two fundamental ways a website can be attacked by a file upload. The first way involves the type of file uploaded. A file could overwrite another file that already exists with the exact same name on the server. If this were a critical file, the new file could cause the website to function incorrectly, or not at all. The new file could be used to deface the website by replacing an existing page, or it could be used to edit the list of allowed file types in order to make further attacks simpler.

The second way a website could be attacked by a malicious file upload involves the content of the uploaded file. The uploaded file could contain malicious code in the form of an exploit, virus, Trojan or malware, which could be used to gain control of the Web server. For example, it is possible to hide PHP code inside an image file and still have it appear to be an image. When the image is opened, it also executes the code hidden in the file. The file could contain scripts or tags that exploit other well-known Web application vulnerabilities, such as cross-site scripting (XSS).

Alternatively, the file space of the Web server could be exhausted by the attacker uploading a huge file.

If the uploaded file can be accessed by entering a specific URL path, it could be especially dangerous because the file could be executed immediately after uploading.

Defending against file upload attacksThere are six steps to protecting a website from file-upload attacks.

The application should use a whitelist of allowed file types. This list determines the types of files that can be uploaded, and rejects all files that do not match approved types.

The application should use client- or server-side input validation to ensure evasion techniques have not been used to bypass the whitelist filter. These evasion techniques could include appending a second file type to the file name (e.g. image.jpg.php) or using trailing space or dots in the file name.

The application should set a maximum length for the file name, and a maximum size for the file itself.

The directory to which files are uploaded should be outside of the website root.

The application should not use the file name supplied by the user. Instead, the uploaded file should be renamed according to a predetermined convention.

While these techniques cannot guarantee a website will never be attacked from a malicious file upload, they will go a long way toward protecting the website while still providing users with the benefits of uploading files when needed.