🛡️ Sentinel: [CRITICAL] Fix path traversal vulnerabilities and handle exceptions securely#89
Conversation
Co-authored-by: socialawy <24765060+socialawy@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
|
||
| return False | ||
| except (ValueError, RuntimeError, OSError): | ||
| resolved_root = root.resolve() |
| return False | ||
| except (ValueError, RuntimeError, OSError): | ||
| resolved_root = root.resolve() | ||
| resolved_path = path.resolve() |
There was a problem hiding this comment.
Code Review
This pull request enhances path validation and traversal prevention by fixing an AttributeError in the SafeStaticFiles middleware and simplifying the validate_path_within utility to use Path.resolve().is_relative_to() for more robust symlink protection. It also introduces fail-closed error handling when normalizing paths for static file access. Review feedback suggested a documentation improvement to more accurately describe why Path objects do not support string methods like .lower().
| **Prevention:** Always use established sanitization helpers (like `sanitize_filename`) or bound checks (like `validate_path_within`) for any user-supplied string that forms part of a filesystem path. Ensure bypass parameters like `FORCE_NO_MUSIC` are handled before and mutually exclusively from sanitization. | ||
| ## 2025-02-21 - Path Validation and Traversal Prevention Enhancements | ||
| **Vulnerability:** The custom `SafeStaticFiles` middleware crashed with an `AttributeError` when a path (e.g. `NoneType`) was improperly passed since `Path(path).lower()` was used, and the system used `os.path.abspath` for path bound checks, creating a risk of symlink bypasses. | ||
| **Learning:** `Path` object construction does not perform lowercase conversions like strings. Further, path string manipulators like `os.path.abspath` are insecure against advanced traversal via symlink resolution. Exceptions thrown during path manipulation must fail closed explicitly via a try...except returning an `HTTPException`, to not expose internal errors and avoid the service from breaking or failing open. |
There was a problem hiding this comment.
For improved clarity and accuracy in this learning summary, it's better to state that pathlib.Path objects do not have string methods like .lower(), which was the direct cause of the AttributeError. The current phrasing is slightly misleading as it focuses on object construction rather than the incorrect method call on the object instance.
| **Learning:** `Path` object construction does not perform lowercase conversions like strings. Further, path string manipulators like `os.path.abspath` are insecure against advanced traversal via symlink resolution. Exceptions thrown during path manipulation must fail closed explicitly via a try...except returning an `HTTPException`, to not expose internal errors and avoid the service from breaking or failing open. | |
| **Learning:** `pathlib.Path` objects do not have string methods like `.lower()`; case conversion must be performed on the string representation before creating the `Path` object. Further, path string manipulators like `os.path.abspath` are insecure against advanced traversal via symlink resolution. Exceptions thrown during path manipulation must fail closed explicitly via a try...except returning an `HTTPException`, to not expose internal errors and avoid the service from breaking or failing open. |
Understood. Acknowledging that this work is now obsolete as it has been superseded by #90. Stopping work on this task. |
🚨 Severity: CRITICAL
💡 Vulnerability:
SafeStaticFilesmiddleware inapp.pycrashed with anAttributeErrorif an unexpected path object was received becausePath(path).lower()was used instead of explicitly coercing to string first.validate_path_withinhelper usedos.path.abspathfor path boundary checks. This is vulnerable to directory traversal bypasses using symlinks and casing trickery.🎯 Impact: Allowed advanced path traversal attacks that could bypass directory constraints. Could also crash the server handling static files.
🔧 Fix:
SafeStaticFiles.get_responseto safely stringify inputs withPath(str(path).lower())and catch generic exceptions directly returning anHTTPException(400 Bad Request) to fail closed.validate_path_withinto natively usePath.resolve().is_relative_to(). Added broad exception handling so that any malformed inputs properly returnFalserather than erroring out the pipeline.Pathcoercion and resolution mechanisms.✅ Verification: Ran linters, formatters, and isolated security tests locally successfully.
PR created automatically by Jules for task 3667402170605514993 started by @socialawy