shahzadbuckstec's avatar

Laravel upload image validation failed | hacking threat

Hi There,

I have a very serious issue, Laravel upload image validation not working I can still upload malicious files into the server with an extension "image.shtml". why did validation fail to deduct it?

here is my validation code:

   public function store(Request $request){

        $validator = Validator::make($request->all(),[
                'title' => 'required',
                'image' => 'image|mimes:jpeg,png,jpg,gif',
               
            ]);
        if ($validator->fails()) {
            return redirect('memberproduct/create')
                        ->withErrors($validator)
                        ->withInput();
        }
  }```
0 likes
8 replies
Snapey's avatar

is the file an image (according to its Mime type, not its filename)?

Snapey's avatar

checkout validation of mime types and mimes by file extension

mimetypes:text/plain,... The file under validation must match one of the given MIME types:

'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime' To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.

mimes:foo,bar,.. The file under validation must have a MIME type corresponding to one of the listed extensions.

shahzadbuckstec's avatar

@Snapey , please i need help,

I added these validation rules and now I'm unable to upload normal(png, jpeg) images.

what should I do now?

 $validator = Validator::make($request->all(),[
                'title' => 'required',
                'image' => 'mimetypes:jpeg,png,jpg',
            ]);

I got this error but I double check I'm uploading right file

The image must be a file of type: jpeg, png, jpg.

andylord565's avatar

change 'image' => 'mimetypes:jpeg,png,jpg', to 'image' => 'mimes:jpeg,png,jpg'

That should work

lostdreamer_nl's avatar

@shahzadbuckstec Are you testing your code on a linux server or locally on a windows PC?

It might just be that your setup is not capable of correctly determining the mime type of files.

shahzadbuckstec's avatar

Here is my server detail

CentOS Linux release 7.5.1804 (Core)

PHP 7.2.7 (cli) (built: Jul 17 2018 16:51:02) ( NTS )

laravel/framework v5.6.29

This code seems working but it allows the user to upload a file like "xyz.shtml".

'image' => 'mimes:jpeg,png,jpg'

Please or to participate in this conversation.