Description
A database using password authentication may reject or accept an empty password, depending on its server settings. An account that authenticates with a certificate or external identity provider can intentionally connect without a password.
Potential impact
If the server accepts an empty password, a permitted client may read, change, or delete data within that account’s permissions. An administrative account may also be able to change server settings.
Remediation
- Check how the account authenticates. For password authentication, set a strong password and configure the server to reject empty passwords. For certificate or external authentication, verify the method in both the server and driver settings.
- Supply the password from outside the code, using a secret store such as HashiCorp Vault or a restricted runtime environment. A KMS manages encryption keys. Choose separately where to store and retrieve the password.
- Grant only the permissions and network access the account needs. For remote connections, use TLS with server-certificate verification.
Examples
Before
java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class InsecureDBConnection {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String password = ""; // Empty password
Connection conn = DriverManager.getConnection(url, user, password);
}
}
After
java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SecureDBConnection {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/mydb";
String user = System.getenv("DB_USER"); // Load from the environment.
String password = System.getenv("DB_PASSWORD");
if (password == null || password.isEmpty()) {
throw new SecurityException("Database password is not set");
}
Connection conn = DriverManager.getConnection(url, user, password);
}
}
Explanation:
- Before: Passes an empty password for
root. Whether the connection succeeds depends on the database server settings. - After: Reads the account and password from environment variables and stops if the password is absent or empty. Environment variables alone do not protect a secret; restrict access to the runtime environment and logs. Connection cleanup and driver-specific TLS configuration are omitted.