Database Security – How to Handle Database Connection Password?

database

I have a question about using databases in my application. I have the User table which stores passwords for my users. I know how to handle user authentication, but how to handle the password for the database itself? I searched online, and all articles mention only storing user passwords, not the database password itself.

To be more specific, let's say I'm working on an application that requires the database password to access the database, and only then authenticates the user. Where and how to safely store it/check it?

Best Answer

What you've come up against is one of the fundamental problems of client-server architecture. In order for the client machine to access the database, you need to have credentials for that database on the client machine. As accepted answer in to the that question provided by Robert Harvey explains, there's no way to completely prevent the user of the application from getting those credentials.

This is one problem that is made easier by moving to a N-tier architecture. Other than that, I see two main approaches you can take:

  1. Create database credentials for each user. Then there is no need to store the DB credentials on the client machine. Logging into the DB could then take the place of managing user passwords in a table. You would then need to manage the rights within the DB for each user to prevent them from being able to access or change data outside of their authority.

  2. Encrypt the database password using the clients credentials. This is extremely error-prone (especially around credential updates (both user and DB). Ideally you would find a well tested pre-built solution for this but I doubt you'll find one because such a solution still leaves you with the reality that someone with user credentials (legitimately or otherwise) can intercept the database credentials. You will need to consider what access those database credentials provide and what someone with them could do to your database.

It might not be easy but the preferred approach to improve security here is to move to a more contemporary architecture.

Related Topic