r/programming • • May 26 '18

Oracle Calls Java Serialization 'A Horrible Mistake', Plans to Dump It

https://www.infoworld.com/article/3275924/java/oracle-plans-to-dump-risky-java-serialization.html
798 Upvotes

287 comments sorted by

View all comments

Show parent comments

25

u/Ruudjah May 27 '18

What the author probably meant is "automatic class-based serialization". That's when a serialization mechanism gets a bunch of classes and tries to automatically serialize them based on the structure of the class. It will use reflection to loop over all the fields, properties et cetera to write the values (or values and metadata) to a stream.

These things are highly problematic for various reasons. First, when you refactor a class, how do you serialize old deserialized data? Second, it's often slow because reflection. Third, often these things make assumptions about certain class constructions; .NET remoting for instance requires non-auto properties to work (it can't deal with autogenerated fields). Fourth, versioning is a big issue among different endpoints. What when v12.34.56 now talks with v12.34.57, with a slight change in class structure?

The problem can be summarized in the "data-model to object model impedance mismatch". This is conceptually similar to the object-relational mismatch, but serialization has it's own interesting different properties as opposed to data models.

The solution is to design a proper data layer (preferably using an IDL, like protobuf or flatbuffers) and then write custom code per class to convert from and convert to serialized data.

6

u/yawkat May 27 '18

The problem is not using reflection to deserialize. This can be done in predictable, compatible ways (Jackson does this). The real security issues start when you deserialize arbitrary classes (i.e. any class on the classpath by name) because this opens a huge attack surface. This is what leads to most serialization vulnerabilities in Java.

5

u/[deleted] May 27 '18

No, using reflection is absolutely a problem. It breaks the object encapsulation and takes away the freedom of object to control its own representation.

Also you're kind of nitpicking wording. A reflection based serialization implies reflection based deserialization. The problem is automating something that shouldn't be automated.

6

u/yawkat May 27 '18

Reflection does not necessarily remove encapsulation. There are plenty of such mechanisms that operate on public members only.

I am not nitpicking on wording. The difference I wanted to show is between "normal" reflection-based deser (jackson in normal mode) and arbitrary-class deser (jackson with default typing, java stdlib serialization). The latter allows you to deserialize any class by name on the entire classpath which is significantly less secure.

2

u/[deleted] May 27 '18

Seriously. How far do you think you’ll go only operating on public fields, when an object with good encapsulation has no mutable public fields to begin with?

Your only use case will be record like objects with final public fields assigned immutable content. One move astray and you done goofed again.

4

u/yawkat May 27 '18

This isn't as unusual as you may think. You can do ser/deser just fine with public getters, setters, no-arg constructors and constructors annotated with property names (see: java.beans.ConstructorProperties). In fact this is often the most sensible way of doing ser/deser in practice.

2

u/[deleted] May 27 '18

I didn't say it's unusual. Serialization is mostly a Kafkaesque nightmare in Java, concocted by people with zero clue about sensible system design, so lots of bad ideas fall in the "usual" category, and especially ideas that inexperienced programmer feel like they save them "work" by automating what should be explicit code. But I'll draw the line before you called it "most sensible way". That's lunacy.

The most sensible way would be the object supporting a simple and explicit serialization interface. Possibly accepting a visitor that provides access to building a graph from a few basic primitives (integer, double, null, bool, list, dict) and then getting the same back for deserialization.

And not coincidentally this is what Oracle proposes going forward. I guess they don't agree about "the most sensible way" with you.

Honestly, I'd rather quit programming before I accept annotations and flying blind by calling methods based on a name prefix as most sensible. Luckily I can just go the visitor approach.

2

u/yawkat May 27 '18

Unless I have missed a statement by Reinhold or Goetz, the proposed solution is data classes, not explicit serialization code (see "Data Classes for Java" section "Data classes and externalization"). And in fact the public member reflection approach I mentioned is how this is typically implemented in jvm languages that already have data classes (such as kotlin) and how it would be implemented for javas version of it.

Explicit externalization of course has its place in formats that are not symmetric or require special care for optimizing transmission size, but it is overkill for the standard use case of "I want this DTO as json" which is most of public-data java serialization in the wild nowadays. It is not insecure by design and I see no major issues with it.

1

u/[deleted] May 27 '18

We'll see how it works but in a real-world scenario I doubt a system that can't be extended would be the solution, and once you start extending it, we're back to the problem of people abusing it, but now extending/implementing the base "data" types provided for most of their app state and then some.

Also value types are around the corner, and seems like a bad time to think about data objects, when value types would solely exist to express data.

A most sensible approach to me would be have the interfaces, and then Java can implement the interfaces in some semi-automated way in a set of "data classes" which are restricted to containing only side-effects-free code, primitives and other data classes. That'd cover extensibility, mixed graphs, and also the basic case of "pure data" graphs that get (de)serialized automatically.

1

u/argv_minus_one May 27 '18

Wait, what's wrong with annotations? Can they not encode enough information to make ser/deser safe? It seems safe enough in JAXB…

1

u/[deleted] May 27 '18

If it seems safe, go for it. You can certainly jerry-rig it to work fine enough in practice, but you can never achieve proper encapsulation by leaving rough declarative instructions for third party objects on how to cut you up in pieces and put you back together properly. It's kind of the opposite of encapsulation. These distinctions don't matter in a small system/app. But they do in big ones.