Skip to content

Commit e5244a2

Browse files
committed
Prevent recursive loading when a schema id is own URL
1 parent 6245eac commit e5244a2

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
4141
this.subSchema = obainSubSchemaNode(schemaNode);
4242
}
4343

44+
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
45+
ValidatorTypeCode validatorType, JsonSchema subSchema) {
46+
this.schemaPath = schemaPath;
47+
this.schemaNode = schemaNode;
48+
this.parentSchema = parentSchema;
49+
this.validatorType = validatorType;
50+
this.subSchema = subSchema;
51+
}
52+
4453
protected String getSchemaPath() {
4554
return schemaPath;
4655
}

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ public class JsonSchema extends BaseJsonValidator {
5454
read(schemaNode);
5555
}
5656

57+
JsonSchema(ObjectMapper mapper, String schemaPath, JsonNode schemaNode,
58+
JsonSchema parent, JsonSchema subSchema) {
59+
super(schemaPath, schemaNode, parent, null, subSchema);
60+
this.mapper = mapper;
61+
62+
validators = new LinkedHashMap<String, JsonValidator>();
63+
64+
read(schemaNode);
65+
}
66+
67+
public JsonSchema(ObjectMapper mapper, JsonNode schemaNode, JsonSchema subSchema) {
68+
this(mapper, "#", schemaNode, null, subSchema);
69+
}
70+
5771
/**
5872
* Find the schema node for $ref attribute.
5973
*

src/main/java/com/networknt/schema/JsonSchemaFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
import java.net.URL;
2727

2828
public class JsonSchemaFactory {
29+
30+
// Draft 6 uses "$id"
31+
private static final String DRAFT_4_ID = "id";
32+
2933
private static final Logger logger = LoggerFactory
3034
.getLogger(JsonSchemaFactory.class);
3135
private ObjectMapper mapper;
@@ -60,8 +64,15 @@ public JsonSchema getSchema(InputStream schemaStream) {
6064

6165
public JsonSchema getSchema(URL schemaURL) {
6266
try {
67+
6368
JsonNode schemaNode = mapper.readTree(schemaURL.openStream());
69+
70+
if (this.idMatchesSourceUrl(schemaNode, schemaURL)) {
71+
return new JsonSchema(mapper, schemaNode, null);
72+
}
73+
6474
return new JsonSchema(mapper, schemaNode);
75+
6576
} catch (IOException ioe) {
6677
logger.error("Failed to load json schema!", ioe);
6778
throw new JsonSchemaException(ioe);
@@ -72,4 +83,18 @@ public JsonSchema getSchema(JsonNode jsonNode) {
7283
return new JsonSchema(mapper, jsonNode);
7384
}
7485

86+
private boolean idMatchesSourceUrl(JsonNode schema, URL schemaUrl) {
87+
88+
JsonNode idNode = schema.get(DRAFT_4_ID);
89+
90+
if (idNode == null) {
91+
return false;
92+
}
93+
94+
String id = idNode.asText();
95+
logger.info("Matching " + id + " to " + schemaUrl.toString());
96+
return id.equals(schemaUrl.toString());
97+
98+
}
99+
75100
}

src/test/java/com/networknt/schema/JsonSchemaTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.junit.BeforeClass;
2929
import org.junit.Test;
3030

31-
import java.io.File;
32-
import java.io.InputStream;
31+
import java.io.*;
32+
import java.net.URL;
3333
import java.util.ArrayList;
3434
import java.util.List;
3535

@@ -105,6 +105,14 @@ private void runTestFile(String testCaseFile) throws Exception {
105105
}
106106
}
107107

108+
@Test(/*expected = java.lang.StackOverflowError.class*/)
109+
public void testLoadingWithId() throws IOException {
110+
URL url = new URL("http://localhost:1234/self_ref/selfRef.json");
111+
JsonNode schemaJson = mapper.readTree(url);
112+
JsonSchemaFactory factory = new JsonSchemaFactory();
113+
JsonSchema schema = factory.getSchema(schemaJson);
114+
}
115+
108116
@Test
109117
public void testBignumValidator() throws Exception {
110118
runTestFile("tests/optional/bignum.json");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "http://localhost:1234/self_ref/selfRef.json",
3+
"description": "Schema with ID set to its own URL"
4+
}

0 commit comments

Comments
 (0)