refactor: implement safe_relative_path utility for secure file handling

This commit is contained in:
2026-06-25 13:51:06 +08:00
parent be19dbc888
commit 90f4ca9290
3 changed files with 19 additions and 5 deletions
+11
View File
@@ -1,6 +1,17 @@
from pathlib import Path
def safe_relative_path(value: str, fallback: str = "upload.bin") -> Path:
parts = [
part
for part in str(value or "").replace("\\", "/").split("/")
if part and part not in {".", ".."}
]
if not parts:
parts = [fallback]
return Path(*parts)
def remove_file_safely(path: Path) -> None:
try:
path.unlink()