- java.lang.Object
-
- java.io.InputStream
-
- java.io.ObjectInputStream
-
- com.rabbitmq.jms.util.WhiteListObjectInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.DataInput,java.io.ObjectInput,java.io.ObjectStreamConstants,java.lang.AutoCloseable
public class WhiteListObjectInputStream extends java.io.ObjectInputStreamAn
ObjectInputStreamimplementation that checks loaded classes against a list of trusted packages or package prefixes.Heavily inspired by and derived from org.apache.activemq.util.ClassLoadingAwareObjectInputStream in ActiveMQ as well as https://github.com/spring-projects/spring-amqp/commit/4150f107e60cac4a7735fcf7cb4c1889a0cbab6c.
- See Also:
ObjectInputStream
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.List<java.lang.String>DEFAULT_TRUSTED_PACKAGES-
Fields inherited from interface java.io.ObjectStreamConstants
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, SERIAL_FILTER_PERMISSION, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING
-
-
Constructor Summary
Constructors Constructor Description WhiteListObjectInputStream(java.io.InputStream in)Creates an ObjectInputStream that reads from the specified InputStream.WhiteListObjectInputStream(java.io.InputStream in, java.util.List<java.lang.String> trustedPackages)Creates an ObjectInputStream that reads from the specified InputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddTrustedPackage(java.lang.String trustedPackage)java.util.List<java.lang.String>getTrustedPackages()protected java.lang.Class<?>resolveClass(java.io.ObjectStreamClass desc)Load the local class equivalent of the specified stream class description.protected java.lang.Class<?>resolveProxyClass(java.lang.String[] interfaces)voidsetTrustedPackages(java.util.List<java.lang.String> trustedPackages)booleanshouldTrustAllPackages()-
Methods inherited from class java.io.ObjectInputStream
available, close, defaultReadObject, enableResolveObject, getObjectInputFilter, read, read, readBoolean, readByte, readChar, readClassDescriptor, readDouble, readFields, readFloat, readFully, readFully, readInt, readLine, readLong, readObject, readObjectOverride, readShort, readStreamHeader, readUnshared, readUnsignedByte, readUnsignedShort, readUTF, registerValidation, resolveObject, setObjectInputFilter, skipBytes
-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Constructor Detail
-
WhiteListObjectInputStream
public WhiteListObjectInputStream(java.io.InputStream in) throws java.io.IOExceptionCreates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.
- Parameters:
in- input stream to read from- Throws:
java.io.IOException- if an I/O error occurs while reading stream headerjava.lang.SecurityException- if untrusted subclass illegally overrides security-sensitive methodsjava.lang.NullPointerException- ifinisnull- See Also:
ObjectInputStream(),ObjectInputStream.readFields()
-
WhiteListObjectInputStream
public WhiteListObjectInputStream(java.io.InputStream in, java.util.List<java.lang.String> trustedPackages) throws java.io.IOExceptionCreates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.
- Parameters:
in- input stream to read fromtrustedPackages- List of packages that are trusted. Classes in them will be serialized.- Throws:
java.io.IOException- if an I/O error occurs while reading stream headerjava.lang.SecurityException- if untrusted subclass illegally overrides security-sensitive methodsjava.lang.NullPointerException- ifinisnull- See Also:
ObjectInputStream(),ObjectInputStream.readFields()
-
-
Method Detail
-
resolveClass
protected java.lang.Class<?> resolveClass(java.io.ObjectStreamClass desc) throws java.io.IOException, java.lang.ClassNotFoundExceptionLoad the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source.The corresponding method in
ObjectOutputStreamisannotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return aClassobject. Once returned, if the class is not an array class, its serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and an exception is thrown.The default implementation of this method in
ObjectInputStreamreturns the result of callingClass.forName(desc.getName(), false, loader)whereloaderis determined as follows: if there is a method on the current thread's stack whose declaring class was defined by a user-defined class loader (and was not a generated to implement reflective invocations), thenloaderis class loader corresponding to the closest such method to the currently executing frame; otherwise,loaderisnull. If this call results in aClassNotFoundExceptionand the name of the passedObjectStreamClassinstance is the Java language keyword for a primitive type or void, then theClassobject representing that primitive type or void will be returned (e.g., anObjectStreamClasswith the name"int"will be resolved toInteger.TYPE). Otherwise, theClassNotFoundExceptionwill be thrown to the caller of this method.- Overrides:
resolveClassin classjava.io.ObjectInputStream- Parameters:
desc- an instance of classObjectStreamClass- Returns:
- a
Classobject corresponding todesc - Throws:
java.io.IOException- any of the usual Input/Output exceptions.java.lang.ClassNotFoundException- if class of a serialized object cannot be found or isn't trusted.
-
resolveProxyClass
protected java.lang.Class<?> resolveProxyClass(java.lang.String[] interfaces) throws java.io.IOException, java.lang.ClassNotFoundException- Overrides:
resolveProxyClassin classjava.io.ObjectInputStream- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
addTrustedPackage
public void addTrustedPackage(java.lang.String trustedPackage)
-
getTrustedPackages
public java.util.List<java.lang.String> getTrustedPackages()
- Returns:
- list of packages trusted for deserialization from ObjectMessage payloads
-
setTrustedPackages
public void setTrustedPackages(java.util.List<java.lang.String> trustedPackages)
- Parameters:
trustedPackages- list of packages trusted for deserialization from ObjectMessage payloads
-
shouldTrustAllPackages
public boolean shouldTrustAllPackages()
- Returns:
- true if this object stream considers all packages to be trusted, false otherwise
-
-