04 Aug 2010I just had to write unit tests for a file upload script I had to write. As it is not that easy to do, I'll share my findings with you.
My problem was on how I was going to simulate a file upload in a test case. Sure I could simulate a whole post request either using curl of simpleTest webtester. But that would only give me a full overview of the upload process, not its inner details.
There was a way to do that using PHPT, which seems to be the unit tests used by PHP itself. It is supposed to simulate any kind of query. Unfortunatly, setting that up seemed a little to complex for me.
So, how did I do ?
I finally found a way to do that by :
- Spoofing the
$_FILES array and putting arbitrary test data inside - Copying a test file to the
tmp/ directory for testing purpose. Actually the directory does not matter (see 3.) - Wrapping all calls to
move_uploaded_file and is_uploaded_file to its own methods. Those two php methods won't work with dummy upload because they weren't really uploaded through POST
So, instead of calling move_uploaded_file, I called $this->moveUploadedFile(), and instead of calling is_uploaded_file(), I called $this->isUploadedFile()
And when times comes to test my class, I just extends the class, overwrite those two methods with new one that uses rename()and file_exists() instead.`
`
What does that change ?
The fondamental difference between the former and the latter functions is that the former checks that the target really was uploaded through POST while the latter does not care.
It is extremely important that you use the correct upload method because it provide an additional security check. If you just blindly rename() files specified by your user, you'll ending up putting the database.php and config.php files in the webroot renamed as i_want_to_be_hacked.txt
The other good news is that by wrapping those methods around those functions, you can create mock objects and test all the various return scenarios.
04 Aug 2010Say you want to disable PHP scripts in a whole directory. Like your upload/ directory, because you don't want your users to upload .php files with direct webroot access on your server...
Just drop a .htaccess file in it, and add the following rules
# We don't want php files from being parsed by the server in this directory, so we will return them as plain text
AddType text/plain .php .php3 .php4 .php5 .php6 .phtml
# Or, if the first rule does not work on your server, you may want to completely turn off PHP
#php_flag engine off
04 Aug 2010One trick I've been dragging with me on all this years of PHP programing is a little snippet to correctly create directories with the chmod I want.
By simply calling mkdir('my_dir', 0777) I used to often end up with directories that I can't write to nor delete, even if I was correctly setting the chmod.
The trick was to reset the mask (using umask(0)) before the mkdir()call and then reapplying the old mask after.
$tmpUmask = umask(0);
mkdir('my_dir', 0777);
umask($tmpUmask);
I must admit that I've never really understand why it was working better than simply calling mkdir() but hey, it's been years that I'm using that now and I never run into access rights issues since.
04 Aug 2010For testing purpose I just needed to overwrite an existing class to add some more logic to some methods before returning a call to its parent method.
I needed to do that in a test case to save in an inner variable the result so I can clean up my mess after each test ran.
The key is calling call_user_func_array with the right syntax. It seems that some version of PHP prior to 5.3 choke (like in segmentation fault) if an alternate syntax is given.
Here is what worked for me :
class TestDocument extends Document {
function foo() {
return $this->fooResult= call_user_func_array(array('parent', 'foo'), func_get_args());
}
}
03 Aug 2010One thing that always sent me an awful hours of debugging is the fact that the Flash player identify itself as a whole different browser that the one it is integrated into.
I've ran into this issue multiple times when dealing with SWFUpload and today was one more. As I always spent quite a lot of time debugging issues arising from this, I guess I should post about it here.
The Flash player uses a different userAgent that your browser
Most of the time, this is not a problem. But when dealing with restricted areas of a website built on top of cakePHP, it can become one.
The thing is that as the userAgentstring used by the Flash player is not the same as the one used by your browser. So, when you make a request to your site, cake will see that as whole new request and you won't be able to access your current session variables.
As you can't overwrite the userAgenthash sent, you need to send the sessionIdalong with your flash request. That way, you'll be able to call a
$this->Session->id($sessionId);
$this->Session->start();
in your controller beforeFilter(or component initialize)
In cake 1.2, you'll also have to call $this->Session->destroy() before to delete the Flash session created.
The Flash player uses a different cookie pool that your browser
This used to be an issue in 1.2 but not longer is.
Cake stores in a cookie the name of the session you're supposed to use. Flash having its own cookie pool, it will save its own cookie (usually somewhere you don't even know) and will always check for the session specified inside.
This took me quite a long time to found out why the flash request where reading an other request that the one I was setting.
In 1.2, you needed to delete the cookie created by cake whenever you made a flash request to avoid conflicts
setcookie(Configure::read('Session.cookie'), '', time() - 42000, $this->Session->path);
cakePHP used to overwrite the userAgent string in session
In cake 1.2, when you manually change the current session, cake would update the inner userAgenthash saved to your current userAgent hash.
This meant that whenever you were moving from the Flash session to the correct session, you had to overwrite the userAgenthash to the correct one.
$this->Session->write('Config.userAgent', $userAgent);
This is no longer the case in 1.3, changing the current session does not alter it anymore.
Doesn't all that stuff alter security ?
Well, most of the answers I found online were among the lines of "Just disable the check of the userAgent", so I think my method is a little bit more secure.
Anyway, passing a sessionIdas a parameter seems a little risky, even to me. I guess there should be a way of encrypting it to not pass it as clear text
UPDATE !
I had to fiddle with this script some more. It appears that, as we are changing the sessionId, we need to do that BEFORE we access the session. It means that any call to Session::read() or Session::check(), or almost any other Session method BEFORE setting the id will block this trick from working. So, make sure that this code is executed before anything else, put it in a component that will be loaded first.
It also appears that if you follow my advice, you'll only have to call id($sessionId), and none of the hocum pocum about destroy, writeand userAgenthashes I talked about...
I just lost some hours finding this out. I add a call to Session in my I18ncomponent that was rendering this whole fix useless. It was driving me crazy...