Fix infinite loop in role hierarchy resolving
Issue: gh-7035
This commit is contained in:
parent
2d36062846
commit
d3eaef66fc
|
@ -215,33 +215,19 @@ public class RoleHierarchyImpl implements RoleHierarchy {
|
|||
// iterate over all higher roles from rolesReachableInOneStepMap
|
||||
|
||||
for (GrantedAuthority role : this.rolesReachableInOneStepMap.keySet()) {
|
||||
Set<GrantedAuthority> rolesToVisitSet = new HashSet<>();
|
||||
|
||||
if (this.rolesReachableInOneStepMap.containsKey(role)) {
|
||||
rolesToVisitSet.addAll(this.rolesReachableInOneStepMap.get(role));
|
||||
}
|
||||
|
||||
Set<GrantedAuthority> rolesToVisitSet = new HashSet<>(this.rolesReachableInOneStepMap.get(role));
|
||||
Set<GrantedAuthority> visitedRolesSet = new HashSet<>();
|
||||
|
||||
while (!rolesToVisitSet.isEmpty()) {
|
||||
// take a role from the rolesToVisit set
|
||||
GrantedAuthority aRole = rolesToVisitSet.iterator().next();
|
||||
rolesToVisitSet.remove(aRole);
|
||||
visitedRolesSet.add(aRole);
|
||||
if (this.rolesReachableInOneStepMap.containsKey(aRole)) {
|
||||
Set<GrantedAuthority> newReachableRoles = this.rolesReachableInOneStepMap
|
||||
.get(aRole);
|
||||
|
||||
// definition of a cycle: you can reach the role you are starting from
|
||||
if (rolesToVisitSet.contains(role)
|
||||
|| visitedRolesSet.contains(role)) {
|
||||
if (!visitedRolesSet.add(aRole) || !this.rolesReachableInOneStepMap.containsKey(aRole)) {
|
||||
continue; // Already visited role or role with missing hierarchy
|
||||
} else if (role.equals(aRole)) {
|
||||
throw new CycleInRoleHierarchyException();
|
||||
}
|
||||
else {
|
||||
// no cycle
|
||||
rolesToVisitSet.addAll(newReachableRoles);
|
||||
}
|
||||
}
|
||||
rolesToVisitSet.addAll(this.rolesReachableInOneStepMap.get(aRole));
|
||||
}
|
||||
this.rolesReachableInOneOrMoreStepsMap.put(role, visitedRolesSet);
|
||||
|
||||
|
|
|
@ -168,6 +168,12 @@ public class RoleHierarchyImplTests {
|
|||
}
|
||||
catch (CycleInRoleHierarchyException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
roleHierarchyImpl.setHierarchy("ROLE_C > ROLE_B\nROLE_B > ROLE_A\nROLE_A > ROLE_B");
|
||||
fail("Cycle in role hierarchy was not detected!");
|
||||
} catch (CycleInRoleHierarchyException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue