Serialization as an Attack Vector
You'll learn why deserializing untrusted data is one of Java's most dangerous security vulnerabilities.
The Core Danger
When you deserialize an object using ObjectInputStream, Java doesn't just read data—it executes code. Specifically, it calls constructors, readObject() methods, and can trigger all sorts of class initialization logic. If an attacker controls the serialized byte stream, they can craft malicious payloads that exploit this automatic code execution.
Think of deserialization like opening a package at your door. If it's from a trusted friend, great! But if it's from an unknown source, that "package" might contain something harmful that activates the moment you open it.
How Attackers Exploit It
Attackers don't need to write new classes. They exploit libraries already on your classpath. They chain together existing classes (called "gadget chains") whose readObject() methods perform dangerous operations—file system access, network calls, or even arbitrary command execution. By carefully crafting the serialized bytes, they trigger these chains automatically during deserialization.
The infamous Apache Commons Collections vulnerability (CVE-2015-7501) allowed attackers to execute arbitrary system commands just by sending a malicious serialized object to applications that deserialized untrusted data.
Why It's So Effective
- Automatic execution:
readObject()runs automatically—no explicit method call needed - Complex classpaths: Modern applications have hundreds of libraries, increasing gadget chain possibilities
- Hard to defend: You can't easily "sanitize" serialized data without deserializing it first
The Golden Rule
Never deserialize data from untrusted sources. If you must accept external data, use safe formats like JSON or XML with strict schemas, or implement authentication and integrity checks (digital signatures) before deserialization.
Key Takeaway: Deserialization of untrusted data is effectively remote code execution waiting to happen—treat serialized objects from unknown sources like executable files from strangers.