fix: handle ValueError from os.path.commonpath on cross-drive paths (Windows)#13146
Conversation
…Windows) On Windows, os.path.commonpath raises ValueError when the two paths reside on different drives (e.g. C:\ vs D:\). This crashed ComfyUI with an unhandled exception whenever output/input/upload directories were on a different drive than the ComfyUI installation. Add folder_paths.is_path_within_directory() as a safe wrapper that catches ValueError and returns False (different drives means the path is clearly not inside the directory). Replace all bare os.path.commonpath security checks across server.py, folder_paths.py, and app/user_manager.py with this helper. In comfy/sd1_clip.py, narrow the bare except clause to except ValueError with a descriptive comment. Fixes Comfy-Org#1488
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughThe PR refactors directory traversal security checks across multiple files by introducing a new 🚥 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. 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 |
Summary
Fixes #1488
On Windows,
os.path.commonpath()raisesValueErrorwhen the two paths reside on different drives (e.g.C:\vsD:\). This caused ComfyUI to crash with an unhandled exception whenever output, input, or upload directories were configured on a different drive than the ComfyUI installation.Changes:
folder_paths.is_path_within_directory(path, directory)— a safe wrapper aroundos.path.commonpaththat catchesValueErrorand returnsFalse(different drives means the path is clearly not inside the directory)os.path.commonpathsecurity-check patterns inserver.py(3 call sites),folder_paths.py(1 call site), andapp/user_manager.py(2 call sites) with the new helpercomfy/sd1_clip.py, narrow the bareexcept:clause toexcept ValueError:with a descriptive comment explaining the cross-drive scenarioos.path.commonpathcall that was embedded in the error message string infolder_paths.py(which itself would crash on different drives before the error could be logged)ValueErrorTest plan
pytest tests-unit/comfy_test/folder_path_test.py)D:\output) than ComfyUI installation (e.g.C:\ComfyUI)