Skip to content

[Ldap] Incorrect determination of RelativeDistinguishedName for the "move" operation #39518

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
Dec 17, 2020
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
6 changes: 3 additions & 3 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/EntryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function addAttributeValues(Entry $entry, string $attribute, array $value
$con = $this->getConnectionResource();

if (!@ldap_mod_add($con, $entry->getDn(), [$attribute => $values])) {
throw new LdapException(sprintf('Could not add values to entry "%s", attribute %s: ', $entry->getDn(), $attribute).ldap_error($con));
throw new LdapException(sprintf('Could not add values to entry "%s", attribute "%s": ', $entry->getDn(), $attribute).ldap_error($con));
}
}

Expand All @@ -94,7 +94,7 @@ public function removeAttributeValues(Entry $entry, string $attribute, array $va
$con = $this->getConnectionResource();

if (!@ldap_mod_del($con, $entry->getDn(), [$attribute => $values])) {
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute %s: ', $entry->getDn(), $attribute).ldap_error($con));
throw new LdapException(sprintf('Could not remove values from entry "%s", attribute "%s": ', $entry->getDn(), $attribute).ldap_error($con));
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public function applyOperations(string $dn, iterable $operations): void

private function parseRdnFromEntry(Entry $entry): string
{
if (!preg_match('/^([^,]+),/', $entry->getDn(), $matches)) {
if (!preg_match('/(^[^,\\\\]*(?:\\\\.[^,\\\\]*)*),/', $entry->getDn(), $matches)) {
Copy link
Member

Choose a reason for hiding this comment

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

the regexp won't work when several \ are used, eg with \\\\ or \\\\\,,isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number of \ shouldn't matter. Compared to the old code, basically the same is done, except that now the comma is not matched if it is escaped with a \.

It's a bit tricky with the "not matching regex groups" (starting with ?: ). It is quite hard to read and understand.

throw new LdapException(sprintf('Entry "%s" malformed, could not parse RDN.', $entry->getDn()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,34 @@ public function testGetResources()
$entryManager = new EntryManager($connection);
$entryManager->update($entry);
}

/**
* @see https://tools.ietf.org/html/rfc4514#section-3
*
* @dataProvider moveWithRFC4514DistinguishedNameProvider
*/
public function testMoveWithRFC4514DistinguishedName(string $dn, string $expectedRdn)
{
$connection = $this->createMock(Connection::class);

$entry = new Entry($dn);
$entryManager = new EntryManager($connection);

$method = (new \ReflectionClass(EntryManager::class))->getMethod('parseRdnFromEntry');
$method->setAccessible(true);

$cn = $method->invokeArgs($entryManager, [$entry, 'a']);

$this->assertSame($expectedRdn, $cn);
}

public function moveWithRFC4514DistinguishedNameProvider(): array
{
return [
['CN=Simple,DC=example,DC=net', 'CN=Simple'],
['CN=James \"Jim\" Smith\, III,DC=example,DC=net', 'CN=James \"Jim\" Smith\, III'],
Copy link
Member

Choose a reason for hiding this comment

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

Sorry if it's a dumb question: should the expected value be 'CN=James "Jim" Smith, III'?

Copy link
Contributor Author

@astepin astepin Dec 17, 2020

Choose a reason for hiding this comment

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

Certain characters in the canonical name must be escaped for them to work with LDAP server.

So to continue working with the LDAP server we need the backslash in front of the quotes. => CN=James \"Jim\" Smith\, III

['UID=jsmith,DC=example,DC=net', 'UID=jsmith'],
["CN=Before\0dAfter,DC=example,DC=net", "CN=Before\0dAfter"],
];
}
}