CRITICAL
Rule Definition
It is often convenient to serialize objects for communication or to save them for later use. However, deserialized data or code can often be modified without using the provided accessor functions if it does not use cryptography to protect itself. Furthermore, any cryptography would still be client-side security -- which is a dangerous security assumption.
Data that is untrusted can not be trusted to be well-formed.
Deserialized data or code can often be modified if the functions readObject() of ObjectInputStream is not defined in class which include readObject() function of ObjectInputStream with deserialize signature.
Remediation
It is often convenient to serialize objects for communication or to save them for later use. However, deserialized data or code can often be modified without using the provided accessor functions if it does not use cryptography to protect itself. Furthermore, any cryptography would still be client-side security -- which is a dangerous security assumption.
Data that is untrusted can not be trusted to be well-formed.
When developers place no restrictions on "gadget chains," or series of instances and method invocations that can self-execute during the deserialization process (i.e., before the object is returned to the caller), it is sometimes possible for attackers to leverage them to perform unauthorized actions, like generating a shell.
Violation Code Sample
public static class Person
{
private String fname = null;
private String lname = null;
}
try {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.txt"));
Person personRead = (Person) objectInputStream.readObject(); // violation because Person does not implement readObject
objectInputStream.close();
}
Fixed Code Sample
public static class Person
{
private String fname = null;
private String lname = null;
private final void readObject(ObjectInputStream aInputSteam) throws ClassNotFoundException, IOException
{
aInputSteam.defaultReadObject();
}
}
try {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.txt"));
Person personRead = (Person) objectInputStream.readObject(); // no violation because Person does implement readObject as private and final method that cannot be overwritten
objectInputStream.close();
}
Reference
https://cwe.mitre.org/data/definitions/502.html
https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
Related Technologies
Technical Criterion
CWE-502 - Deserialization of Untrusted Data
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.