Summary
APScheduler's JSONSerializer and CBORSerializer perform insecure deserialization that can instantiate arbitrary Python classes and inject attacker-controlled state.
Any application that accepts untrusted JSON or CBOR data and passes it into these serializers may allow a remote attacker to achieve code execution within the application process.
Affected context
This vulnerability arises in Python applications that embed APScheduler and configure it to use the JSONSerializer or CBORSerializer with data that can be influenced by external users.
It is most relevant when:
- Job definitions, job metadata, or scheduler messages are serialized/deserialized across trust boundaries (e.g., APIs, message queues, webhooks).
- Untrusted or partially trusted JSON/CBOR payloads are passed directly into APScheduler's serializer.
- The runtime environment exposes sensitive classes whose initialization or __setstate__ logic can perform side effects.
Exploit preconditions
- Attack surface: Any endpoint, message queue consumer, RPC interface, or background worker that:
- Accepts JSON or CBOR input, and
- Deserializes it via APScheduler's JSONSerializer or CBORSerializer.
- Required access: Ability to supply or control serialized job data or scheduler payloads (remote network access is sufficient if such interfaces are exposed).
- Trigger point: Invocation of
unmarshal_objectduring deserialization of attacker-supplied JSON/CBOR. - Why exploitation is possible: The deserializer:
- Dynamically imports modules and looks up arbitrary classes referenced in the payload.
- Instantiates those classes and calls
__setstate__with attacker-controlled data. - Does not enforce an allow-list of types or perform robust validation on the deserialized structure, matching the pattern described by CWE-502 (Unsafe Deserialization).
Why this happens
The underlying flaw is the use of a general-purpose object deserialization mechanism that implicitly trusts type information embedded in serialized data.
APScheduler's unmarshal_object reconstructs Python objects by:
- Interpreting type metadata in the incoming JSON/CBOR payload.
- Dynamically importing modules and resolving arbitrary classes available in the environment.
- Calling special methods such as
__setstate__with attacker-controlled state.
This design couples data with executable behavior and treats serialized input as instructions for what code to run and which objects to create.
In CWE-502 terms, untrusted data is deserialized into live objects without applying strict type constraints, schema validation, or a safe object mapping, enabling remote code execution or other unintended side effects.
Potential impact
- Remote execution of arbitrary code within the APScheduler-hosting process.
- Execution of attacker-controlled logic during object construction or state restoration.
- Compromise of application data, including reading or modifying database contents handled by scheduled jobs.
- Lateral movement within the environment by abusing credentials, tokens, or network access available to the compromised process.
- Denial of service by instantiating large or expensive objects, exhausting memory, CPU, or other resources.
- Integrity loss of scheduled workflows, including the injection, modification, or deletion of jobs.
General mitigation guidance
Effective mitigation focuses on breaking the trust between untrusted serialized data and arbitrary object construction:
- Prefer data-only serialization:
- Use safe serializers that map incoming JSON/CBOR to primitive types and well-defined data structures, not arbitrary Python objects.
- Represent jobs and scheduler configuration as explicit schemas or simple dictionaries, then manually construct objects from validated data.
- Apply strict type allow-listing:
- If object reconstruction is required, maintain an explicit list of allowed types and reject any payload referencing types outside this list.
- Avoid dynamic module imports based on attacker-controllable input.
- Enforce input validation and schema checks:
- Validate JSON/CBOR against a strict schema before any deserialization logic that may create objects.
- Reject unexpected fields, nested structures, or type metadata embedded in the payload.
- Reduce privilege and side effects:
- Run APScheduler and associated workers with the least privileges needed (filesystem, network, and credentials).
- Separate high-privilege operations out of deserialization paths; avoid performing I/O or security-sensitive work in constructors or
__setstate__. - Restrict exposure of serialization endpoints:
- Avoid exposing internal scheduler serialization interfaces directly to the public internet.
- Ensure that only trusted components (internal services, authenticated backends) can feed data into these serializers.
How teams detect issues like this
Identifying and monitoring this class of issue typically involves multiple layers of analysis:
- Static analysis and code scanning:
- Search for usage of APScheduler's
JSONSerializer,CBORSerializer, orunmarshal_objectin application code. - Flag patterns where these serializers are passed data originating from HTTP request bodies, message queues, or other untrusted channels.
- Enforce rules that prevent dynamic type loading or module import based on deserialized content.
- Code review cues:
- During review, look for:
- Deserialization of complex objects from JSON/CBOR instead of simple, schema-validated structures.
- Logic that directly trusts type information or module paths from input data.
- Constructors and
__setstate__implementations that perform I/O, system calls, or security-sensitive operations. - Runtime monitoring and logging:
- Instrument deserialization paths to log unexpected class names, module imports, or unusual job definitions.
- Monitor for anomalies such as:
- Jobs that execute unfamiliar functions or modules.
- Sudden spikes in scheduler activity or resource usage during deserialization.
- Policy and CI/CD enforcement:
- Incorporate security checks in CI pipelines that block new uses of unsafe object deserialization patterns.
- Optionally use automated review tools configured to detect CWE-502 patterns and enforce organization-wide serialization guidelines.

