Sit down, let me explain something that sounds boring and is not. You know how an application needs to save things? A shopping cart, a user’s preferences, a job waiting in a queue. The program has an object in memory, with fields and values, and it needs to turn that into bytes so it can be written to a file, put in a cookie, or sent to another service. That conversion is called serialisation. Reading the bytes back and rebuilding the object is deserialisation.
Nothing wrong so far. The trouble starts when the bytes come from someone you should not trust, and the rebuilding step does more than copy values into fields.
Rebuilding is not the same as reading
Compare two ways of receiving data. If you receive a plain text record that says a quantity is 3 and a product is “notebook”, your code reads two values and decides what to do with them. You stay in control. The data never chooses its own behaviour.
Now take a rich serialisation format, such as Java’s native object serialisation, Python’s pickle, or the binary formatters in older .NET. These formats do not just carry values. They carry the name of the class to create. The library reads “make an object of this class”, finds the class in your application, and creates it, often running special methods along the way: constructors, cleanup hooks, methods that fire when a field is assigned. Those methods were written by developers for honest purposes, like restoring a database connection after loading.
So the sender of the bytes gets to pick which of your classes come alive, and with what field values. They cannot write new code, but they can choose from every class already loaded in your application, including all your libraries. That is a much bigger menu than you imagined when you wrote a simple “load the saved cart” feature.
Gadgets: borrowing your own code
Security researchers call the harmful combinations “gadget chains”. Think of it like this. Class A has a cleanup method that calls a method on one of its fields. Class B, from a different library, has a method that does something powerful, such as invoking a command or reading a file. If an attacker can build a serialised A whose field is a B, then merely loading the bytes triggers the powerful action. No single class is buggy. The danger is in the combination, and the combinations multiply as your dependency list grows.
This is why the 2015 wave of Java deserialisation findings affected so many products at once. Widely used libraries contained usable building blocks, so any application that deserialised untrusted bytes and had those libraries on its classpath was exposed. Insecure deserialisation also sat in the OWASP Top 10 for 2017 as its own entry and was folded into the “software and data integrity failures” category in the 2021 edition.
I will not describe how to assemble a chain. You do not need that to defend against it, and the defence is mostly design.
Where untrusted bytes sneak in
Ask yourself: where does my application accept serialised objects from outside? Common answers, some of them surprising:
- A hidden form field or cookie that stores session or view state on the client side, so the server stays “stateless”.
- A message queue or cache that several internal services share. Internal does not mean trusted if any one service is compromised.
- A file upload feature that accepts “saved project” files.
- Remote method calls between services built on native serialisation.
- A machine-learning model file loaded from a public hub. Some model formats are pickled objects underneath, so downloading a model is, in effect, deserialising someone else’s bytes.
That last one catches data science teams who would never dream of running an unknown program, yet happily load an unknown model file.
A small walkthrough
Imagine a ticket-booking site run by a small team in Kochi. To keep servers stateless, they store the user’s booking draft in a cookie as a serialised object, signed by nothing, because “users cannot read binary anyway”. Someone who captures that cookie sees it is a standard serialised blob. They do not need to understand the booking fields. They only need to replace the blob with a different serialised object and send it back. The server dutifully rebuilds whatever arrives. At best the booking logic misbehaves; at worst, a chain of classes from the framework does something the developers never intended.
Notice that encryption of the transport (HTTPS) does nothing here. The attacker is the legitimate client, sending data over a perfectly secure connection.
What actually helps
In rough order of strength:
- Do not deserialise untrusted data into rich objects. Use a plain data format such as JSON, and map it onto simple data classes that you define. A parser that only yields strings, numbers and lists cannot instantiate arbitrary classes. (Note that some JSON libraries have an optional “polymorphic typing” mode that reintroduces the same risk. Leave it off for untrusted input.)
- Keep state on the server. Store the draft against a session identifier. The client holds a random ID, not an object.
- If you must accept native formats, restrict the classes. Modern Java lets you set deserialisation filters that allow only an explicit list of types. An allow-list is meaningful; a block-list of “known bad” classes is not, because new gadgets keep being found.
- Authenticate before you parse. If the data must travel through the client, sign it with a key only the server holds and verify the signature first. Then a tampered blob is rejected before any object is built. This protects against outsiders, not against a bug in what you yourself signed.
- Run with less privilege. If a deserialisation bug does exist, the damage depends on what the process can reach. A service account with narrow permissions turns a disaster into an incident.
Spotting it in review
When you read code or a design document, search for the names of native deserialisation calls in your language, and for any place where a byte stream from a network, queue or file is passed straight into them. Ask who controls those bytes. Software composition tools help as well, since many known gadget-bearing library versions are catalogued, but they cannot see your own classes, so treat their silence as partial comfort only.
Detection at runtime is hard, because a malicious payload looks like an ordinary binary blob. You may notice unusual child processes spawned by an application server, or unexpected outbound connections right after a request carrying a large opaque parameter. That is why prevention at design time matters far more than clever monitoring.
So the next time someone says “we just serialise it and load it back, simple”, ask the follow-up: loaded back by whom, from where, and what can that data cause to happen?
Figures and names in the examples are illustrative.
Leave a Reply