Skip to content

Commit 231091c

Browse files
gbradleyGraham Bradley
and
Graham Bradley
authored
[11.x] Gracefully handle null passwords when verifying credentials (#53156)
* Gracefully handle null passwords when verifying credentials * Removed method calls in tests. --------- Co-authored-by: Graham Bradley <gbradley@onlyexcel.com>
1 parent d62e92d commit 231091c

4 files changed

+40
-5
lines changed

src/Illuminate/Auth/DatabaseUserProvider.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,15 @@ protected function getGenericUser($user)
154154
*/
155155
public function validateCredentials(UserContract $user, #[\SensitiveParameter] array $credentials)
156156
{
157-
return $this->hasher->check(
158-
$credentials['password'], $user->getAuthPassword()
159-
);
157+
if (is_null($plain = $credentials['password'])) {
158+
return false;
159+
}
160+
161+
if (is_null($hashed = $user->getAuthPassword())) {
162+
return false;
163+
}
164+
165+
return $this->hasher->check($plain, $hashed);
160166
}
161167

162168
/**

src/Illuminate/Auth/EloquentUserProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ public function validateCredentials(UserContract $user, #[\SensitiveParameter] a
152152
return false;
153153
}
154154

155-
return $this->hasher->check($plain, $user->getAuthPassword());
155+
if (is_null($hashed = $user->getAuthPassword())) {
156+
return false;
157+
}
158+
159+
return $this->hasher->check($plain, $hashed);
156160
}
157161

158162
/**

tests/Auth/AuthDatabaseUserProviderTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testCredentialValidation()
162162
$this->assertTrue($result);
163163
}
164164

165-
public function testCredentialValidationFailed()
165+
public function testCredentialValidationFails()
166166
{
167167
$conn = m::mock(Connection::class);
168168
$hasher = m::mock(Hasher::class);
@@ -175,6 +175,19 @@ public function testCredentialValidationFailed()
175175
$this->assertFalse($result);
176176
}
177177

178+
public function testCredentialValidationFailsGracefullyWithNullPassword()
179+
{
180+
$conn = m::mock(Connection::class);
181+
$hasher = m::mock(Hasher::class);
182+
$hasher->shouldReceive('check')->never();
183+
$provider = new DatabaseUserProvider($conn, $hasher, 'foo');
184+
$user = m::mock(Authenticatable::class);
185+
$user->shouldReceive('getAuthPassword')->once()->andReturn(null);
186+
$result = $provider->validateCredentials($user, ['password' => 'plain']);
187+
188+
$this->assertFalse($result);
189+
}
190+
178191
public function testRehashPasswordIfRequired()
179192
{
180193
$hasher = m::mock(Hasher::class);

tests/Auth/AuthEloquentUserProviderTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ public function testCredentialValidationFailed()
152152
$this->assertFalse($result);
153153
}
154154

155+
public function testCredentialValidationFailsGracefullyWithNullPassword()
156+
{
157+
$hasher = m::mock(Hasher::class);
158+
$hasher->shouldReceive('check')->never();
159+
$provider = new EloquentUserProvider($hasher, 'foo');
160+
$user = m::mock(Authenticatable::class);
161+
$user->shouldReceive('getAuthPassword')->once()->andReturn(null);
162+
$result = $provider->validateCredentials($user, ['password' => 'plain']);
163+
164+
$this->assertFalse($result);
165+
}
166+
155167
public function testRehashPasswordIfRequired()
156168
{
157169
$hasher = m::mock(Hasher::class);

0 commit comments

Comments
 (0)