Fix ReDoS in RFC3966 domain validation and integer underflow in matcher#3987
Open
sharadboni wants to merge 2 commits intogoogle:masterfrom
Open
Fix ReDoS in RFC3966 domain validation and integer underflow in matcher#3987sharadboni wants to merge 2 commits intogoogle:masterfrom
sharadboni wants to merge 2 commits intogoogle:masterfrom
Conversation
Bug A: The rfc3966_domainlabel_ and rfc3966_toplabel_ regex patterns used nested quantifiers of the form `[chars]+((\\-)*[chars])*` which cause O(N^2) backtracking on non-matching inputs via ICU regex. This is exploitable through the phone-context parameter in RFC3966 URIs parsed by IsPhoneContextValid(). Rewrite patterns to `[chars]+(-[chars]+)*` which requires at least one character after each hyphen, eliminating quantifier ambiguity. Bug B: In AllNumberGroupsAreExactlyPresent(), when candidate_groups is empty, `candidate_groups.size() - 1` underflows size_t (unsigned) producing a huge value, leading to undefined behavior when cast to int and used as a vector index. Add an early return guard.
Author
|
@rohininidhi Could you review this security fix? It rewrites ReDoS-vulnerable regex patterns in RFC3966 domain validation and fixes an integer underflow in AllNumberGroupsAreExactlyPresent. |
The previous fix changed the domain label regex from [chars]+((\\-)*[chars])* to [chars]+(-[chars]+)*, which broke parsing of phone-context values like 'a--z' that contain consecutive hyphens (valid per RFC). Use -+ instead of - to allow one or more hyphens between alphanumeric groups, while still preventing ReDoS since hyphen and alphanum character classes are disjoint.
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.
Summary
Bug A (ReDoS): The
rfc3966_domainlabel_andrfc3966_toplabel_regex patterns inphonenumberutil.ccuse nested quantifiers[chars]+((\\-)*[chars])*that cause O(N^2) backtracking on adversarial inputs via ICU regex. This is reachable through thephone-contextparameter in RFC3966 URIs parsed byIsPhoneContextValid(). The fix rewrites both patterns to[chars]+(-[chars]+)*, which requires at least one character after each hyphen, eliminating the quantifier nesting ambiguity.Bug B (Integer underflow): In
AllNumberGroupsAreExactlyPresent()inphonenumbermatcher.cc, whencandidate_groupsis empty,candidate_groups.size() - 1underflowssize_t(unsigned) producing a huge value. This is then cast tointand used as a vector index, causing undefined behavior. The fix adds an earlyreturn falseguard whencandidate_groupsis empty.Test plan
phone-contextdomain no longer causes quadratic regex timeAllNumberGroupsAreExactlyPresentreturns false for empty candidate groups instead of triggering UB