Reject unbalanced parentheses in profile expressions#36550
Open
daguimu wants to merge 1 commit intospring-projects:mainfrom
Open
Reject unbalanced parentheses in profile expressions#36550daguimu wants to merge 1 commit intospring-projects:mainfrom
daguimu wants to merge 1 commit intospring-projects:mainfrom
Conversation
ProfilesParser.parseTokens() silently accepts unbalanced parentheses in profile expressions such as "dev)" or "(dev", treating them as valid. This can lead to unexpected behavior where malformed @Profile annotations are silently interpreted instead of being rejected. This commit tightens the validation in parseTokens() to reject: - Unmatched closing parenthesis at the top level - Unmatched opening parenthesis when tokens are exhausted Also fixes an existing test that inadvertently relied on this lenient behavior by using "spring&framework)" instead of "(spring&framework)". Fixes spring-projectsgh-36540 Signed-off-by: daguimu <daguimu.geek@gmail.com>
63c73f3 to
d3ac9ca
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ProfilesParsersilently accepts unbalanced parentheses in profile expressions. For example,Profiles.of("dev)")or@Profile("dev)")is accepted without error and treated as"dev". This can lead to unexpected behavior where malformed profile annotations are silently interpreted instead of being rejected.Root Cause
In
ProfilesParser.parseTokens(), when a closing)is encountered outside of aContext.PARENTHESIScontext (i.e., without a matching opening(), the parser merges the current elements, clears state, and continues parsing instead of raising a validation error. Similarly, when all tokens are consumed while still inside aPARENTHESIScontext (unmatched opening(), the method returns normally without detecting the imbalance.Fix
")"case ofparseTokens(), reject unmatched closing parentheses by callingassertWellFormed(expression, false)when not inPARENTHESIScontext, instead of silently continuingassertWellFormed(expression, context != Context.PARENTHESIS)to reject unmatched opening parentheses when tokens are exhaustedofAndExpressionWithoutSpaces) that inadvertently relied on this lenient behavior by using"spring&framework)"instead of"(spring&framework)"Tests Added
)at top levelmalformedExpressionsWithUnbalancedParentheses()— verifies"dev)","spring&framework)","(dev))"all throwIllegalArgumentException(when tokens exhaustmalformedExpressionsWithUnbalancedParentheses()— verifies"(dev","((dev)"throwIllegalArgumentExceptionofAndExpressionWithoutSpaces()— now uses"(spring&framework)"and still passesassertAndExpressionchecksAll 37 tests in
ProfilesTestspass, including all pre-existing tests (no regressions).Impact
Expressions with unbalanced parentheses that were previously silently accepted will now throw
IllegalArgumentExceptionwith a "Malformed profile expression" message. This is consistent with the existing behavior for standalone"("and")"which already throw. Well-formed expressions are not affected.Fixes #36540