Recent Thoughts

Downloads

Contact Us

Email:
Topic:
Message:

Google Ad

File Upload Plugin v6.0 Security Released

Date: Wed, Jun 2nd 2010, 16:49 Author: nick Views: 1166 Comments: 4 share

CakePHP File Upload Plugin

Info:
Get it:
  • Download Now
  • svn co http://svn.github.com/webtechnick/CakePHP-FileUpload-Plugin file_upload

What's new in 6.0



FileUploadPlugin v6.0 is a security release rather than a features release. There are two major changes that you should be aware of.

First, a bug fix -- if a fileNameCallback function returns false the upload will be halted as documented.

Second, I've changed the way the plugin validates a file being uploaded. Previously the uploader only checks the mime type of a file being uploaded. Now the uploader checks the extension as well as the mime type. Both must match or a validation error occurs and the upload halted. This fixes a potential hole that would allow an attacker to alter the mime type of a file to pass in a malicious file with its extension intact. This is an issue as most servers decide how to execute said file by its extension (not mime type).

This new change required a change in how the allowedTypes array is constructed. Previously, allowedTypes was a single layer array full of mime types. Now its a multi dimensional array with extension as its key and their linked mime types as an array.

Some Examples



Example Setting With a Controller:
  1. //validate only image/jpeg and image/pjpeg mime types with ext .jpg
  2. //validate only image/png mime type file with ext .png
  3. //validate all MIME types for ext .gif
  4. //validate all MIME types for ext .swf
  5. //validate only application/pdf for ext .pdf
  6.  
  7. $this->FileUpload->allowedTypes(array(
  8.   'jpg' => array('image/jpeg', 'image/pjpeg'),
  9.   'png' => array('image/png'),
  10.   'gif',
  11.   'swf',
  12.   'pdf' => array('application/pdf'),
  13. ));


Sample In a Behavior
  1. //validate only image/jpeg and image/pjpeg mime types with ext .jpg
  2. //validate only image/png mime type file with ext .png
  3. //validate all MIME types for ext .gif
  4. //validate all MIME types for ext .swf
  5. //validate only application/pdf for ext .pdf
  6.  
  7. var $actsAs = array(
  8.   'FileUpload.FileUpload' => array(
  9.     'allowedTypes' => array(
  10.       'jpg' => array('image/jpeg', 'image/pjpeg'),
  11.       'png' => array('image/png'),
  12.       'gif',
  13.       'swf',
  14.       'pdf' => array('application/pdf'),
  15.     )
  16.   )
  17. );


The default types are the same:
  1. 'allowedTypes' => array(
  2.   'jpg' => array('image/jpeg', 'image/pjpeg'),
  3.   'jpeg' => array('image/jpeg', 'image/pjpeg'),
  4.   'gif' => array('image/gif'),
  5.   'png' => array('image/png','image/x-png'),
  6. ),


Included in the newest release is the Migration Guide from 5.0 to 6.0. I encourage you to read over that document as well.

Enjoy the changes,
Nick

Comments

06/16/2010 10:15 am

FileUpload plugin and cake13

Hi im trying to configure upload plugin ver 6 in cake13. I dropped folder file_upload in app/plugins directory, configured model :
class QuestionAttachment extends AppModel {

var $name = 'QuestionAttachment';
var $actsAs = array(
'FileUpload.FileUpload' => array(
'uploadDir' => 'files/questions',
'fields' => array('name' => 'name', 'type' => 'type', 'size' => 'size'),
'allowedTypes' => array('pdf' => array('application/pdf')),
'required' => false, //default is false, if true a validation error would occur if a file wsan't uploaded.
'maxFileSize' => '10000', //bytes OR false to turn off maxFileSize (default false)
'unique' => false, //filenames will overwrite existing files of the same name. (default true)
'fileNameFunction' => 'sha1' //execute the Sha1 function on a filename before saving it (default false)
)
);

var $belongsTo = array(
'Question' => array(
'className' => 'Question',
'foreignKey' => 'question_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

}

still, im getting message:
Error: The Behavior file app/models/behaviors/file_upload.file_upload.php can not be found or does not exist.

Error: Create the class below in file: app/models/behaviors/file_upload.file_upload.php

class FileUpload.FileUploadBehavior extends ModelBehavior {

}
?>
Notice: If you want to customize this error message, create app/views/errors/missing_behavior_file.ctp

what am i doing wrong ?
06/17/2010 7:02 am

FileUpload plugin and cake13

you need to change plugin foldername from file_upload_plugin to file_upload
06/18/2010 9:45 pm

Indeed file_upload is the name of the plugin

That is correct, I should rename the folder in the svn directory. The reason it was named file_upload_plugin instead of just file_upload was because there was an original file_upload component that was named file_upload. I then repackaged the component with a helper into a plugin, while keeping the original component name. So the name difference was referring to its packaging. It's been quite a while since I've updated the original component. I think its time to depreciate it and remove _plugin from the plugin as it only has caused confusion.

Anyway, hope that helps,
Nick
06/20/2010 5:54 am

Great... but.

Just upgraded to the latest version of this plugin (thank you), and it's working just fine, apart from one small thing.

If I go in and delete a record, the record in the upload table, along with the file itself, are all deleted - which is exactly what it's meant to do.

But if I go in and replace the file that is uploaded, the record in the upload table gets updated, and the new file is uploaded to the server, but the old file doesn't get deleted like it should.

Any ideas? Thank you.

Add Comment

Please login or register to submit a comment.