← PHP / Laravel · Contents

PHP-014 / Beginner

Trusting only uploaded file extension or MIME type

A04:2021 Insecure Design · CWE-434 Unrestricted Upload of File with Dangerous Type

Why is this a problem?

Client-supplied Content-Type and extensions can be forged; bypasses such as .php.jpg also exist.

How to fix it

php
$request->validate(['img' => 'required|image|mimes:jpg,png|max:2048']);
$path = $request->file('img')->store('avatars', 'private'); // Random filename, private disk

Further reading