fix(#887): double array breaking JSONTokener.nextValue

change(#887): input validation
This commit is contained in:
rikkarth 2024-04-21 11:03:15 +01:00
parent ce074e9f9a
commit 3dcd5b2fab
No known key found for this signature in database
GPG Key ID: 11E5F28B0AED6AC7
3 changed files with 20 additions and 20 deletions

View File

@ -133,6 +133,17 @@ public class JSONArray implements Iterable<Object> {
case ']':
if (jsonParserConfiguration.isStrictMode()) {
nextChar = x.nextClean();
if (nextChar == ','){
x.back();
return;
}
if (nextChar == ']'){
x.back();
return;
}
if (nextChar != 0) {
throw x.syntaxError("invalid character found after end of array: " + nextChar);
}
@ -161,27 +172,14 @@ public class JSONArray implements Iterable<Object> {
char cursor = x.getPrevious();
boolean isEndOfArray = cursor == ']';
boolean nextCharacterIsNotEoF = x.nextClean() != 0;
char nextChar = x.nextClean();
boolean nextCharacterIsNotEoF = nextChar != 0;
if (isEndOfArray && nextCharacterIsNotEoF) {
String completeInput = collectCompleteInput(x);
throw new JSONException("Provided Array is not compliant with strict mode guidelines: " + completeInput);
throw x.syntaxError(String.format("Provided Array is not compliant with strict mode guidelines: '%s'", nextChar));
}
}
private String collectCompleteInput(JSONTokener x) {
String nonCompliantStringAfterArray = collectNonCompliantStringAfterArray(x);
return myArrayList + nonCompliantStringAfterArray;
}
private String collectNonCompliantStringAfterArray(JSONTokener x) {
StringBuilder sb = new StringBuilder().append(x.getPrevious());
while(x.nextClean() != 0){
sb.append(x.getPrevious());
}
return sb.toString();
}
/**
* Construct a JSONArray from a source JSON text.
*

View File

@ -440,7 +440,7 @@ public class JSONTokener {
case '[':
this.back();
try {
return new JSONArray(this);
return new JSONArray(this, jsonParserConfiguration);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
@ -516,6 +516,10 @@ public class JSONTokener {
String string = sb.toString().trim();
if (string.isEmpty()) {
throw this.syntaxError("Missing value");
}
if (strictMode) {
boolean isBooleanOrNumeric = checkIfValueIsBooleanOrNumeric(string);
@ -526,9 +530,6 @@ public class JSONTokener {
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
}
if (string.isEmpty()) {
throw this.syntaxError("Missing value");
}
return JSONObject.stringToValue(string);
}

View File

@ -218,6 +218,7 @@ public class JSONParserConfigurationTest {
*/
private List<String> getNonCompliantJSONList() {
return Arrays.asList(
"[[a]]",
"[]asdf",
"[]]",
"[]}",