Skip to content
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

NullPointerException thrown if principal or credentials are null #538

Closed
philwebb opened this issue Aug 16, 2019 · 8 comments
Closed

NullPointerException thrown if principal or credentials are null #538

philwebb opened this issue Aug 16, 2019 · 8 comments
Assignees
Labels
Milestone

Comments

@philwebb
Copy link
Member

Originally raised in spring-projects/spring-boot#17861

The org.springframework.ldap.core.support.AbstractContextSource class has a setupAuthenticatedEnvironment which is passed a principal and credentials. If either of these are null a NullPointerException is thrown because null values are not permitted in a Hashtable.

I think it might be better if DirContextAuthenticationStrategy implementations could guard against null values and not add entries in such cases.

@filiphr
Copy link

filiphr commented Aug 16, 2019

Thanks for raising the issue in spring-ldap @philwebb.

Just some extra info. There could be a guard when setting the user and password in the AbstractContextSource. Something like that is already done for setBase.

@rwinch
Copy link
Member

rwinch commented Aug 21, 2019

Thanks for the report @philwebb

I don't think I agree with not adding null entries. If the credentials are null and anonymousReadOnly = false, then the user should get an error because it is misconfigured. Is there a reason you would expect it to ignore the credentials?

@philwebb
Copy link
Member Author

@rwinch I'd have to defer to @filiphr on that.

My original understanding of the issue was that if the credentials are null you get an exception regardless of the anonymousReadOnly setting. I think Boot triggered the error because it called setAnonymousReadOnly(true), setUserDn(null) and setPassword(null). Those last two call changed the defaults from an empty string to null.

I guess a better exception message is needed regardless. Perhaps it's just a case of doing Assert.notNull in the setUserDn/setPassword methods.

@philwebb
Copy link
Member Author

BTW, we fixed Boot in the meantime to not call the setters with null.

@filiphr
Copy link

filiphr commented Aug 22, 2019

Was looking at the master code of spring-ldap and I saw that this should not be an issue anymore if the user is set to null, since anonymousReadOnly is set to true when there is no userDn. Was actually fixed as part of #473. Which is the exact problem that I was seeing. And the best thing is that this was already reported by @philwebb due to a similar problem in Spring Boot.

However, if the user is set and the password is somehow set to null then the same NPE would occur again.

The logging message if the password has no text is a bit misleading as well

LOG.info("Property 'password' not set - " + "blank password will be used");

Which is not quite correct by looking at the code. Since the password would be send as is (so a null in this case).

@rwinch
Copy link
Member

rwinch commented Aug 22, 2019

Thanks for the additional details @filiphr and @philwebb! A better error message would be useful.

Perhaps it's just a case of doing Assert.notNull in the setUserDn/setPassword methods.

I don't think this will help much since the values are null by default. This means if a user doesn't set them at all, the improved error message wouldn't be used.

@filiphr Would you be interested in sending a PR to clean up the error message and logging?

@filiphr
Copy link

filiphr commented Aug 22, 2019

@rwinch yes I can try to craft a PR, but what I don't get what the resolution is now?

If you ask me the fix should be in

class SimpleAuthenticationSource implements AuthenticationSource {
public String getPrincipal() {
return userDn;
}
public String getCredentials() {
return password;
}
}

And return empty string if the userDn or password is null. That is what is being done in SpringSecurityAuthenticationSource.

btw the values are empty string by default, not null, that is why not setting null works :)

@jzheaux
Copy link
Contributor

jzheaux commented Dec 14, 2021

Hi, @filiphr. I think the fix is based on Rob's comment here:

If the credentials are null and anonymousReadOnly = false, then the user should get an error because it is misconfigured.

Given that, empty creds should not be inferred from null creds. Instead, the application should error to alert the user.

And return empty string if the userDn or password is null.

I can see your point here, though I believe there is a difference between the framework inferring an empty string and the context source setting a different value than what the application asked for. That is, calling setPassword(null) is more likely an indication of an application error, and we should alert instead of infer.

@jzheaux jzheaux added this to the 2.4.0-M1 milestone Dec 14, 2021
@jzheaux jzheaux modified the milestones: 2.4.0-M1, 2.4.0-M2 Jan 13, 2022
@jzheaux jzheaux modified the milestones: 2.4.0-M2, 2.4.0-M3 Feb 17, 2022
@jzheaux jzheaux modified the milestones: 2.4.0-M3, 2.4.x Mar 24, 2022
jzheaux added a commit that referenced this issue May 12, 2022
@jzheaux jzheaux modified the milestones: 2.4.x, 2.4.0 May 12, 2022
@jzheaux jzheaux self-assigned this May 12, 2022
@jzheaux jzheaux added type: bug A general bug in: core labels May 12, 2022
jzheaux added a commit that referenced this issue May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants