2121import java .lang .reflect .Constructor ;
2222import java .lang .reflect .InvocationTargetException ;
2323import java .nio .ByteBuffer ;
24+ import java .util .Arrays ;
25+ import java .util .List ;
2426import org .apache .avro .Schema ;
2527import org .apache .avro .generic .GenericData ;
2628import org .apache .avro .util .Utf8 ;
3436
3537public class AvroConverters {
3638
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+
3748 public abstract static class AvroGroupConverter extends GroupConverter {
3849 protected final ParentValueContainer parent ;
3950
@@ -261,6 +272,7 @@ static final class FieldStringableConverter extends BinaryConverter<Object> {
261272
262273 public FieldStringableConverter (ParentValueContainer parent , Class <?> stringableClass ) {
263274 super (parent );
275+ checkSecurity (stringableClass );
264276 stringableName = stringableClass .getName ();
265277 try {
266278 this .ctor = stringableClass .getConstructor (String .class );
@@ -277,6 +289,33 @@ public Object convert(Binary binary) {
277289 throw new ParquetDecodingException ("Cannot convert binary to " + stringableName , e );
278290 }
279291 }
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+ }
280319 }
281320
282321 static final class FieldEnumConverter extends BinaryConverter <Object > {
0 commit comments