Skip to content

Commit 5ba3e50

Browse files
committed
jsonld-java#144 : Check for trailing content when parsing JSON object
1 parent 443dddf commit 5ba3e50

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

core/src/main/java/com/github/jsonldjava/utils/JsonUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static Object fromInputStream(InputStream input, String enc) throws IOExc
103103
*/
104104
public static Object fromReader(Reader reader) throws IOException {
105105
final JsonParser jp = JSON_FACTORY.createParser(reader);
106-
Object rval = null;
106+
Object rval ;
107107
final JsonToken initialToken = jp.nextToken();
108108

109109
if (initialToken == JsonToken.START_ARRAY) {
@@ -123,6 +123,16 @@ public static Object fromReader(Reader reader) throws IOException {
123123
throw new JsonParseException("document doesn't start with a valid json element : "
124124
+ initialToken, jp.getCurrentLocation());
125125
}
126+
127+
JsonToken t ;
128+
try { t = jp.nextToken(); }
129+
catch (JsonParseException ex) {
130+
throw new JsonParseException("Document contains more content after json-ld element - (possible mismatched {}?)",
131+
jp.getCurrentLocation());
132+
}
133+
if ( t != null )
134+
throw new JsonParseException("Document contains possible json content after the json-ld element - (possible mismatched {}?)",
135+
jp.getCurrentLocation());
126136
return rval;
127137
}
128138

core/src/test/java/com/github/jsonldjava/utils/JsonUtilsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import static org.junit.Assert.assertTrue;
44

5+
import java.io.IOException ;
56
import java.util.Map;
67

8+
import com.fasterxml.jackson.core.JsonParseException ;
9+
710
import org.junit.Test;
811

912
public class JsonUtilsTest {
@@ -31,4 +34,32 @@ public void fromStringTest() {
3134
assertTrue(true);
3235
}
3336
}
37+
38+
@Test
39+
public void trailingContent_1() throws JsonParseException, IOException { trailingContent("{}") ; }
40+
41+
@Test
42+
public void trailingContent_2() throws JsonParseException, IOException { trailingContent("{} \t \r \n \r\n ") ; }
43+
44+
@Test(expected=JsonParseException.class)
45+
public void trailingContent_3() throws JsonParseException, IOException { trailingContent("{}x") ; }
46+
47+
@Test(expected=JsonParseException.class)
48+
public void trailingContent_4() throws JsonParseException, IOException { trailingContent("{} x") ; }
49+
50+
@Test(expected=JsonParseException.class)
51+
public void trailingContent_5() throws JsonParseException, IOException { trailingContent("{} \"x\"") ; }
52+
53+
@Test(expected=JsonParseException.class)
54+
public void trailingContent_6() throws JsonParseException, IOException { trailingContent("{} {}") ; }
55+
56+
@Test(expected=JsonParseException.class)
57+
public void trailingContent_7() throws JsonParseException, IOException { trailingContent("{},{}") ; }
58+
59+
@Test(expected=JsonParseException.class)
60+
public void trailingContent_8() throws JsonParseException, IOException { trailingContent("{},[]") ; }
61+
62+
private void trailingContent(String string) throws JsonParseException, IOException {
63+
JsonUtils.fromString(string) ;
64+
}
3465
}

0 commit comments

Comments
 (0)