From 661114c50dcfd53bb041aab66f14bb91e0a87c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 20 Sep 2023 10:50:48 -0700 Subject: [PATCH 1/3] Generalize the logic to disallow nested objects and arrays as keys in objects. Fixes #771. --- src/main/java/org/json/JSONObject.java | 16 ++++----------- src/main/java/org/json/JSONTokener.java | 20 ++++++++++++++----- .../java/org/json/junit/JSONObjectTest.java | 18 +++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5e00eb9a3..3986c56f9 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -208,22 +208,14 @@ public JSONObject(JSONTokener x) throws JSONException { throw x.syntaxError("A JSONObject text must begin with '{'"); } for (;;) { - char prev = x.getPrevious(); c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; - case '{': - case '[': - if(prev=='{') { - throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array."); - } - // fall through default: - x.back(); - key = x.nextValue().toString(); + key = x.nextSimpleValue(c).toString(); } // The key is followed by ':'. @@ -1712,12 +1704,12 @@ && isValidMethodName(method.getName())) { final Object result = method.invoke(bean); if (result != null) { // check cyclic dependency and throw error if needed - // the wrap and populateMap combination method is + // the wrap and populateMap combination method is // itself DFS recursive if (objectsRecord.contains(result)) { throw recursivelyDefinedObjectException(key); } - + objectsRecord.add(result); this.map.put(key, wrap(result, objectsRecord)); @@ -1726,7 +1718,7 @@ && isValidMethodName(method.getName())) { // we don't use the result anywhere outside of wrap // if it's a resource we should be sure to close it - // after calling toString + // after calling toString if (result instanceof Closeable) { try { ((Closeable) result).close(); diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 5dc8ae85a..4a7122f7d 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -402,12 +402,7 @@ public String nextTo(String delimiters) throws JSONException { */ public Object nextValue() throws JSONException { char c = this.nextClean(); - String string; - switch (c) { - case '"': - case '\'': - return this.nextString(c); case '{': this.back(); try { @@ -423,6 +418,21 @@ public Object nextValue() throws JSONException { throw new JSONException("JSON Array or Object depth too large to process.", e); } } + return nextSimpleValue(c); + } + + Object nextSimpleValue(char c) { + String string; + + switch (c) { + case '"': + case '\'': + return this.nextString(c); + case '{': + throw syntaxError("Nested object not expected here."); + case '[': + throw syntaxError("Nested array not expected here."); + } /* * Handle unquoted text. This could be the values true, false, or diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f815c..23feda9d6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2224,6 +2224,24 @@ public void jsonObjectParsingErrors() { "Expected a ',' or '}' at 15 [character 16 line 1]", e.getMessage()); } + try { + // key is a nested map + String str = "{{\"foo\": \"bar\"}: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Nested object not expected here. at 2 [character 3 line 1]", + e.getMessage()); + } + try { + // key is a nested array containing a map + String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Nested array not expected here. at 10 [character 11 line 1]", + e.getMessage()); + } try { // \0 after , String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; From 16967f322ee65c301b48fa79bb681e38896fd212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 27 Sep 2023 12:42:04 -0700 Subject: [PATCH 2/3] Simplify the check for object keys that are themselves objects. For object keys, we can just skip the part of `nextValue()` that parses values that are objects or arrays. Then the existing logic for unquoted values will already stop at `{` or `[`, and that will produce a `Missing value` exception. --- src/main/java/org/json/JSONTokener.java | 4 ---- src/test/java/org/json/junit/JSONObjectTest.java | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 4a7122f7d..4a83a6971 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -428,10 +428,6 @@ Object nextSimpleValue(char c) { case '"': case '\'': return this.nextString(c); - case '{': - throw syntaxError("Nested object not expected here."); - case '[': - throw syntaxError("Nested array not expected here."); } /* diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 23feda9d6..88115c844 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2230,7 +2230,7 @@ public void jsonObjectParsingErrors() { assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Nested object not expected here. at 2 [character 3 line 1]", + "Missing value at 1 [character 2 line 1]", e.getMessage()); } try { @@ -2239,7 +2239,7 @@ public void jsonObjectParsingErrors() { assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Nested array not expected here. at 10 [character 11 line 1]", + "Missing value at 9 [character 10 line 1]", e.getMessage()); } try { From dbb113176b143b519ad0a50b033a9997cc2248fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Thu, 28 Sep 2023 11:05:50 -0700 Subject: [PATCH 3/3] Add more test cases for unquoted text in objects and arrays. --- .../java/org/json/junit/JSONArrayTest.java | 16 ++++++++++++- .../java/org/json/junit/JSONObjectTest.java | 24 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ad938cf50..5a98878d6 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -118,7 +118,7 @@ public void nullException() { * Expects a JSONException. */ @Test - public void emptStr() { + public void emptyStr() { String str = ""; try { assertNull("Should throw an exception", new JSONArray(str)); @@ -460,6 +460,20 @@ public void failedGetArrayValues() { Util.checkJSONArrayMaps(jsonArray); } + /** + * The JSON parser is permissive of unambiguous unquoted keys and values. + * Such JSON text should be allowed, even if it does not strictly conform + * to the spec. However, after being parsed, toString() should emit strictly + * conforming JSON text. + */ + @Test + public void unquotedText() { + String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; + JSONArray jsonArray = new JSONArray(str); + List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); + assertEquals(expected, jsonArray.toList()); + } + /** * Exercise JSONArray.join() by converting a JSONArray into a * comma-separated string. Since this is very nearly a JSON document, diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 88115c844..b9ff59e31 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -205,13 +205,17 @@ public void jsonObjectByNullBean() { */ @Test public void unquotedText() { - String str = "{key1:value1, key2:42}"; + String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; JSONObject jsonObject = new JSONObject(str); String textStr = jsonObject.toString(); assertTrue("expected key1", textStr.contains("\"key1\"")); assertTrue("expected value1", textStr.contains("\"value1\"")); assertTrue("expected key2", textStr.contains("\"key2\"")); assertTrue("expected 42", textStr.contains("42")); + assertTrue("expected 1.2", textStr.contains("\"1.2\"")); + assertTrue("expected 3.4", textStr.contains("3.4")); + assertTrue("expected -7E+5", textStr.contains("\"-7E+5\"")); + assertTrue("expected something!", textStr.contains("\"something!\"")); Util.checkJSONObjectMaps(jsonObject); } @@ -2242,6 +2246,24 @@ public void jsonObjectParsingErrors() { "Missing value at 9 [character 10 line 1]", e.getMessage()); } + try { + // key contains } + String str = "{foo}: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } + try { + // key contains ] + String str = "{foo]: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } try { // \0 after , String str = "{\"myKey\":true, \0\"myOtherKey\":false}";