Polymorphic Deserialization

The class of deserialization bugs in “data” formats — JSON, YAML, XML, BSON — that arises when the serializer lets the document name the concrete type to instantiate, re-introducing the object-injection problem people switched to JSON to avoid.

Why “safe” formats aren’t

After the 2015–16 Java wave, the common advice was to replace native serialization with JSON/XML. Black Hat “Friday the 13th JSON Attacks” (Muñoz & Mirosh) showed this is false when the library supports polymorphism: the requirements for RCE are (1) the library invokes methods on user-named types (constructors, setters, callbacks) and (2) a gadget space exists. The serialization format — binary, XML, JSON — is irrelevant. Data-only means the parser can construct nothing the schema did not ask for; the moment a $type/@type/!!python/object tag can pick a class, it is an object format again.

Mechanisms by stack

  • Java JSON/XML: Jackson polymorphic typing (enableDefaultTyping, @JsonTypeInfo) — CVE-2017-7525 and a long tail; Fastjson AutoType reading @type → class load → setter invocation, with JNDI sinks (JdbcRowSetImpl.setDataSourceName), patched/bypassed repeatedly below 1.2.83 / 2.0.45. XStream rebuilds arbitrary, even non-Serializable, types.
  • Jackson PTV generics bypass (CVE-2026-54512): with BasicPolymorphicTypeValidator correctly allowlisting java.util.ArrayList, a type id of java.util.ArrayList<com.attacker.Gadget> takes the < branch that validates only the outer class, then the type factory loads and instantiates the inner one. Jackson’s separate hardcoded block-list (~100 classes) still stops the famous universal gadgets, so RCE needs an app-specific, non-blocklisted class on the classpath. Affects 2.18.2 (WildFly 35); fixed with recursive type-parameter validation (WildFly later ships 2.21.6) (WildFly two roads).
  • Puppet rich data (__ptype): FromDataConverter parses an attacker-supplied __ptype in agent report events as a Puppet type; an Object[...] type with no registered implementation is built by RubyGenerator, which class_evals a class body containing the RubyMethod annotation. CVE-2026-115731, CISA KEV; fixed in Puppet Core 8.22.2/9.1.3 and PE 2023.8.13/2025.12.1/2026.0.1 (Ostorlab Puppet).
  • Java YAML: SnakeYAML instantiates arbitrary classes by default (CVE-2022-1471), “insecure by default” with a history of prior issues before the CVE.
  • .NET JSON: Json.NET TypeNameHandling != None, JavaScriptSerializer type resolvers, FastJSON — the type discriminator selects the class — see .NET Deserialization.
  • Ruby: Oj default :object mode instantiates arbitrary classes from JSON (httpx gem, CVE-2026-129784 / GHSA-jgqw-w3x2-9h6q), reachable when ActiveSupport supplies a chain; unsafe JSON/YAML (YAML.load) across the ecosystem.
  • Node / JS: node-serialize encodes functions with the _$$ND_FUNC$$_ marker inside otherwise ordinary JSON (so it does not look binary); seroval type-confusion in TanStack/Solid Start (CVE-2026-59940, CVE-2026-116580) where an unvalidated typed-array “buffer” child selects a TypedArray constructor overload and can invoke a registered callable; React Server Components “Flight” protocol (CVE-2025-55182).
  • BSON: MongoDB C# Driver and generic BSON type-restriction allow-list bypasses — an overly permissive type-check heuristic constructs unintended types.

Detection and prevention

  • Look for $type/@type/__type/__ptype/!!python/object/--- !ruby/object/_$$ND_FUNC$$_ markers in request bodies; these are the tell that polymorphism is in play.
  • Disable type binding: Jackson default typing off, Fastjson safeMode, Json.NET TypeNameHandling.None, SnakeYAML SafeConstructor, PyYAML safe_load, Oj :strict. Where types are genuinely needed, bind to a concrete expected type or a strict allowlist. Validate the type of every child node (the seroval bugs were missing a runtime type check behind a TypeScript cast), including generic type parameters: an allowlist that inspects only the container is not an allowlist (CVE-2026-54512).

Sources