How to prevent a leak like Tumblr’s
blog.kevburnsjr.com
blog.kevburnsjr.com
How would that have helped here? The file was obviously still valid syntactically. Try it for yourself: create a PHP file that looks like
i?php
$config_data = array();
and run it through PHP's syntax check (php -l /path/to/file from the command line). It will verify that the script is valid. However, the content will still not be parsed as PHP.One way to fix this is to store your configuration data in another file format (eg: YAML) outside of the document root. Since the configuration file doesn't try to be parsed as PHP, it can't be leaked in this way.
Also, unit tests should definitely find that error.. no? I mean, a simple high level test that use something in config.php should fail.
Of course whenever someone does a fresh pull of the project, that is one file that they'd have to create for themselves. (So this is documented very clearly.)
It's minor inconvenience but it solves the problems like this.
SetEnv DB_USER=kermit SetEnv DB_PASS=Shhh
And then connect to the DB using code such as:
mysql_connect(getenv('DB_NAME'), getenv('DB_USER'), getenv('DB_PASS'))
Environmental config data should never be stored in source code.
Edit: Unless of course you're using YAML or some other representation rather than a pure PHP file.
Edit: Here's a way to force 500 for fatal errors in PHP http://blog.kevburnsjr.com/php-fatal-error-500
Many people have made suggestions on how to trap these kinds of error... storing PHP files outside of document root is a good practice, otherwise the code can be revealed if the webserver stops interpreting PHP files for whatever reason.
Any amount of testing would catch accidentally dumping the configuration file, if it isn't being parsed then nothing on the site will work. Development -> testing -> production setups aren't as common in the PHP world as they should be, though.
Personally, I'm tempted to say store configuration in a .ini or YAML file or similar. If you're building a small site, it won't be a big speed issue anyway. If it becomes a speed issue, there are many ways to mitigate it, such as compiling the human-written config file to a PHP file, removing the human element that is more likely to transpose a character.