Skip to content

Commit 3b764e1

Browse files
gszadovszkyRyanSkraba
authored andcommitted
AVRO-4189: [java] Simplify the setting of the serializable classes (#3525)
* AVRO-4189: [java] Simplify the setting of the serializable classes * Fix missing license header * Fix test failures + copilot findings * Fix system property settings in pomx
1 parent 9110c69 commit 3b764e1

File tree

13 files changed

+506
-91
lines changed

13 files changed

+506
-91
lines changed

lang/java/avro/pom.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@
9090
<artifactId>maven-surefire-plugin</artifactId>
9191
<configuration>
9292
<parallel>none</parallel>
93-
<systemProperties>
94-
<org.apache.avro.SERIALIZABLE_CLASSES>java.math.BigDecimal,java.math.BigInteger,java.net.URI,java.net.URL,java.io.File,java.lang.Integer,org.apache.avro.reflect.TestReflect$R10</org.apache.avro.SERIALIZABLE_CLASSES>
95-
</systemProperties>
9693
</configuration>
9794
<executions>
9895
<execution>

lang/java/avro/src/it/pom.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,26 @@
9494
<artifactId>maven-surefire-plugin</artifactId>
9595
<version>@maven-surefire-plugin.version@</version>
9696
<configuration>
97-
<systemProperties>
98-
<org.apache.avro.SERIALIZABLE_CLASSES>java.math.BigDecimal,java.math.BigInteger,java.net.URI,java.net.URL,java.io.File,java.lang.Integer,org.apache.avro.reflect.TestReflect$R10</org.apache.avro.SERIALIZABLE_CLASSES>
99-
</systemProperties>
10097
<useModulePath>false</useModulePath>
10198
<failIfNoTests>true</failIfNoTests>
99+
<systemPropertyVariables>
100+
101+
<!-- Repeating the related system properties here because this pom does not inherit the configuration. -->
102+
<org.apache.avro.SERIALIZABLE_CLASSES>
103+
java.net.URI,java.net.URL,
104+
java.io.File,
105+
java.util.HashMap,
106+
java.util.List,
107+
java.util.Collection,
108+
java.util.Map,
109+
java.util.Set,
110+
java.util.concurrent.ConcurrentHashMap,
111+
java.util.LinkedHashMap,
112+
java.util.TreeMap
113+
</org.apache.avro.SERIALIZABLE_CLASSES>
114+
<org.apache.avro.SERIALIZABLE_PACKAGES>org.apache.avro</org.apache.avro.SERIALIZABLE_PACKAGES>
115+
116+
</systemPropertyVariables>
102117
</configuration>
103118
</plugin>
104119
</plugins>

lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumReader.java

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
import org.apache.avro.AvroRuntimeException;
2323
import org.apache.avro.generic.GenericDatumReader;
2424
import org.apache.avro.io.ResolvingDecoder;
25+
import org.apache.avro.util.ClassSecurityValidator.SystemPropertiesPredicate;
2526
import org.apache.avro.util.ClassUtils;
2627

2728
import java.io.IOException;
28-
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.List;
31-
import java.util.HashSet;
32-
import java.util.Set;
33-
import java.util.stream.Stream;
31+
import org.apache.avro.util.ClassSecurityValidator;
3432

3533
/**
3634
* {@link org.apache.avro.io.DatumReader DatumReader} for generated Java
@@ -39,47 +37,20 @@
3937
public class SpecificDatumReader<T> extends GenericDatumReader<T> {
4038

4139
/**
42-
* @deprecated prefer to use {@link #SERIALIZABLE_CLASSES} instead.
40+
* @deprecated Use {@link SystemPropertiesPredicate} instead.
41+
* @see ClassSecurityValidator
4342
*/
4443
@Deprecated
45-
public static final String[] SERIALIZABLE_PACKAGES;
46-
47-
public static final String[] SERIALIZABLE_CLASSES;
48-
49-
static {
50-
// no serializable classes by default
51-
SERIALIZABLE_CLASSES = streamPropertyEntries(System.getProperty("org.apache.avro.SERIALIZABLE_CLASSES"))
52-
.toArray(String[]::new);
53-
54-
// no serializable packages by default
55-
SERIALIZABLE_PACKAGES = streamPropertyEntries(System.getProperty("org.apache.avro.SERIALIZABLE_PACKAGES"))
56-
// Add a '.' suffix to ensure we'll be matching package names instead of
57-
// arbitrary prefixes, except for the wildcard "*", which allows all
58-
// packages (this is only safe in fully controlled environments!).
59-
.map(entry -> "*".equals(entry) ? entry : entry + ".").toArray(String[]::new);
60-
}
44+
public static final String[] SERIALIZABLE_PACKAGES = SystemPropertiesPredicate.SERIALIZABLE_PACKAGES
45+
.toArray(new String[0]);
6146

6247
/**
63-
* Parse a comma separated list into non-empty entries. Leading and trailing
64-
* whitespace is stripped.
65-
*
66-
* @param commaSeparatedEntries the comma separated list of entries
67-
* @return a stream of the entries
48+
* @deprecated Use {@link SystemPropertiesPredicate} instead.
49+
* @see ClassSecurityValidator
6850
*/
69-
private static Stream<String> streamPropertyEntries(String commaSeparatedEntries) {
70-
if (commaSeparatedEntries == null) {
71-
return Stream.empty();
72-
}
73-
return Stream.of(commaSeparatedEntries.split(",")).map(String::strip).filter(s -> !s.isEmpty());
74-
}
75-
76-
// The primitive "class names" based on Class.isPrimitive()
77-
private static final Set<String> PRIMITIVES = new HashSet<>(Arrays.asList(Boolean.TYPE.getName(),
78-
Character.TYPE.getName(), Byte.TYPE.getName(), Short.TYPE.getName(), Integer.TYPE.getName(), Long.TYPE.getName(),
79-
Float.TYPE.getName(), Double.TYPE.getName(), Void.TYPE.getName()));
80-
81-
private final List<String> trustedPackages = new ArrayList<>();
82-
private final List<String> trustedClasses = new ArrayList<>();
51+
@Deprecated
52+
public static final String[] SERIALIZABLE_CLASSES = SystemPropertiesPredicate.SERIALIZABLE_CLASSES
53+
.toArray(new String[0]);
8354

8455
public SpecificDatumReader() {
8556
this(null, null, SpecificData.get());
@@ -106,15 +77,11 @@ public SpecificDatumReader(Schema writer, Schema reader) {
10677
*/
10778
public SpecificDatumReader(Schema writer, Schema reader, SpecificData data) {
10879
super(writer, reader, data);
109-
trustedPackages.addAll(Arrays.asList(SERIALIZABLE_PACKAGES));
110-
trustedClasses.addAll(Arrays.asList(SERIALIZABLE_CLASSES));
11180
}
11281

11382
/** Construct given a {@link SpecificData}. */
11483
public SpecificDatumReader(SpecificData data) {
11584
super(data);
116-
trustedPackages.addAll(Arrays.asList(SERIALIZABLE_PACKAGES));
117-
trustedClasses.addAll(Arrays.asList(SERIALIZABLE_CLASSES));
11885
}
11986

12087
/** Return the contained {@link SpecificData}. */
@@ -156,51 +123,29 @@ private Class getPropAsClass(Schema schema, String prop) {
156123
if (name == null)
157124
return null;
158125
try {
159-
checkSecurity(name);
160126
Class clazz = ClassUtils.forName(getData().getClassLoader(), name);
161127
return clazz;
162128
} catch (ClassNotFoundException e) {
163129
throw new AvroRuntimeException(e);
164130
}
165131
}
166132

167-
private boolean trustAllPackages() {
168-
return (trustedPackages.size() == 1 && "*".equals(trustedPackages.get(0)));
169-
}
170-
171-
private void checkSecurity(String className) throws ClassNotFoundException {
172-
if (trustAllPackages() || PRIMITIVES.contains(className)) {
173-
return;
174-
}
175-
176-
for (String trustedClass : getTrustedClasses()) {
177-
if (className.equals(trustedClass)) {
178-
return;
179-
}
180-
}
181-
182-
for (String trustedPackage : getTrustedPackages()) {
183-
if (className.startsWith(trustedPackage)) {
184-
return;
185-
}
186-
}
187-
188-
throw new SecurityException("Forbidden " + className + "! This class is not trusted to be included in Avro "
189-
+ "schemas using java-class. Please set the system property org.apache.avro.SERIALIZABLE_CLASSES to the comma "
190-
+ "separated list of classes you trust. You can also set the system property "
191-
+ "org.apache.avro.SERIALIZABLE_PACKAGES to the comma separated list of the packages you trust.");
192-
}
193-
194133
/**
195-
* @deprecated Use getTrustedClasses() instead
134+
* @deprecated Use {@link SystemPropertiesPredicate} instead.
135+
* @see ClassSecurityValidator
196136
*/
197137
@Deprecated
198138
public final List<String> getTrustedPackages() {
199-
return trustedPackages;
139+
return Arrays.asList(SERIALIZABLE_PACKAGES);
200140
}
201141

142+
/**
143+
* @deprecated Use {@link SystemPropertiesPredicate} instead.
144+
* @see ClassSecurityValidator
145+
*/
146+
@Deprecated
202147
public final List<String> getTrustedClasses() {
203-
return trustedClasses;
148+
return Arrays.asList(SERIALIZABLE_CLASSES);
204149
}
205150

206151
@Override

0 commit comments

Comments
 (0)