Security

What Is Unsafe Deserialization?

A CWE-502 vulnerability where untrusted serialized data becomes live objects that can execute attacker-controlled code or trigger unsafe behavior.

What Is Unsafe Deserialization?

Unsafe deserialization is a security vulnerability where untrusted serialized data is converted into live objects and can trigger attacker-controlled code, gadget chains, or unsafe object state during parsing. In LLM and agent systems, it is a security failure mode at tool, connector, cache, artifact, and eval-pipeline boundaries. FutureAGI anchors this risk to eval:UnsafeDeserializationDetector, the detector for CWE-502 unsafe deserialization, so teams can catch risky object loaders before model-controlled files, messages, or state reach privileged runtimes.

Why it matters in production LLM/agent systems

Unsafe deserialization turns a data-loading step into an execution boundary. A document-analysis agent might accept a user-uploaded pickle file. A workflow runner might restore agent memory from a cache entry. A connector might decode YAML from a third-party ticketing system. A model-evaluation job might load model artifacts or tool state produced by an untrusted sandbox. If any of those loaders instantiate objects, invoke constructors, or resolve classes from attacker-controlled bytes, the agent worker can execute code before business logic sees the payload.

Ignoring it creates two practical failures. Remote code execution happens when a serialized payload triggers a gadget chain in Python, Java, .NET, Ruby, PHP, or a framework-specific loader. State poisoning happens when an object is loaded successfully but contains permissions, tool arguments, file paths, or callbacks the application never meant to trust. Developers see strange class names, parser exceptions, worker restarts, or serialized blobs attached to tool calls. SREs see unexpected outbound requests, file writes, subprocess launches, or crash loops near artifact-loading spans. Security teams need the exact source: prompt, upload, retrieved file, cache key, connector, or model artifact.

This is sharper in 2026-era agentic pipelines because agents persist state between steps, exchange tool messages, replay traces, and load artifacts across workers. A single unsafe parser can turn model-influenced data into privileged application behavior.

How FutureAGI handles unsafe deserialization

FutureAGI anchors this issue to eval:UnsafeDeserializationDetector. In the inventory, UnsafeDeserializationDetector is the security detector for unsafe deserialization, mapped to CWE-502. Engineers use it against agent tools, connector handlers, cache restore paths, artifact importers, and evaluation scripts that deserialize untrusted or model-influenced data. For release reporting, findings can roll into SerializationSecurityScore by route, service, tenant, and prompt version.

A real workflow: a LangChain support agent exposes an import_customer_profile tool. The planner can select a file returned by a connector, and the tool implementation calls pickle.loads(profile_blob) so downstream steps can access structured profile fields. With traceAI-langchain, the trace records the user request, retrieved file metadata, agent.trajectory.step, tool name, and route such as profile-import. The eval pipeline runs UnsafeDeserializationDetector on the tool implementation and flags the loader before the release ships.

FutureAGI’s approach is to connect the static finding to runtime evidence. Compared with a generic Semgrep rule that reports a dangerous API call in isolation, FutureAGI keeps the detector result beside the trace, route, source artifact, and regression dataset. Compared with an OWASP LLM Top 10 checklist, it gives the engineer a concrete next action: replace pickle or unsafe YAML with JSON plus schema validation, allowlist expected classes only when unavoidable, reject serialized binary uploads at an Agent Command Center pre-guardrail, and fail the release if the detector result or SerializationSecurityScore crosses the approved threshold.

How to measure or detect it

Use code findings, trace evidence, and runtime controls together:

  • UnsafeDeserializationDetector — detects unsafe deserialization vulnerabilities associated with CWE-502 in code paths that load serialized data.
  • SerializationSecurityScore — summarizes serialization findings so teams can compare releases, routes, services, and tenants.
  • Trace evidence — inspect agent.trajectory.step, tool name, tool.input, source file metadata, content type, cache key, and guarded route.
  • Dashboard signal — track detector-fail-rate by route, guarded-tool-block-rate, serialized-artifact ingestion count, parser error rate, and worker crash rate.
  • User-feedback proxy — watch escalation tickets mentioning corrupted profiles, unexpected file access, repeated tool restarts, or agent actions after artifact import.
from fi.evals import UnsafeDeserializationDetector

tool_code = "profile = pickle.loads(profile_blob)"
result = UnsafeDeserializationDetector().evaluate(input=tool_code)
print(result.score, result.reason)

A confirmed CWE-502 path should block release until the loader is removed, replaced with a safe parser, or constrained by strict type and source controls. Regression tests should include the exact source span and payload family, not only the final exception.

Common mistakes

Most unsafe deserialization bugs survive because teams treat serialization as storage plumbing instead of an execution boundary.

  • Treating formats as data-only. Pickle, Java serialization, and unsafe YAML can invoke constructors, resolvers, or gadget chains.
  • Scanning only prompts. Payloads can arrive from uploads, MCP connector state, cache entries, model artifacts, or tool output.
  • Allowing model-selected files. An agent can choose a cached object or artifact that a privileged worker later loads.
  • Checking types after load. By the time validation runs, a gadget chain may already have executed.
  • Dropping trace context. Review needs prompt, route, source artifact, detector result, loader, class name, and release.

Frequently Asked Questions

What is unsafe deserialization?

Unsafe deserialization is a security vulnerability where untrusted serialized data is converted into live objects, allowing attacker-controlled classes, methods, or payloads to run during parsing.

How is unsafe deserialization different from code injection?

Code injection puts attacker-controlled code into an executable surface. Unsafe deserialization abuses a parser or object loader, so execution can happen before the application treats the data as normal input.

How do you measure unsafe deserialization?

Use FutureAGI's UnsafeDeserializationDetector for CWE-502 findings, then track SerializationSecurityScore, detector-fail-rate by route, and guarded deserialization attempts in traces.