BCrypt hashing password and throwing ArrayIndexOutOfBoundsException
Summary
We have recently upgraded to Spring Security 5.0.4 and am seeing an odd behavior with Spring Security. We are getting an ArrayIndexOutOfBoundsException when it is trying to Bcrypt hash the password that came in on the session. It happens at random times. Not sure what type of string could cause BCrypt to crash.
Actual Behavior
The only behavior I can see is the following stack trace in our log files in our production environment. Since the password being hashed comes from the session which is entered by the user.
java.lang.ArrayIndexOutOfBoundsException: 0
org.springframework.security.crypto.bcrypt.BCrypt.streamtoword(BCrypt.java:410)
org.springframework.security.crypto.bcrypt.BCrypt.ekskey(BCrypt.java:466)
org.springframework.security.crypto.bcrypt.BCrypt.crypt_raw(BCrypt.java:508)
org.springframework.security.crypto.bcrypt.BCrypt.hashpw(BCrypt.java:590)
org.springframework.security.crypto.bcrypt.BCrypt.checkpw(BCrypt.java:659)
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.matches(BCryptPasswordEncoder.java:94)
org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:86)
org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166)
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:146)
org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
Expected Behavior
Not to throw an ArrayIndexOutOfBoundsException. If something like this occurs, not sure if it would be a good idea (It is kind of a security risk) to spit out the string it was trying to hash??
Configuration
<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">
<security:password-encoder ref="bcryptEncoder" />
</security:authentication-provider>
</security:authentication-manager>
Version
5.0..4
Sample
Can not give a sample because I do not have the necessary data.
Sorry for the confusion earlier. I meant to cancel the ticket once I had a little more information.
Figured out what is going on. If, somehow, a password passed in by the user is NULL, or Empty, the exception is thrown. Spring Security should really check to make sure the password is NOT NULL or Empty. Here is the fix for BCrypt. Not sure about the other encryption types, but there maybe a similar issue.
Change org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.matches()
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("Empty encoded password");
return false;
}
if (!BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
logger.warn("Encoded password does not look like BCrypt");
return false;
}
return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
}
TO:
public boolean matches(CharSequence rawPassword, String encodedPassword) {
if (rawPassword == null || rawPassword.length() == 0 ) {
logger.warn("Empty encoded password");
return false;
}
if (encodedPassword == null || encodedPassword.length() == 0) {
logger.warn("Empty encoded password");
return false;
}
if (!BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
logger.warn("Encoded password does not look like BCrypt");
return false;
}
return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
}
I would also suggest adding the same logic to:
org.springframework.security.crypto.bcrypt.Bcrypt.hashpw()