Skip to content

[Ldap] fail if whoami() is called before saslBind() #58144

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

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Ldap\Exception\ConnectionTimeoutException;
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Exception\NotBoundException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -116,6 +117,10 @@ public function saslBind(?string $dn = null, #[\SensitiveParameter] ?string $pas
*/
public function whoami(): string
{
if (!$this->connection) {
throw new NotBoundException(\sprintf('Cannot execute "%s()" before calling "%s::saslBind()".', __METHOD__, __CLASS__));
}

if (false === $authzId = ldap_exop_whoami($this->connection)) {
throw new LdapException(ldap_error($this->connection));
}
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,39 @@ public function testLdapEscape()
*/
public function testSaslBind()
{
$h = @ldap_connect(getenv('LDAP_HOST'), getenv('LDAP_PORT'));
@ldap_set_option($h, \LDAP_OPT_PROTOCOL_VERSION, 3);

if (!$h || !@ldap_bind($h)) {
$this->markTestSkipped('No server is listening on LDAP_HOST:LDAP_PORT');
}

if (!@ldap_start_tls($h)) {
ldap_unbind($h);
$this->markTestSkipped('Cannot establish an encrypted connection');
Copy link
Member Author

Choose a reason for hiding this comment

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

Investigating the failing integration test I found that SASL bind cannot be used when not having a secure connection. Since I didn't find a way to let our LDAP server used in the test have a proper TLS configuration I suggest that we skip tests in those cases.

Copy link
Member

Choose a reason for hiding this comment

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

Spent an hour investigating on this and I have the same conclusion. I tried a few things to enable TLS with bitnami/openldap but it would require additional work, more than just turning TLS switch to "on" it seems.

}

ldap_unbind($h);

$ldap = new Adapter($this->getLdapConfig());

$ldap->getConnection()->saslBind('cn=admin,dc=symfony,dc=com', 'symfony');
$this->assertEquals('cn=admin,dc=symfony,dc=com', $ldap->getConnection()->whoami());
}

/**
* @group functional
*/
public function testWhoamiWithoutSaslBind()
{
$ldap = new Adapter($this->getLdapConfig());

$this->expectException(NotBoundException::class);
$this->expectExceptionMessage('Cannot execute "Symfony\Component\Ldap\Adapter\ExtLdap\Connection::whoami()" before calling "Symfony\Component\Ldap\Adapter\ExtLdap\Connection::saslBind()".');

$ldap->getConnection()->whoami();
}

/**
* @group functional
*/
Expand Down