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 detecting cyclic group memberships #25757

Merged
merged 1 commit into from
Mar 1, 2021
Merged
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
31 changes: 21 additions & 10 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public function getDynamicGroupMembers(string $dnGroup): array {
private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
if ($seen === null) {
$seen = [];
// the root entry has to be marked as processed to avoind infinit loops,
// but not included in the results laters on
$excludeFromResult = $dnGroup;
}
$allMembers = [];
if (array_key_exists($dnGroup, $seen)) {
Expand Down Expand Up @@ -290,13 +293,19 @@ private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
$seen[$dnGroup] = 1;
$members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr);
if (is_array($members)) {
$fetcher = function ($memberDN, &$seen) {
$fetcher = function ($memberDN) use (&$seen) {
return $this->_groupMembers($memberDN, $seen);
};
$allMembers = $this->walkNestedGroups($dnGroup, $fetcher, $members);
$allMembers = $this->walkNestedGroups($dnGroup, $fetcher, $members, $seen);
}

$allMembers += $this->getDynamicGroupMembers($dnGroup);
if (isset($excludeFromResult)) {
$index = array_search($excludeFromResult, $allMembers, true);
if ($index !== false) {
unset($allMembers[$index]);
}
}

$this->access->connection->writeToCache($cacheKey, $allMembers);
if (isset($attemptedLdapMatchingRuleInChain)
Expand Down Expand Up @@ -335,7 +344,7 @@ private function _getGroupDNsFromMemberOf(string $dn): array {
return $this->filterValidGroups($groups);
}

private function walkNestedGroups(string $dn, Closure $fetcher, array $list): array {
private function walkNestedGroups(string $dn, Closure $fetcher, array $list, array &$seen = []): array {
$nesting = (int)$this->access->connection->ldapNestedGroups;
// depending on the input, we either have a list of DNs or a list of LDAP records
// also, the output expects either DNs or records. Testing the first element should suffice.
Expand All @@ -354,19 +363,21 @@ private function walkNestedGroups(string $dn, Closure $fetcher, array $list): ar
return $list;
}

$seen = [];
while ($record = array_shift($list)) {
$recordDN = $recordMode ? $record['dn'][0] : $record;
$recordDN = $record['dn'][0] ?? $record;
if ($recordDN === $dn || array_key_exists($recordDN, $seen)) {
// Prevent loops
continue;
}
$fetched = $fetcher($record, $seen);
$fetched = $fetcher($record);
$list = array_merge($list, $fetched);
$seen[$recordDN] = $record;
if (!isset($seen[$recordDN]) || is_bool($seen[$recordDN]) && is_array($record)) {
$seen[$recordDN] = $record;
}
}

return $recordMode ? $seen : array_keys($seen);
// on record mode, filter out intermediate state
return $recordMode ? array_filter($seen, 'is_array') : array_keys($seen);
}

/**
Expand Down Expand Up @@ -841,7 +852,7 @@ private function getGroupsByMember(string $dn, array &$seen = null): array {
$groups = $this->access->fetchListOfGroups($filter,
[strtolower($this->access->connection->ldapGroupMemberAssocAttr), $this->access->connection->ldapGroupDisplayName, 'dn']);
if (is_array($groups)) {
$fetcher = function ($dn, &$seen) {
$fetcher = function ($dn) use (&$seen) {
if (is_array($dn) && isset($dn['dn'][0])) {
$dn = $dn['dn'][0];
}
Expand All @@ -852,7 +863,7 @@ private function getGroupsByMember(string $dn, array &$seen = null): array {
$dn = "";
}

$allGroups = $this->walkNestedGroups($dn, $fetcher, $groups);
$allGroups = $this->walkNestedGroups($dn, $fetcher, $groups, $seen);
}
$visibleGroups = $this->filterValidGroups($allGroups);
return array_intersect_key($allGroups, $visibleGroups);
Expand Down