fix: Path Traversal Safety for MedCheck - #104
Conversation
Addressed unsafe code patterns found during security review: - path traversal in src/medcheck/providers/local.py: Zip Slip / Path Traversal due to insecure path prefix validation. The startswith check without a trailing path separat Tested locally, no regressions observed.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesZIP Traversal Guard
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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 |
Liohtml
left a comment
There was a problem hiding this comment.
Thanks for the contribution! The core change is correct and a real improvement.
The fix is sound. The previous str(...).startswith(str(tmp_dir.resolve())) check has a genuine sibling-directory bypass: if the temp dir resolves to /tmp/abc, a member resolving to /tmp/abc-evil/... would pass the prefix check. Path.is_relative_to() enforces a true directory-boundary check, so this closes that gap. is_relative_to() is available on Python 3.9+ and the project requires >=3.10, so compatibility is fine.
Two things before this can merge:
-
Issue reference is wrong. This PR links #100, but #100 is the CSRF on
POST /api/analyzefinding. The zip-slip/path-traversal hardening you're fixing here is tracked in #61 — please update "Relates to" to#61. -
Please add a regression test. There's currently no test covering
_scan_ziptraversal handling (intests/unit/test_providers/test_local.py), so this fix could silently regress. A small test that builds a ZIP with a../evil.dcmmember and asserts_scan_zipraisesValueError("Unsafe path in ZIP: ...")would lock the behavior in. Something like:import zipfile import pytest def test_scan_zip_rejects_traversal(tmp_path): zip_path = tmp_path / "evil.zip" with zipfile.ZipFile(zip_path, "w") as zf: zf.writestr("../escape.dcm", b"not really dicom") provider = LocalProvider() # adjust to however the suite constructs it with pytest.raises(ValueError, match="Unsafe path in ZIP"): provider._scan_zip(zip_path)
(Note for maintainer: CI hasn't run yet — first-time-contributor workflows need approval.)
Generated by Claude Code
|
Thanks for this fix! The Generated by Claude Code |
Hey there! 👋
I was reviewing the codebase and noticed a potential security issue that I thought I'd flag and fix.
What I found
src/medcheck/providers/local.py: Zip Slip / Path Traversal due to insecure path prefix validation. Thestartswithcheck without a trailing path separatWhat I changed
The fix is minimal and targeted — I added proper validation/sanitization where user-controlled or untrusted data enters sensitive operations. No changes to existing functionality or public APIs.
Testing
Ran the existing test suite locally, everything passes. The change is backward-compatible.
Happy to discuss if you have questions!
Relates to: #100
💛 If this fix helps, donations are appreciated (ETH/ERC-20):
0x1478f1BDEACc7b434b4405350A15993cDcddc79F(Etherscan)Summary by CodeRabbit