views:

148

answers:

2

This is not a real bug BUT for sure it is not what you would expect. I have this sample code to upload images:

if($type=="image/jpg" || $type=="image/jpeg" || $type=="image/pjpeg" || $type=="image/tiff" || $type=="image/gif" || $type=="image/png") {
   // make upload
else echo "Incorrect format ...."; 

The problem is that if I modify the extension of an image, let's say to .jpgq or even .jpg% and I try to upload it, FF and Chrome will say that the file"s type is "application/octet-stream" and normally the condition will be false.

IE, on the other hand, will say that the file's type is "image/jpeg", the condition will be true and the file will be uploaded. Of course, any browser trying to read the image later will not be able to do so.

It is not a bug because on msdn.microsoft.com it says that: "If the "suggested" (server-provided) MIME type is unknown (not known and not ambiguous), FindMimeFromData immediately returns this MIME type" and "If the server-provided MIME type is either known or ambiguous, the buffer is scanned in an attempt to verify or obtain a MIME type from the actual content."

My questions are:

  1. Why does IE / the server knows the real MIME type on upload BUT it will fail to read it from the server?
  2. How can I work around this issue (if the file doesn't have the right extension, the condition has to be false)? Is it wise to check the extension format (and not the MIME type)?
  3. is any of the above extensions not recomended to use? Should I add others?
+3  A: 

Forget checking the mime type. Use getimagesize() instead.

Pekka
Yes this it will work too, Thanks Pekka.
@silversky you're welcome. This will actually work way better, because it can't be forged by the user as the uploaded MIME type can - getimagesize() actually parses the uploaded file's header. Plus, you will always get reliable results. But then, of course, it will only work for images.
Pekka
not, still exactly what I'm looking. Because for example getimagesize('file.jpg%')[2] will still fire - 2 - wich of course is true BUT latter the browser will not be able to read the image.
Ahh @silversky now I see. I misread you there, I apologize. One solution might be sending along the correct `content-type` header when serving the file, but I don't know whether that is usable in your situation.
Pekka
probablly I could also add an regex to take the last 4 char and check as a string with jpeg, gif .... But i think I spent already to much time with this issue. i don't think that, anyone has time to add char's at the end of an image 'just for fun' and probablly this issue it's not a good solution for a hacker to cover something
@silversky I think it's an issue worth looking at. Imagine somebody uploading a EXE file that way that then ends up as a download when people visit the site. I would check the extension using Pathinfo http://de.php.net/pathinfo.
Pekka
But an EXE, will be able to pass as MIME type image ?
it's worth mentioning that with $_FILES['type'], FF and Chrome will not validate an image.jpgw or image image.jpg% ........ as a image type
@silversky an attacker can fake the incoming MIME type, which is why it's deemed unreliable.
Pekka
so should I use a regex to check the last 4 char or is any other better solution?
@silversky best use `pathinfo` that I linked to above.
Pekka
yes pathinfo('fileName')['extension'] and in_array($ext, $accepted) will do the trick.do you think it is safe enough to check only the extention or is better for any case, to still check the type also ?
@silversky that depends on what you're going to do with the image. If you are going to serve it back to people, checking the type is certainly a good idea.
Pekka
@Pekka Thank you very much for your help and sugestions (I'll probably check for type also)
A: 

For performance reasons your webserver doesn't usually inspect the file for it's mimetype, it usually only uses the extension.

Therefore on upload you need to read the mimetype and then save the file with an extension appropriate for the mimetype if you wish the webserver to directly serve the file. The alternative is to use a download wrapper that reads the mimetype from the file and passes it onto the client.

Basic example,

/* verify and sanitize any file extension from mimetype
 */
    switch($subtype) {
    case 'pjpeg':
    case 'jpeg':
        if (!preg_match('/\.jp(e)?g$/i', $real_name)) {
            $real_name .= '.jpg';
        }
        break;

    default:
        if (!preg_match('/\.'.$subtype.'$/i', $real_name)) {
            $real_name .= ".$subtype";
        }
        break;
    }
Steve-o
If I understand right your suggestion is to extract the extention with regex but I think it's faster and cleaner to use pathinfo () build in fn.