Skip to content

Fix NPE in TokenBasedRememberMeServices with null UserDetails #7252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

codeconsole
Copy link
Contributor

Fixes #7251

Checks to see if the userService found a userDetails or not. If the user has since been deleted, this will end up with an uncaught null pointer exception and result in the user not being able to visit the site.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 11, 2019
Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the report and PR @codeconsole . I have left some feedback inline.

@@ -123,6 +123,11 @@ protected UserDetails processAutoLoginCookie(String[] cookieTokens,
UserDetails userDetails = getUserDetailsService().loadUserByUsername(
cookieTokens[0]);

if (userDetails == null) {
throw new InvalidCookieException("Cookie token[0] contained username '"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the contract of the loadUserByUsername method, it should never return null if the user is not found. Instead it should throw a UsernameNotFoundException. Therefore, a more appropriate error message would be that there is an interface contract violation. Here are 2 examples of where we already do this:


and

throw new InvalidCookieException("Cookie token[0] contained username '"
+ cookieTokens[0] + "' that does not exist.");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this scenario in TokenBasedRememberMeServicesTests. The test will be very similar to autoLoginClearsCookieIfUserNotFound.

@rwinch rwinch changed the title Check that userdetails for username exists. #7251 Fix NPE in TokenBasedRememberMeServices with null UserDetails Aug 14, 2019
@eleftherias eleftherias self-assigned this Aug 14, 2019
@eleftherias
Copy link
Contributor

@codeconsole I've added your commit to master and followed up with a polish commit that adds the changes I mentioned.

@eleftherias eleftherias closed this Sep 3, 2019
@eleftherias eleftherias added in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 19, 2019
@eleftherias eleftherias added this to the 5.2.0.RC1 milestone Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TokenBasedRememberMeServices.processAutoLoginCookie (TokenBasedRememberMeServices.java:134) java.lang.NullPointerException
3 participants