fix(tools): pin FileWriterTool writes to the workspace directory#6201
fix(tools): pin FileWriterTool writes to the workspace directory#6201Jiangrong-W wants to merge 1 commit into
Conversation
FileWriterTool already rejected filename escapes (../, absolute filename, symlink) by checking that the resolved path is relative to the target directory. However, that directory is itself a fully model-controlled argument with no workspace anchoring, so an absolute root such as /etc/cron.d or ~/.ssh passed as `directory` resolved the final path "inside" an attacker-chosen root and the containment check passed — yielding an out-of-workspace write (and os.makedirs of the chosen root) with no non-default flag. Anchor the resolved write target to the current working directory using validate_file_path() from crewai_tools.security.safe_path — the same cwd-pinned validator FileReadTool already uses. The existing is_relative_to() filename-escape guard is kept as defense in depth. The documented CREWAI_TOOLS_ALLOW_UNSAFE_PATHS escape hatch continues to let callers opt in to writes outside the workspace, so explicit/trusted usage stays backward compatible. The public _run() signature is unchanged. Existing tests that wrote to absolute temp dirs are updated to run inside an isolated workspace (the established FileReadTool test idiom). New regression tests cover the directory-as-root vector and the opt-in escape hatch. Signed-off-by: christop <825583681@qq.com>
|
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 (2)
📝 WalkthroughWalkthroughThe ChangesFileWriterTool path confinement test coverage
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
FileWriterTool already rejected filename escapes (../, absolute filename, symlink) by checking that the resolved path is relative to the target directory. However, that directory is itself a fully model-controlled argument with no workspace anchoring, so an absolute root such as /etc/cron.d or ~/.ssh passed as
directoryresolved the final path "inside" an attacker-chosen root and the containment check passed — yielding an out-of-workspace write (and os.makedirs of the chosen root) with no non-default flag.Anchor the resolved write target to the current working directory using validate_file_path() from crewai_tools.security.safe_path — the same cwd-pinned validator FileReadTool already uses. The existing is_relative_to() filename-escape guard is kept as defense in depth.
The documented CREWAI_TOOLS_ALLOW_UNSAFE_PATHS escape hatch continues to let callers opt in to writes outside the workspace, so explicit/trusted usage stays backward compatible. The public _run() signature is unchanged.
Existing tests that wrote to absolute temp dirs are updated to run inside an isolated workspace (the established FileReadTool test idiom). New regression tests cover the directory-as-root vector and the opt-in escape hatch.
Summary by CodeRabbit