Skip to content

LDAP authentication should return a meaningful error when the LDAP server is unavailable #44896

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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 @@ -13,7 +13,7 @@

use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Ldap\Exception\ConnectionException;
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
use Symfony\Component\Ldap\LdapInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\LogicException;
Expand Down Expand Up @@ -99,7 +99,7 @@ public function onCheckPassport(CheckPassportEvent $event)
}

$ldap->bind($dn, $presentedPassword);
} catch (ConnectionException $e) {
} catch (InvalidCredentialsException $e) {
throw new BadCredentialsException('The presented password is invalid.');
}

Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Component/Ldap/Security/LdapUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Ldap\Security;

use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\ConnectionException;
use Symfony\Component\Ldap\Exception\ExceptionInterface;
use Symfony\Component\Ldap\LdapInterface;
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -74,17 +73,10 @@ public function loadUserByUsername(string $username)

public function loadUserByIdentifier(string $identifier): UserInterface
{
try {
$this->ldap->bind($this->searchDn, $this->searchPassword);
$identifier = $this->ldap->escape($identifier, '', LdapInterface::ESCAPE_FILTER);
$query = str_replace(['{username}', '{user_identifier}'], $identifier, $this->defaultSearch);
$search = $this->ldap->query($this->baseDn, $query);
} catch (ConnectionException $e) {
$e = new UserNotFoundException(sprintf('User "%s" not found.', $identifier), 0, $e);
$e->setUserIdentifier($identifier);

throw $e;
}
$this->ldap->bind($this->searchDn, $this->searchPassword);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could opt to catch InvalidCredentialsException here in order to throw something more-specific that would indicate it is the search bind credentials (used during enumeration) that are invalid, as opposed to the credentials that the user has provided.

$identifier = $this->ldap->escape($identifier, '', LdapInterface::ESCAPE_FILTER);
$query = str_replace(['{username}', '{user_identifier}'], $identifier, $this->defaultSearch);
$search = $this->ldap->query($this->baseDn, $query);

$entries = $search->execute();
$count = \count($entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Ldap\Adapter\CollectionInterface;
use Symfony\Component\Ldap\Adapter\QueryInterface;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\ConnectionException;
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
use Symfony\Component\Ldap\LdapInterface;
use Symfony\Component\Ldap\Security\CheckLdapCredentialsListener;
use Symfony\Component\Ldap\Security\LdapBadge;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function testBindFailureShouldThrowAnException()
$this->expectExceptionMessage('The presented password is invalid.');

$this->ldap->method('escape')->willReturnArgument(0);
$this->ldap->expects($this->any())->method('bind')->willThrowException(new ConnectionException());
$this->ldap->expects($this->any())->method('bind')->willThrowException(new InvalidCredentialsException());

$listener = $this->createListener();
$listener->onCheckPassport($this->createEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LdapUserProviderTest extends TestCase
{
public function testLoadUserByUsernameFailsIfCantConnectToLdap()
{
$this->expectException(UserNotFoundException::class);
$this->expectException(ConnectionException::class);

$ldap = $this->createMock(LdapInterface::class);
$ldap
Expand Down