Return a safe absolute path for an uploaded file within base_dir. Strips all directory components from *filename* and verifies the resolved path stays inside *base_dir*. Raises ValueError on path traversal attempts.
(filename: str, base_dir)
| 633 | |
| 634 | |
| 635 | def safe_upload_path(filename: str, base_dir) -> str: |
| 636 | """Return a safe absolute path for an uploaded file within base_dir. |
| 637 | |
| 638 | Strips all directory components from *filename* and verifies the resolved |
| 639 | path stays inside *base_dir*. Raises ValueError on path traversal attempts. |
| 640 | """ |
| 641 | safe_name = Path(filename).name |
| 642 | base = Path(base_dir).resolve() |
| 643 | file_path = (base / safe_name).resolve() |
| 644 | if not file_path.is_relative_to(base): |
| 645 | raise ValueError("Invalid filename.") |
| 646 | return str(file_path) |
| 647 | |
| 648 | |
| 649 | def is_protected_path(file_path): |
no outgoing calls
no test coverage detected