2

I have a simple site which allows users to upload files (among other things obviously). I am teaching myself php/html as I go along.

Currently the site has the following traits: --When users register a folder is created in their name. --All files the user uploads are placed in that folder (with a time stamp added to the name to avoid any issues with duplicates). --When a file is uploaded information about it is stored in an SQL database.

simple stuff.

So, now my question is what steps do I need to take to:

  • Prevent google from archiving the uploaded files.
  • Prevent users from accessing the uploaded files unless they are logged in.
  • Prevent users from uploading malicious files.

Notes:

I would assume that B, would automatically achieve A. I can restrict users to only uploading files with .doc and .docx extensions. Would this be enough to save against C? I would assume not.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
user187680
  • 663
  • 1
  • 6
  • 20
  • To solve A and B, simply authenticate (based on session information) before serving the uploaded files. As for C, you first need to define "malicious".... – Oliver Charlesworth Jun 11 '12 at 22:20
  • Okay. So I need to "authenticate" can someone point me in the direction of a guide on what that might look like. – user187680 Jun 11 '12 at 22:21
  • You need to read up on [sessions](http://www.php.net/manual/en/intro.session.php). But then you need to use sessions for almost anything useful on a dynamic website! – Oliver Charlesworth Jun 11 '12 at 22:22

3 Answers3

1

There is a number of things you want to do, and your question is quite broad.

  • For the Google indexing, you can work with the /robots.txt. You did not specify if you also want to apply ACL (Access Control List) to the files, so that might or might not be enough. Serving the files through a script might work, but you have to be very careful not to use include, require or similar things that might be tricked into executing code. You instead want to open the file, read it and serve it through File operations primitives.

  • Read about "path traversal". You want to avoid that, both in upload and in download (if you serve the file somehow).

  • The definition of "malicious files" is quite broad. Malicious for who? You could run an antivirus on the uplaod, for instance, if you are worried about your side being used to distribute malwares (you should). If you want to make sure that people can't harm the server, you have at the very least make sure they can only upload a bunch of filetypes. Checking extensions and mimetype is a beginning, but don't trust that (you can embed code in png and it's valid if it's included via include()). Then there is the problem of XSS, if users can upload HTML contents or stuff that gets interpreted as such. Make sure to serve a content-disposition header and a non-html content type.

That's a start, but as you said there is much more.

0

Your biggest threat is going to be if a person manages to upload a file with a .php extension (or some other extension that results in server side scripting/processing). Any code in the file runs on your server with whatever permissions the web server has (varies by configuration).

If the end result of the uploads is just that you want to be able to serve the files as downloads (rather than let someone view them directly in the browser), you'd be well off to store the downloads in a non web-accessible directory, and serve the files via a script that forces a download and doesn't attempt to execute anything regardless of the extension (see http://php.net/header).

This also makes it much easier to facilitate only allowing downloads if a person is logged in, whereas before, you would need some .htaccess magic to achieve this.

drew010
  • 68,777
  • 11
  • 134
  • 162
0

You should not upload to webserver-serving directories if you do not want the files to be available.

I suggest you use X-Sendfile, which is a header that instructs the server to send a file to the user. Your PHP script called 'fetch so-and-so file' would do whatever authentication you have in place (I assume you have something already) and then return the header. So long as the web server can access the file, it will then serve the file.

See this question: Using X-Sendfile with Apache/PHP

Community
  • 1
  • 1
Joe
  • 46,419
  • 33
  • 155
  • 245