spring-projects / spring-projects/spring-security

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

Open Beginner friendly
#19,535 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

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();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at TokenBasedRememberMeServices::onLoginSuccess around line 227 and inspect the path where Authentication has no password and loadUserByUsername returns null. Done means this case returns without generating a cookie or throwing a NullPointerException, while existing UserDetails and UsernameNotFoundException behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, backend, security
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.