Skip to content

Don't allow null User.principal #51988

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

Merged
merged 2 commits into from
Feb 7, 2020
Merged

Conversation

tvernum
Copy link
Contributor

@tvernum tvernum commented Feb 6, 2020

Some parts of the User class (e.g. equals/hashCode) assumed that
principal could never be null, but the constructor didn't enforce
that.

This adds a null check into the constructor and fixes a few tests that
relied on being able to pass in null usernames.

Some parts of the User class (e.g. equals/hashCode) assumed that
principal could never be null, but the constructor didn't enforce
that.

This adds a null check into the constructor and fixes a few tests that
relied on being able to pass in null usernames.
@tvernum tvernum added >non-issue :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v8.0.0 v7.7.0 labels Feb 6, 2020
@tvernum tvernum requested a review from jkakavas February 6, 2020 10:14
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security (:Security/Authentication)

@tvernum
Copy link
Contributor Author

tvernum commented Feb 6, 2020

This is some yak shaving for multiple authentication headers.

That PR got big, so I'm working on splitting out some standalone pieces.

@tvernum
Copy link
Contributor Author

tvernum commented Feb 7, 2020

@elasticmachine update branch

Copy link
Member

@jkakavas jkakavas left a comment

Choose a reason for hiding this comment

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

LGTM, 2 suggestions but fine to merge as is

threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, new Authentication(user, realmRef, null));
ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>());
processor = new SetSecurityUserProcessor("_tag", threadContext, "_field", EnumSet.allOf(Property.class));
public void testProcessorWithEmptyUserData() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

(need a mock, because a real user cannot have a null username)

Could we just remove this test case instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only if we also removed the code from the security processor that deals with nulls.
Which I considered, but I was already down a rabbit hole.

What the processor is doing makes sense - it doesn't make any assumptions about whether a User can have a null principal (although it would also be fine for it to rely on it not being null), so I decided not to bite off even more right now, and just leave it.

@@ -49,7 +50,7 @@ public User(String username, String[] roles, String fullName, String email, Map<

private User(String username, String[] roles, String fullName, String email, Map<String, Object> metadata, boolean enabled,
User authenticatedUser) {
this.username = username;
this.username = Objects.requireNonNull(username);
Copy link
Member

Choose a reason for hiding this comment

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

nit: I hate when I get NPE in a line that has many calls and it's not quite obvious what threw it so I prefer to add a message to requireNotNull. Not sure if this is worth rerunning CI here though, just a suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But this line doesn't have many calls. As long as you can link the stacktrace to the code it's clear why this line throughs NPE.

That said, I'd be happy for the whole codebase to have a forbidden API on requireNonNull without a message, but we don't.

@tvernum tvernum merged commit 3c94f80 into elastic:master Feb 7, 2020
tvernum added a commit to tvernum/elasticsearch that referenced this pull request Feb 7, 2020
Some parts of the User class (e.g. equals/hashCode) assumed that
principal could never be null, but the constructor didn't enforce
that.

This adds a null check into the constructor and fixes a few tests that
relied on being able to pass in null usernames.

Backport of: elastic#51988
tvernum added a commit that referenced this pull request Feb 10, 2020
Some parts of the User class (e.g. equals/hashCode) assumed that
principal could never be null, but the constructor didn't enforce
that.

This adds a null check into the constructor and fixes a few tests that
relied on being able to pass in null usernames.

Backport of: #51988
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v7.7.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants