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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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?


$dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember);

Expand Down