Unsafe Jackson Default Typing Deserialization in langchain4j-agentic AgenticScopeSerializer
Description
In affected versions, AgenticScopeSerializer.fromJson(String) restores a DefaultAgenticScope using the default JacksonAgenticScopeJsonCodec, which enables Jackson default typing (activateDefaultTyping) with the mapper's default (laissez-faire) PolymorphicTypeValidator. Because DefaultAgenticScope.state is a Map<String,Object>, attacker-controlled JSON can embed polymorphic type metadata (["<fqcn>", {...}]) for state values and drive instantiation of arbitrary classes. With a suitable gadget on the target classpath this can escalate to remote code execution.
Details
- Sink:
JacksonAgenticScopeJsonCodecbuilds a globalObjectMapperand callsmapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator()). On a plain mappergetPolymorphicTypeValidator()returns a laissez-faire validator that allows all subtypes, so type metadata in the JSON selects the runtime class. - Carrier:
DefaultAgenticScope.stateisMap<String,Object>; theObject-typed values fall under default typing (OBJECT_AND_NON_CONCRETE), making the map a polymorphic deserialization sink. - Note on scope: the top-level object is bound via a
@JsonCreator, so a["...DefaultAgenticScope", {...}]wrapper is rejected at the root and never reaches the sink. A working exploit must use the real top-level envelope and place the gadget inside astatevalue (see PoC).
Vulnerable sink (affected versions): ```java // JacksonAgenticScopeJsonCodec static ObjectMapper agenticScopeJsonSerializer() { ObjectMapper mapper = agenticScopeJsonMapperBuilder().build(); mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator()); // allow-all validator return mapper; }
@Override public DefaultAgenticScope fromJson(String json) { return MAPPER.readValue(json, DefaultAgenticScope.class); // instantiates Object-typed state values } ```
Preconditions (both required for exploitation)
- The consuming application feeds untrusted or tamperable AgenticScope JSON into
AgenticScopeSerializer.fromJson— e.g. persisted session/workflow-recovery state in a low-privilege-writable store, or cross-service scope messages. - A usable deserialization gadget class is present on the target classpath.
The library itself never passes untrusted data to fromJson; only application code can create the untrusted-input path.
Proof of concept
The payload must use the real top-level envelope and carry the gadget in a state value: ``json { "memoryId": "attacker", "kind": "PERSISTENT", "state": ["java.util.concurrent.ConcurrentHashMap", { "exploit": ["<gadget-fqcn-on-classpath>", { /* gadget-specific properties */ }] }], "agentInvocations": ["java.util.Collections$SynchronizedRandomAccessList", []], "context": ["java.util.Collections$SynchronizedRandomAccessList", []] } ` Passing this to AgenticScopeSerializer.fromJson(...) instantiates the class named under state`. In a safe test, a class with an observable constructor side effect confirms instantiation; on a classpath with a real Jackson gadget, gadget-specific effects (including RCE) may occur.
Impact
Instantiation of attacker-chosen classes from JSON. With a classpath gadget this can reach remote code execution; without one, it can still cause denial of service, SSRF, file access, or object-state pollution, depending on how the application persists and trusts AgenticScope.state.
Remediation
Fixed in 1.18.1-beta28, and backported to 1.11.10-beta19 (1.11.x LTS) and 1.5.3-beta11 (1.5.x LTS). Upgrade to one of these or later.
The 1.12.x line (1.12.1-beta21, 1.12.2-beta22) received no backport; users on that line should move to 1.18.1-beta28 or later.
The default codec no longer uses laissez-faire polymorphic typing. Instead:
- Default typing is validated by a strict allow-list (
ConfigurablePolymorphicTypeValidator) that permits only primitive wrappers,String,Boolean,Character, enums, controlled JDK collection/math types, and the specific langchain4j message/data and internal types actually serialized. Everything else is denied. - Array type ids (
"[L...;") are validated by their element type, so a disallowed class cannot be smuggled in wrapped as an array. - Denied types raise a clear
UnserializableAgenticScopeExceptioninstead of instantiating.
Breaking Changes
Applications that store their own domain objects in AgenticScope state must now allow-list those types before deserialization, otherwise fromJson throws UnserializableAgenticScopeException:
``java AgenticScopeSerializer.allowDeserializationType(com.acme.Order.class); // or, for a whole package: AgenticScopeSerializer.allowDeserializationPackagePrefix("com.acme."); ``
Registration is process-wide and permanent, and must occur before the fromJson call that encounters the type.
As always, treat fromJson input as security-sensitive: only deserialize trusted data unless a hardened codec and allow-list are in place.