Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Securing DB password in php

Am new to web development. I am curious as to how people do it.

I am writing some php code that uses a mysql DB. I have the password hardcoded in the code as of now. This code can be checked out by all devs and so every one has access to the password. Seems very very wrong to me. On top of that I can think of some complications. I am listing the issues in bullet point form -

  1. Password hard coded in code is wrong. I don't want all devs to have access to it as all of them can check out the code.

  2. How to differentiate between production and development servers/credentials? I have the same file containing both prod and dev DB credentials. What is the best way to handle this?

  3. I want to prevent against lazy/drunk times so that devs do not delete/drop tables etc. I can obviously have different access to different devs. So is that the solution to all of this?

Potential solution: Do not have the password in code. Ask devs to add the password themselves and make sure its never checked in.

Problem with solution: Tedious process of deployment. Have to add the password for production/QA deployment manually and make sure its able to connect to the DB everytime before deployment. Sounds too painful and error prone. What do people usually do?

Also on the same note (kind of linked to the above question)

  1. If you have 4 devs in the team how do you set up the dev environment? Do all of them use the same DB? If not how do you create the tables and populate the tables with test data? Do you have to write code to populate the test data?

Thanks a lot for any input.

like image 627
user220201 Avatar asked Jul 13 '11 19:07

user220201


People also ask

How to store db password in PHP?

Set the database password in an environment variable in the PHP-FPM configuration file (env[DB_PASSWD] = MyPassword). Then use PHP's getenv() function in the config. php file.

Is it safe to store password in PHP?

PHP provides a native password hashing API that safely handles both hashing and verifying passwords in a secure manner. Another option is the crypt() function, which supports several hashing algorithms.

How can we encrypt the username and password using PHP?

Encryption of the password: To generate a hash from the string, we use the password_hash() function. The password_hash() function creates a new password hash of the string using one of the available hashing algorithm.


2 Answers

Put the password in a separate PHP file, containing all your app settings, and include it at the top of the page. This file can then be kept out of Version Control, and replaced for each deployment.

Make sure that you keep the config.php file (or whatever you choose to name it) out of your root directory, also, so that it can't be accidentally served up to any users of your app. Also, as a further precaution, make sure that you give it the .php extension, so that if it somehow does still get served up, it should be parsed by PHP first, and any useful information (hopefully) removed - a common practice would be to name it with a .conf.php or .inc.php extension for this reason.

As for the Dev Environment, we use a single database shared by all the devs. It was originally created from live client data, cloned into our database, with certain information redacted / replaced for privacy reasons. The same database is used in our development build as well as our localhost builds.

like image 154
a_m0d Avatar answered Sep 25 '22 17:09

a_m0d


In that situation you describe, you could write a deployment script that "fills" the password in the correct spot in the source code automatically. Then your production passwords only reside in your production environment deployment scripts. You can have developers manually add it to their own local environments.

Also, you could have a configuration file with all this settings and have your app load them from it, or a even a separate php file as someone else suggested. Either configuration/php file should not be in source control and each developer can do its own, and you can have the correct one in production.

like image 30
Francisco Soto Avatar answered Sep 23 '22 17:09

Francisco Soto