8000 [Ldap] Escape carriage returns in LDAP DNs. · symfony/ldap@ac765d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac765d7

Browse files
ChadSikorrafabpot
authored andcommitted
[Ldap] Escape carriage returns in LDAP DNs.
1 parent 2f425c8 commit ac765d7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

LdapClient.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ public function find($dn, $query, $filter = '*')
9999
*/
100100
public function escape($subject, $ignore = '', $flags = 0)
101101
{
102-
return ldap_escape($subject, $ignore, $flags);
102+
$value = ldap_escape($subject, $ignore, $flags);
103+
104+
// Per RFC 4514, leading/trailing spaces should be encoded in DNs, as well as carriage returns.
105+
if ((int) $flags & LDAP_ESCAPE_DN) {
106+
if (!empty($value) && $value[0] === ' ') {
107+
$value = '\\20'.substr($value, 1);
108+
}
109+
if (!empty($value) && $value[strlen($value) - 1] === ' ') {
110+
$value = substr($value, 0, -1).'\\20';
111+
}
112+
$value = str_replace("\r", '\0d', $value);
113+
}
114+
115+
return $value;
103116
}
104117

105118
private function connect()

Tests/LdapClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Tests;
13+
14+
use Symfony\Component\Ldap\LdapClient;
15+
use Symfony\Polyfill\Php56\Php56 as p;
16+
17+
/**
18+
* @requires extension ldap
19+
*/
20+
class LdapClientTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testLdapEscape()
23+
{
24+
$ldap = new LdapClient();
25+
26+
$this->assertEquals('\20foo\3dbar\0d(baz)*\20', $ldap->escape(" foo=bar\r(baz)* ", null, p::LDAP_ESCAPE_DN));
27+
}
28+
}

0 commit comments

Comments
 (0)
0