Skip to content

fix: add MAX_INPUT_STRING_LENGTH check in C++ ParseHelper#3974

Open
Tulgaaaaaaaa wants to merge 2 commits intogoogle:masterfrom
Tulgaaaaaaaa:fix/add-max-input-length-check-cpp
Open

fix: add MAX_INPUT_STRING_LENGTH check in C++ ParseHelper#3974
Tulgaaaaaaaa wants to merge 2 commits intogoogle:masterfrom
Tulgaaaaaaaa:fix/add-max-input-length-check-cpp

Conversation

@Tulgaaaaaaaa
Copy link

@Tulgaaaaaaaa Tulgaaaaaaaa commented Mar 23, 2026

Problem

The C++ ParseHelper() function is missing the MAX_INPUT_STRING_LENGTH input length check that both the Java and JavaScript implementations enforce.

  • Java (PhoneNumberUtil.java:70,3279): MAX_INPUT_STRING_LENGTH = 250, throws TOO_LONG
  • JavaScript (phonenumberutil.js:135,4267): MAX_INPUT_STRING_LENGTH_ = 250, throws TOO_LONG
  • C++: No equivalent check — unbounded input reaches regex evaluation

The default C++ build uses ICU regexp (USE_ICU_REGEXP=ON in CMakeLists.txt:87), which is a backtracking NFA engine. Without input length bounds, long strings can cause excessive CPU consumption in regex operations.

The Java source comment explicitly states the purpose (line 68-69): "We don't allow input strings for parsing to be longer than 250 chars. This prevents malicious input from overflowing the regular-expression engine."

Fix

Added kMaxInputStringLength = 250 check at the top of ParseHelper(), returning NOT_A_NUMBER for inputs exceeding 250 characters. This matches the Java/JS behavior of rejecting oversized inputs before regex evaluation.

Tests added

  • ParseRejectsInputLongerThanMaxLength in phonenumberutil_test.cc
  • Tests 251-char input (rejected), 250-char boundary (passes length check), and normal phone number (unaffected)

The Java and JavaScript implementations both enforce a 250-character
limit on input strings before regex evaluation to prevent excessive
CPU consumption. The C++ implementation was missing this check,
allowing unbounded input to reach the ICU backtracking regex engine
(the default build configuration).

Add the same kMaxInputStringLength=250 guard at the top of ParseHelper,
matching the Java implementation at PhoneNumberUtil.java:3279.

Bug: CWE-1333 (ReDoS via Inefficient Regular Expression)
@Tulgaaaaaaaa Tulgaaaaaaaa requested a review from a team as a code owner March 23, 2026 05:49
@google-cla
Copy link

google-cla bot commented Mar 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Verify that inputs longer than 250 characters are rejected with
NOT_A_NUMBER, matching the Java/JS MAX_INPUT_STRING_LENGTH behavior.
Tests boundary at exactly 250 chars and normal parsing still works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants