Install
consolepip install path-containment
API
pythonfrom path_containment import (
UnsafePathError, is_within, safe_join, safe_member_name, safe_target, sanitize_filename,
)
is_within("/srv/uploads", "/srv/uploads/a/b.txt") # True
is_within("/srv/uploads", "/srv/other") # False
safe_join("/srv/uploads", "reports", "q1.pdf") # /srv/uploads/reports/q1.pdf
safe_join("/srv/uploads", "../etc/passwd") # raises UnsafePathError
safe_member_name("./docs\\a.txt") # "docs/a.txt"
safe_member_name("/etc/passwd") # raises
safe_target("/srv/uploads", "..\\..\\win.ini") # raises
sanitize_filename('a/b:c?.txt') # "a_b_c_.txt"
sanitize_filename("CON") # "_CON"
sanitize_filename("name.txt...") # "name.txt"
Every refusal raises UnsafePathError, a ValueError, with a message that says what was wrong with which component.
Why not os.path.join
os.path.join("uploads", "../../etc/passwd") returns a path outside uploads, and Path.resolve() alone only tells you where it lands — after the fact. On Windows ntpath.isabs("/etc/passwd") is False (there is no drive), so a check that relies on isabs alone lets that through. Both cases are covered here, and the resolved result is compared against the resolved base, which also catches a symlink that points outside.
Behaviour worth knowing
- `
safe_join` returns an absolute, resolved path. It creates nothing. - A missing target is fine: only the path is checked, not existence.
- `
safe_member_namenormalises\to/, strips./, collapses//`, and refuses
absolute paths, drive letters, control characters and any .. component.
- `
sanitize_filenamekeeps unicode, replaces/ \ : * ? " < > |` and control
characters, trims trailing dots and spaces, prefixes reserved Windows device names (CON, NUL, COM1…) and truncates to max_length without leaving a trailing dot.
Development
consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest
The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.
License
MIT.
Other libraries here
One problem each, standard library only, no runtime dependencies.
- argrepair — turn a bad tool call into a message the model can act on
- backoff-headers — turn a rate-limit header into the exact time to wait
- columncheck — find the CSV defect before the pipeline does
- dir-digest — one hash for a directory tree, identical on every OS
- docanchor — catch every broken heading anchor in your docs
- envfile-diff — diff two .env files without printing the secrets
- fsync-replace — replace a file so a crash cannot leave it half-written
- header-date — HTTP dates parsed strictly, formatted correctly
- junit-merge — merge JUnit XML from every job into one honest report
- npm-semver — npm semver ranges, parsed the way npm parses them
- outputcap — keep a huge tool result out of the context window
- parse-size — byte sizes in and out, MB and MiB never confused
- redactscope — stop secrets in tool results before they reach the model
- runbudget — a per-run cost cap that warns before it stops
- testflakes — rank your flaky tests from the history you already have
- tool-loop-guard — catch an agent repeating itself while it is still cheap
- toolcall-ledger — a hash-chained record of every tool call
- toolcircuit — stop retrying a tool that is down
- toolquota — client-side rate limits your agent loop can wait on
- wheeldeps — read a wheel's dependencies offline, markers included
- zip-deterministic — byte-identical archives, build after build