MCPcopy Create free account
hub / github.com/Dispatcharr/Dispatcharr / safe_upload_path

Function safe_upload_path

core/utils.py:635–646  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

633
634
635def 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
649def is_protected_path(file_path):

Callers 4

createMethod · 0.90
updateMethod · 0.90
uploadMethod · 0.90
upload_backupFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected