I know that you can never be sure that you have done enough to be secure, and I also know that file uploading is hard to make correct. Before asking this question I read some of the related posts here like risk of php image upload, what steps should be taken to validate... and security risks of uploading.
So I think that I have done everything that is needed, but I would be very grateful if someone can take a look at what I have and tell if I have done enough. So I am running the latest PHP 5.5.9-1+sury.org~saucy+1
on Apache/2.4.7
. And my uploading method looks as follows:
public static function uploadTemp($number, $file){
//check if the filename exist and upload was without an error
if (!$file['name'] || $file['error']){
return false;
}
// check if extension is valid
if (!Helper::validExtension($file['name'])){
return false;
}
// check is the size of file is valid
if ($file['size'] > (1024 * 1024 * 6) || $file['size'] < 1024 * 10){
return false;
}
// no need to upload images less than 50x50. Also $file['size'] can be spoofed
$imageSize = getimagesize($file['tmp_name']);
if ($imageSize === false || $imageSize[0] < 50 || $imageSize[1] < 50){
return false;
}
require_once('SimpleImage.php');
$image = new SimpleImage();
$image->load($file['tmp_name']);
// saving a file to a temporary directory and renaming it.
$image->save(Image::$tempDir.$number.'.jpg');
return true;
}
SimpleImage is an open source tool for manipulating an image, inside of a tool I changed only one thing (function save to save every file with 644 permissions). My $number
is a string which is a concatenation of random number and a current timestamp, $file = $_FILES['fileToUpload']
and validExtension looks the following way:
public static function validExtension($filename){
$extensions = array('jpg', 'jpeg', 'png');
$arr = explode('.', $filename);
if ( in_array(strtolower(end($arr)), $extensions) ){
return true;
} else return false;
}
My temporary folder has 755 permissions.
So my question is: am I missing something here or is there I way I can improve things:
- may be restrict further permissions (I do not need to do anything with images except to view them by the client. The folder is used only for image uploading)
- may be changing some parameters in php.ini or apache