Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Document SchemaToModelFactory receiver-side bypass and migration plan

Louis Choquel7c64bd5338c0
diff --git a/pipelex/cogt/content_generation/schema_to_model_factory.py b/pipelex/cogt/content_generation/schema_to_model_factory.py
index a229ef4ea..9d4f4b6e9 100644
--- a/pipelex/cogt/content_generation/schema_to_model_factory.py
+++ b/pipelex/cogt/content_generation/schema_to_model_factory.py
@@ -272,6 +272,16 @@ class SchemaToModelFactory:
Cached by sha256(source_code) so repeated payloads carrying the same dynamic
class don't re-exec. Returns a shallow copy so callers can mutate freely.
+
+ Security note: this path takes pre-generated Python source (typically delivered
+ via `__kajson_class_source__` on a Temporal payload) and exec()'s it directly.
+ Layer 1 (`_reject_unsafe_schema_extensions`) does NOT apply here — only Layer 2
+ (`_make_restricted_builtins`) does, so the attack surface is materially wider
+ than the sender path in `make_from_json_schema`. An attacker who can inject a
+ crafted `__kajson_class_source__` string into a cross-process payload bypasses
+ the JSON-schema extension check entirely. The TODO in `_exec_source_to_types`
+ to migrate to `pydantic.create_model()` would close this gap by removing the
+ exec primitive on both paths.
"""
cache_key = hashlib.sha256(source_code.encode()).hexdigest()
with cls._source_cache_lock:
diff --git a/wip/deferred-items.md b/wip/deferred-items.md
index 1493e019d..9965152dc 100644
--- a/wip/deferred-items.md
+++ b/wip/deferred-items.md
@@ -68,3 +68,27 @@ Items related to the event log and graph tracing system (Phase 4.5). Also docume
|---|---|---|
| Causal event ordering in assembler | **Known limitation** — works for current topologies | `read_events()` sorts by `(workflow_id, sequence)` which groups by lexicographic workflow ID, not execution order. In parent/child workflow topologies, this can cause `_stuff_producer_map` overwrites in the wrong order during `GraphSpecAssembler.pass_one()`, producing incorrect DATA edge sources. Consider sorting by timestamp or processing events in a topology-aware order. Flagged by codex on PR #796. |
| PipeCondition SELECTED_OUTCOME wiring | **Deferred** — no production use yet | PipeCondition has no tracer calls in production code currently. If SELECTED_OUTCOME edges are needed, that's a separate change. |
+
+---
+
+## Schema Reconstruction Hardening
+
+Items related to `SchemaToModelFactory` (`pipelex/cogt/content_generation/schema_to_model_factory.py`), which reconstructs Pydantic BaseModel classes from JSON schemas across process boundaries (Temporal payloads). The current implementation `exec()`'s code generated by `datamodel-code-generator` and ships that source verbatim as `__kajson_class_source__` so receivers can re-exec it. See the module docstring for the full threat model.
+
+### `exec()` → `pydantic.create_model()` Migration
+
+**Status**: Deferred — non-trivial refactor, no immediate threat-model trigger.
+
+Replace `exec()` in `_exec_source_to_types` with programmatic class construction via `pydantic.create_model()`. Eliminates the exec primitive on both the sender path (`make_from_json_schema`) and the receiver path (`make_types_from_source`), and lets us delete `_make_restricted_builtins`, `_restricted_import`, and `_reject_unsafe_schema_extensions`.
+
+Non-trivial because nested models, enums, and forward references currently rely on `datamodel-code-generator`'s output structure. A faithful port has to walk the JSON schema directly, build the class graph in dependency order, and apply `model_rebuild()` after the fact to resolve forward refs.
+
+This is the right structural fix; everything below is a stopgap until it lands.
+
+### Receiver-Side Schema Validation Gap
+
+**Status**: Known gap — documented in `make_types_from_source`'s docstring; only worth closing if the migration above is materially delayed.
+
+`make_from_json_schema` (sender) calls `_reject_unsafe_schema_extensions` before generating source — Layer 1. `make_types_from_source` (receiver) takes pre-generated source and execs it directly — only Layer 2 (`_make_restricted_builtins`) applies. An attacker who can inject a crafted `__kajson_class_source__` string into a Temporal payload bypasses Layer 1 entirely.
+
+Interim mitigation: AST-scan the source before exec, rejecting any top-level node that isn't an allowlisted import, a class def, or a docstring (and any class body that isn't fields / `model_config` / docstring). Mirrors Layer 1's "reject loudly" failure mode at the structural level. Trade-off: tied to `datamodel-code-generator`'s emitted output shape, which is not a stable API. Skip if the `create_model()` migration is near-term; the migration closes the gap permanently.