Skip to content

Commit a28328c

Browse files
authored
Merge pull request stleary#1067 from mechko/1063-biginteger-exponent-dos
stleary#1063: bound BigDecimal→BigInteger expansion in objectToBigInteger (completes CVE-2026-59171 fix)
2 parents 90563eb + 703ac34 commit a28328c

3 files changed

Lines changed: 220 additions & 8 deletions

File tree

‎src/main/java/org/json/JSONArray.java‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,29 @@ public BigDecimal getBigDecimal (int index) throws JSONException {
483483
* to a BigInteger.
484484
*/
485485
public BigInteger getBigInteger (int index) throws JSONException {
486+
return this.getBigInteger(index, new JSONParserConfiguration());
487+
}
488+
489+
/**
490+
* Get the BigInteger value associated with an index.
491+
*
492+
* @param index
493+
* The index must be between 0 and length() - 1.
494+
* @param jsonParserConfiguration
495+
* A configuration whose {@code maxNumberLength} bounds the number of
496+
* decimal digits in the returned integer. Values exceeding this length
497+
* are treated as unconvertible. Pass a configuration with
498+
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
499+
* this check.
500+
* @return The value.
501+
* @throws JSONException
502+
* If the key is not found or if the value cannot be converted
503+
* to a BigInteger.
504+
*/
505+
public BigInteger getBigInteger (int index, JSONParserConfiguration jsonParserConfiguration)
506+
throws JSONException {
486507
Object object = this.get(index);
487-
BigInteger val = JSONObject.objectToBigInteger(object, null);
508+
BigInteger val = JSONObject.objectToBigInteger(object, null, jsonParserConfiguration);
488509
if(val == null) {
489510
throw wrongValueFormatException(index, "BigInteger", object, null);
490511
}
@@ -960,8 +981,8 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
960981
}
961982

962983
/**
963-
* Get the optional BigInteger value associated with an index. The
964-
* defaultValue is returned if there is no value for the index, or if the
984+
* Get the optional BigInteger value associated with an index. The
985+
* defaultValue is returned if there is no value for the index, or if the
965986
* value is not a number and cannot be converted to a number.
966987
*
967988
* @param index
@@ -971,8 +992,31 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
971992
* @return The value.
972993
*/
973994
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
995+
return this.optBigInteger(index, defaultValue, new JSONParserConfiguration());
996+
}
997+
998+
/**
999+
* Get the optional BigInteger value associated with an index. The
1000+
* defaultValue is returned if there is no value for the index, or if the
1001+
* value is not a number and cannot be converted to a number.
1002+
*
1003+
* @param index
1004+
* The index must be between 0 and length() - 1.
1005+
* @param defaultValue
1006+
* The default value.
1007+
* @param jsonParserConfiguration
1008+
* A configuration whose {@code maxNumberLength} bounds the number of
1009+
* decimal digits in the returned integer. Values exceeding this length
1010+
* are treated as unconvertible and {@code defaultValue} is returned.
1011+
* Pass a configuration with
1012+
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
1013+
* this check.
1014+
* @return The value.
1015+
*/
1016+
public BigInteger optBigInteger(int index, BigInteger defaultValue,
1017+
JSONParserConfiguration jsonParserConfiguration) {
9741018
Object val = this.opt(index);
975-
return JSONObject.objectToBigInteger(val, defaultValue);
1019+
return JSONObject.objectToBigInteger(val, defaultValue, jsonParserConfiguration);
9761020
}
9771021

9781022
/**

‎src/main/java/org/json/JSONObject.java‎

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,29 @@ public boolean getBoolean(String key) throws JSONException {
760760
* be converted to BigInteger.
761761
*/
762762
public BigInteger getBigInteger(String key) throws JSONException {
763+
return this.getBigInteger(key, new JSONParserConfiguration());
764+
}
765+
766+
/**
767+
* Get the BigInteger value associated with a key.
768+
*
769+
* @param key
770+
* A key string.
771+
* @param jsonParserConfiguration
772+
* A configuration whose {@code maxNumberLength} bounds the number of
773+
* decimal digits in the returned integer. Values exceeding this length
774+
* are treated as unconvertible. Pass a configuration with
775+
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
776+
* this check.
777+
* @return The numeric value.
778+
* @throws JSONException
779+
* if the key is not found or if the value cannot
780+
* be converted to BigInteger.
781+
*/
782+
public BigInteger getBigInteger(String key, JSONParserConfiguration jsonParserConfiguration)
783+
throws JSONException {
763784
Object object = this.get(key);
764-
BigInteger ret = objectToBigInteger(object, null);
785+
BigInteger ret = objectToBigInteger(object, null, jsonParserConfiguration);
765786
if (ret != null) {
766787
return ret;
767788
}
@@ -1381,8 +1402,31 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
13811402
* @return An object which is the value.
13821403
*/
13831404
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
1405+
return this.optBigInteger(key, defaultValue, new JSONParserConfiguration());
1406+
}
1407+
1408+
/**
1409+
* Get an optional BigInteger associated with a key, or the defaultValue if
1410+
* there is no such key or if its value is not a number. If the value is a
1411+
* string, an attempt will be made to evaluate it as a number.
1412+
*
1413+
* @param key
1414+
* A key string.
1415+
* @param defaultValue
1416+
* The default.
1417+
* @param jsonParserConfiguration
1418+
* A configuration whose {@code maxNumberLength} bounds the number of
1419+
* decimal digits in the returned integer. Values exceeding this length
1420+
* are treated as unconvertible and {@code defaultValue} is returned.
1421+
* Pass a configuration with
1422+
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
1423+
* this check.
1424+
* @return An object which is the value.
1425+
*/
1426+
public BigInteger optBigInteger(String key, BigInteger defaultValue,
1427+
JSONParserConfiguration jsonParserConfiguration) {
13841428
Object val = this.opt(key);
1385-
return objectToBigInteger(val, defaultValue);
1429+
return objectToBigInteger(val, defaultValue, jsonParserConfiguration);
13861430
}
13871431

13881432
/**
@@ -1392,14 +1436,43 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
13921436
* to convert.
13931437
*/
13941438
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
1439+
return objectToBigInteger(val, defaultValue, new JSONParserConfiguration());
1440+
}
1441+
1442+
/**
1443+
* @param val value to convert
1444+
* @param defaultValue default value to return is the conversion doesn't work or is null.
1445+
* @param jsonParserConfiguration parser configuration whose {@code maxNumberLength}
1446+
* bounds the number of decimal digits in the resulting integer. Values whose
1447+
* integer part would exceed this length are treated as unconvertible and
1448+
* {@code defaultValue} is returned. Pass a configuration with
1449+
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable this check.
1450+
* @return BigInteger conversion of the original value, or the defaultValue if unable
1451+
* to convert.
1452+
*/
1453+
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue,
1454+
JSONParserConfiguration jsonParserConfiguration) {
13951455
if (NULL.equals(val)) {
13961456
return defaultValue;
13971457
}
1458+
if (jsonParserConfiguration == null) {
1459+
jsonParserConfiguration = new JSONParserConfiguration();
1460+
}
1461+
final int maxNumberLength = jsonParserConfiguration.getMaxNumberLength();
13981462
if (val instanceof BigInteger){
13991463
return (BigInteger) val;
14001464
}
14011465
if (val instanceof BigDecimal){
1402-
return ((BigDecimal) val).toBigInteger();
1466+
BigDecimal bd = (BigDecimal) val;
1467+
// Same ceiling as the parse-time maxNumberLength guard: refuse to
1468+
// materialise an integer whose decimal representation would exceed
1469+
// maxNumberLength digits. Prevents DoS via short exponent literals
1470+
// like 1e100000000 (CVE-2026-59171, see issue #1063).
1471+
if (maxNumberLength != ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH
1472+
&& (long) bd.precision() - bd.scale() > maxNumberLength) {
1473+
return defaultValue;
1474+
}
1475+
return bd.toBigInteger();
14031476
}
14041477
if (val instanceof Double || val instanceof Float){
14051478
if (!numberIsFinite((Number)val)) {
@@ -1422,7 +1495,12 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
14221495
*/
14231496
final String valStr = val.toString();
14241497
if(isDecimalNotation(valStr)) {
1425-
return new BigDecimal(valStr).toBigInteger();
1498+
BigDecimal bd = new BigDecimal(valStr);
1499+
if (maxNumberLength != ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH
1500+
&& (long) bd.precision() - bd.scale() > maxNumberLength) {
1501+
return defaultValue;
1502+
}
1503+
return bd.toBigInteger();
14261504
}
14271505
return new BigInteger(valStr);
14281506
} catch (Exception e) {

‎src/test/java/org/json/junit/JSONObjectTest.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,96 @@ public void bigNumberOperations() {
13671367
Util.checkJSONArrayMaps(jsonArray1, jsonObject0.getMapType());
13681368
}
13691369

1370+
/**
1371+
* Verifies that getBigInteger / optBigInteger do not attempt to materialise a
1372+
* BigInteger whose decimal representation would exceed
1373+
* ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH digits. A short exponent-notation
1374+
* literal such as 1e100000000 is stored compactly as a BigDecimal at parse time
1375+
* but would otherwise expand to ~100 000 000 digits in BigDecimal.toBigInteger(),
1376+
* stalling the thread / OOM (CVE-2026-59171, issue #1063).
1377+
*/
1378+
@Test(timeout = 5000)
1379+
public void getBigIntegerHugeExponentReturnsDefault() {
1380+
// BigDecimal path: value arrives via the parser as a BigDecimal
1381+
JSONObject jo = new JSONObject("{\"x\":1e100000000}");
1382+
assertTrue("huge-exponent literal parses to BigDecimal", jo.get("x") instanceof BigDecimal);
1383+
assertNull("optBigInteger returns default for huge exponent", jo.optBigInteger("x", null));
1384+
try {
1385+
jo.getBigInteger("x");
1386+
fail("getBigInteger should throw for huge exponent");
1387+
} catch (JSONException expected) {
1388+
// expected: integer part exceeds DEFAULT_MAX_NUMBER_LENGTH digits
1389+
}
1390+
1391+
// String path: value put() as a String, exercised via objectToBigInteger's
1392+
// isDecimalNotation branch
1393+
JSONObject jo2 = new JSONObject();
1394+
jo2.put("x", "1e100000000");
1395+
assertNull("optBigInteger returns default for huge-exponent string", jo2.optBigInteger("x", null));
1396+
1397+
// JSONArray accessors delegate to the same helper
1398+
JSONArray ja = new JSONArray("[1e100000000]");
1399+
assertTrue("optBigInteger returns default for huge exponent (array)",
1400+
BigInteger.ONE.equals(ja.optBigInteger(0, BigInteger.ONE)));
1401+
1402+
// Boundary: a value at the limit still converts correctly
1403+
JSONObject jo3 = new JSONObject("{\"x\":1e999}");
1404+
assertEquals("1e999 still converts", 0,
1405+
jo3.getBigInteger("x").compareTo(BigInteger.TEN.pow(999)));
1406+
}
1407+
1408+
/**
1409+
* Verifies that the JSONParserConfiguration.maxNumberLength setting is honoured
1410+
* by the getBigInteger / optBigInteger overloads on JSONObject and JSONArray.
1411+
*/
1412+
@Test(timeout = 5000)
1413+
public void getBigIntegerHonorsMaxNumberLengthConfig() {
1414+
JSONObject jo = new JSONObject("{\"a\":1e1500,\"b\":1e2500}");
1415+
1416+
// Default config: DEFAULT_MAX_NUMBER_LENGTH == 1000, both rejected
1417+
assertNull("1e1500 rejected under default", jo.optBigInteger("a", null));
1418+
assertNull("1e2500 rejected under default", jo.optBigInteger("b", null));
1419+
1420+
// Custom raised limit
1421+
JSONParserConfiguration cfg2000 = new JSONParserConfiguration().withMaxNumberLength(2000);
1422+
assertEquals("1e1500 accepted under maxNumberLength=2000", 0,
1423+
jo.getBigInteger("a", cfg2000).compareTo(BigInteger.TEN.pow(1500)));
1424+
assertNull("1e2500 rejected under maxNumberLength=2000",
1425+
jo.optBigInteger("b", null, cfg2000));
1426+
try {
1427+
jo.getBigInteger("b", cfg2000);
1428+
fail("getBigInteger should throw for 1e2500 under maxNumberLength=2000");
1429+
} catch (JSONException expected) {
1430+
// expected: integer part exceeds configured maxNumberLength
1431+
}
1432+
1433+
// Custom lowered limit
1434+
JSONParserConfiguration cfg5 = new JSONParserConfiguration().withMaxNumberLength(5);
1435+
assertNull("1e1500 rejected under maxNumberLength=5",
1436+
jo.optBigInteger("a", null, cfg5));
1437+
JSONObject small = new JSONObject("{\"x\":1234}");
1438+
assertEquals("small value accepted under maxNumberLength=5",
1439+
BigInteger.valueOf(1234), small.getBigInteger("x", cfg5));
1440+
1441+
// Disabled: -1 turns the guard off. Use a moderate exponent so the test
1442+
// completes in a few ms while still exceeding the default limit.
1443+
JSONParserConfiguration cfgOff = new JSONParserConfiguration()
1444+
.withMaxNumberLength(ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH);
1445+
assertEquals("1e2500 accepted when maxNumberLength is disabled", 0,
1446+
jo.getBigInteger("b", cfgOff).compareTo(BigInteger.TEN.pow(2500)));
1447+
1448+
// null config falls back to default
1449+
assertNull("null config behaves like default", jo.optBigInteger("a", null, null));
1450+
1451+
// JSONArray overloads follow the same rules
1452+
JSONArray ja = new JSONArray("[1e1500]");
1453+
assertNull("array: 1e1500 rejected under default", ja.optBigInteger(0, null));
1454+
assertEquals("array: 1e1500 accepted under maxNumberLength=2000", 0,
1455+
ja.getBigInteger(0, cfg2000).compareTo(BigInteger.TEN.pow(1500)));
1456+
assertEquals("array: 1e1500 accepted when disabled", 0,
1457+
ja.optBigInteger(0, null, cfgOff).compareTo(BigInteger.TEN.pow(1500)));
1458+
}
1459+
13701460
/**
13711461
* The purpose for the static method getNames() methods are not clear. This
13721462
* method is not called from within JSON-Java. Most likely uses are to prep

0 commit comments

Comments
 (0)