---
title: "10. Preventing file upload vulnerabilties"
date: 2024-12-04
draft: false
---


**Extension validation**
While whitelisting extensions is always more secure, as we have seen previously, it is recommended to use both by whitelisting the allowed extensions and blacklisting dangerous extensions. This way, the blacklist list will prevent uploading malicious scripts if the whitelist is ever bypassed (e.g. `shell.php.jpg`)

Example PHP code
```php
$fileName = basename($_FILES["uploadFile"]["name"]);

// blacklist test
if (preg_match('/^.+\.ph(p|ps|ar|tml)/', $fileName)) {
    echo "Only images are allowed";
    die();
}

// whitelist test
if (!preg_match('/^.*\.(jpg|jpeg|png|gif)$/', $fileName)) {
    echo "Only images are allowed";
    die();
}
```

blacklist checks: 
if ext exists anywhere in filename
whitelist checks: 
if filename ends with extension 

**Content validation**
Always validate both extension and its' content. 
```php
$fileName = basename($_FILES["uploadFile"]["name"]);
$contentType = $_FILES['uploadFile']['type'];
$MIMEtype = mime_content_type($_FILES['uploadFile']['tmp_name']);

// whitelist test
if (!preg_match('/^.*\.png$/', $fileName)) {
    echo "Only PNG images are allowed";
    die();
}

// content test
foreach (array($contentType, $MIMEtype) as $type) {
    if (!in_array($type, array('image/png'))) {
        echo "Only PNG images are allowed";
        die();
    }
}
```
- validate both file signature and HTTP Content-Type header

**Upload disclosure**

Avoid disclosing the uploads directory location or providing direct access to the uploaded files. 
Always recommended to hide upload directory from end-users and only allow them to download the uploaded files through a download page 

Write a download.php script to fetch the requested file from uploads and then download the file for the end user, this way the directory is hidden 
- significantly reduces the chances of accessing a maliciously uploaded script to execute code 

Make sure that the download.php script only grants access to files owned by the user who uploaded it. (i.e. avoiding IDOR/LFI)  and users do not have access to the upload directory receiving a 403 error. 
- Content-Disposition header and nosniff 

In addition - randomize names of uploaded files in storage + store their `sanitized` names in a database. when download.php needs to download a file, it fetches its' original name from the db + provides it at download time for the user. 
- this way, user knows neither upload directory nor uploaded file name. 
- also allows us to avoid vulns caused by injections in filenames 


Store uploaded files in separate server or container. 
- if attker gains RCE it only compromises the upload server and not the entire backend 
- furthermore webservers can be configured to prevent webapplications from accessing files outside of their restricted directories by using configurations like `open_basedir` in php 

---
**Disabling functions that enable RCE**
- In PHP for example - `disable_functions` configurations in `php.ini` + add such dangerous functions for example `exec`, `shell_exec` , `system`, `passthru` and there's a few others. 

- disable showing any system or server errors to avoid sensitive info disclosure 
	- handle errors at webapp level + print out simple errors that explain the error w/o disclosing any sensitive/specific details 

few other tips: 
- limit file size
- update used libraries 
- scan uploaded files for malware or malicious strings
- utilize a web application firewall as a secondary layer of protection (WAF) 

