Skip to content

Commit b3aab2f

Browse files
Potential fix for code scanning alert no. 7: Uncontrolled data used in path expression
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 41a1b0f commit b3aab2f

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

backend/clients/local_storage_utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
from pathlib import Path
1010
from typing import Tuple
11+
import os
1112

1213
import config
1314

@@ -55,12 +56,16 @@ def resolve_storage_path(bucket: str, relative_path: str) -> Path:
5556

5657
# Construct file path rooted in the configured bucket directory
5758
bucket_base = storage_base / bucket_name
58-
file_path = bucket_base / relative_path
59-
resolved_path = file_path.resolve()
6059
bucket_resolved = bucket_base.resolve()
6160

61+
file_path = bucket_resolved / relative_path
62+
resolved_path = file_path.resolve()
63+
6264
# Security: ensure path stays within the bucket directory
63-
if not resolved_path.is_relative_to(bucket_resolved):
65+
bucket_root = bucket_resolved.as_posix()
66+
candidate = resolved_path.as_posix()
67+
common = os.path.commonpath([bucket_root, candidate])
68+
if common != bucket_root:
6469
logger.warning(f"Path traversal attempt detected: {relative_path}")
6570
raise PathTraversalError("Path traversal detected")
6671

0 commit comments

Comments
 (0)