Deserializing Untrusted Data
Using ObjectInputStream.readObject() on data from HTTP requests, files, or network sockets.
Unsafe deserialization vulnerabilities occur when ObjectInputStream.readObject() processes untrusted data without filtering or validation. Attackers can craft malicious serialized objects containing gadget chains from libraries in the classpath that execute arbitrary code during deserialization, leading to complete system compromise.
import java.io.*;
import javax.servlet.http.*;
@RestController
public class DataController {
@PostMapping("/api/data/import")
public String importData(@RequestBody byte[] data) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis);
// DANGEROUS: deserializes untrusted data
Object obj = ois.readObject();
return "Data imported";
} catch (Exception e) {
return "Import failed";
}
}
}
// Attacker sends malicious serialized payload with gadget chain
// Payload executes arbitrary code during readObject()import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.http.*;
@RestController
public class SecureDataController {
private final ObjectMapper jsonMapper = new ObjectMapper();
@PostMapping("/api/data/import")
public String importData(@RequestBody String jsonData) {
try {
// SAFE: JSON deserialization to known DTO
UserDataDTO userData = jsonMapper.readValue(
jsonData, UserDataDTO.class);
// Validate before processing
if (userData.isValid()) {
processData(userData);
return "Data imported";
}
return "Invalid data";
} catch (Exception e) {
return "Import failed";
}
}
}The vulnerable code uses ObjectInputStream.readObject() on untrusted data, allowing attackers to execute arbitrary code via gadget chains. The secure version uses JSON deserialization which only constructs simple data objects without executing code, and validates the data before processing.
Using ObjectInputStream.readObject() on data from HTTP requests, files, or network sockets.
Sourcery automatically identifies remote code execution via unsafe java object deserialization and many other security issues in your codebase.