views:

106

answers:

5

I want to allow my users to upload .php files to the server, but also want to make sure the files are harmless to my application.

Any suggestions?

Thank you.

A: 

Just don't execute them, that's enough.

Make sure that your application doesn't run or include user-uploaded files at any point (e.g. by using a GET parameter to include a file without any sanitation).

Also make sure the uploaded files are not accessible from outside with the .php extension.

In a normal, halfway properly coded workflow, uploaded .php files do not pose a security risk no matter what code they contain.

Pekka
Or allow other users to download them...
Dominic Rodger
Yeah. I removed the paragraph from my answer, it's not really necessary to make the point.
Pekka
+1  A: 

Option 1: Store them with a different filename and store the original filename as part of the metadata, e.g. in a database table.

Option 2: Disable script execution on the uploads directory.

Option 3: Rename them to .phps (an accepted extension for the display of raw PHP)

enbuyukfener
+6  A: 

I recommend the ‘disable script execution in the uploads directory’ approach. This is the only solution that’s completely safe.

Just add this rule to the .htaccess file inside the uploads directory:

php_value engine off
Mathias Bynens
+1 This is a great solution, but it will protect only from requests made through the web server, not from internal execution through inclusion (but that is a very minor risk anyway).
Pekka
That goes without saying! Running an application that executes code uploaded by users without any sanitation, is of course a bad idea. But you’re right, of course.
Mathias Bynens
You could also change the mime-type to `text/plain` or something similar.
DisgruntledGoat
+1  A: 

A couple of solutions:

  • Rename them to .phps
  • Disable execution of PHP in the upload directory in your webserver configuration
Emil Vikström
A: 

Make the upload directory separate, write-only and not visible from the internet (this will prevent them accessing their own script after uploading).

You can then moderate new scripts and do as you see fit.

Paolo