Blog | G5 Cyber Security

File Upload Security: Extension Blacklisting

TL;DR

Blacklisting file extensions is a common but often insufficient security measure for file uploads. Attackers can easily bypass it by renaming files or using multiple extensions. A better approach combines whitelisting, content inspection, and other validation techniques.

1. Why Blacklisting Fails

Blacklisting relies on blocking known dangerous file types (e.g., .exe, .php, .sh). However:

Therefore, relying solely on blacklisting creates a false sense of security.

2. The Right Approach: Whitelisting

Whitelisting is far more secure. Instead of blocking bad extensions, you only allow specific, safe ones (e.g., .jpg, .png, .pdf). This prevents unexpected file types from being uploaded.

3. Implementing Whitelisting in Code

Here’s an example using PHP:

Important: Always convert the filename to lowercase using strtolower() before comparing it to your whitelist. This avoids case-sensitivity issues.

4. Beyond Extensions: Content Inspection

Even whitelisting isn’t foolproof. A malicious actor could try to embed code within an allowed file type. Content inspection helps detect this:

5. Additional Security Measures

Exit mobile version