ZF-4969: Why move_uploaded_file() in Zend_File_Transfer_Adapter_Http? Possible filename collisions.

Description

Zend_File_Transfer_Adapter_Http defaults to storing file uploads in the system temp folder. And, if I'm not mistaken, it renames all incoming files to the filename provided (indirectly) from $_FILES['userfile']['name'], which is the "original" name of the file on the client machine (as provided by the browser).

Why is it doing file renaming at all (unless asked to do so by the a filter)? Isn't this introducing the potential for file naming collisions if two users simultaneously upload an identically named file?

In many use cases, an upload file is to be immediately processed (i.e. resize an image) and then moved to another server (i.e. on a content delivery network), so adding the extra overhead of running move_uploaded_file() seems unnecessary. Why rename the file at all by default, and why rename it to a filename that is not be guaranteed to be unique?

Thanks.

Comments

You can not edit the temporary file directly. This will result in an attacker exception from php when the file is processed.

Therefor we have to move the file to a temporary location before applying filters. But many users don't set a filter.

So we now have the problem that, if we would rename the file to a "unique" self-created name, the file upload would be corrupted, as it would not be processed further, after it has left the php temp path. Therefor we accept the possibility to have filename collisions, which you have also when you do uploading manually with php.

Keep in mind that you can not process the file as long as it has not been moved from php's temp location to a working location.

The reason is security and this comes to your second question: php works internally with an hash algorithm and can detect file intrusions and other attacks. But this works only when you call is_uploaded_file/move_uploaded_file. Otherwise it would be possible to intrude the system by php's temp path when you do file uploading.

And the question is also: How often does it happen that users upload the same file in the same millisecond. One would be processed, and the other would get an "File upload corrupted... Please try again".

Can be solved with userland filters, and issue is not specific to ZF.

We will implement a partitial solution. Therefor we reopen this issue but not as bug but as improvement.

But this does only work when you added the rename filter. When not, there is no way for a solution.

An attached rename filter will now be taken in account as with r13799. The file will not be moved twice anymore.

I've got an issue with multifile upload. I want to add as many rename filters as files I have, so I do it by specifying different options (since new files get new unique names) to rename filter. The documentation says: "Note that even though setting the same filter multiple times is allowed, doing so can lead to issues when using different options for the same filter."

You say it's not a bug, but there is simply NO NORMAL WAY to upload many files and rename them. I spent hours trying to understand if it is possible, but ended up with a desire to throw away the mechanism provided with ZF. So, to be more concrete, here is the example.

I have multifile form field with base name details_image, and it is set to 3 files. Before uploading I do this

$upload = new Zend_File_Transfer_Adapter_Http(); $cnt = 0; foreach ($files as $file) { $upload->addFilter('Rename', array('target' => '/some/unique/file/name.txt'), 'details_image_' . $cnt . '_'); $cnt++; }

It doesn't work for multifile, but for simple only.

Closing as resolved as the original issue was already cleared