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

fix(authentication): Handle null or empty string password hash #36653

Merged
merged 1 commit into from
Feb 20, 2023

Conversation

nickvergessen
Copy link
Member

@nickvergessen nickvergessen commented Feb 10, 2023

This can happen when the auth.storeCryptedPassword config is used, which previously errored with:
Hasher::verify(): Argument #2 ($hash) must be of type string, null given

As observed in https://drone.nextcloud.com/nextcloud/spreed/11732/1/4

Checklist

This can happen when the auth.storeCryptedPassword config is used,
which previously errored with:
Hasher::verify(): Argument #2 ($hash) must be of type string, null given

Signed-off-by: Joas Schilling <coding@schilljs.com>
@nickvergessen nickvergessen added this to the Nextcloud 26 milestone Feb 10, 2023
@nickvergessen nickvergessen requested review from juliusknorr, come-nc and a team February 10, 2023 08:22
@nickvergessen nickvergessen self-assigned this Feb 10, 2023
@nickvergessen nickvergessen requested review from ArtificialOwl and icewind1991 and removed request for a team February 10, 2023 08:22
@@ -113,7 +113,7 @@ public function generateToken(string $token,
// We need to check against one old token to see if there is a password
// hash that we can reuse for detecting outdated passwords
$randomOldToken = $this->mapper->getFirstTokenForUser($uid);
$oldTokenMatches = $randomOldToken && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
$oldTokenMatches = $randomOldToken && $randomOldToken->getPasswordHash() && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$oldTokenMatches = $randomOldToken && $randomOldToken->getPasswordHash() && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
$oldTokenMatches = $randomOldToken && ($pwHash = $randomOldToken->getPasswordHash()) && $this->hasher->verify(sha1($password) . $password, $pwHash);

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't do inline assignments in Nextcloud, also it's a just replacign a local member call here, so not sure it's worth the possible confusion.

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is not to avoid the local call, it’s that you cannot know that getPasswordHash will return the same thing twice, so checking it returns a non-falsy value and then calling it againg and using it as an object is dangerous.
I’m not sure how is the pretty way to write this, and I do dislike the inline assignment, I used it to show the smalest change possible to show the idea.

psalm will yell at you for doing this kind of stuff. Especially when most of the time we only know the interface used and not the class.

Suggested change
$oldTokenMatches = $randomOldToken && $randomOldToken->getPasswordHash() && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
if ($randomOldToken) {
$pwHash = $randomOldToken->getPasswordHash();
$oldTokenMatches = $pwHash && $this->hasher->verify(sha1($password) . $password, $pwHash);
} else {
$oldTokenMatches = false;
}

Or if PHP>=8:

Suggested change
$oldTokenMatches = $randomOldToken && $randomOldToken->getPasswordHash() && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
$pwHash = $randomOldToken?->getPasswordHash();
$oldTokenMatches = $pwHash && $this->hasher->verify(sha1($password) . $password, $pwHash);

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem is not to avoid the local call, it’s that you cannot know that getPasswordHash will return the same thing twice

We have code like this with all our "Entities" all the time? How could the existing object change in the meantime?

Copy link
Contributor

@come-nc come-nc left a comment

Choose a reason for hiding this comment

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

I still don’t like it, and I’m surprised psalm does not complain on such things.

@nickvergessen nickvergessen merged commit c550aca into master Feb 20, 2023
@nickvergessen nickvergessen deleted the bugfix/noid/more-defensive-old-token-handling branch February 20, 2023 10:15
@skjnldsv skjnldsv mentioned this pull request Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants