Skip to content

[Ldap] Escape carriage returns in LDAP DNs. #16842

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Escape carriage returns in LDAP DNs.
  • Loading branch information
ChadSikorra committed Dec 5, 2015
commit f80c7d78cba353da7fa78b74e9d51a74b6a8f83f
15 changes: 14 additions & 1 deletion src/Symfony/Component/Ldap/LdapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ public function find($dn, $query, $filter = '*')
*/
public function escape($subject, $ignore = '', $flags = 0)
{
return ldap_escape($subject, $ignore, $flags);
$value = ldap_escape($subject, $ignore, $flags);

// Per RFC 4514, leading/trailing spaces should be encoded in DNs, as well as carriage returns.
if ((int)$flags & LDAP_ESCAPE_DN) {
if (!empty($value) && $value[0] === ' ') {
$value = '\\20' . substr($value, 1);
}
if (!empty($value) && $value[strlen($value) - 1] === ' ') {
$value = substr($value, 0, -1) . '\\20';
}
$value = str_replace("\r", '\0d', $value);
}

return $value;
}

private function connect()
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Ldap/Tests/LdapClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Ldap\Tests;

use Symfony\Component\Ldap\LdapClient;
use Symfony\Polyfill\Php56\Php56 as p;

class LdapClientTest extends \PHPUnit_Framework_TestCase
Copy link
Contributor

Choose a reason for hiding this comment

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

You should skip the test if the extension is not enabled, which is the case in one of the two Appveyor builds (one is run with the extension, the other without).

{
public function testLdapEscape()
{
$ldap = new LdapClient();

$this->assertEquals('\20foo\3dbar\0d(baz)*\20', $ldap->escape(" foo=bar\r(baz)* ", null, p::LDAP_ESCAPE_DN));
}
}