[Security\Core] Fix user enumeration via response body on invalid credentials

This commit is contained in:
Robin Chalas 2021-05-13 12:05:25 +02:00 committed by Nicolas Grekas
parent d0d17db1c5
commit e85070088e
2 changed files with 21 additions and 2 deletions

View File

@ -84,8 +84,8 @@ abstract class UserAuthenticationProvider implements AuthenticationProviderInter
$this->userChecker->checkPreAuth($user);
$this->checkAuthentication($user, $token);
$this->userChecker->checkPostAuth($user);
} catch (AccountStatusException $e) {
if ($this->hideUserNotFoundExceptions) {
} catch (AuthenticationException $e) {
if ($this->hideUserNotFoundExceptions && ($e instanceof AccountStatusException || $e instanceof BadCredentialsException)) {
throw new BadCredentialsException('Bad credentials.', 0, $e);
}

View File

@ -18,6 +18,7 @@ use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Core\User\UserInterface;
class UserAuthenticationProviderTest extends TestCase
{
@ -62,6 +63,24 @@ class UserAuthenticationProviderTest extends TestCase
$provider->authenticate($this->getSupportedToken());
}
public function testAuthenticateWhenCredentialsAreInvalidAndHideIsTrue()
{
$provider = $this->getProvider();
$provider->expects($this->once())
->method('retrieveUser')
->willReturn($this->createMock(UserInterface::class))
;
$provider->expects($this->once())
->method('checkAuthentication')
->willThrowException(new BadCredentialsException())
;
$this->expectException(BadCredentialsException::class);
$this->expectExceptionMessage('Bad credentials.');
$provider->authenticate($this->getSupportedToken());
}
/**
* @group legacy
*/