#1063: bound BigDecimal->BigInteger expansion in objectToBigInteger

Completes the CVE-2026-59171 fix started in ab92bb9 / #1065. The
1000-char length guard in stringToValue admits short exponent-notation
literals (e.g. 1e100000000, 11 chars) which are stored compactly as
BigDecimal and only expand when getBigInteger/optBigInteger calls
BigDecimal.toBigInteger(), materialising ~10^8 digits and stalling the
thread or throwing OOM.

Guard both toBigInteger() sites in objectToBigInteger by rejecting any
BigDecimal whose integer part would exceed
ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH decimal digits
(precision() - scale(), both O(1) reads). Returns defaultValue on
overflow, matching the method's existing behaviour for non-finite and
unparseable values.

Covers JSONObject.getBigInteger/optBigInteger and
JSONArray.getBigInteger/optBigInteger (all delegate to this helper).

Adds JSONObjectTest.getBigIntegerHugeExponentReturnsDefault with a 5s
timeout so a regression fails fast rather than hanging CI.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
Mirko Swillus
2026-07-14 11:59:27 +02:00
co-authored by Claude
parent 90563eb877
commit fce83c91bb
2 changed files with 51 additions and 2 deletions
+14 -2
View File
@@ -1399,7 +1399,15 @@ public class JSONObject {
return (BigInteger) val;
}
if (val instanceof BigDecimal){
return ((BigDecimal) val).toBigInteger();
BigDecimal bd = (BigDecimal) val;
// Same ceiling as the parse-time maxNumberLength guard: refuse to
// materialise an integer whose decimal representation would exceed
// DEFAULT_MAX_NUMBER_LENGTH digits. Prevents DoS via short exponent
// literals like 1e100000000 (CVE-2026-59171, see issue #1063).
if ((long) bd.precision() - bd.scale() > ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH) {
return defaultValue;
}
return bd.toBigInteger();
}
if (val instanceof Double || val instanceof Float){
if (!numberIsFinite((Number)val)) {
@@ -1422,7 +1430,11 @@ public class JSONObject {
*/
final String valStr = val.toString();
if(isDecimalNotation(valStr)) {
return new BigDecimal(valStr).toBigInteger();
BigDecimal bd = new BigDecimal(valStr);
if ((long) bd.precision() - bd.scale() > ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH) {
return defaultValue;
}
return bd.toBigInteger();
}
return new BigInteger(valStr);
} catch (Exception e) {