refactor(security): improve sanitization logic and tests based on code review

- Change consecutive dots regex to simple replace for better precision
- Update tests to verify exact sanitized output
- Fix docstring syntax warning with raw string
- Add detailed comments explaining sanitization behavior
- All 43 tests pass (21 file upload + 22 filename utils)

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-09 21:04:03 +00:00
parent 2bcd774d6d
commit 43b512fee8
2 changed files with 18 additions and 8 deletions
+3 -3
View File
@@ -70,7 +70,7 @@ def get_unique_filename(original_path, check_exists_func=None):
def sanitize_filename(filename):
"""
r"""
Sanitize a filename to ensure it's valid across different file systems
and prevent path traversal attacks.
@@ -94,8 +94,8 @@ def sanitize_filename(filename):
sanitized = re.sub(r"[^\w\-\. ]", "_", sanitized)
# Remove or replace path traversal patterns
# Replace consecutive dots with a single underscore to prevent .. patterns
sanitized = re.sub(r"\.\.+", "_", sanitized)
# Replace specifically '..' to prevent path traversal while preserving single dots
sanitized = sanitized.replace("..", "_")
# Replace multiple spaces/underscores with single ones
sanitized = re.sub(r"__+", "_", sanitized)