spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

TokenBasedRememberMeServices::onLoginSuccess can throw a NPE when UserDetailsService::loadUserByUsername returns null.

Open skylarsutton opened this issue 3 months ago • 2 comments

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:

  1. When the username exists and supports remember me --> an instance of UserDetails
  2. When the username exists and does NOT support remember me --> a null response
  3. 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();

skylarsutton avatar Aug 12 '26 14:08 skylarsutton

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.

GangEunzzang avatar Sep 07 '26 06:09 GangEunzzang

Scratch my comment above — #19552 already covers this, so I've closed #19672 in its favour.

GangEunzzang avatar Sep 08 '26 22:09 GangEunzzang