After the user uploads an image to the server, should we sanitize $_FILES['filename']['name']?
I do check file size/file type etc. But I don’t check other things. Is there a potential security hole?
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Absolutely! As @Bob has already mentioned it’s too easy for common file names to be overwritten.
There are also some issues that you might want to cover, for instance not all the allowed chars in Windows are allowed in *nix, and vice versa. A filename may also contain a relative path and could potentially overwrite other non-uploaded files.
Here is the
Upload()method I wrote for the phunction PHP framework:The important parts are:
array_map('basename', ...), this makes sure that the file doesn’t contain any relative paths.ph()->Text->Slug(), this makes sure only.0-9a-zA-Zare allowed in the filename, all the other chars are replaced by underscores (_)md5_file(), this is added to the filename iff another file with the same name already existsI prefer to use the user supplied name since search engines can use that to deliver better results, but if that is not important to you a simple
microtime(true)ormd5_file()could simplify things a bit.Hope this helps! =)