views:

416

answers:

3

Hello everyone, I'm developing a photo sharing web site using the CodeIgniter PHP framework. The idea is that people could upload their photos, manage them (through some sort of file browser which allows them to create subfolders, drag files around, etc) and edit them (some basic things like resizing, rotating and cropping to start with, and later on, I'll add some advanced features).

I've already implemented a third party authentication solution for CI (Redux Authentication 2 Beta) and I'm now integrating a JS/PHP file manager (AjaxExplorer), but the problem is that the PHP backend for managing files (moving, copying, etc) is trusting too much on the user input from the ajax calls. For instance, it's doing things like this (simplified for the sake of clarity):

move_uploaded_file($_FILES['upload']['tmp_name'], $root.$username.$_POST['destination_dir']);

As you can see, there are obvious security concerns as it blindly accepts whatever path the user throws in! I can already see someone sending something like "../AnotherUser/" as the $_POST['destination_dir'] value.

My question is: What's the best way to "sandbox" a user, in order to only allow him to manage his own data? Do I just validate+filter the inputs, hoping to catch every attempt of intrusion? Are there any libraries/packages dedicated to address this specific issue?

I think this problem must be somehow solved in any (mature enough) project, which gives its users the power of managing their files through a web browser, so I expected to find some clear guidelines around this (as there are a lot about SQL Injection, XSS, CSRF, etc) but I guess I'm not using the right keywords.

A: 

I'm not sure what your destination_dir looks like, but what I thought of was assigning directories keys, and then getting the directory based on that key. For example:

//$_POST['destination_dir'] = '4hg43h5g453j45b3';
*_query('SELECT dir FROM destinations WHERE key = ? LIMIT 1'); //etc.

However you have to predefine keys before hand. Another alternative could be the opposite: md5/sha1 the input and use that as the destination_dir, then store that key in the database with the associated label.

Ross
A: 

There are no library's that I know of.
However in your particular example, strip all (back)slashes and dots from the string and then append a slash to the end of it, that way the user can't change folders.

$destdir = str_replace(array('.', '/', '\\'), '', $_POST['destination_dir']); 
$destdir .= "/";
Pim Jager
String with all invalid characters gets mapped to the server root folder.
bobince
Thanks for the answer, however AFAIK dots, slashes and backslashes are all acceptable characters for naming a folder, at least in the *NIX world.
fandelost
Not so true bobince, since I can prepend a path, but I won't be using this solution anyway.
fandelost
+6  A: 

What's the best way to "sandbox" a user, in order to only allow him to manage his own data?

Allow any filenames/directory names the user wants, but simply don't use them on the server side filesystem. Instead, write the path names into a database with a primary key, and use the primary key as a filename like ‘34256.dat’ in a flat storage directory (or even as a BLOB in the database if you prefer). Then serve up via a download script or URL rewrite to make the desired filename appear in the URL.

Sanitising incoming filenames is hard. Detecting ‘..’ is only the beginning. Too-long filenames; too-short filenames; combinations of leading and trailing dots; combinations of leading and trailing whitespace; the different directory separators of different platforms; characters that are invalid on some platforms; control characters; Unicode characters and the environment-specific ways of addressing them; ADSs; filenames (‘.htaccess’) or extensions (‘.php’, ‘.cgi’) that might be ‘special’ to your web server; Windows's reserved filenames...

You can spend a lifetime tracking down funny little quirks of filepath rules on various platforms, or you can just forget it and use the database.

bobince
Thanks for the answer bobince! It's a good solution, but I'm concerned about portability of the data, since doing a simple backup of the files would imply running some script first to reverse the naming scheme, and if my database ever gets compromised or corrupted, I'd lose the dir's tree structure.
fandelost
Good solution. @fandelost, then make sure you backup both database and files
Pim Jager
I'm sorry to took so long to respond. I've finally resigned "AjaxExplorer" and built my own file managing solution, based on bobince's advice, using a database to store everything related to the files, but not touching them after they're uploaded outside the web root. I've also restricted the 'name' field for each one to a limited set of characters, and I won't be using nested folders, I instead use a single level of grouping (just to avoid the reference mess).
fandelost
hmm i had my doubts because you create a database dependency but on the other hand, it's easier to query a database than a filesystem for searching/sorting purposes and it's more secure...
Sander Versluys