Input Validation / Upload of Unexpected File Types
Description
Upload of Unexpected File Types is an input validation vulnerability, also known as CWE-434. It is categorized as an A2 vulnerability in the OWASP Top 10 Web Application Security Risks and is a type of injection vulnerability. This vulnerability occurs when an application allows users to upload unexpected file types, either through a web interface or API. This type of upload can lead to a variety of malicious activities, including code injection, cross-site scripting, and malware propagation.
Risk
This vulnerability can have serious impacts on an application, as it can be used to inject malicious code and spread malicious software throughout an application. It can also be used to gain access to sensitive information, such as usernames and passwords. Additionally, it can lead to the unauthorized access of valuable resources and data.
Solution
The best way to mitigate this vulnerability is to implement a whitelisting approach to input validation. This means that the application should only allow certain file types to be uploaded and should reject all other file types. Additionally, the application should also validate the contents of the uploaded files to ensure that they do not contain malicious code or malware.
Example
The following code is an example of a vulnerable application that allows a user to upload an unexpected file type:
<?php
$filename = $_FILES['userfile']['name'];
$destination = "/uploads/" . $filename;
move_uploaded_file($_FILES['userfile']['tmp_name'], $destination);
?>
In the above code, the application does not validate the file type that the user is uploading, allowing any type of file to be uploaded. As a result, it is possible for a malicious user to upload a file containing malicious code or malware.