views:

656

answers:

4

I have considered the following

$mysqlPass = 'password'; // doesn't seem right

$config['mysql_pass'] = 'password'; // seems slightly better

define('MYSQL_PASSWORD', 'password'); // seems dangerous having this data accessible by anything. but it can't be changed via this method

class Config
{
    const static MYSQL_PASSWORD = 'password';
} 

Config::MYSQL_PASSWORD; // don't know if this is a good idea

This is all I have thought of somewhere. I was going to have a require /config.inc.php with all these vars in. What works for you, and what are best practices concerning this?

Update

How would you store your directories variables? These are less of a security concern than a MySQL password, so would CONFIG_APP_ROOT, CONFIG_DIR_BASE etc be fine?

+1  A: 

I've always gone with option #2 and just ensure that no one but the owner has ANY sort of access to it. It's the most popular method among PHP applications like Joomla, vBulletin, Gallery, and numerous others.

First method is too messy to me (readability) and the third is WAY too dangerous to do. I've never thought about the Class method, so someone else can provide their input on that one. But I guess it's fine so long as the right access is used on the class' usage.


Example..

define('EXAMPLE1', "test1"); // scenario 1
$example2 = "test2"; // scenario 2

function DealWithUserInput($input)
{
   return eval($input);
}

Now this example of code is really dumb, but just an example. Consider what could be returned by the function depending on which scenario the user could try to use in their input.

Scenario 2 would only cause an issue if you made it a global within the function. Otherwise it's out of scope and unreachable.

invenetix
Is it dangerous in the way that if some user input comes in with MYSQL_PASSWORD it might be passed with the actual password?
alex
That would be what could happen if you follow the second method you listed. It essentially sets it as a global variable, viewable in any scope.You can choose how classes or functions can access the $config array, so as long as you don't have it visible in the wrong scope, you should be fine.
invenetix
Edited post to give an example on two different ways you could have stuff defined in your code and how the user could get the information.
invenetix
Thanks for that, I posted a bit more to my question too.
alex
Doing an eval on user input is dangerous regardless of whether or not you've defined a constant. Can anyone provide another example of how defining constants is "WAY too dangerous"?
chyne
I agree with chyne. The vulnerability lies with eval-ing user input, not in having a global variable. Not globaling sensitive data mitigates the risk, but the real vulnerability is the eval.
GloryFish
I agree with the eval() comments above as well. Thats why I commented "Now this example of code is really dumb, but just an example.". Just oversimplified and improbable example of what could happen. But to me, the MySQL password should never be defined as a constant, visible to everything. There...
invenetix
...shouldn't be a reason for it to be that accessible. It's like assuming everyone coming in the front door of an office building is responsible and has purpose for being there - so there isn't a reason to lock offices and labs inside the building. It should only be visible to the code that uses it.
invenetix
Actually, I've looked into the Joomla I've used, and their config is stored in a class.
alex
+1  A: 

I generally use the second method... When handling database connections I generally open a connection at the beginning of the request, then close it at the end. I have a function that establishes the connection, then removes the username/password from the global array (with the unset() function), This prevents other parts of the system from accessing the "sensitive" mysql connection data.

Jim OHalloran
A: 

I'm also with option 2 for most config values. If you were going to implement the Class then I would tie the specific values to the Class that it affects instead of a general config Class.

In your example, your Class would be for database connections and an instance would save the password, db_name, etc. This would encapsulate the data properly and also provide an easy means to create multiple connections if that was ever needed.

Paulo
Yeah that is true, except when you'd like to edit one file to be able to change any config data, then constants attached to a Db class will make it more difficult than a single access point (which is what I want)
alex
+1  A: 

I'd say it also depends of userbase a bit. If configurations has to be very user friendly or user has to have ability to change config via web etc.

I use Zend Config Ini for this and other settings are stored in SQL DB.

raspi
Never heard of that method before. Good to know!
invenetix