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
|
// iterate over all higher roles from rolesReachableInOneStepMap
|
||||||
|
|
||||||
for (GrantedAuthority role : this.rolesReachableInOneStepMap.keySet()) {
|
for (GrantedAuthority role : this.rolesReachableInOneStepMap.keySet()) {
|
||||||
Set<GrantedAuthority> rolesToVisitSet = new HashSet<>();
|
Set<GrantedAuthority> rolesToVisitSet = new HashSet<>(this.rolesReachableInOneStepMap.get(role));
|
||||||
|
|
||||||
if (this.rolesReachableInOneStepMap.containsKey(role)) {
|
|
||||||
rolesToVisitSet.addAll(this.rolesReachableInOneStepMap.get(role));
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<GrantedAuthority> visitedRolesSet = new HashSet<>();
|
Set<GrantedAuthority> visitedRolesSet = new HashSet<>();
|
||||||
|
|
||||||
while (!rolesToVisitSet.isEmpty()) {
|
while (!rolesToVisitSet.isEmpty()) {
|
||||||
// take a role from the rolesToVisit set
|
// take a role from the rolesToVisit set
|
||||||
GrantedAuthority aRole = rolesToVisitSet.iterator().next();
|
GrantedAuthority aRole = rolesToVisitSet.iterator().next();
|
||||||
rolesToVisitSet.remove(aRole);
|
rolesToVisitSet.remove(aRole);
|
||||||
visitedRolesSet.add(aRole);
|
if (!visitedRolesSet.add(aRole) || !this.rolesReachableInOneStepMap.containsKey(aRole)) {
|
||||||
if (this.rolesReachableInOneStepMap.containsKey(aRole)) {
|
continue; // Already visited role or role with missing hierarchy
|
||||||
Set<GrantedAuthority> newReachableRoles = this.rolesReachableInOneStepMap
|
} else if (role.equals(aRole)) {
|
||||||
.get(aRole);
|
throw new CycleInRoleHierarchyException();
|
||||||
|
|
||||||
// definition of a cycle: you can reach the role you are starting from
|
|
||||||
if (rolesToVisitSet.contains(role)
|
|
||||||
|| visitedRolesSet.contains(role)) {
|
|
||||||
throw new CycleInRoleHierarchyException();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// no cycle
|
|
||||||
rolesToVisitSet.addAll(newReachableRoles);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
rolesToVisitSet.addAll(this.rolesReachableInOneStepMap.get(aRole));
|
||||||
}
|
}
|
||||||
this.rolesReachableInOneOrMoreStepsMap.put(role, visitedRolesSet);
|
this.rolesReachableInOneOrMoreStepsMap.put(role, visitedRolesSet);
|
||||||
|
|
||||||
|
|
|
@ -168,6 +168,12 @@ public class RoleHierarchyImplTests {
|
||||||
}
|
}
|
||||||
catch (CycleInRoleHierarchyException e) {
|
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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue