The PHP documentation for is_uploaded_file says:
Returns TRUE if the file named by filename was uploaded via HTTP POST. This is useful to help ensure that a malicious user hasn't tried to trick the script into working on files upon which it should not be working--for instance,
/etc/passwd.
It also suggests this:
For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.
But as far as I know tmp_name isn't user controlled anyways, so the check shouldn't be required.
And if I use name instead of tmp_name when uploading files (for example by using copy instead of move_uploaded_file), my file upload script wouldn't actually work, as it would always move the wrong file.
My questions:
- The comments mention that
move_uploaded_fileperforms theis_uploaded_filecheck itself, it that true? - Is
tmp_nameuser controlled in any way? - Is there a realistic scenario where
is_uploaded_fileis actually required when uploading a file - or when performing any other action such as reading, deleting, etc. - , as there would be a vulnerability without it? Or is the function completely useless?
is_uploaded_fileis not called? – tim Aug 24 '16 at 09:50is_uploaded_filemethod is to check wheter the given file was uploaded via a HTTP POST request. This can help determine the user is not user other methods like $_GET or $_SESSION key to pass in a filename (if your script supports that at all). – Oldskool Aug 24 '16 at 12:04is_uploaded_fileonly works if I pass intmp_name, right? If that's the only parameter that makes sense, and iftmp_nameis never user-controlled, I already know that that file is uploaded, so there shouldn't be a need foris_uploaded_file. Or am I misunderstanding something completely? – tim Aug 24 '16 at 14:18is_uploaded_filechecks a local file on the server on which PHP is ran. It determines if that file ended up on the server by a HTTP POST request. It can basically be any filename, as long as it's local. So if you check a value that could potentially be altered by an attacker, it at least verified if it was POSTed to the server and not uploaded in another way (like through a backdoor), which could allow an attacker to trigger your upload handling on his custom file. So it's a very specific use case. In any reasonable script I don't see any added value. At least not in PHP5+. – Oldskool Aug 24 '16 at 15:10