Spaces:
Paused
Paused
Download backend/app/security/paths.py from 2Btainment/repospace: direct link, hf CLI and curl.
- Browser
- Download file 621 Bytes
-
https://huggingface.co/spaces/2Btainment/repospace/resolve/main/backend/app/security/paths.py
- Command line
-
hf download hf://spaces/2Btainment/repospace/backend/app/security/paths.py
-
curl -L -o paths.py https://huggingface.co/spaces/2Btainment/repospace/resolve/main/backend/app/security/paths.py
621 Bytes
| """Path safety helpers.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| def is_within_directory(base: Path, target: Path) -> bool: | |
| try: | |
| base_r = base.resolve() | |
| target_r = target.resolve() | |
| return target_r == base_r or base_r in target_r.parents | |
| except OSError: | |
| return False | |
| def safe_join(base: Path, *parts: str) -> Path: | |
| """Join and ensure result stays under base (no path traversal).""" | |
| candidate = base.joinpath(*parts).resolve() | |
| if not is_within_directory(base, candidate): | |
| raise ValueError("path traversal blocked") | |
| return candidate | |