Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ private int parseInt32(JsonElement json) throws InvalidProtocolBufferException {
// "1.000" are treated as equal in JSON. For this reason we accept floating point values for
// integer fields as well as long as it actually is an integer (i.e., round(value) == value).
try {
BigDecimal value = new BigDecimal(json.getAsString());
BigDecimal value = parseBigDecimal(json.getAsString());
return value.intValueExact();
} catch (RuntimeException e) {
InvalidProtocolBufferException ex =
Expand All @@ -1906,7 +1906,7 @@ private long parseInt64(JsonElement json) throws InvalidProtocolBufferException
// "1.000" are treated as equal in JSON. For this reason we accept floating point values for
// integer fields as well as long as it actually is an integer (i.e., round(value) == value).
try {
BigDecimal value = new BigDecimal(json.getAsString());
BigDecimal value = parseBigDecimal(json.getAsString());
return value.longValueExact();
} catch (RuntimeException e) {
InvalidProtocolBufferException ex =
Expand All @@ -1930,7 +1930,7 @@ private int parseUint32(JsonElement json) throws InvalidProtocolBufferException
// "1.000" are treated as equal in JSON. For this reason we accept floating point values for
// integer fields as well as long as it actually is an integer (i.e., round(value) == value).
try {
BigDecimal value = new BigDecimal(json.getAsString());
BigDecimal value = parseBigDecimal(json.getAsString());
if (value.signum() < 0 || value.compareTo(MAX_UINT32) > 0) {
throw new InvalidProtocolBufferException("Out of range uint32 value: " + json);
}
Expand All @@ -1950,9 +1950,24 @@ private int parseUint32(JsonElement json) throws InvalidProtocolBufferException
private static final BigDecimal MAX_UINT64 =
new BigDecimal(new BigInteger("FFFFFFFFFFFFFFFF", 16));

// Maximum length for numeric strings passed to BigDecimal constructor.
// BigDecimal(String) has O(N^2) time complexity for N-digit strings on JDK < 18,
// allowing a DoS with a single long numeric JSON value. Valid protobuf numeric
// values never exceed ~350 characters, so 1000 is a generous upper bound.
private static final int MAX_NUMERIC_STRING_LENGTH = 1000;

private static BigDecimal parseBigDecimal(String value)
throws InvalidProtocolBufferException {
if (value.length() > MAX_NUMERIC_STRING_LENGTH) {
throw new InvalidProtocolBufferException(
"Numeric value is too long: " + value.length() + " characters");
}
return new BigDecimal(value);
}

private long parseUint64(JsonElement json) throws InvalidProtocolBufferException {
try {
BigDecimal value = new BigDecimal(json.getAsString());
BigDecimal value = parseBigDecimal(json.getAsString());
if (value.signum() < 0 || value.compareTo(MAX_UINT64) > 0) {
throw new InvalidProtocolBufferException("Out of range uint64 value: " + json);
}
Expand Down Expand Up @@ -2030,7 +2045,7 @@ private double parseDouble(JsonElement json) throws InvalidProtocolBufferExcepti
// We don't use Double.parseDouble() here because that function simply
// accepts all values. Here we parse the value into a BigDecimal and do
// explicit range check on it.
BigDecimal value = new BigDecimal(json.getAsString());
BigDecimal value = parseBigDecimal(json.getAsString());
if (value.compareTo(MAX_DOUBLE) > 0 || value.compareTo(MIN_DOUBLE) < 0) {
throw new InvalidProtocolBufferException("Out of range double value: " + json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,25 @@ public void testParserRejectOutOfRangeNumericValues() throws Exception {
assertRejects("optionalDouble", minDouble.multiply(moreThanOne).toString());
}

@Test
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately you need to avoid using .repeat() here since our min support is java8 and .repeat() was only added after

Thanks!

String[] fields = {
"optionalInt32", "optionalInt64", "optionalUint32", "optionalUint64", "optionalDouble"
};
for (String field : fields) {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
try {
mergeFromJson("{\"" + field + "\":\"" + longNumber + "\"}", builder);
assertWithMessage("Exception expected for " + field + " with long numeric string").fail();
} catch (InvalidProtocolBufferException expected) {
// Expected: rejected before expensive BigDecimal construction.
}
}
}

@Test
public void testParserAcceptNull() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
Expand Down
Loading