Kaileshwar16 commented on code in PR #838:
URL: https://github.com/apache/wayang/pull/838#discussion_r4559740978
##########
wayang-commons/wayang-basic/src/main/java/org/apache/wayang/basic/operators/ObjectFileSerialization.java:
##########
@@ -121,6 +163,304 @@ private static List<Object> deserializeLegacy(byte[]
payload) throws IOException
}
}
+ /**
+ * Creates an {@link ObjectInputFilter} enforcing resource limits and
class allowlists for legacy Java deserialization.
+ *
+ * @param elementType the expected element type (optional, may be {@code
null})
+ * @return an {@link ObjectInputFilter} that permits only expected, safe
classes within configured resource bounds
+ */
+ public static ObjectInputFilter createObjectInputFilter(Class<?>
elementType) {
+ final long maxDepth = getMaxDepth();
+ final long maxArrayLength = getMaxArrayLength();
+ final long maxReferences = getMaxReferences();
+ final long maxBytes = getMaxBytes();
+
+ ObjectInputFilter filter = filterInfo -> {
+ // Check stream/graph resource bounds before serialClass() since
limit callbacks may pass serialClass == null.
+ if (filterInfo.depth() > maxDepth ||
+ filterInfo.arrayLength() > maxArrayLength ||
+ filterInfo.references() > maxReferences ||
+ filterInfo.streamBytes() > maxBytes) {
+ return ObjectInputFilter.Status.REJECTED;
+ }
+
+ Class<?> clazz = filterInfo.serialClass();
+ if (clazz == null) {
+ return ObjectInputFilter.Status.UNDECIDED;
+ }
+ if (isAllowedClass(clazz, elementType)) {
+ return ObjectInputFilter.Status.ALLOWED;
+ }
+ return ObjectInputFilter.Status.REJECTED;
+ };
+
+ ObjectInputFilter globalFilter =
ObjectInputFilter.Config.getSerialFilter();
+ if (globalFilter != null) {
+ return ObjectInputFilter.merge(globalFilter, filter);
+ }
+ return filter;
+ }
+
+ private static boolean isAllowedClass(Class<?> clazz, Class<?>
elementType) {
+ Class<?> current = clazz;
+ while (current.isArray()) {
+ current = current.getComponentType();
+ }
+ if (current.isPrimitive()) {
+ return true;
+ }
+
+ // Standard base object and scalar types
+ if (current == Object.class ||
+ current == String.class ||
+ current == Boolean.class ||
+ current == Character.class ||
+ current == Class.class ||
+ current == Void.class ||
+ current == java.lang.Record.class ||
+ isStandardNumber(current) ||
+ Enum.class.isAssignableFrom(current) ||
+ isStandardCharSequence(current)) {
+ return true;
+ }
+
+ // Standard math
+ if (current.getName().startsWith("java.math.")) {
+ return true;
+ }
+
+ // Standard date and time types
+ if (current.getName().startsWith("java.time.") ||
+ (Date.class.isAssignableFrom(current) &&
+ (current.getName().startsWith("java.util.") ||
current.getName().startsWith("java.sql.")))) {
+ return true;
+ }
+
+ // Standard collections, maps, and common utilities
+ if (((Collection.class.isAssignableFrom(current) ||
+ Map.class.isAssignableFrom(current) ||
+ Map.Entry.class.isAssignableFrom(current)) &&
+ current.getName().startsWith("java.util.")) ||
+ current.getName().equals("java.util.CollSer") ||
+ current.getName().startsWith("java.util.Collections$") ||
+ current.getName().startsWith("java.util.Arrays$") ||
+
current.getName().startsWith("java.util.ImmutableCollections$") ||
+ current == UUID.class ||
+ current == BitSet.class ||
+ current == Locale.class ||
+ current == Currency.class ||
+ current == Optional.class) {
+ return true;
+ }
+
+ // Wayang internal data quantum types
+ if (current.getName().equals("org.apache.wayang.basic.data.Record") ||
+
current.getName().equals("org.apache.wayang.basic.data.Tuple2") ||
+
current.getName().equals("org.apache.wayang.basic.data.Tuple5") ||
+ current.getName().equals("org.apache.wayang.core.util.Tuple")
||
+
current.getName().equals("org.apache.wayang.basic.model.op.Op") ||
+
current.getName().equals("org.apache.wayang.flink.compiler.criterion.WayangValue")
||
+
current.getName().equals("org.apache.wayang.flink.compiler.criterion.WayangListValue"))
{
+ return true;
+ }
+
+ // Scala tuples and basic collections (for Spark / Scala platform
compatibility)
+ if (current.getName().startsWith("scala.Tuple") ||
+ current.getName().startsWith("scala.collection.")) {
+ return true;
+ }
Review Comment:
Would it be safer to restrict the Scala allowlist to known library classes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]