views:

722

answers:

8

So I have a web application that integrates with several other APIs and services which require authentication. My question is, is it safe to store my authentication credentials in plain text in my source code? What can I do to store these credentials securely?

I think this is a common problem, so I'd like to see a solution which secures credentials in the answers.

In response to comment: I frequently use PHP, Java, and RoR

I'd like to see some more votes for an answer on this question.

+3  A: 

It is not to be recommended.

An encrypted web.config would be a more suitable place (but note can't be used with a web farm)

Mitch Wheat
But... in order to use that, don't you have to decrypt it at run time? And if so, doesn't that mean the decryption method is in your source code? I don't understand how this is any more secure.
Andrew
@Andrew knowing the method of decryption does not get you any closer to decrypting. It's about securing the key(s). That's why good encryption algorithms can be public knowledge.
Rex M
But then doesn't the key have to be in the source code to decrypt the web.config file?
OverloadUT
It uses the machine key, which is why the technique does not work on a server farm. If you can get the machine key, then it's likely the server is compromised...
Mitch Wheat
@OverloadUT: Exactly. The key or at least it's location is stored in the source code. So all you're really doing is wasting a few minutes of the hackers' time. The solution is just to secure your server and restrict access to the files in the first place, which everyone should be doing anyway.
Andrew
@Andrew: I'm not sure what your point is? It's fairly obvious that the server should be secured. Encrypting a web.config stops people accidently seeing the credentials. It's a valid technique.
Mitch Wheat
My point is: People shouldn't think that this secures their application. They still need to make sure hackers can't gain access to their source, even when it's encrypted.
Andrew
+1  A: 

No, it is not.

Plus, you might want to change your password one day, and probably having yo change the source code may not be the best option.

OscarRyz
+1  A: 

No. Sometimes it is unavoidable. Better approach is to have an architecture set up where the service will implicitly trust your running code based on another trust. (Such as trusting the machine the code is running on, or trusting the application server that is running the software)

If neither of these are available, it would be perfectly acceptable to write your own trust mechanism, though I would keep it completely separate from the application code. Also, would recommend researching ways to keep passwords out of the hands of predators, even when stored on local machine - remembering that you can't protect anything if someone has control of the physical machine it is on.

+4  A: 

The only reason to NOT store the PW in the code is simply because of the configuration issue (i.e. need to change the password and don't want to rebuild/compile the application).

But is the source a "safe" place for "security sensitive" content (like passwords, keys, algorithms). Of course it is.

Obviously security sensitive information needs to be properly secured, but that's a basic truth regardless of the file used. Whether it's a config file, a registry setting, or a .java file or .class file.

From an architecture point of view, it's a bad idea for the reason mentioned above, just like you shouldn't "hard code" any "external" dependencies in your code if you can avoid it.

But sensitive data is sensitive data. Embedding a PW in to a source code file makes that file more sensitive than other source code files, and if that's your practice, I'd consider all source code as sensitive as the password.

Will Hartung
Yes. It may also be worth mentioning that even after compiling, the object/executable code produced is also sensitive.
thomasrutter
+4  A: 

Here's what we do with our passwords.

$db['hostname'] = 'somehost.com'
$db['port'] = 1234;

$config = array();
include '/etc/webapp/db/config.php';

$db['username'] = $config['db']['username'];
$db['password'] = $config['db']['password'];

No one but webserver user has access to /etc/webapp/db/config.php, this way you are protecting the username and password from developers.

michal kralik
If you have access to the source code, you can just print out the password from the array, or put it into a file, or e-mail it once the code is put into production.
MaxVT
Yes, true, you can also hack into users' accounts and steal credit card numbers without even knowing the db password if you have access to the source code and nobody can stop you if you are clever enough.
michal kralik
+1  A: 

If you control the Web server, and maintain it for security updates, then in the source (preferably in a configuration module) or in a configuration file that the source uses is probably best.

If you do not control the Web server (say, you are on a shared or even dedicated server provided by a hosting company), then encryption won't help you very much; if the application can decrypt the credentials on a given host, than the host can be used to decrypt the credentials without your intervention (think root or Administrator looking at the source code, and adapting the decryption routine so that it can be used to read the configuration). This is even more of a possibility if you are using unobfuscated managed code (e.g., JVM or .NET) or a Web scripting language that resides in plaintext on the server (like PHP).

As is usually the case, there is a tradeoff between security and accessibility. I'd think about what threats are the ones you are trying to guard against and come up with a means to protect against the situations that you need. If you're working with data that needs to be secure, you should probably be redacting the database fairly regularly and moving data offline to a firewalled and well-protected database server as soon as it becomes stale on the site. This would include data like social security numbers, billing information, etc., which can be referenced. This would also mean that you'd ideally want to control the servers on your own network which provide billing services or secure data storage.

Michael Trausch
A: 

It appears the answer is the following:

  1. Don't put credentials in source code but...
  2. Put credentials in a configuration file
  3. Sanitize log files
  4. Set proper permissions/ownership on configs

Probably more depending on platform...

Blaine
A: 

I prefer to keep them in a separate config file, located somewhere outside the web server's document root.

While this doesn't protect against an attacker subverting my code in such a way that it can be coerced into telling them the password, it does still have an advantage over putting the passwords directly into the code (or any other web-accessible file) in that it eliminates concern over a web server misconfiguration (or bug/exploit) allowing an attacker to download the password-containing file directly.

Dave Sherohman