You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(flow): isolate nested dictionary initial state - #9502
Flow._create_initial_state copied a dict initial state with dict(init_state).
Nested containers stayed shared between Flow instances and the caller.
Deep-copy the dict, and fall back to a shallow copy when deep copy fails.
Verification
Tests added or updated for the changed behavior
Relevant tests and quality checks pass locally
uv run pytest lib/crewai/tests/test_flow.py::test_flow_initial_dict_state_isolates_nested_values -q --allowed-hosts=127.0.0.1
PR summary: Flow._create_initial_state now deep-copies a dict initial state instead of taking a shallow dict() copy, falling back to the shallow copy when a nested value cannot be deep-copied, so nested containers are no longer shared between Flow instances and the caller. A regression test covers the isolation.
Review summary
This PR changes how a dict initial_state is copied in Flow._create_initial_state: a copy.deepcopy replaces the previous shallow dict(init_state), with a dict() fallback when a nested value cannot be deep-copied, plus a regression test for nested-container isolation. The change is a correctness/ownership fix. Tracing every route that populates initial_state shows it is developer-supplied — either set on the Flow subclass or passed by the embedding application — while checkpoint and persistence payloads are JSON-serialized and rehydrated from JSON, so no attacker-controlled object reaches the new deepcopy. The narrow exception handler and its shallow-copy fallback were assessed as a reliability property rather than a security weakness, and the sibling declarative-state path (_create_definition_state) that still shallow-copies its default was examined too: its default is author-authored and no in-repo path serves multiple principals from one shared definition. No security vulnerability was confirmed in the changed code.
Suspected issues examined and rejected:
Flow construction now failing on deeply nested or non-deep-copyable initial state: input to this path is developer-controlled, and the same narrow handler already exists in _copy_state on the kickoff path, so the change crosses no attacker boundary.
The shallow-copy fallback leaving nested containers shared: it only fires for non-deep-copyable values the author placed in the state, pre-PR every nested container was shared, and both Flow instances live in the same trust domain.
Declarative state defaults still being shared across instances built from one definition: the default is authored by the flow developer, no in-repo path instantiates flows for distinct principals off a shared definition, and per-caller input enters only through the newly isolated initial_state path.
Ruled out (3)
Can deeply nested or non-deep-copyable initial state now make Flow construction fail where it never failed before? Everything that reaches this path is developer-supplied and the identical narrow exception handler already exists in _copy_state on the kickoff path, so no attacker controls the input that could raise RecursionError or copy.Error.
Does the shallow-copy fallback still share nested state between flows or with the caller? The fallback only triggers for non-deep-copyable values the author deliberately placed in the state, pre-PR every nested container was shared unconditionally, and both Flow instances sit in the same process and trust domain.
Do declarative state defaults in _create_definition_state still leak nested data across Flow instances built from one definition? The nested default is authored by the flow developer rather than a caller, no in-repo path runs flows for distinct principals off a shared definition, and per-caller input enters only through the initial_state path this PR now isolates.
Review 1 on this PR · 6c8ac23 · requested Nov 17 05:30 UTC when the PR was opened
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue
Fixes #9383
Summary
Flow._create_initial_state copied a dict initial state with dict(init_state).
Nested containers stayed shared between Flow instances and the caller.
Deep-copy the dict, and fall back to a shallow copy when deep copy fails.
Verification
uv run pytest lib/crewai/tests/test_flow.py::test_flow_initial_dict_state_isolates_nested_values -q --allowed-hosts=127.0.0.1
Additional context
None.