Restrict PickleHandler deserialization to safe builtins#5946
Restrict PickleHandler deserialization to safe builtins#5946White-Mouse wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a restricted pickle unpickler with a module allowlist and updates ChangesPickle Deserialization Security
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/tests/utilities/test_file_handler.py`:
- Line 84: The test currently uses a fixed environment key
"CREWAI_PICKLE_HANDLER_EXPLOITED" (marker) and unconditionally deletes it, which
can remove pre-existing environment state; change the test to generate a unique
marker (e.g., append a UUID) or otherwise derive a temp env var name, save the
original value using original = os.environ.get(marker) before mutating, perform
the exploit assertions, and then restore the environment in a finally block: if
original is None remove the key, otherwise set it back to original; apply this
same pattern to the other block covering lines 95-101 so the test does not
delete or overwrite external env state.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 007310e1-421d-47e7-a183-e3fea7d167c5
📒 Files selected for processing (1)
lib/crewai/tests/utilities/test_file_handler.py
PickleHandler.load() currently uses unrestricted pickle.load() to deserialize *.pkl artifacts, notably training_data.pkl and trained_agents_data.pkl. Because trained guidance files are loaded by agents during normal execution, a crafted pickle placed in the working directory can execute arbitrary code during deserialization (CWE-502).
This PR keeps the existing on-disk format but hardens loads by using a RestrictedUnpickler allowlist for primitive/container builtins only. This preserves the expected CrewAI training artifact shapes while rejecting unsafe globals such as builtins.exec or os.system.
Compatibility note
This change intentionally fails closed for legacy pickle artifacts that require custom classes, Pydantic models, datetime/path objects, or other non-primitive globals. CrewAI training artifacts should contain JSON-like primitive/container data. If a user has a custom or manually edited pickle that depends on arbitrary class imports, it will now be rejected instead of imported during load.
Tests
Verified locally:
Summary by CodeRabbit
Bug Fixes
Tests