Skip to content

Infinite loop in role hierarchy resolving #7035

Closed

Description

@karelmaxa

Summary

spring-security allows to define hierarchical roles to offer a convenient means of simplifying the access-control configuration data. When you provide hierarchy definition with cycle that does not include currently resolved role, resolving process will fall into infinite loop.

Actual Behavior

Resolving of role hierarchy is not able to correctly detect cycle when currently resolved role is not included in the cycle. Resolving process falls into infinite loop.

Expected Behavior

CycleInRoleHierarchyException should be thrown.

Version

5.2.0.M3, 5.1.5.RELEASE

Sample

Add provided test fragment into RoleHierarchyImplTests#testCyclesInRoleHierarchy [1].

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) {
}

I am probably able to resolve the issue and submit pull request. But i have to say that current implementation of role hierarchy resolving looks little strange. For example this condition [2] is completely useless?.

[1] https://github.com/spring-projects/spring-security/blob/5.1.5.RELEASE/core/src/test/java/org/springframework/security/access/hierarchicalroles/RoleHierarchyImplTests.java#L139
[2] https://github.com/spring-projects/spring-security/blob/5.1.5.RELEASE/core/src/main/java/org/springframework/security/access/hierarchicalroles/RoleHierarchyImpl.java#L220

Activity

  1. pavelhoral commented on Jun 25, 2019

    @pavelhoral
    Contributor

    I would add that there are strange methods marked with SEC-863 comment in RoleHierarchyImpl. I understand that there was a need to check authorities based on their string representation, but more obvious implementation would be to use HashMap. Such implementation would not require additional methods such as getRolesReachableInOneOrMoreSteps and addReachableRoles And those methods are not even used consistently. You can be sure that when building the hierarchy you are working exclusively with SimpleGrantedAuthority with proper equals implementation. So those two methods are AFAIK useful only in #getReachableGrantedAuthorities method call and are just making the class unnecessarily more complex.

  2. rwinch commented on Jun 27, 2019

    @rwinch
    Member

    @pavelhoral Thank you for the Pull Request. A PR would be most welcome. I'd suggest trying to split up the changes into distinct goals as large changes take longer to get merged.

  3. pavelhoral commented on Jun 27, 2019

    @pavelhoral
    Contributor

    @rwinch I have one question - what is the required java version compatibility? I can see gradle defines 1.8, but official documentation is mentioning 1.5. Can we use lambdas, new collection methods and other goodies from Java 8 (not that we will necessarily use them, but want to know the limits)?

  4. rwinch commented on Jun 27, 2019

    @rwinch
    Member

    As of Spring Security 5 the minimum is Java 8. Would you be interested in sending a PR to update the appendix as well?

  5. added
    status: ideal-for-contributionAn issue that we actively are looking for someone to help us with
    in: coreAn issue in spring-security-core
    and removed on Jun 27, 2019
  6. ankurpathak commented on Jul 9, 2019

    @ankurpathak
    Contributor

    @rwinch Can i take PR of updating appendix?

  7. karelmaxa commented on Jul 15, 2019

    @karelmaxa
    ContributorAuthor

    We have created pull request #7106 that resolves the original issue. The pull request to resolve appendix issue has not been created yet. Maybe it should be processed in dedicated issue?.

  8. rwinch commented on Jul 23, 2019

    @rwinch
    Member

    Yes let's do that as a dedicated issue.

  9. self-assigned this
    on Jul 23, 2019
  10. added this to the 5.2.0.M4 milestone on Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coreAn issue in spring-security-coretype: bugA general bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions