TokenBasedRememberMeServices::onLoginSuccess can throw a NPE when UserDetailsService::loadUserByUsername returns null.
When the Authentication does not contain a password, TokenBasedRememberMeServices::onLoginSuccess performs a call to UserDetailsService::loadUserByUsername (~line 227), to obtain a UserDetails object.
It then proceeds to read UserDetails::getPassword without performing a null check.
In our workflow only a subset of usernames support "remember me", meaning the three possible responses from UserDetailsService::loadUserByUsername are:
- When the username exists and supports remember me --> an instance of
UserDetails - When the username exists and does NOT support remember me --> a null response
- When the username does NOT exist --> a
UsernameNotFoundException
Because TokenBasedRememberMeServices::onLoginSuccess is designed to simply return without generating a cookie when the password cannot be obtained, this issue can be fixed by simply changing line 228
from:
password = user.getPassword();
to:
password = (user == null) ? password : user.getPassword();
I'd like to take this one. The fix is a null check before user.getPassword() so onLoginSuccess bails out the same way it already does for an empty password, plus a test built on the existing loadUserByUsername → null setup in TokenBasedRememberMeServicesTests. Happy to wait for triage if you'd rather.
Scratch my comment above — #19552 already covers this, so I've closed #19672 in its favour.