Skip to content

Commit 53fa5de

Browse files
committed
fixes networknt#523 synched ipv4 and ipv6 and fix some gaps for the IP format validation
1 parent 416572e commit 53fa5de

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4949
}
5050

5151
if (format != null) {
52+
if(format.getName().equals("ipv6")) {
53+
if(!node.textValue().trim().equals(node.textValue())) {
54+
// leading and trailing spaces
55+
errors.add(buildValidationMessage(at, format.getName(), format.getErrorMessageDescription()));
56+
} else if(node.textValue().contains("%")) {
57+
// zone id is not part of the ipv6
58+
errors.add(buildValidationMessage(at, format.getName(), format.getErrorMessageDescription()));
59+
}
60+
}
5261
try {
5362
if (!format.matches(node.textValue())) {
5463
errors.add(buildValidationMessage(at, format.getName(), format.getErrorMessageDescription()));

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ static PatternFormat pattern(String name, String regex) {
4343
COMMON_BUILTIN_FORMATS.add(pattern("time", "^\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?$"));
4444
COMMON_BUILTIN_FORMATS.add(pattern("ip-address",
4545
"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"));
46-
COMMON_BUILTIN_FORMATS.add(pattern("ipv4",
47-
"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"));
46+
COMMON_BUILTIN_FORMATS.add(pattern("ipv4", "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-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A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"));
5049

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ public void testFormatHostnameValidator() throws Exception {
5858
}
5959

6060
@Test
61-
@Disabled
6261
public void testFormatIpv4Validator() throws Exception {
6362
runTestFile("draft4/optional/format/ipv4.json");
6463
}
6564

6665
@Test
67-
@Disabled
6866
public void testFormatIpv6Validator() throws Exception {
6967
runTestFile("draft4/optional/format/ipv6.json");
7068
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public V6JsonSchemaTest() {
2323
super(SpecVersion.VersionFlag.V6);
2424
}
2525

26+
@Test
27+
public void testFormatIpv4Validator() throws Exception {
28+
runTestFile("draft4/optional/format/ipv4.json");
29+
}
30+
2631
@Test
2732
public void testOptionalBignumValidator() throws Exception {
2833
runTestFile("draft6/optional/bignum.json");
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[
2+
{
3+
"description": "validation of IP addresses",
4+
"schema": { "format": "ipv4" },
5+
"tests": [
6+
{
7+
"description": "all string formats ignore integers",
8+
"data": 12,
9+
"valid": true
10+
},
11+
{
12+
"description": "all string formats ignore floats",
13+
"data": 13.7,
14+
"valid": true
15+
},
16+
{
17+
"description": "all string formats ignore objects",
18+
"data": {},
19+
"valid": true
20+
},
21+
{
22+
"description": "all string formats ignore arrays",
23+
"data": [],
24+
"valid": true
25+
},
26+
{
27+
"description": "all string formats ignore booleans",
28+
"data": false,
29+
"valid": true
30+
},
31+
{
32+
"description": "all string formats ignore nulls",
33+
"data": null,
34+
"valid": true
35+
},
36+
{
37+
"description": "a valid IP address",
38+
"data": "192.168.0.1",
39+
"valid": true
40+
},
41+
{
42+
"description": "an IP address with too many components",
43+
"data": "127.0.0.0.1",
44+
"valid": false
45+
},
46+
{
47+
"description": "an IP address with out-of-range values",
48+
"data": "256.256.256.256",
49+
"valid": false
50+
},
51+
{
52+
"description": "an IP address without 4 components",
53+
"data": "127.0",
54+
"valid": false
55+
},
56+
{
57+
"description": "an IP address as an integer",
58+
"data": "0x7f000001",
59+
"valid": false
60+
},
61+
{
62+
"description": "an IP address as an integer (decimal)",
63+
"data": "2130706433",
64+
"valid": false
65+
},
66+
{
67+
"description": "leading zeroes should be rejected, as they are treated as octals",
68+
"comment": "see https://sick.codes/universal-netmask-npm-package-used-by-270000-projects-vulnerable-to-octal-input-data-server-side-request-forgery-remote-file-inclusion-local-file-inclusion-and-more-cve-2021-28918/",
69+
"data": "087.10.0.1",
70+
"valid": false
71+
},
72+
{
73+
"description": "value without leading zero is valid",
74+
"data": "87.10.0.1",
75+
"valid": true
76+
},
77+
{
78+
"description": "non-ascii digits should be rejected",
79+
"data": "1২7.0.0.1",
80+
"valid": false
81+
}
82+
]
83+
}
84+
]

0 commit comments

Comments
 (0)