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.
1// Noncompliant: Password is hardcoded
2fun noncompliant() {
3 val username = "admin"
4 val password = "password123"
5 val ssh = SSHClient()
6 ssh.authPassword(username, password)
7}
1// Compliant: Password is retrieved from environment variables.
2fun compliant() {
3 val username = System.getenv("SSH_USERNAME")
4 val password = System.getenv("SSH_PASSWORD")
5 val ssh = SSHClient()
6 ssh.authPassword(username, password)
7}