Subclass of a pathlib.Path object that does not allow traversal outside of a trusted root.
Project description
PathRoot
Purpose
PathRoot is a Python library that provides a secure way to work with filesystem paths. It extends Python's pathlib.Path to prevent directory traversal attacks by restricting all path operations to a trusted root directory. This is particularly useful when:
- Building applications that handle user-provided file paths
- Creating systems that need to prevent access outside a designated directory
- Implementing sandboxed file access in web applications or APIs
- Ensuring path safety without manual validation checks
Installation
You can install PathRoot using pip:
pip install pathroot
PathRoot requires Python 3.12 or higher and has no external dependencies.
How to Use PathRoot
Basic Usage
Initialize a PathRoot object with a path and optional safe_root parameter:
from pathroot import PathRoot
# Initialize with explicit safe_root
root = PathRoot('/Users/foo/bar', safe_root='/Users/foo/bar')
# Or initialize without safe_root (defaults to the path itself)
root = PathRoot('/Users/foo/bar')
Path Operations
Once initialized, you can use PathRoot like any standard pathlib.Path object:
from pathroot import PathRoot
root = PathRoot('/Users/foo/bar')
# Safe operations within the root
my_file = root / 'groceries.txt' # Works fine
config = root / 'config' / 'settings.json' # Works fine
Security Protection
PathRoot automatically prevents directory traversal attacks by raising a PathOutsideRootError exception:
from pathroot import PathRoot
root = PathRoot('/Users/foo/bar')
# This raises PathOutsideRootError - attempting to traverse outside the root
my_file = root / '..' / '..' / 'groceries.txt'
Features
- Full
pathlib.Pathcompatibility for all safe operations - Automatic validation of all path operations
- Prevents directory traversal exploits
- No external dependencies
- Type-safe with Python 3.10+
Error Handling
When a path operation would traverse outside the trusted root, PathRoot raises a PathOutsideRootError exception. Always catch and handle this exception when working with untrusted paths:
from pathroot import PathRoot, PathOutsideRootError
root = PathRoot('/Users/foo/bar')
try:
unsafe_path = root / user_input
except PathOutsideRootError:
# Handle the error - the path would have escaped the root
print("Invalid path: traversal outside root directory")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pathroot-1.1.1.tar.gz.
File metadata
- Download URL: pathroot-1.1.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87f3656c0de77251a7702752eb9f368d816ebc2e227ee9456c2c7a0a28aa2b47
|
|
| MD5 |
58e28045407849256811cf05bf52c8cc
|
|
| BLAKE2b-256 |
bdcfc21bcf89f75b3ff5bbe6297b3af0ce07f924311fd1ec1c6b9fc82bd947ce
|
File details
Details for the file pathroot-1.1.1-py3-none-any.whl.
File metadata
- Download URL: pathroot-1.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08c6562644f473a78cbf920da7c6c407971f74522d13f8b177c65db948b2c7e8
|
|
| MD5 |
904e8e97076201c89eefcba46191cf65
|
|
| BLAKE2b-256 |
13386638d39b2f3185f3c674ea087ab47e436b8ba716066fcf20d378b3c725f7
|