Quick revision
The short answer
Commons Collections 3.2.1 contains classes, such as InvokerTransformer, that an attacker can chain together inside a serialized Java object so that simply deserialising it runs arbitrary code. The risk is real only if the service deserialises untrusted data, but a scanner cannot tell, so we remove it: find who pulls it in with mvn dependency:tree, upgrade to 3.2.2 or move to commons-collections4 4.1 or later, and add a JEP 290 deserialisation filter as a second line of defence.
What to remember
In this answer, we saw that Commons Collections 3.2.1 is dangerous because its InvokerTransformer and related classes can be chained inside a serialised object to run code during deserialisation, a 2015 discovery that affected many servers. The real risk depends on whether the service deserialises untrusted data, but because a scanner cannot know, we remove it anyway: find the parent with dependency:tree, move to 3.2.2 or commons-collections4 4.1 or later, and add a JEP 290 allow-list filter so the next gadget chain is also blocked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Commons Collections 3.2.1 contains classes, such as InvokerTransformer, that an attacker can chain together inside a serialized Java object so that simply deserialising it runs arbitrary code. The risk is real only if the service deserialises untrusted data, but a scanner cannot tell, so we remove it: find who pulls it in with mvn dependency:tree, upgrade to 3.2.2 or move to commons-collections4 4.1 or later, and add a JEP 290 deserialisation filter as a second line of defence.
Why this question is asked
This finding shows up in almost every old Java service, because commons-collections 3.2.1 was a transitive dependency of half the ecosystem in the early 2010s. Interviewers like it because a good answer needs two things at once: understanding why a library with no bug in its own logic can be dangerous, and knowing the practical steps to fix it without guessing. In this answer, we will recap Java deserialisation, see how a gadget chain works, decide how exposed the service really is, and then go through the fix, the filter and how to find the dependency.
1A Quick Recap of Java Deserialisation
Java serialisation turns an object graph into bytes, and deserialisation turns the bytes back into objects. ObjectInputStream.readObject() reads the class names from the stream, creates instances of those classes, and fills in their fields. Crucially, it can also call methods while doing so: any class may define a private readObject method that runs during deserialisation, and collections such as HashMap call hashCode() and equals() on their keys while rebuilding themselves.
So the bytes decide which classes are created and which code runs. If the bytes come from an attacker, the attacker chooses any class that is on our classpath and implements Serializable. That is the root of the problem, and it is a property of Java serialisation, not of Commons Collections.
2How a Gadget Chain Turns Data Into Code
A gadget is an ordinary class whose methods, when called during deserialisation, do something useful to an attacker. A gadget chain links several of them. In Commons Collections 3.x, InvokerTransformer is the key gadget: it is Serializable, and its transform method calls any method by name using reflection. ChainedTransformer runs several transformers in a row, and LazyMap calls a transformer when a missing key is looked up.
An attacker builds an object graph offline in which a trigger class, one whose readObject touches a map, holds a LazyMap whose transformer chain ends in Runtime.getRuntime().exec(command). They serialise it and send the bytes to any endpoint that calls readObject. Our code never has to use Commons Collections itself; it only has to be on the classpath. This was published in 2015 and caused vulnerabilities in WebLogic, WebSphere, JBoss and Jenkins, among others.
6 messages in order. Play them, or step through them one at a time.
The command runs during deserialisation; casting or validating the result afterwards is too late.
Notice that the attack happens inside readObject, before our code gets the object back and before any cast or validation we might do. That is why checking the type afterwards does not help.
3How Exposed Is This Service Really?
The risk becomes real only when the service deserialises data that an attacker can influence. Typical places are endpoints that accept Java-serialised objects over HTTP, RMI registries, JMX ports exposed over the network, old messaging systems that carry ObjectMessage payloads, session or cache stores that hold serialised objects, and some older remoting protocols. A modern Spring Boot service that only speaks JSON over HTTP may have no such entry point at all.
But we should not stop at that argument. The scanner cannot see entry points, a future change could add one, and a library or framework inside the service might deserialise something we did not notice. Proving the absence of every deserialisation path is harder than removing the library. So the decision is simple: we remove or upgrade it, and we treat the exposure analysis as telling us how urgent it is, not whether to do it.
4Finding Who Pulls It In
In old services, commons-collections is almost never a direct dependency. Some other library brings it in, so the first step is to find out which one.
Now we know the parent. Often the cleanest fix is to upgrade that parent to a version that no longer needs the old library, or that depends on a fixed one. If the parent cannot be upgraded, we pin the fixed version ourselves in dependencyManagement, or exclude the old artifact and add the replacement.
5The Fix: 3.2.2 or commons-collections4
There are two safe targets. Version 3.2.2, released in November 2015, keeps the same package, org.apache.commons.collections, and disables deserialisation of the dangerous functor classes such as InvokerTransformer by default; deserialising them throws an UnsupportedOperationException unless someone sets the system property org.apache.commons.collections.enableUnsafeSerialization=true, which we must never do. Because the package is the same, 3.2.2 is a drop-in replacement and the right quick fix for a transitive dependency.
The long-term target is commons-collections4, a different artifact with a different package, org.apache.commons.collections4. Because the package differs, code must be changed to use it, but it is the maintained line. Note that version 4.0 had the same problem and 4.1 fixed it, so we want 4.1 or later; check the current release, which is 4.5 at the time of writing.
After the change we run dependency:tree again to confirm that 3.2.1 is gone, and rerun the scanner. Commons Collections is not the only library with gadgets, which is why the next step matters.
6A Second Line of Defence: JEP 290 Filters
Upgrading removes this particular gadget chain, but other libraries have had their own. JEP 290, available since Java 9 and backported to 8u121, lets us filter which classes ObjectInputStream may create. The filter runs before each class is instantiated, so a rejected class never gets the chance to run code. We can set it for the whole JVM with the jdk.serialFilter system property, or per stream in code, and since Java 17 a filter factory can apply context-specific filters (JEP 415).
The pattern lists allowed packages and ends with !*, which rejects everything not listed. An allow list is the safe form; a block list of known gadgets is always one gadget behind. Best of all, where we control both ends, we replace Java serialisation with JSON or another format that does not create arbitrary classes.
7Follow-up questions
If the service never deserialises anything, is the finding a false positive?
It is a low-risk finding rather than a false positive. The vulnerable code is present, and a future change, a framework feature or an exposed JMX port could create a path to it. Upgrading to 3.2.2 is cheap and removes the doubt, so we fix it and record the exposure analysis as the reason for its priority.
Why does checking the object type after readObject not protect us?
Because the malicious work happens during readObject, while the object graph is being rebuilt. By the time readObject returns and our code casts the result, the command has already run. Protection has to act before classes are instantiated, which is exactly what a JEP 290 filter does.
8Conclusion
In this answer, we saw that Commons Collections 3.2.1 is dangerous because its InvokerTransformer and related classes can be chained inside a serialised object to run code during deserialisation, a 2015 discovery that affected many servers. The real risk depends on whether the service deserialises untrusted data, but because a scanner cannot know, we remove it anyway: find the parent with dependency:tree, move to 3.2.2 or commons-collections4 4.1 or later, and add a JEP 290 allow-list filter so the next gadget chain is also blocked.
Open with
The issue is the 2015 Java deserialisation gadget chain. Java's ObjectInputStream creates whatever classes the byte stream names and runs their readObject logic, and Commons Collections 3.x has Serializable classes like InvokerTransformer that call arbitrary methods by reflection.
Then, if they want more
Chained with LazyMap and a trigger class, an attacker can make readObject run Runtime.exec, just because the library is on the classpath. So the real risk depends on whether the service deserialises anything an attacker can influence: Java-serialised HTTP payloads, RMI or JMX ports, old ObjectMessage queues, serialised session stores.
I would check that to set the priority, but I would fix it either way, because the scanner cannot see entry points and proving there are none is harder than upgrading. First I run mvn dependency:tree with an includes filter to find which library pulls it in, since it is usually transitive.
The quick fix is pinning 3.2.2 in dependencyManagement, which is a drop-in replacement and disables the unsafe classes by default. Longer term I would move code to commons-collections4, version 4.1 or later, which uses a different package. And I would add a JEP 290 allow-list filter with jdk.serialFilter, ending in !*, so other gadget chains are blocked too. Where possible I would replace Java serialisation with JSON altogether.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A security scan flags commons-collections 3.2.1 in an old service, referencing...": https://scrivio-interview-library.netlify.app/java/utility-libraries/13f5a068b7/