Add an optional class filter to JavaSerializer deserialization - #1295
Open
Nexory wants to merge 1 commit into
Open
Add an optional class filter to JavaSerializer deserialization#1295Nexory wants to merge 1 commit into
Nexory wants to merge 1 commit into
Conversation
JavaSerializer.read deserializes with ObjectInputStream.readObject and previously applied no restriction on which classes may be deserialized; Kryo's setRegistrationRequired does not apply on this path because the bytes are handed to the JDK serialization mechanism. Add an opt-in setClassFilter(Predicate<Class>), checked in resolveClass, so a class for which the predicate returns false is rejected with a KryoException before it is used. The default is unchanged (no filter) and the existing classloader resolution is preserved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What / Why
JavaSerializer.read(...)deserializes withObjectInputStream.readObject(), and itsObjectInputStreamWithKryoClassLoaderoverrides onlyresolveClass(...)for classloader resolution. It applies no restriction on which classes may be deserialized. Kryo's ownsetRegistrationRequired(true)does not help on this path, because the bytes are handed to the JDK serialization mechanism rather than resolved through Kryo's class registration, so a Kryo stream that usesJavaSerializerfor any field type deserializes arbitrary classes from the input.This adds an opt-in class filter to
JavaSerializeras defense-in-depth when reading serialized data from an untrusted source:setClassFilter(Predicate<Class>)/getClassFilter().resolveClass(...): a class for which the predicate returnsfalseis rejected with aKryoExceptionbefore it is used.Non-breaking
The default is unchanged. With no filter set (
null, the default) behavior is exactly as before, and the existing classloader-resolution logic inresolveClass(...)is preserved.Java 8 note
Kryo targets Java 8, so this uses a
Predicate<Class>checked in the existingresolveClass(...)hook rather than the Java 9+java.io.ObjectInputFilter(JEP-290). If you prefer, I am happy to additionally wire a realObjectInputFilteron Java 9+ (resolved reflectively so the Java 8 build is unaffected), or to expose aSet<String>allowlist instead of a predicate. Whichever shape you prefer, the check stays in one place.Tests
JavaSerializerTestadds two cases: a filter that disallows the class rejects deserialization (asKryoException), and a filter that allows it still round-trips. The existing tests are unchanged. Verified withmvn -Dtest=JavaSerializerTest teston JDK 11 (source/target 8).