tags:

views:

53

answers:

3

Which is the question my partner said I'd not need answering, so to prove my point I removed php and simulated a server problem. And hey presto, the webserver serves up the PHP source as a download.

So, the question, is there a way to stop source being served up in the event of a server glitch, bug, or temporary loss of connection to the relevant modules etc?

+2  A: 

Put the PHP files outside the document root, then require them from PHP files inside the document root. They'll still get a little bit of code like this or something, but that won't give them very many details:

require(dirname(dirname(__FILE__))."/real_file.php");
icktoofay
That's an interesting trick.
staticsan
+3  A: 

The only way source is revealed is if you change the server configuration by error – for instance, you're using a mod_php for Apache and accidentally remove the module.

For that particular case, you can do this:

<IfModule php5_module>
   AddHandler php5-script .php
   PHPIniDir "/etc"
</IfModule>
<IfModule !php5_module>
  <FilesMatch "\.php$">
      Order allow,deny
      Deny from all
      Satisfy All
  </FilesMatch>
</IfModule>
Artefacto
Facebook leaked some of their main page's source code. Apparently, the reason was that there was a bug in `mod_php` under high load. http://techcrunch.com/2007/08/11/facebook-source-code-leaked/
icktoofay
@ick Well... that's a bug. Not much you can do about it except fixing it or putting a filter (e.g. another module or a proxy) in front of Apache that kills the request when it sees source code (which would be difficult to do).
Artefacto
@Artefacto: Yeah, it was a bug, but it could potentially happen again.
icktoofay
@ick By the way, I'd like to see a source of the claim that `mod_php` leaks source code under high load (the link you posted simply uses weasel words). It could very well be a false or at least outdated claim.
Artefacto
@Artefacto: Yes, I know; I'd like to, also. It most likely is resolved by now, anyways.
icktoofay
Okay cool, that works for if the php module has been removed or wasn't in place. I get a 403 for that. Still serves code if the `.php` extension is removed from httpd. What I'm wondering and the reason I asked the question in the first place, is on sites I've seen this bug and files be served when 3 secs earlier they weren't and 2 secs after that they weren't is it because of the module having issues in which case your code would solve the problem or is it a different problem, in which case it may not.
Oh and +1 for the `if` statement that Sarfraz neglected to include :P
A: 

I wrote a couple of simple functions to parse out a URL MVC-style (based on kissmvc.com) and this is the only php in my document root:

<?php

require '../bootstrap.php';
parse_request();

So, if something did happen, that's all anyone would see. Everything else is handled starting at bootstrap. If I wanted, I could move the parse_request() into bootstrap.php, too, but I did it this way so I could include my bootstrap and not dispatch all the web site stuff if I was only hitting a cron script (and this was before I realized about the php_sapi_name() function and constant).

That does no good if you've got an existing site with php in the docroot, or are using Wordpress, etc, but if you have the option, it will prevent this specific problem from kicking you in the boys.

Hans
I'll have to look into that, might take some rewriting of code I didn't wholly design myself, but for the long term moving all of my code is the aim.