-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3985: Add trusted packages support in SpecificData #2934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,25 @@ | |
| import org.apache.avro.io.ResolvingDecoder; | ||
| import org.apache.avro.util.ClassUtils; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * {@link org.apache.avro.io.DatumReader DatumReader} for generated Java | ||
| * classes. | ||
| */ | ||
| public class SpecificDatumReader<T> extends GenericDatumReader<T> { | ||
|
|
||
| public static final String[] SERIALIZABLE_PACKAGES; | ||
|
|
||
| static { | ||
| SERIALIZABLE_PACKAGES = System.getProperty("org.apache.avro.SERIALIZABLE_PACKAGES", | ||
| "java.lang,java.math,java.io,java.net,org.apache.avro.reflect").split(","); | ||
| } | ||
|
|
||
| private final List<String> trustedPackages = new ArrayList<>(); | ||
|
|
||
| public SpecificDatumReader() { | ||
| this(null, null, SpecificData.get()); | ||
| } | ||
|
|
@@ -55,6 +68,7 @@ public SpecificDatumReader(Schema writer, Schema reader) { | |
| */ | ||
| public SpecificDatumReader(Schema writer, Schema reader, SpecificData data) { | ||
| super(writer, reader, data); | ||
| trustedPackages.addAll(Arrays.asList(SERIALIZABLE_PACKAGES)); | ||
| } | ||
|
|
||
| /** Construct given a {@link SpecificData}. */ | ||
|
|
@@ -101,12 +115,43 @@ private Class getPropAsClass(Schema schema, String prop) { | |
| if (name == null) | ||
| return null; | ||
| try { | ||
| return ClassUtils.forName(getData().getClassLoader(), name); | ||
| Class clazz = ClassUtils.forName(getData().getClassLoader(), name); | ||
| checkSecurity(clazz); | ||
| return clazz; | ||
| } catch (ClassNotFoundException e) { | ||
| throw new AvroRuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private boolean trustAllPackages() { | ||
| return (trustedPackages.size() == 1 && "*".equals(trustedPackages.get(0))); | ||
| } | ||
|
|
||
| private void checkSecurity(Class clazz) throws ClassNotFoundException { | ||
| if (trustAllPackages() || clazz.isPrimitive()) { | ||
| return; | ||
| } | ||
|
|
||
| boolean found = false; | ||
| Package thePackage = clazz.getPackage(); | ||
| if (thePackage != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can take a look on this too as I'm the original author
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MichalFoksa Would you like to send a Pull Request?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Else I can work on a PR tomorrow
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for (String trustedPackage : getTrustedPackages()) { | ||
| if (thePackage.getName().equals(trustedPackage) || thePackage.getName().startsWith(trustedPackage + ".")) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!found) { | ||
| throw new SecurityException("Forbidden " + clazz | ||
| + "! This class is not trusted to be included in Avro schema using java-class. Please set org.apache.avro.SERIALIZABLE_PACKAGES system property with the packages you trust."); | ||
martin-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| public final List<String> getTrustedPackages() { | ||
| return trustedPackages; | ||
| } | ||
|
|
||
| @Override | ||
| protected Object readRecord(Object old, Schema expected, ResolvingDecoder in) throws IOException { | ||
| SpecificData data = getSpecificData(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.