Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/com/networknt/schema/utils/RFC5892.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ public static boolean isValid(String value) {
if ("".equals(value)) {
return false; // empty string should fail
}
if (value.length() == 1 && value.matches(LABEL_SEPARATOR_REGEX)) {
return false; // single label separator should fail
}
// RFC 5892 calls each segment in a host name a label. They are separated by all the recognized label separators.
String[] labels = value.split(LABEL_SEPARATOR_REGEX);
if (labels.length == 0) {
return false; // a value made up only of label separators (e.g. "." or "...") has no labels
}
for (String label : labels) {
if (label.isEmpty()) continue; // A DNS entry may contain a trailing '.'.
// String.split() drops trailing empty strings, so a trailing '.' never
// produces an empty label here. A leading or interior empty label
// (e.g. ".example" or "a..b") is invalid.
if (label.isEmpty()) return false;
Comment on lines 107 to +111

String unicode = label;
if (isACE(label)) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/networknt/schema/FormatValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ void draft7DisableFormat() {
assertEquals(0, messages.size());
}

@Test
void idnHostnameRejectsEmptyLabels() {
String schemaData = "{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"format\":\"idn-hostname\"}";
Schema schema = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12).getSchema(schemaData);
// A leading or interior empty label (a leading dot, or two adjacent dots) is invalid.
assertFalse(schema.validate("\".example\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
assertFalse(schema.validate("\"a..b\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
Comment on lines +228 to +232
// A value made up only of label separators has no labels and is invalid.
assertFalse(schema.validate("\"...\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
assertFalse(schema.validate("\"。。\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
// Ordinary host names remain valid.
assertTrue(schema.validate("\"example\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
assertTrue(schema.validate("\"sub.example.com\"", InputFormat.JSON,
ec -> ec.executionConfig(c -> c.formatAssertionsEnabled(true))).isEmpty());
}

@Test
void uriTemplateAllowsApostropheInLiteral() {
String schemaData = "{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"format\":\"uri-template\"}";
Expand Down
Loading