Skip to content

fix(security): accept a role hierarchy written without spaces around the > operator - #16969

Open
borinquenkid wants to merge 5 commits into
7.2.xfrom
fix/role-hierarchy-arrow-spacing-7.0.x
Open

borinquenkid wants to merge 5 commits into
7.2.xfrom
fix/role-hierarchy-arrow-spacing-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Fixes #15892 (reported by fjloma; a merge to 7.0.x does not close it, so it is closed by hand once the merge-ups land).

grails.plugin.springsecurity.roleHierarchy = 'ROLE_ADMIN>ROLE_USER' gave the user no extra role and no message, as the issue says. The cause is in Spring Security, not in the plugin: it splits a hierarchy line only where the > operator has whitespace on each side, and ignores a line without. I checked it directly against Spring Security 6.5.11 (setHierarchy and fromHierarchy) and 7.2.0-M2 (fromHierarchy): ROLE_ADMIN>ROLE_USER reaches [ROLE_ADMIN], and ROLE_ADMIN > ROLE_USER reaches [ROLE_USER, ROLE_ADMIN]. Grails 3, on an older Spring Security, accepted the form without spaces.

Change: a small RoleHierarchyNormalizer writes each line with one space on each side of every > (trimming, dropping blank lines, accepting \r\n), and the plugin applies it where it assigns the hierarchy to the roleHierarchy bean. The line in SpringSecurityCoreGrailsPlugin is the one assignment site, for the roleHierarchy setting and for the persistent hierarchy alike. The page of hierarchical roles in the guide now says the spaces are optional. I did not add a warning for a line without >: I did not confirm what Spring does with one, and accepting the syntax removes the surprise the issue is about.

Tests: RoleHierarchyNormalizerSpec (14 rows) feeds the normalized form to RoleHierarchyImpl.fromHierarchy and checks the reachable roles (no spaces, one-sided spaces, a multiline block, two chained lines, Windows line ends), the normalized text, and that null and an empty string stay as they are. Without the class the spec fails, as run in the repo. All 187 tests of the plugin pass; CodeNarc, Checkstyle and rat are clean. CI is off (no ci).

Not covered: a running application (the spec exercises the form and Spring's parser, not the bean wiring), and 8.0.x, where MutableRoleHierarchy replaced the bean: the same assignment line is merged up there and the class works with it, to be confirmed at the merge-up.

Two of the three reviewers (matrei, codeconsole, jdaugherty), as 7.0.x is released.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…the > operator

Spring Security splits a hierarchy line only where the > operator has whitespace on each side, and ignores a line such as
ROLE_ADMIN>ROLE_USER without a message, so a user got no role from it. Grails 3 accepted that form. The plugin now writes
each line with one space on each side of the operator before it hands the hierarchy to the roleHierarchy bean.

RoleHierarchyNormalizerSpec runs the form through RoleHierarchyImpl.fromHierarchy and checks the roles that are reachable,
and the page of hierarchical roles in the guide says the spaces are optional.

Fixes #15892

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A hierarchy written with repeated > operators is rewritten into a chain that grants the lower role.

…tten

Answers the review of #16969: ROLE_ADMIN>>>ROLE_USER was rewritten to ROLE_ADMIN >  >  > ROLE_USER, which Spring Security reads with the operator as a role, so ROLE_ADMIN could reach ROLE_USER. A line is rewritten only when every piece is a role; a line with an empty piece (repeated, leading or trailing operator) stays as written and grants nothing, as before the rewrite. The spec tries each line shape through RoleHierarchyImpl. RoleHierarchyNormalizer is package-private: it is used only by the plugin, and a public class is new API that a patch release does not add.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Answers the >>> case. A line is now rewritten only when every piece between the operators is a role. A line with an empty piece (a repeated, leading or trailing operator) stays as written, so Spring Security reads it as it did before the rewrite and nothing is granted.

I did not drop the empty pieces as the comment suggests: ROLE_ADMIN>>>ROLE_USER would then become ROLE_ADMIN > ROLE_USER and grant ROLE_USER, which the first sentence of the comment says it must not do. A real chain, written with or without spaces or with tabs, still grants every role after the first.

RoleHierarchyNormalizerSpec has the line shapes, each checked through RoleHierarchyImpl for the authorities ROLE_ADMIN reaches (chains, repeated, leading and trailing operators, a chain broken by an empty piece, a good line beside a malformed one); 10 of its 28 rows fail without the change. The security plugin module: 201 tests, 0 failures; codeStyle clean.

Not tried: a role name that itself contains whitespace.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A line with an empty piece is left as written, and a line without spaces still parses. A hierarchy loaded from the database loses those roles when it is reloaded.

… startup

reloadDBRoleHierarchy assigned the stored lines to the role hierarchy as written, while startup reads them through the
normalizer that puts one space on each side of the operator. A stored line such as ROLE_ADMIN>ROLE_USER, without spaces, then
granted ROLE_USER at startup and lost it after a reload. The reload path reads the stored lines through the same normalizer.

SpringSecurityServiceRoleHierarchySpec reloads lines with and without spaces, a chain of three roles, two lines, and lines
with an empty piece, which are left as written, and compares the roles each reaches with the hierarchy built at startup.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you, that is the reload path I missed. Startup reads the stored hierarchy through the normalizer, and SpringSecurityService.reloadDBRoleHierarchy() assigned the stored lines as they were: with ROLE_ADMIN>ROLE_USER in the table, ROLE_USER was granted at startup and gone after a reload.

The reload path now reads the stored lines through the same normalizer, so the two cannot disagree. I searched for any other place that assigns the hierarchy: startup and reload are the only two.

SpringSecurityServiceRoleHierarchySpec reloads the stored lines through the service and compares the roles each reaches with the hierarchy built at startup from the same text: a line with no spaces (your case), with spaces, a chain of three roles, two lines, and the lines with an empty piece (ROLE_ADMIN>>>ROLE_USER, a leading operator) and an empty table, which stay as written and grant nothing, as at startup. The three rows without spaces fail without the fix (3 of 7) and pass with it, the plugin module is green (208 tests), and codeStyle is clean.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items this review found have been addressed. A reloaded hierarchy is read the same way as at startup, and a line with an empty piece is left as written.

@borinquenkid
borinquenkid marked this pull request as draft November 26, 2026 00:58
The 7.0.x patch line takes no more fixes after the 2026-11-25 releases;
this PR moves to 7.2.x.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid
borinquenkid changed the base branch from 7.0.x to 7.2.x November 26, 2026 02:41
@borinquenkid

Copy link
Copy Markdown
Member Author

Moved to 7.2.x: after the 2026-11-25 releases the 7.x line takes fixes on 7.2.x only. The branch has 7.2.x merged in and its module tests and codeStyle pass on it. Nothing else changed, so the review so far still applies; a fresh approval on the new head is needed for the merge rule.

@borinquenkid
borinquenkid marked this pull request as ready for review November 26, 2026 02:41
@borinquenkid borinquenkid added run ci and removed no ci labels Nov 26, 2026
sbglasius
sbglasius previously approved these changes Nov 26, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

Fix looks right, and startup and reload now read the hierarchy the same way. LGTM with one note inline.

}

applicationContext.roleHierarchy.hierarchy = roleHierarchy
applicationContext.roleHierarchy.hierarchy = RoleHierarchyNormalizer.normalize(roleHierarchy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing A>B lines that were silently ignored will start granting roles on upgrade. Worth a short note in upgrading72x.adoc, since it widens effective authorization.

…around >

A line such as ROLE_ADMIN>ROLE_USER, which Spring Security ignored before,
now grants its roles, so the effective authorization of an upgraded
application can widen (review on #16969).

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@sbglasius Good point, thanks. I added the note in 8c375fb, in a new Grails 7.2.6 section of the upgrade guide (upgrading72x.adoc#L136).

It says that a line such as ROLE_ADMIN>ROLE_USER, which Spring Security ignored before, now grants its roles, both in grails.plugin.springsecurity.roleHierarchy and in the persistent entries of roleHierarchyEntryClassName. It asks users to check those lines before upgrading.

@github-actions
github-actions Bot dismissed sbglasius’s stale review November 26, 2026 20:11

Dismissed automatically: the pull request changed after this approval, from cc21a20 to 8c375fb.

@testlens-app

testlens-app Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

⚠️ TestLens detected flakiness ⚠️

Test Summary

Tip

Inspect all details with the TestLens CLI: testlens pr 16969

CI / Functional Tests (Java 17, indy=true) > :grails-test-examples-datasources:integrationTest

Test Runs Flakiness
OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded ❌ ✅ 0% 🟢

🏷️ Commit: 8c375fb
▶️ Tests: 8395 executed
⚪️ Checks: 62/62 completed

Test Failures

OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded (:grails-test-examples-datasources:integrationTest in CI / Functional Tests (Java 17, indy=true) | Attempt 1/2)
Condition failed with Exception:

http('/requestSession/lazyVolumes?name=asynchronous').assertEquals(200, 'first,second')
|                                                     |
|                                                     org.opentest4j.AssertionFailedError: HTTP Status differs
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
|                                                     	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
<org.apache.grails.testing.http.client.TestHttpResponse@1aac10b5 delegate=(GET http://localhost:43353/requestSession/lazyVolumes?name=asynchronous) 500 jsonSlurperConfig=org.apache.grails.testing.http.client.utils.JsonUtils$SlurperConfig(null, null, null, null, null) xmlSlurperConfig=org.apache.grails.testing.http.client.utils.XmlUtils$SlurperConfig@71bc6914>

	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
Caused by: org.opentest4j.AssertionFailedError: HTTP Status differs
	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
	... 1 more

Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants