[#665] Fix StackOverflowError while parsing long ACI with repetitive targets - #666
Merged
Merged
Conversation
…OpenIdentityPlatform#665) Aci.decode's quick syntax check ran Pattern.matches(aciRegex, input), whose targets part ((\(\s*(\w+)\s*(!?=)\s*"([^"]+)"\s*\)\s*)*) repeats a group with an unbounded greedy quantifier. java.util.regex matches such repetition recursively - one stack frame per repetition - so an ACI value with hundreds of repeated target rules like (targetscope="&O")... overflowed the JVM stack with a StackOverflowError instead of the expected AciException. Being an Error, it escaped catch(Exception) handlers on the aci-value validation paths (AciSyntaxImpl, AciHandler, AciList) and could kill the worker thread. - AciTargets.targetRegex/targetsRegex: use possessive quantifiers (\s*+, \w++, (...)*+) so the regex engine matches the repetition iteratively; match results are unchanged (each possessive run is followed by a disjoint character class) and capture-group numbering is preserved. - Harden the same-shaped list regexes: oidListRegex (Aci), attrListRegex (TargetAttr), rightsRegex (Permission). New Aci constants WORD_GROUP_POSSESSIVE and ZERO_OR_MORE_WHITESPACE_POSSESSIVE; the shared greedy constants are left untouched. - Aci.decode: defense in depth - catch StackOverflowError and rethrow as AciException so a pathological ACI is treated as invalid instead of killing the thread (same approach as GHSA-rv4q-c6mr-wxp7). - Add AciDecodeStackOverflowTestCase: bounded-stack (256 KB) regression tests for the decode path and the raw targets regex, plus a valid multi-target decode check.
…or cause, test malformed target - Aci.decode: pass the caught StackOverflowError as the cause of the rethrown AciException to ease diagnostics if the fallback ever fires. - AciDecodeStackOverflowTestCase: add malformedTargetStillRejected to verify the possessive target regex still rejects invalid target syntax.
maximthomas
approved these changes
Jul 2, 2026
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.
Fixes #665
Problem
Aci.decodeperforms a quick syntax check viaPattern.matches(aciRegex, input). The targets part of that regex,repeats a group with an unbounded greedy quantifier.
java.util.regexmatches such repetition recursively — one stack frame per repetition — so an ACI value with hundreds of repeated target rules like(targetscope="&O")(targetscope="&O")...overflows the JVM stack withStackOverflowErrorinstead of the expectedAciException. Being anError, it escapescatch(Exception)handlers on the aci-value validation paths (AciSyntaxImpl,AciHandler,AciList) and can kill the worker thread.Fix
AciTargets.targetRegex/targetsRegex: use possessive quantifiers (\s*+,\w++,(...)*+), as suggested in the issue, so the regex engine matches the repetition iteratively. Match results are unchanged (each possessive run is followed by a disjoint character class) and capture-group numbering is preserved.oidListRegex(Aci),attrListRegex(TargetAttr),rightsRegex(Permission). New constantsWORD_GROUP_POSSESSIVEandZERO_OR_MORE_WHITESPACE_POSSESSIVE; the shared greedy constants are left untouched (ATTR_NAMEintentionally not modified — it is shared withTargAttrFilterList/ParentInheritance).Aci.decodecatchesStackOverflowErrorand rethrows it asAciException, so a pathological ACI is treated as invalid instead of killing the thread (same approach as the GHSA-rv4q-c6mr-wxp7 fix).Tests
New
AciDecodeStackOverflowTestCase(runs decode on a bounded 256 KB stack thread so the pre-fix overflow is deterministic):(targetscope="&O")targets → rejected withAciException, noStackOverflowError;AciTargets.targetsRegexmatches the same input iteratively (validates the regex fix independently of the defense-in-depth catch);Verified: old regex overflows a 256 KB stack on the reproducer while the new one matches iteratively, with identical match results on a corpus of valid/invalid target strings. Adjacent ACI suites pass (
AciBodyTest,TargetTestCase,TargetAttrTestCase,TargAttrFiltersTestCase,ExtOpTestCase,TargetControlTestCase,EnumRightTest— 134 tests, 0 failures).