2222import org .apache .avro .AvroRuntimeException ;
2323import org .apache .avro .generic .GenericDatumReader ;
2424import org .apache .avro .io .ResolvingDecoder ;
25+ import org .apache .avro .util .ClassSecurityValidator .SystemPropertiesPredicate ;
2526import org .apache .avro .util .ClassUtils ;
2627
2728import java .io .IOException ;
28- import java .util .ArrayList ;
2929import java .util .Arrays ;
3030import 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
3937public 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