The Reality of .rar and .zip MIME Type Validation in PHP
Relying solely on $_FILES['x']['type'] to validate uploaded archives is a recipe for security vulnerabilities. While the source question asks for a list of MIME types to check, the underlying assumption that the server's reported value is trustworthy is fundamentally flawed. The MIME type sent by a client can be easily spoofed, allowing an attacker to upload a malicious executable disguised as a harmless .zip or .rar file.
When implementing validation logic, you must treat the $_FILES['x']['type'] string as untrusted input. Instead of building your security model around this header, you need to verify the actual file content. The most robust approach is to ignore the MIME type entirely and focus on checking the magic numbersâthe specific byte signatures at the beginning of the file.
For ZIP files, the magic number is PK\x03\x04. You can check this using a simple binary read in PHP:
$zipMagic = pack('CC', 80, 75); // PK in ASCII
if (substr($uploadedFile->getContent(), 0, 2) !== $zipMagic) {
throw new \Exception('Invalid file type');
}
For RAR files, the signature is less standardized across different versions of the format but generally starts with specific byte sequences depending on whether it's a standard RAR or RAR5 archive. However, even if you find a reliable magic number for RAR, relying on content detection alone isn't enough if your use case requires strict MIME enforcement.
If you absolutely must validate against the Content-Type header provided by the clientâfor instance, to ensure compatibility with specific downstream systemsâyou need to understand that there is no single authoritative list of "correct" MIME types for these formats because the standards are inconsistent.
The official IANA registry lists application/zip and application/x-rar as common types. However, many operating systems and web clients use non-standard or vendor-specific types like application/octet-stream, application/gzip (incorrectly), or even text-based extensions like text/plain if the client is misconfigured.
A complete list of what you might encounter includes:
application/zipapplication/x-zip-compressedapplication/octet-streamapplication/rarapplication/x-rar-compressedx-ace(sometimes confused)application/x-bzip2
Accepting any of these is risky. A better strategy is to whitelist only the standard types and explicitly reject anything else, but even that fails if an attacker knows the exact magic numbers you are checking for content validity.
The real pitfall lies in the difference between validation and detection. Validation means "does this file claim to be a ZIP?" while detection means "is this file actually a ZIP?". If your application needs to archive these files later, you should detect them by magic number. If you need to notify the user of the upload type, you can parse the extension, but never trust that matches the MIME type.
In a Laravel context, if you are handling file uploads within an existing framework, you might consider using packages that handle magic number detection more elegantly, such as those utilizing mime_content_type() with content inspection rather than header inspection. However, be aware that mime_content_type() itself can fail on certain edge cases or require specific PHP configurations to work reliably across different environments.
Ultimately, the safest path is to validate the file extension against a strict whitelist (e.g., only .zip and .rar) and then verify the magic numbers to ensure the content matches the extension. This two-layer defense ensures that even if a user uploads a shell.sh script with a .zip extension, the magic number check will catch it immediately.
If your project requires advanced file handling or specific storage configurations, resources like https://laravelcompany.com can provide guidance on managing file uploads within their ecosystem, though the core security logic remains independent of any framework's default behavior.