I am working on a script that will process user uploads to the server, and as an added layer of security I'd like to know:
Is there a way to detect a file's true extension/file type, and ensure that it is not another file type masked with a different extension?
Is there a byte stamp or some unique identifier for each type/extension?
I'd like to be able to detect that someone hasn't applied a different extension onto the file they are uploading.
Thank you,
Others have already mentioned FileInfo, which I think is the correct solution, but I'll add this just in case you can't use that one for some reason. Most (all?) *nix distros include a command called
file
that when run on a file will output its type. It has a switch to output in human readable format (default) or the MIME type. You could have your script invoke this program on the uploaded file and read the result. Again, this is not the preferred approach. If you're on Windows, this utility is available through Cygwin.Executables in general have a "signature" on the first bytes; I find it hard though to really ascertain what the file type really is.
What file types do you expect? Maybe you could check that it conforms to what you expect and reject everything else.
PHP has a superglobal $_FILES that holds information like size and file type. It looks like the type is taken form some sort of a header, not an extension, but I may be wrong.
There is an example of it on w3schools site.
I am going to test if it is can be tricked when I get a chance.
UPDATE:
Everyone else probably knew this, but $_FILES can be tricked. I was able to determine it this way:
It basically uses Unix's file command. There are probably better ways, but I haven't used PHP in a while. I usually avoid using system commands if possible.
PHP has a couple of ways of reading file contents to determine its MIME type, depending on which version of PHP you are using:
Have a look at the Fileinfo functions if you're running PHP 5.3+
Alternatively, check out mime_content_type for older versions.
Note that just validating the file type isn't enough if you want to be truly secure. Someone could, for example, upload a valid JPEG file which exploits a vulnerability in a common renderer. To guard against this, you would need a well maintained virus scanner.