21
21
import java .lang .reflect .Constructor ;
22
22
import java .lang .reflect .InvocationTargetException ;
23
23
import java .nio .ByteBuffer ;
24
+ import java .util .Arrays ;
25
+ import java .util .List ;
24
26
import org .apache .avro .Schema ;
25
27
import org .apache .avro .generic .GenericData ;
26
28
import org .apache .avro .util .Utf8 ;
34
36
35
37
public class AvroConverters {
36
38
39
+ public static final String [] SERIALIZABLE_PACKAGES ;
40
+
41
+ static {
42
+ SERIALIZABLE_PACKAGES = System .getProperty (
43
+ "org.apache.parquet.avro.SERIALIZABLE_PACKAGES" ,
44
+ "java.lang,java.math,java.io,java.net,org.apache.parquet.avro" )
45
+ .split ("," );
46
+ }
47
+
37
48
public abstract static class AvroGroupConverter extends GroupConverter {
38
49
protected final ParentValueContainer parent ;
39
50
@@ -261,6 +272,7 @@ static final class FieldStringableConverter extends BinaryConverter<Object> {
261
272
262
273
public FieldStringableConverter (ParentValueContainer parent , Class <?> stringableClass ) {
263
274
super (parent );
275
+ checkSecurity (stringableClass );
264
276
stringableName = stringableClass .getName ();
265
277
try {
266
278
this .ctor = stringableClass .getConstructor (String .class );
@@ -277,6 +289,33 @@ public Object convert(Binary binary) {
277
289
throw new ParquetDecodingException ("Cannot convert binary to " + stringableName , e );
278
290
}
279
291
}
292
+
293
+ private void checkSecurity (Class <?> clazz ) throws SecurityException {
294
+ List <String > trustedPackages = Arrays .asList (SERIALIZABLE_PACKAGES );
295
+
296
+ boolean trustAllPackages = trustedPackages .size () == 1 && "*" .equals (trustedPackages .get (0 ));
297
+ if (trustAllPackages || clazz .isPrimitive ()) {
298
+ return ;
299
+ }
300
+
301
+ boolean found = false ;
302
+ Package thePackage = clazz .getPackage ();
303
+ if (thePackage != null ) {
304
+ for (String trustedPackage : trustedPackages ) {
305
+ if (thePackage .getName ().equals (trustedPackage )
306
+ || thePackage .getName ().startsWith (trustedPackage + "." )) {
307
+ found = true ;
308
+ break ;
309
+ }
310
+ }
311
+ if (!found ) {
312
+ throw new SecurityException ("Forbidden " + clazz
313
+ + "! This class is not trusted to be included in Avro schema using java-class."
314
+ + " Please set org.apache.parquet.avro.SERIALIZABLE_PACKAGES system property"
315
+ + " with the packages you trust." );
316
+ }
317
+ }
318
+ }
280
319
}
281
320
282
321
static final class FieldEnumConverter extends BinaryConverter <Object > {
0 commit comments