Skip to content

Commit 703ac34

Browse files
mechkoclaude
andcommitted
stleary#1063: add JSONParserConfiguration overloads for getBigInteger/optBigInteger
Per review on stleary#1067: - objectToBigInteger(val, dflt, JSONParserConfiguration) uses cfg.getMaxNumberLength() for the digit-count guard; -1 disables it. Existing 2-arg form delegates with a default config. - New public overloads on JSONObject and JSONArray: getBigInteger(key, cfg) / optBigInteger(key, dflt, cfg). Existing methods delegate with a default config. - objectToBigDecimal left unchanged (no expansion path; agreed on PR). - Tests cover default (1000), raised (2000), lowered (5), disabled (-1), null config, and JSONArray overloads. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e40d933 commit 703ac34

3 files changed

Lines changed: 172 additions & 10 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: 72 additions & 6 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,19 +1436,40 @@ 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){
14021466
BigDecimal bd = (BigDecimal) val;
14031467
// Same ceiling as the parse-time maxNumberLength guard: refuse to
14041468
// materialise an integer whose decimal representation would exceed
1405-
// DEFAULT_MAX_NUMBER_LENGTH digits. Prevents DoS via short exponent
1406-
// literals like 1e100000000 (CVE-2026-59171, see issue #1063).
1407-
if ((long) bd.precision() - bd.scale() > ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH) {
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) {
14081473
return defaultValue;
14091474
}
14101475
return bd.toBigInteger();
@@ -1431,7 +1496,8 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
14311496
final String valStr = val.toString();
14321497
if(isDecimalNotation(valStr)) {
14331498
BigDecimal bd = new BigDecimal(valStr);
1434-
if ((long) bd.precision() - bd.scale() > ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH) {
1499+
if (maxNumberLength != ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH
1500+
&& (long) bd.precision() - bd.scale() > maxNumberLength) {
14351501
return defaultValue;
14361502
}
14371503
return bd.toBigInteger();

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,58 @@ public void getBigIntegerHugeExponentReturnsDefault() {
14051405
jo3.getBigInteger("x").compareTo(BigInteger.TEN.pow(999)));
14061406
}
14071407

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+
14081460
/**
14091461
* The purpose for the static method getNames() methods are not clear. This
14101462
* method is not called from within JSON-Java. Most likely uses are to prep

0 commit comments

Comments
 (0)