This is an automated email from the ASF dual-hosted git repository.
iemejia pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new bd916d34ae AVRO-4189: [Java] Use ClassUtils.forName() in
FastReaderBuilder for consistency (#3693)
bd916d34ae is described below
commit bd916d34ae32faa3128d079f576b5cc4b4fa4c1a
Author: Ismaël Mejía <[email protected]>
AuthorDate: Tue Mar 31 09:49:46 2026 +0200
AVRO-4189: [Java] Use ClassUtils.forName() in FastReaderBuilder for
consistency (#3693)
FastReaderBuilder.findClass() was using data.getClassLoader().loadClass()
directly, bypassing ClassUtils.forName() which is used everywhere else in
the codebase. This change aligns FastReaderBuilder with the standard class
loading path and adds tests for class loading behavior.
---
.../java/org/apache/avro/io/FastReaderBuilder.java | 5 +-
.../avro/io/TestFastReaderBuilderClassLoading.java | 120 +++++++++++++++++++++
.../avro/util/TestClassSecurityValidator.java | 23 ++++
3 files changed, 146 insertions(+), 2 deletions(-)
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
index dbd06f305e..7ff80e2a4f 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
@@ -52,6 +52,7 @@ import org.apache.avro.io.parsing.ResolvingGrammarGenerator;
import org.apache.avro.reflect.ReflectionUtil;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificRecordBase;
+import org.apache.avro.util.ClassUtils;
import org.apache.avro.util.Utf8;
import org.apache.avro.util.WeakIdentityHashMap;
import org.apache.avro.util.internal.Accessor;
@@ -446,8 +447,8 @@ public class FastReaderBuilder {
private Optional<Class<?>> findClass(String clazz) {
try {
- return Optional.of(data.getClassLoader().loadClass(clazz));
- } catch (ReflectiveOperationException e) {
+ return Optional.of(ClassUtils.forName(data.getClassLoader(), clazz));
+ } catch (ClassNotFoundException e) {
return Optional.empty();
}
}
diff --git
a/lang/java/avro/src/test/java/org/apache/avro/io/TestFastReaderBuilderClassLoading.java
b/lang/java/avro/src/test/java/org/apache/avro/io/TestFastReaderBuilderClassLoading.java
new file mode 100644
index 0000000000..2feedaedaa
--- /dev/null
+++
b/lang/java/avro/src/test/java/org/apache/avro/io/TestFastReaderBuilderClassLoading.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.io;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Collections;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.GenericRecordBuilder;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.util.ClassSecurityValidator;
+import org.apache.avro.util.ClassSecurityValidator.ClassSecurityPredicate;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests that FastReaderBuilder.findClass() routes class loading through
+ * ClassUtils.forName(), so that ClassSecurityValidator is applied
consistently.
+ */
+public class TestFastReaderBuilderClassLoading {
+
+ private static final String TEST_VALUE = "https://example.com";
+
+ private ClassSecurityPredicate originalValidator;
+
+ @BeforeEach
+ public void saveValidator() {
+ originalValidator = ClassSecurityValidator.getGlobal();
+ }
+
+ @AfterEach
+ public void restoreValidator() {
+ ClassSecurityValidator.setGlobal(originalValidator);
+ }
+
+ /**
+ * When the validator blocks a class referenced by java-class,
FastReaderBuilder
+ * must propagate the SecurityException so the caller knows why the class was
+ * rejected.
+ */
+ @Test
+ void blockedClassThrowsSecurityException() {
+ // Block java.net.URI
+
ClassSecurityValidator.setGlobal(ClassSecurityValidator.composite(ClassSecurityValidator.DEFAULT_TRUSTED_CLASSES,
+
ClassSecurityValidator.builder().add("org.apache.avro.util.Utf8").build()));
+
+ assertThrows(SecurityException.class, () ->
readWithJavaClass("java.net.URI"),
+ "Blocked class should cause a SecurityException to propagate");
+ }
+
+ /**
+ * When the validator trusts a class referenced by java-class,
FastReaderBuilder
+ * should instantiate it normally.
+ */
+ @Test
+ void trustedClassIsInstantiated() {
+
ClassSecurityValidator.setGlobal(ClassSecurityValidator.composite(ClassSecurityValidator.DEFAULT_TRUSTED_CLASSES,
+
ClassSecurityValidator.builder().add("java.net.URI").add("org.apache.avro.util.Utf8").build()));
+
+ GenericRecord result = readWithJavaClass("java.net.URI");
+
+ assertInstanceOf(URI.class, result.get("value"));
+ assertEquals(URI.create(TEST_VALUE), result.get("value"));
+ }
+
+ /**
+ * Encode a string, then read it back through FastReaderBuilder with the
given
+ * java-class.
+ */
+ private GenericRecord readWithJavaClass(String javaClass) {
+ try {
+ Schema stringSchema = Schema.create(Schema.Type.STRING);
+ stringSchema.addProp(SpecificData.CLASS_PROP, javaClass);
+ stringSchema.addProp(GenericData.STRING_PROP,
GenericData.StringType.String.name());
+
+ Schema recordSchema = Schema.createRecord("TestRecord", null, "test",
false);
+ recordSchema.setFields(Collections.singletonList(new
Schema.Field("value", stringSchema, null, null)));
+
+ // Encode
+ GenericRecord record = new
GenericRecordBuilder(recordSchema).set("value", TEST_VALUE).build();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
+ new GenericDatumWriter<GenericRecord>(recordSchema).write(record,
encoder);
+ encoder.flush();
+
+ // Decode with fast reader enabled
+ GenericData data = new GenericData();
+ data.setFastReaderEnabled(true);
+ GenericDatumReader<GenericRecord> reader = new
GenericDatumReader<>(recordSchema, recordSchema, data);
+ return reader.read(null, DecoderFactory.get().binaryDecoder(new
ByteArrayInputStream(out.toByteArray()), null));
+ } catch (IOException e) {
+ return fail("Unexpected IOException during encode/decode", e);
+ }
+ }
+}
diff --git
a/lang/java/avro/src/test/java/org/apache/avro/util/TestClassSecurityValidator.java
b/lang/java/avro/src/test/java/org/apache/avro/util/TestClassSecurityValidator.java
index 4dc1a55c54..e3b62b0112 100644
---
a/lang/java/avro/src/test/java/org/apache/avro/util/TestClassSecurityValidator.java
+++
b/lang/java/avro/src/test/java/org/apache/avro/util/TestClassSecurityValidator.java
@@ -97,6 +97,29 @@ public class TestClassSecurityValidator {
assertEquals("Not inner", e.getMessage());
}
+ @Test
+ void testClassUtilsEnforcesValidator() {
+
ClassSecurityValidator.setGlobal(ClassSecurityValidator.builder().add("java.lang.String").build());
+
+ assertThrows(SecurityException.class, () ->
ClassUtils.forName("java.net.URI"),
+ "ClassUtils.forName should reject classes not in the trusted set");
+
+ assertDoesNotThrow(() -> ClassUtils.forName("java.lang.String"),
+ "ClassUtils.forName should allow classes in the trusted set");
+ }
+
+ @Test
+ void testDirectLoadClassDoesNotUseValidator() throws ClassNotFoundException {
+
ClassSecurityValidator.setGlobal(ClassSecurityValidator.builder().add("java.lang.String").build());
+
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ Class<?> loaded = cl.loadClass("java.net.URI");
+ assertNotNull(loaded, "Direct ClassLoader.loadClass() loads any class
regardless of the validator");
+
+ assertThrows(SecurityException.class, () ->
ClassUtils.forName("java.net.URI"),
+ "ClassUtils.forName correctly applies the validator");
+ }
+
@Test
void testBuildComplexPredicate() {
ClassSecurityValidator.setGlobal(ClassSecurityValidator.composite(