When building AI agents, MCP servers, archive extractors, or multi-tenant web backends, confining file operations to a designated directory is critical.
Yet the standard Python idiom for path validation—normalizing with os.path.realpath() or Path.resolve(), checking a string prefix, and then calling open()—is fundamentally vulnerable to TOCTOU race conditions.
To solve this without requiring C extensions or sacrificing performance, I built PathFence: a zero-dependency, pure-Python path containment library backed by OS kernel primitives and mathematically verified in Lean 4.
hiroki-abe-58
/
pathfence
Verified lexical path confinement for AI-agent file tools, in Lean 4
PathFence
A path-confinement check for tools that let an AI agent (or any untrusted caller) read and write files, written and proven in Lean 4.
You give it the directories the caller may use, optionally directories inside them it may not use and a requested path. It answers allow (with the resolved path, and the allowed root it is under) deny (with the resolved path) or reject (with a reason). Lean theorems state exactly when it allows, and a command-line tool makes the proven function usable from any language.
$ pathfence check --allow /tmp/allowed_dir -- /tmp/allowed_dir_malicious/secret ../x a/../b
{"input":"/tmp/allowed_dir_malicious/secret","decision":"deny","path":"/tmp/allowed_dir_malicious/secret"}
{"input":"../x","decision":"reject","reason":"escapes"}
{"input":"a/../b","decision":"allow","path":"/tmp/allowed_dir/b","root":"/tmp/allowed_dir","rel":"b"}
Status: v0.1.0, experimental. No human security review yet.
PathFence is not a sandbox. The decision is lexical. It looks only at the path string, never at the file system. A symlink, a race between the check and the open, a hard link, a mount or a case-insensitive file system…
The Problem: Why String-Based Check-Then-Use Fails
Consider the classic validation pattern found in thousands of production codebases:
# VULNERABLE: Classic TOCTOU race condition
real_root = os.path.realpath(root)
real_target = os.path.realpath(os.path.join(root, untrusted_path))
if not real_target.startswith(real_root + os.sep):
raise PermissionError("Path traversal detected")
# An attacker can swap a path component with a symlink right here!
with open(real_target, "rb") as f:
data = f.read()
Between the moment os.path.realpath() inspects the filesystem and the moment open() resolves the path again in the kernel, a local attacker or concurrent process can replace an intermediate directory with a symlink pointing to /etc or another sensitive location. Because the check and the use are two separate operations on path strings, the containment guarantee evaporates under concurrency.
sequenceDiagram
participant App as Python Process
participant Kernel as OS Kernel / VFS
participant Attacker as Concurrent Attacker
App->>Kernel: 1. realpath("/sandbox/work/data.txt")
Kernel-->>App: "/sandbox/work/data.txt" - Prefix OK
Attacker->>Kernel: 2. rename & symlink "/sandbox/work" -> "/etc"
App->>Kernel: 3. open("/sandbox/work/passwd")
Kernel-->>App: Returns file descriptor for /etc/passwd!
How PathFence Closes the Gap
PathFence shifts the root of trust from mutable path strings to pinned directory file descriptors. Every operation resolves relative to an open directory handle using OS-level flags that forbid symlink traversal and upward escape at the kernel level.
graph TD
Input["pathfence.safe_open / Workspace"] --> Lex["Lexical Normalization & Windows Reserved Name Check"]
Lex --> Platform{"Platform Kernel Strategy"}
Platform -->|Linux 5.6+| Openat2["openat2 syscall + RESOLVE_BENEATH / NO_SYMLINKS"]
Platform -->|macOS / Older Linux| StepWalk["Component-by-component openat + O_NOFOLLOW + fstat"]
Platform -->|Windows| WinHandle["Reparse Point Inspection + GetFinalPathNameByHandleW"]
Openat2 --> Result["Verified File Descriptor / Stream"]
StepWalk --> Result["Verified File Descriptor / Stream"]
WinHandle --> Result["Verified File Descriptor / Stream"]
1. Linux 5.6+ Fast Path: Single-Syscall Atomic Containment
On modern Linux kernels, PathFence invokes the openat2(2) syscall directly via ctypes with four strict resolution flags:
-
RESOLVE_BENEATH: Prevents resolution from escaping above the root directory file descriptor. -
RESOLVE_NO_SYMLINKS: Rejects all symbolic links anywhere in the path. -
RESOLVE_NO_MAGICLINKS: Blocks procfs magic-link traversal such as/proc/self/fd/*. -
RESOLVE_NO_XDEV: Prevents crossing mount-point boundaries.
2. Portable POSIX Fallback: Step-by-Step O_NOFOLLOW Walk
On macOS, BSD, and older Linux kernels, PathFence pins the root directory with O_RDONLY | O_DIRECTORY | O_NOFOLLOW and walks each path segment one at a time using openat(..., O_NOFOLLOW). At each step, it verifies fstat device and inode identities to detect concurrent directory replacement.
3. Windows Defense-in-Depth
On Windows, PathFence blocks reserved device names such as CON, PRN, AUX, NUL, COM1-COM9, and LPT1-LPT9, NTFS Alternate Data Streams containing :, trailing dots or spaces, and UNC/device prefixes. It inspects FSCTL_GET_REPARSE_POINT to reject junctions and mount points, and verifies the final handle path via GetFinalPathNameByHandleW.
Formal Verification with Lean 4
Unit tests and fuzzing can catch bugs, but they cannot prove the absence of edge cases across infinite path combinations. PathFence includes a complete formal specification written in Lean 4 under the formal/ directory.
We model path components, depth tracking, lexical normalization, and step-by-step directory resolution, proving 15 theorems with zero sorry placeholders and zero custom axioms:
| Theorem | Verified Security Property |
|---|---|
normalize_rejects_escape |
Any component sequence whose cumulative depth drops below zero is strictly rejected. |
normalize_preserves_containment |
Every accepted path resolves strictly within the subtree rooted at the pinned base. |
symlink_free_walk_stays_in_root |
Inductive proof that a symlink-free O_NOFOLLOW walk can never transition outside the root boundary. |
normalize_idempotent |
Normalizing an already normalized path yields the exact same result. |
beneath_composable |
Sub-workspaces created via subworkspace() transitively preserve parent containment guarantees. |
Complete Workspace Lifecycle API & Benchmarks
Beyond opening files, Workspace confines the entire file lifecycle—including mkdir, unlink, rmdir, rename, listdir, and crash-safe atomic_write—inside the pinned boundary.
import pathfence
from pathfence import Workspace
# One-liner safe open
with pathfence.safe_open("/var/sandbox", "notes/hello.txt", "r") as f:
print(f.read())
# Full lifecycle confinement for AI agents or file processors
with Workspace("/var/sandbox") as ws:
ws.mkdir("output", exist_ok=True)
ws.atomic_write("output/result.json", b'{"status": "verified"}')
# Sub-workspaces are mathematically guaranteed to stay inside the parent
with ws.subworkspace("output") as sub:
data = sub.read_bytes("result.json")
Microsecond-Scale Performance
Because PathFence uses direct syscall bindings in pure Python, security comes with virtually zero latency penalty. Benchmarked over 100,000 iterations on Linux 6.8:
-
openat2fast path: 3.6 μs per open (~277k ops/sec) — faster than naiverealpath+openat 3.9 μs -
O_NOFOLLOWfallback (depth 1): 9.8 μs per open (~102k ops/sec) -
O_NOFOLLOWfallback (depth 3): 17.4 μs per open (~57k ops/sec)
Try It Out
PathFence requires Python 3.9+ and has zero runtime dependencies.
Check out the repository, inspect the Lean 4 proofs in formal/, and let me know your thoughts in the comments!
hiroki-abe-58
/
pathfence
Verified lexical path confinement for AI-agent file tools, in Lean 4
PathFence
A path-confinement check for tools that let an AI agent (or any untrusted caller) read and write files, written and proven in Lean 4.
You give it the directories the caller may use, optionally directories inside them it may not use and a requested path. It answers allow (with the resolved path, and the allowed root it is under) deny (with the resolved path) or reject (with a reason). Lean theorems state exactly when it allows, and a command-line tool makes the proven function usable from any language.
$ pathfence check --allow /tmp/allowed_dir -- /tmp/allowed_dir_malicious/secret ../x a/../b
{"input":"/tmp/allowed_dir_malicious/secret","decision":"deny","path":"/tmp/allowed_dir_malicious/secret"}
{"input":"../x","decision":"reject","reason":"escapes"}
{"input":"a/../b","decision":"allow","path":"/tmp/allowed_dir/b","root":"/tmp/allowed_dir","rel":"b"}
Status: v0.1.0, experimental. No human security review yet.
PathFence is not a sandbox. The decision is lexical. It looks only at the path string, never at the file system. A symlink, a race between the check and the open, a hard link, a mount or a case-insensitive file system…
Top comments (0)