A user password should never be stored in clear-text, instead a hash should be produced from it using a secure algorithm:
- not vulnerable to
brute force attacks
.
- not vulnerable to
collision attacks
(see rule s4790).
- and a salt should be added to the password to lower the risk of
rainbow table attacks
(see rule s2053).
This rule raises an issue when a password is stored in clear-text or with a hash algorithm vulnerable to bruce force attacks
. These
algorithms, like md5
or SHA-family
functions are fast to compute the hash and therefore brute force attacks are possible
(it’s easier to exhaust the entire space of all possible passwords) especially with hardware like GPU, FPGA or ASIC. Modern password hashing
algorithms such as bcrypt
, PBKDF2
or argon2
are recommended.
Noncompliant Code Example
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("SELECT * FROM users WHERE username = ?")
.passwordEncoder(new StandardPasswordEncoder()); // Noncompliant
// OR
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("SELECT * FROM users WHERE username = ?"); // Noncompliant; default uses plain-text
// OR
auth.userDetailsService(...); // Noncompliant; default uses plain-text
// OR
auth.userDetailsService(...).passwordEncoder(new StandardPasswordEncoder()); // Noncompliant
}
Compliant Solution
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("Select * from users where username=?")
.passwordEncoder(new BCryptPasswordEncoder());
// or
auth.userDetailsService(null).passwordEncoder(new BCryptPasswordEncoder());
}
See