Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java serialization, which creates objects without calling their constructors, has caused many serious security vulnerabilities. What is the long-term lesson about creating objects outside constructors (clone, deserialization, reflection), and what standards follow for a large organisation?

The long-term lesson is that every path that creates an object without running its constructor also skips the validation that keeps the object's invariants, and Java serialization let crafted input choose which classes to create and in what state, which attackers chained into remote code execution. The standards that follow are: never deserialize untrusted data with Java serialization, use explicit formats such as JSON or Protobuf with validation into records, prefer copy constructors over clone(), use the JDK's serialization filters where Java serialization cannot be removed, and scan dependencies for known deserialization issues.

Why this question is asked

Java deserialization flaws were among the most damaging vulnerabilities in enterprise Java for a decade, affecting application servers, build tools and message brokers. At a senior level, interviewers are not asking for the history; they want to see whether we can turn a class of incidents into a standard that hundreds of engineers will follow. In this answer, we will recap how objects normally get their invariants, see how serialization and the other constructor-bypassing paths break that, look at gadget chains and what the JDK changed, and then write the standards for a large organisation and how to enforce them.

1A Quick Recap: Constructors Protect Invariants

An invariant is a rule that must always be true for an object, such as an end date never being before its start date, or a balance never being negative. The constructor is where a class checks these rules, and because every normal path to a new object goes through a constructor, the class can trust its own fields afterwards.

Java has three common ways to create objects that skip the constructor of the class itself. Object.clone() copies fields directly. Java serialization rebuilds an object from a byte stream by allocating it and setting its fields, running only the no-argument constructor of the first non-serializable superclass. And reflection can set private and even final fields on an existing object, or allocate objects through low-level APIs. Each path lets an object exist in a state the constructor would have rejected.

2How Deserialization Became Code Execution

With Java serialization, the byte stream names the classes to create. Any serializable class on the classpath can be chosen by whoever writes the stream. Many classes run code during deserialization, in readObject() methods or indirectly when a deserialized HashMap calls hashCode() on its keys.

Attackers found gadget chains: sequences of ordinary library classes whose deserialization logic, when combined with carefully chosen field values, ends in a call that runs a command. A widely known chain went through Apache Commons Collections around 2015, and public tools appeared that generated payloads for many popular libraries. Any endpoint that accepted Java-serialized data from the network, such as some RMI, JMX or message queue setups, could then be taken over by sending bytes. The application did not need to use those library classes at all; they only had to be on the classpath.

Step throughA gadget chain during deserialization
AttackerService endpointObjectInputStreamLibrary classes
send crafted serialized bytes
readObject()
create classes named in the stream, no constructors run
readObject() and hashCode() hooks call each other
the last gadget invokes a command
returns an object, but the damage is done

6 messages in order. Play them, or step through them one at a time.

The service never asked for these classes; the stream chose them.

3What the JDK Changed

The JDK responded in steps. Serialization filters (JEP 290, in Java 9 and backported to Java 8 updates) let an application allow or reject classes, and limit the depth and size of the object graph, before objects are created. Java 17 added context-specific filters through a filter factory (JEP 415), so different streams can get different rules. Records, a standard feature since Java 16, are deserialized through their canonical constructor, so the compact constructor's validation runs for deserialized records too.

Java
// A process-wide allow list: only our own DTOs and a few JDK types, everything else rejected
ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(
"com.example.dto.*;java.lang.*;java.util.*;maxdepth=10;maxbytes=65536;!*");
ObjectInputFilter.Config.setSerialFilter(filter);
// the same pattern can be set without code: -Djdk.serialFilter=...
record Period(LocalDate start, LocalDate end) implements Serializable {
Period { if (end.isBefore(start)) throw new IllegalArgumentException("end before start"); }
}
// deserializing a Period whose bytes say end < start throws InvalidObjectException

Notice that the filter is an allow list ending in !*, which rejects every class not named. A block list of known gadgets is weaker, because new gadgets keep being found. These measures reduce the risk, but they are defences for code that cannot stop using Java serialization; they are not a reason to start using it.

4The Standards for a Large Organisation

Here is the reasoning a senior person uses: we cannot review every class on every classpath for gadgets, so we remove the mechanism that lets untrusted input choose classes. The first standard is therefore absolute: Java serialization is never used for data that crosses a trust boundary, which includes network endpoints, message queues, caches shared between services and files uploaded by users.

The second standard is the replacement. Data from outside is read with an explicit format, JSON or Protobuf, into a fixed set of types, preferably records, and is validated, for example with Bean Validation annotations or checks in the compact constructor. With JSON we also turn off polymorphic type handling that takes class names from the input, because that reintroduces the same problem in a different library.

The third standard covers the other constructor-bypassing paths. New classes do not implement Cloneable; they offer copy constructors or static copy factories, which run the constructor and its checks. Reflection that sets private fields is limited to well-understood frameworks such as persistence and serialization libraries, not application code.

Standards that follow from the lesson
Path that skips constructorsStandard
Java deserialization of untrusted dataForbidden; use JSON or Protobuf into validated records
Java serialization that cannot be removed yetAllow-list filter with !*, depth and size limits
JSON with class names in the inputPolymorphic typing from input disabled
Object.clone()Copy constructors or static copy factories instead
Reflection setting private fieldsOnly inside vetted frameworks
Vulnerable library versionsDependency scanning in every build

5Enforcing It Across Teams

Standards hold when tools check them. We would add a static analysis rule that flags ObjectInputStream.readObject() outside an approved list, and a dependency scanner in every build that fails on libraries with known deserialization advisories. Where a legacy system still needs Java serialization, it gets a process-wide filter through jdk.serialFilter, owned by a named team with a removal plan.

We would also teach the reason, not only the rule. Engineers who understand that bypassing a constructor bypasses validation will apply the same thinking to new mechanisms, such as frameworks that bind request data directly onto entity fields.

6Follow-up questions

Is Java serialization safe if we only use it between our own services?

Only if every producer is fully trusted and the channel cannot be written by anyone else, which is hard to guarantee over years. A compromised service or a misconfigured queue turns it into an attack path. Explicit formats remove the class-choosing problem entirely, so the standard forbids it rather than trusting the network.

Do records make Java serialization safe?

They help, because a record is rebuilt through its canonical constructor, so its validation runs. But a stream can still name other serializable classes on the classpath, so records alone do not stop gadget chains. Filters and, better, not deserializing untrusted Java streams are still needed.

Why is a block list of known gadget classes not enough?

Because new gadget chains keep being discovered in libraries we already use. A block list protects against yesterday's chain. An allow list that names only the classes we expect, ending with a reject-all rule, protects against chains nobody has found yet.

7Conclusion

In this answer, we saw that creating objects outside constructors skips the validation that protects invariants, and that Java serialization let crafted input pick the classes to create, which gadget chains turned into remote code execution in widely used libraries. The JDK added serialization filters and made records deserialize through their canonical constructor. For a large organisation the standards follow: no Java serialization for untrusted data, explicit formats like JSON or Protobuf with validation, copy constructors instead of clone(), filters for what cannot be removed yet, and dependency scanning.

What to say in the interviewAbout 80 seconds aloud

Open with

The lesson I take is that constructors are where a class protects its invariants, so any mechanism that creates objects without running the constructor, clone, deserialization or reflection, also skips that protection.

Then, if they want more

Java serialization was the worst case because the byte stream itself chooses which classes to instantiate, and attackers found gadget chains in common libraries, Commons Collections being the well-known one, where the deserialization hooks of ordinary classes chain into running a command. The application did not have to use those classes; they just had to be on the classpath.

The JDK responded with serialization filters, from JEP 290, context-specific filters in Java 17, and records are deserialized through their canonical constructor so their validation runs.

For a large organisation my standards would be: never use Java serialization for anything that crosses a trust boundary; read external data with JSON or Protobuf into records and validate it, with polymorphic typing from input turned off; offer copy constructors or copy factories instead of clone; and where legacy code still needs serialization, set an allow-list filter ending in reject-all, with an owner and a removal date.

On top of that, every build runs a dependency scanner for known deserialization issues and a static check that flags readObject outside approved places.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, Java 17, Java 8, 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 "Java serialization, which creates objects without calling their constructors, has caused...": https://scrivio-interview-library.netlify.app/java/creational-patterns/71ff8ce376/