# 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) ([[raw/chern0b.it/two-roads-to-rce-in-wildfly-pre-auth-corba-deserialization-and-json-pa-15864|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_eval`s 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 ([[raw/blog.ostorlab.co/puppet-server-deserialization-rce-cve-2026-115731-ostorlab-15875|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|.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).
## Related
- [[Insecure Deserialization]]
- [[Java Deserialization]]
- [[NET Deserialization|.NET Deserialization]]
- [[Gadget Chains]]
- [[Remote Code Execution]]
## Sources
- [[raw/blackhat.com/friday-the-13th-json-attacks-black-hat-4511|Friday the 13th JSON Attacks (Black Hat)]]
- [[raw/blog.ostorlab.co/puppet-server-deserialization-rce-cve-2026-115731-ostorlab-15875|Puppet Server deserialization RCE (CVE-2026-115731) - Ostorlab]]
- [[raw/chern0b.it/two-roads-to-rce-in-wildfly-pre-auth-corba-deserialization-and-json-pa-15864|Two roads to RCE in WildFly: pre-auth CORBA deserialization and JSON parser bypass]]
- [[raw/github.com/jackson-deserialization-vulnerability-exploit-3-gadgets-github-4507|Jackson deserialization exploit (3 gadgets)]]
- [[raw/greynoise.io/snakeyaml-deserialization-deep-dive-cve-2022-1471-3785|SnakeYAML Deserialization Deep Dive (CVE-2022-1471)]]
- [[raw/medium.com/deep-dive-into-fastjson-deserialization-vulnerabilities-4946|Deep Dive into Fastjson Deserialization Vulnerabilities]]
- [[raw/research.jfrog.com/httpx-rce-via-oj-object-mode-json-deserialization-jfrog-15525|httpx RCE via Oj object-mode JSON deserialization]]
- [[raw/securitylab.github.com/ghsl-2026-200-untrusted-seroval-payloads-12572|GHSL-2026-200: Untrusted Seroval payloads]]
- [[raw/snyk.io/security-advisory-critical-rce-vulnerabilities-in-react-server-compone-8855|Critical RCE in React Server Components (CVE-2025-55182)]]