Hardcoded credentials can be intercepted by malicious actors. Even after removing them from the code they may still pose a risk because an attacker might have recorded them to use them at a later point in time.
1public void createSqlConnectionNoncompliant() throws Exception {
2 // Noncompliant: password is hardcoded.
3 final Connection connection = DriverManager.getConnection("some url",
4 "username", "password");
5 connection.close();
6}
1public void createSqlConnectionCompliant() throws Exception {
2 // Compliant: password is obtained from environment.
3 final Connection connection = DriverManager.getConnection("some url",
4 "user", System.getProperty("pwd"));
5 connection.close();
6}