Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 2025-02-21 - Path Traversal in Mix Endpoint API Parameter
**Vulnerability:** The `/projects/{project_id}/mix` API endpoint in `src/audioformation/server/routes.py` accepted a `music` parameter (meant to specify a filename within the `05_MUSIC/generated` directory) but directly passed it to `mix_project` without sanitization. This allowed directory traversal payloads like `../../../etc/passwd` to be used for background music resolution.
**Learning:** Even internal API inputs that map strictly to filenames inside an expected directory must be sanitized. A simple check for file existence (`if not bg_music_path.exists():`) is insufficient as it confirms existence but allows looking outside the bounded directory.
**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.
**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-28 - Path validation and `Path` object handling
**Vulnerability:** String-based path validation inside `validate_path_within` was prone to symlink and path traversal bypasses. Also, `.lower()` was incorrectly used on `Path` objects in `SafeStaticFiles` leading to `AttributeError` 500 errors.
**Learning:** `Path.resolve().is_relative_to()` is the safest and most canonical path traversal defense in Python. Additionally, `Path` objects in `pathlib` do not inherit string methods like `.lower()`.
**Prevention:** Always use `is_relative_to` after path resolution for boundary checks, and always apply string manipulations to path strings *before* instantiation of a `Path` object.
2 changes: 1 addition & 1 deletion src/audioformation/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SafeStaticFiles(StaticFiles):

async def get_response(self, path: str, scope) -> Response:
# Normalize path for check
p = Path(path).lower()
p = Path(path.lower())
if "00_config" in p.parts or p.name.startswith(".env") or ".git" in p.parts:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-medium medium

The current check for .env files only inspects the final component of the path (p.name). While .env is typically a file, it is more robust to check all path components to prevent access to sensitive files within a directory that might start with .env (e.g., .env.backup/secrets.txt). This change would make the .env protection consistent with how .git and 00_config are handled using p.parts.

Suggested change
if "00_config" in p.parts or p.name.startswith(".env") or ".git" in p.parts:
if "00_config" in p.parts or any(part.startswith(".env") for part in p.parts) or ".git" in p.parts:

raise HTTPException(
status_code=403, detail="Access denied to sensitive resource"
Expand Down
18 changes: 4 additions & 14 deletions src/audioformation/utils/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,10 @@
This prevents path traversal and symlink bypass attacks.
"""
try:
# Resolve to absolute paths first
abs_path = os.path.abspath(str(path))
abs_root = os.path.abspath(str(root))

# On Windows, abspath can have different casing for the drive letter.
# We normalize to lowercase for the preliminary string check.
if abs_path.lower().startswith(abs_root.lower()):
# String check passed, now do the rigorous resolution check
resolved_root = root.resolve()
resolved_path = path.resolve()
return resolved_path.is_relative_to(resolved_root)

return False
except (ValueError, RuntimeError, OSError):
resolved_root = root.resolve()

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
resolved_path = path.resolve()

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
return resolved_path.is_relative_to(resolved_root)
except (TypeError, ValueError, RuntimeError, AttributeError, OSError):
return False


Expand Down
Loading