Skip to content

Implement ObjectInputFilter allowlist for legacy object deserialization - #838

Open
AliRana30 wants to merge 2 commits into
apache:mainfrom
AliRana30:fix/object-input-filter-835
Open

AliRana30 wants to merge 2 commits into
apache:mainfrom
AliRana30:fix/object-input-filter-835

Conversation

@AliRana30

Copy link
Copy Markdown
Contributor

What changes?

  1. ObjectFileSerialization:
    • Adds createObjectInputFilter(Class<?> elementType) to restrict legacy object deserialization to expected primitives, wrappers, standard safe collections, Wayang types, Scala tuples, and expected elementType hierarchies.
    • Adds dynamic allowlist pattern configuration via wayang.basic.serialization.allowlist.
    • Rejects unauthorized classes with InvalidClassException.
  2. WayangValue:
    • Enforces the ObjectInputFilter on Flink criterion deserialization.
    • Refactors convertToByte and convertToObject to use try-with-resources.
  3. Tests:
    • Added unit test cases to verify rejection of disallowed classes, explicit elementType acceptance, and system property pattern matching.

Why are the changes needed?

Directly calling ObjectInputStream.readObject() without an ObjectInputFilter allows arbitrary class instantiation, posing a deserialization security risk (CWE-502).

How was this tested?

  • Unit tests added to ObjectFileSerializationTest and WayangValueTest.
  • Ran mvn test on wayang-basic and wayang-flink.
  • Ran mvn apache-rat:check.

Closes : #835

Copilot AI balanced review requested due to automatic review settings October 10, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@AliRana30

Copy link
Copy Markdown
Contributor Author

@zkaoudi Pr is up for your review.

@novatechflow

Copy link
Copy Markdown
Member

Thanks for the fix. Three issues to address before merging:

  • The allowlist accepts arbitrary Number, Collection, and Map subclasses. Custom readObject() hooks still execute.
  • A fresh WayangValue rejects valid custom payloads and silently returns null because the expected type comes from previous data.
  • List.of(), Set.of(), and Map.of() fail deserialization because java.util.CollSer is missing.

Reproduced all three on Java 17. Please tighten the allowlist and add regression tests.

@AliRana30
AliRana30 force-pushed the fix/object-input-filter-835 branch from 05351a2 to c2850f7 Compare October 21, 2026 23:02
@AliRana30

Copy link
Copy Markdown
Contributor Author

@novatechflow Updated the allowlist and WayangValue as per the feedback. Alos added regression tests for all three cases in ObjectFileSerializationTest and WayangValueTest. Verified clean build on Java 17

@novatechflow

Copy link
Copy Markdown
Member
  • Allowlist bypass: Number.class still permits arbitrary subclasses, and specifying a payload type trusts unrelated classes in its package. Custom readObject() hooks execute in both cases.
  • Fresh Flink values: a no-argument WayangValue still rejects valid custom payloads with InvalidClassException.
  • New tests manually supply the type; Flink’s fresh-value path is still uncovered.

Immutable collections now round-trip correctly, and deserialization failures propagate instead of silently returning null. Great work!

@AliRana30
AliRana30 force-pushed the fix/object-input-filter-835 branch from c2850f7 to 91bacd7 Compare October 24, 2026 21:36
@AliRana30

Copy link
Copy Markdown
Contributor Author

@novatechflow Updated the PR and added the following :
1.Restricted Number to boxed primitives/math types and collections/maps to java.util.* implementations to prevent arbitrary subclasses from executing custom readObject() hooks.
2.Added explicit type support (type field, constructors, getters/setters, and convertToObject overload) to WayangValue for fresh instances and replaced silent null returns with WayangException.
3.Allowed java.util.CollSer to support Java 9+ List.of(), Set.of(), and Map.of().
4.Added regression tests covering all three cases in ObjectFileSerializationTest and WayangValueTest. Verified on Java 17.

@novatechflow

Copy link
Copy Markdown
Member

Took a bit longer to check and write a proper review since its sec relevant, three blockers still reproduced with Java 17:

  • Number.class permits custom subclasses whose readObject() hooks execute.
  • A payload type permits unrelated classes in its package; their hooks execute.
  • A fresh no-argument WayangValue rejects valid custom payloads.
  1. Arbitrary subclasses remain allowed.
    ObjectFileSerialization.java:228–229 accepts any class assignable to the expected type. With Number.class, this includes custom Number subclasses, bypassing the earlier boxed-number restriction.

    Implication: custom readObject() hooks execute. The intended security boundary remains bypassable; actual harm depends on available classes and input control.

  2. An expected type trusts its entire package and subpackages.
    ObjectFileSerialization.java:234–236 permits unrelated classes whose names start with the expected type’s package.

    Implication: selecting one legitimate payload class also permits unrelated classes and their deserialization hooks. The probe confirmed hook execution.

  3. Fresh Flink values still lack the expected type.
    The no-argument constructor sets both data and type to null. Deserialization consequently installs a filter without an expected custom type.

    Implication: valid custom payloads fail with InvalidClassException when read into fresh instances. The regression test manually supplies CustomPayload.class, so it does not cover this failing path.

@AliRana30
AliRana30 force-pushed the fix/object-input-filter-835 branch from 91bacd7 to 6c24cbc Compare October 27, 2026 12:54
@AliRana30

AliRana30 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and security probes. @novatechflow

  • Subclasses & Broad Types: Removed isAssignableFrom matching and restricted Number.class (and broad types) from allowing custom subclasses. Only exact classes and nested member classes are accepted for custom element types.
  • Package-Level Trust: Removed package-prefix matching so unrelated classes in the same package are rejected.
  • Fresh WayangValue: Updated WayangValue.write() and read() to serialize and restore the payload type so fresh no-arg instances retain the expected type during Flink deserialization.
  • Regression Tests: Added tests covering Number.class custom subclass rejection without hook execution, rejection of unrelated classes in the same package, rejection of arbitrary payload subclasses, and fresh no-argument WayangValue deserialization.

@novatechflow

Copy link
Copy Markdown
Member

Thanks, the previous three issues are fixed. One security issue comes with the new type header:

WayangValue.read() takes the class name from the stream and uses it as the expected type for a fresh instance. The filter then allows that class.

So the input itself decides which class to trust. Reproduced on Java 17: an otherwise rejected custom class executes its readObject() hook.

The expected type needs to come from trusted configuration or an allowlist, rather than the stream. Please add a regression test for this case.

@AliRana30
AliRana30 force-pushed the fix/object-input-filter-835 branch from 6c24cbc to 7527c1a Compare November 5, 2026 22:12
@AliRana30

Copy link
Copy Markdown
Contributor Author

hi @novatechflow Thanks for the thorough review and security probe findings!

All three issues along with additional Java 17 edge cases have been resolved and covered by regression tests:

  1. Custom Number & Arbitrary Subclass Hooks:

    • Removed targetComponent.isAssignableFrom(...) and prohibited custom subclasses of Number, Collection, and Map.
    • Concrete custom payload types require exact class matches (allowing inner/nested member classes of the payload only if they are not collection/map/number subclasses).
    • Broadened boundary checks (AutoCloseable, Closeable, Throwable, Exception, Error, Enum, Record).
  2. Package Trust Elimination:

    • Removed targetComponent.getPackage() prefix matching so selecting a payload class will not allow unrelated classes in the same package.
    • Narrowed Wayang internal types from org.apache.wayang.* down to explicit core data structures (Record, Tuple2, Tuple5, Tuple, Op, WayangValue, WayangListValue).
  3. Fresh No-Argument WayangValue Deserialization:

    • Updated WayangValue.write/read to persist and securely resolve the payload class against Serializable, dynamically configuring the deserialization filter with the expected custom type.
    • Tested single and collection (WayangListValue) round-trips for fresh no-arg instances with custom payloads.

@Kaileshwar16

Copy link
Copy Markdown
Contributor

the Serializable check in read() might reject types like Object.class or List.class, even when the actual value is serializable. Also, the allowlist doesn't seem to enforce limits on object depth, array size, or reference count, which might be worth considering. One more thing, WayangValue.read() still seems to trust the class name from the stream when this.type is null. wouldn't that leave the earlier security concern unresolved?

Im still getting familiar with this codebase, so I might be missing some context. happy to hear your thoughts

@novatechflow

Copy link
Copy Markdown
Member

Thanks for the update, and @Kaileshwar16, your concerns are valid.

Rechecked on Java 17: the stream-selected class still executes its readObject() hook. Checking Serializable does not establish trust. The new check also rejects valid payloads declared as Object or List.

The resource-limit concern is valid too: there are no local depth, array-size, or reference limits.

Please resolve the expected type from trusted configuration or an allowlist and add regression tests for these cases.

…ource limits, and allow Object/List payloads
@AliRana30

Copy link
Copy Markdown
Contributor Author

Thanks @novatechflow and @Kaileshwar16 for the review! Pushed a follow-up commit fixing all points:

  1. No stream-selected class trust (WayangValue): Removed the stream type-name header (write()/read() back to [varint len][payload]), so WayangValue.read() never loads or trusts stream-supplied classes. Expected custom types now come only from the declared Class<T> (WayangValue(Class<T>) / setType), trusted config (wayang.flink.value.expected-type or wayang.basic.serialization.expected-type), or configured allowlists (wayang.basic.serialization.allowlist / wayang.flink.serialization.allowlist) via system properties or Wayang Configuration.
  2. Object / List support: Removed the Serializable assignable check in WayangValue.read(). Object.class and List.class (ArrayList, Arrays.asList, List.of) payloads deserialize cleanly with targetType.isInstance(result) validation, while non-allowlisted custom subclasses with hooks remain rejected.
  3. Resource limits & bounds: ObjectFileSerialization.createObjectInputFilter enforces depth (100), arrayLength (1_048_576), references (100_000), and streamBytes (67_108_864) before serialClass() == null returns UNDECIDED (overridable via wayang.basic.serialization.max-*). WayangValue.read() also validates varint lengths against [0, maxBytes] before allocating.
  4. Java 17 regression tests: Added coverage in ObjectFileSerializationTest (19/19) and WayangValueTest (13/13) for stream-hook rejection, Object/List payloads and mismatches, trusted config/allowlist resolution, and resource limits (wayang-basic 33/33 passed; wayang-flink 37 passed, 1 pre-existing skip in FlinkGlobalReduceOperatorTest; apache-rat:check passed).

Comment on lines +268 to +271
if (current.getName().startsWith("scala.Tuple") ||
current.getName().startsWith("scala.collection.")) {
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be safer to restrict the Scala allowlist to known library classes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@novatechflow what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Improvement] Implement ObjectInputFilter allowlist for legacy object deserialization

4 participants