Fail fast when UserDetailsService returns null in onLoginSuccess - #19552
Fail fast when UserDetailsService returns null in onLoginSuccess#19552skdas20 wants to merge 1 commit into
Conversation
|
I guess my objection to this strategy is that, while it does conform to the guards found in Two lines above your proposed change, if the username cannot be obtained it does not fail with an assertion error, it simply returns without generating a cookie: Two lines below your proposed change, if the user can be obtained but has no password, it does not fail with an assertion error, it ALSO simply returns without generating a cookie: IMHO - the implication is that |
onLoginSuccess dereferenced the result of loadUserByUsername without a null check, so a UserDetailsService returning null instead of throwing UsernameNotFoundException surfaced as a bare NullPointerException from inside the remember-me filter. Treat a null user the same way the surrounding code treats a missing username and a missing password: log at debug and return without generating a cookie. Closes spring-projectsgh-19535 Signed-off-by: skdas20 <skdas5405@gmail.com>
|
@skylarsutton that's a fair point and I've switched to it — thanks for pushing back. You're right that the local pattern is the more relevant one. I anchored on Now reads: UserDetails user = getUserDetailsService().loadUserByUsername(username);
if (user == null) {
this.logger.debug("Unable to obtain user details for user: " + username);
return;
}
password = user.getPassword();The test asserts no remember-me cookie is written rather than expecting an exception, and the commit message has been updated to match. Full class is green (26/26). |
b6affdb to
31fc0ab
Compare
When the successful
Authenticationcarries no credentials,onLoginSuccessfalls back to looking the password up through the configuredUserDetailsService, but dereferences the result without checking it:A
UserDetailsServicethat returnsnullrather than throwingUsernameNotFoundExceptiontherefore shows up as a bareNullPointerExceptionfrom inside the remember-me filter, with nothing in the stack trace pointing at the service that is actually misconfigured.processAutoLoginCookiein this same class already guards the identical call, and is explicit about what anullreturn means:autoLoginClearsCookieIfUserServiceMisconfiguredpins that behaviour in the tests. This change applies the same guard toonLoginSuccessso both lookups report the misconfiguration the same way, and addsloginSuccessFailsIfUserServiceMisconfiguredalongside the existingloginSuccesstests. Without the production change that test fails with the reportedNullPointerException.One note on the approach: gh-19535 suggests skipping cookie generation instead, with
I went with the existing in-class precedent rather than that, because silently continuing would quietly swallow a misconfiguration that the class elsewhere treats as a contract violation — and the user would get no remember-me cookie with no indication why. Happy to switch to the lenient form if you'd rather keep
onLoginSuccesstolerant; it's a small change either way.Closes gh-19535