Java

Java

Made by DeepSource

Spring password storage must use a strong hashing function JAVA-S1018

Security
Critical
a02 a07 cwe-327 cwe-287 cwe-522 sans top 25 owasp top 10

This Spring security configuration appears to store passwords in plaintext or hashed with a weak hashing algorithm. This could allow an attacker to easily steal user login information.

Configure Spring to store passwords securely.

Spring allows for great flexibility when configuring how user information is stored in the database.

User passwords in particular are a liability when not stored properly; they must be hashed and salted before storage.

Ideally, a strong hash algorithm is: * Not vulnerable to brute force attacks * Not vulnerable to collision attacks * Not vulnerable to rainbow table attacks; this is achieved by salting the password with random data.

This issue is raised when either no password encoder, or one of the following weak/deprecated password encoders is used:

Bad Practice

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
  auth.jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery("SELECT * FROM users WHERE username = ?")
    .passwordEncoder(new StandardPasswordEncoder()); // StandardPasswordEncoder is not secure.

  // OR
  auth.jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery("SELECT * FROM users WHERE username = ?"); // If no encoder is used, the password is stored as plain-text.

  // OR
  auth.userDetailsService(...); // Again, the password is stored as plain-text.
  // OR
  auth.userDetailsService(...).passwordEncoder(new LdapShaPasswordEncoder()); // Insecure.
}

Recommended

Use DelegatingPassswordEncoder with any of these encoders:

Here's an example using BCryptPasswordEncoder:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
  auth.jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery("Select * from users where username=?")
    .passwordEncoder(new BCryptPasswordEncoder());
}

References