Skip to content

🛡️ Sentinel: [CRITICAL] Fix path traversal vulnerabilities and handle exceptions securely#89

Closed
socialawy-dev wants to merge 1 commit into
mainfrom
sentinel/fix-path-traversal-validation-3667402170605514993
Closed

🛡️ Sentinel: [CRITICAL] Fix path traversal vulnerabilities and handle exceptions securely#89
socialawy-dev wants to merge 1 commit into
mainfrom
sentinel/fix-path-traversal-validation-3667402170605514993

Conversation

@socialawy-dev
Copy link
Copy Markdown
Collaborator

🚨 Severity: CRITICAL
💡 Vulnerability:

  1. The custom SafeStaticFiles middleware in app.py crashed with an AttributeError if an unexpected path object was received because Path(path).lower() was used instead of explicitly coercing to string first.
  2. The validate_path_within helper used os.path.abspath for 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:
  • Updated SafeStaticFiles.get_response to safely stringify inputs with Path(str(path).lower()) and catch generic exceptions directly returning an HTTPException (400 Bad Request) to fail closed.
  • Refactored validate_path_within to natively use Path.resolve().is_relative_to(). Added broad exception handling so that any malformed inputs properly return False rather than erroring out the pipeline.
  • Appended Sentinel learning log detailing the nature of Path coercion and resolution mechanisms.
    Verification: Ran linters, formatters, and isolated security tests locally successfully.

PR created automatically by Jules for task 3667402170605514993 started by @socialawy

Co-authored-by: socialawy <24765060+socialawy@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

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()
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Comment thread .jules/sentinel.md
**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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
**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.

@socialawy
Copy link
Copy Markdown
Owner

Superseded by #90 (merged ac23b55). The Sentinel/Jules SafeStaticFiles chain was iterating on the same middleware; #90's Path(str(path).lower()) fix addresses the root AttributeError. Closing as duplicate; re-open if a distinct vulnerability emerges.

@socialawy socialawy closed this May 13, 2026
@socialawy socialawy deleted the sentinel/fix-path-traversal-validation-3667402170605514993 branch May 13, 2026 19:05
@google-labs-jules
Copy link
Copy Markdown
Contributor

Superseded by #90 (merged ac23b55). The Sentinel/Jules SafeStaticFiles chain was iterating on the same middleware; #90's Path(str(path).lower()) fix addresses the root AttributeError. Closing as duplicate; re-open if a distinct vulnerability emerges.

Understood. Acknowledging that this work is now obsolete as it has been superseded by #90. Stopping work on this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants