java: limit numeric string length before BigDecimal parsing#26908
Closed
MindflareX wants to merge 2 commits into
Closed
java: limit numeric string length before BigDecimal parsing#26908MindflareX wants to merge 2 commits into
MindflareX wants to merge 2 commits into
Conversation
BigDecimal(String) has O(N^2) time complexity for N-digit strings on JDK versions before 18 (JDK-8291514). The JSON parser passes user-controlled strings directly to BigDecimal in parseInt32, parseInt64, parseUint32, parseUint64, and parseDouble, allowing a single long numeric value to consume seconds of CPU. Add a parseBigDecimal helper that rejects strings longer than 1000 characters before constructing BigDecimal. This is generous — valid protobuf numeric values never exceed ~350 characters.
esrauchg
requested changes
Apr 16, 2026
| public void testParserRejectOverlyLongNumericStrings() throws Exception { | ||
| // A numeric string with 10,000 digits should be rejected quickly to prevent | ||
| // O(N^2) BigDecimal parsing DoS. | ||
| String longNumber = "1" + "0".repeat(10000); |
Member
There was a problem hiding this comment.
Unfortunately you need to avoid using .repeat() here since our min support is java8 and .repeat() was only added after
Thanks!
String.repeat() was added in Java 11. Use a StringBuilder loop instead to maintain Java 8 compatibility per project requirements.
53d5def to
a461d0e
Compare
Contributor
Author
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
BigDecimal(String)has O(N²) time complexity for N-digit strings on JDK versions before 18 (JDK-8291514). Five JSON parser methods —parseInt32,parseInt64,parseUint32,parseUint64, andparseDouble— pass user-controlled strings directly tonew BigDecimal()without length validation.A single JSON numeric value with 1,000,000 digits takes ~13 seconds to parse on JDK 17. This can be used to DoS any service that parses protobuf JSON messages with numeric fields from untrusted input.
Benchmark (JDK 17, x86-64 Linux)
Fix
Added a
parseBigDecimal()helper that rejects strings longer than 1000 characters before constructingBigDecimal. This is generous — valid protobuf numeric values never exceed ~350 characters (Double.MAX_VALUE in non-scientific notation is ~309 digits).Affected JDK versions
Test
Added
testParserRejectOverlyLongNumericStringscovering all 5 affected field types.