Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Consiliency_treesitter-chunker /chunker /_internal /path_confinement.py
| """Canonical path containment for filesystem-facing interfaces.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| def resolve_within_root(candidate: str | Path, root: str | Path) -> Path: | |
| """Resolve *candidate* and reject paths that escape *root*. | |
| ``Path.resolve()`` follows existing symlinks and resolves symlinked parent | |
| directories for prospective output paths, so the containment check covers | |
| both reads and file creation. | |
| """ | |
| candidate_path = Path(candidate) | |
| if ".." in candidate_path.parts: | |
| raise ValueError("Path traversal is not allowed") | |
| root_path = Path(root).resolve() | |
| resolved = ( | |
| candidate_path.resolve() | |
| if candidate_path.is_absolute() | |
| else (root_path / candidate_path).resolve() | |
| ) | |
| try: | |
| resolved.relative_to(root_path) | |
| except ValueError as exc: | |
| raise ValueError("Path escapes the configured root") from exc | |
| return resolved | |
Xet Storage Details
- Size:
- 956 Bytes
- Xet hash:
- 8280d3d12196f11e7ca1835d4f6f2f9ab14f647a061faf83fd597c5f9785c886
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.