CONTENTS | PREV | NEXT | Java Object Serialization Specification |
To guarantee that a deserialized object does not have state which violates some set of invariants that need to be guaranteed, a class can define its own serializing and deserializing methods. If there is some set of invariants that need to be maintained between the data members of a class, only the class can know about these invariants, and it is up to the class author to provide a deserialization method that checks these invariants.Security-conscious implementors should keep in mind that a serializable class'
readObject
method is effectively a public constructor, and should be treated as such. This is true whether thereadObject
method is implicit or explicit. It is not safe to assume that the byte stream that is provided to thereadObject
method was generated by serializing a properly constructed object of the correct type. It is good defensive programming to assume that the byte stream is provided by an adversary whose goal is to compromise the object under construction.This is important even if you are not worried about security; it is possible that disk files can be corrupted and serialized data be invalid. So checking such invariants is more than just a security measure; it is a validity measure. However, the only place it can be done is in the code for the particular class, since there is no way the serialization package can determine what invariants should be maintained or checked.