← Java / Spring · Contents

JAVA-027 / Advanced

Java deserialization of untrusted input (ObjectInputStream.readObject)

A08:2021 Software and Data Integrity Failures · CWE-502 Deserialization of Untrusted Data

Why is this a problem?

Gadget chains such as Commons Collections can lead to arbitrary command execution (RCE).

How to fix it

java
// Use a data-only format such as JSON instead of serialization
ObjectMapper om = new ObjectMapper();
Dto dto = om.readValue(request.getInputStream(), Dto.class);
// If unavoidable, allowlist classes using ObjectInputFilter

Further reading