Skip to content
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

Fix: Escape group names for LDAP #37201

Merged
merged 5 commits into from
Oct 2, 2023
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
4 changes: 1 addition & 3 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -1421,9 +1421,7 @@ public function escapeFilterPart($input, $allowAsterisk = false): string {
$asterisk = '*';
$input = mb_substr($input, 1, null, 'UTF-8');
}
$search = ['*', '\\', '(', ')'];
$replace = ['\\*', '\\\\', '\\(', '\\)'];
return $asterisk . str_replace($search, $replace, $input);
return $asterisk . ldap_escape($input, '', LDAP_ESCAPE_FILTER);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions apps/user_ldap/lib/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@
if (is_array($objcs) && count($objcs) > 0) {
$filter .= '(|';
foreach ($objcs as $objc) {
$filter .= '(objectclass=' . $objc . ')';
$filter .= '(objectclass=' . ldap_escape($objc, '', LDAP_ESCAPE_FILTER) . ')';
}
$filter .= ')';
$parts++;
Expand All @@ -925,7 +925,7 @@
}
$base = $this->configuration->ldapBase[0];
foreach ($cns as $cn) {
$rr = $this->ldap->search($cr, $base, 'cn=' . $cn, ['dn', 'primaryGroupToken']);
$rr = $this->ldap->search($cr, $base, 'cn=' . ldap_escape($cn, '', LDAP_ESCAPE_FILTER), ['dn', 'primaryGroupToken']);

Check notice

Code scanning / Psalm

PossiblyNullArgument Note

Argument 2 of OCA\User_LDAP\ILDAPWrapper::search cannot be null, possibly null value provided
if (!$this->ldap->isResource($rr)) {
continue;
}
Expand All @@ -936,10 +936,10 @@
if ($dn === false || $dn === '') {
continue;
}
$filterPart = '(memberof=' . $dn . ')';
$filterPart = '(memberof=' . ldap_escape($dn, '', LDAP_ESCAPE_FILTER) . ')';
if (isset($attrs['primaryGroupToken'])) {
$pgt = $attrs['primaryGroupToken'][0];
$primaryFilterPart = '(primaryGroupID=' . $pgt .')';
$primaryFilterPart = '(primaryGroupID=' . ldap_escape($pgt, '', LDAP_ESCAPE_FILTER) .')';
$filterPart = '(|' . $filterPart . $primaryFilterPart . ')';
}
$filter .= $filterPart;
Expand All @@ -963,7 +963,7 @@
if (is_array($objcs) && count($objcs) > 0) {
$filter .= '(|';
foreach ($objcs as $objc) {
$filter .= '(objectclass=' . $objc . ')';
$filter .= '(objectclass=' . ldap_escape($objc, '', LDAP_ESCAPE_FILTER) . ')';
}
$filter .= ')';
$parts++;
Expand All @@ -973,7 +973,7 @@
if (is_array($cns) && count($cns) > 0) {
$filter .= '(|';
foreach ($cns as $cn) {
$filter .= '(cn=' . $cn . ')';
$filter .= '(cn=' . ldap_escape($cn, '', LDAP_ESCAPE_FILTER) . ')';
}
$filter .= ')';
}
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ public function testEscapeFilterPartValidChars() {

public function testEscapeFilterPartEscapeWildcard() {
$input = '*';
$expected = '\\\\*';
$expected = '\\2a';
$this->assertTrue($expected === $this->access->escapeFilterPart($input));
}

public function testEscapeFilterPartEscapeWildcard2() {
$input = 'foo*bar';
$expected = 'foo\\\\*bar';
$expected = 'foo\\2abar';
$this->assertTrue($expected === $this->access->escapeFilterPart($input));
}

Expand Down
Loading