Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3825: [Java] Disallow invalid namespaces #2430

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,7 @@ public Name(String name, String space) {
space = name.substring(0, lastDot); // get space from name
this.name = validateName(name.substring(lastDot + 1));
}
if ("".equals(space))
space = null;
this.space = space;
this.space = validateSpace(space);
this.full = (this.space == null) ? this.name : this.space + "." + this.name;
}

Expand Down Expand Up @@ -1645,6 +1643,17 @@ private static String validateName(String name) {
return name;
}

private static String validateSpace(String space) {
if ("".equals(space) || space == null) {
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
return null;
}

for (String part : space.split("\\.")) {
validateName(part);
}
return space;
}

private static final ThreadLocal<Boolean> VALIDATE_DEFAULTS = ThreadLocalWithInitial.of(() -> true);

private static JsonNode validateDefault(String fieldName, Schema schema, JsonNode defaultValue) {
Expand Down
13 changes: 13 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,17 @@ void add_types() {
assertNotNull(f1);
assertEquals(schemaRecord1, f1.schema());
}

@Test
void testInvalidNamespace() throws Exception {
File avscFile = Files.createTempFile("testInvalidNamespace", null).toFile();
String schema = "{\"type\":\"record\", \"namespace\": \"foo.123.bar\", \"name\":\"my_record\", \"fields\":[]}";
try (FileWriter writer = new FileWriter(avscFile)) {
writer.write(schema);
writer.flush();
}

Schema.Parser parser = new Schema.Parser();
assertThrows(SchemaParseException.class, () -> parser.parse(avscFile));
}
}