Restrict agent writes to an allowlisted path
An agent that picks its own filenames can overwrite a config, a credential file, or a sibling project. Resolve every write path, check it against an allowlist, and refuse anything outside.
Procedure
- Declare the writable roots once per run:
out/, notes/, tmp/. Everything else is read-only.
- Resolve the target to an absolute real path before checking:
os.path.realpath(p), which collapses .. and symlinks.
- Compare against the resolved roots with a boundary-safe check:
commonpath([root, target]) == root, not a substring test.
- Reject absolute paths supplied by the model unless they already sit inside a root.
- Disallow symlink targets: a link inside
out/ pointing at ~/.ssh must fail the realpath check.
- Create parent directories only inside the root, and refuse to create a root that does not already exist.
- Log the resolved path and the allowlist decision:
write=out/report.csv allowed=yes root=out.
- On rejection, return a clear error naming the attempted path so the model can retarget, not silently drop the write.
import os
ROOTS = {os.path.realpath(r) for r in ("out", "notes", "tmp")}
def safe_write(p):
real = os.path.realpath(p)
if not any(os.path.commonpath([r, real]) == r for r in ROOTS):
raise PermissionError(f"path outside allowlist: {real}")
with open(real, "w") as fh:
fh.write("...")
Pitfalls
- Checking with
".." not in path or a startswith prefix, both of which out/../etc/x and outside/ slip past.
- Checking the path before resolving symlinks, so a link inside the root escapes it.
- Allowing the model to widen the allowlist itself in the same turn.
- Creating an allowlist root on demand, turning deny into allow-on-request.
- Dropping a rejected write silently, so the model believes it succeeded.
- Writing a temp file next to the target with a predictable name and racing another child.
Verification
python3 -c "import os;print(os.path.commonpath([os.path.realpath('out'), os.path.realpath('out/../out/x')]))" # prints the root, not a parent
Report each write's resolved path, the allowlist decision, and any path refused.
1---2name: restrict-agent-writes-to-an-allowlisted-path3description: Use when an agent writes files it chose the path for. Confine every write to a declared directory, reject path traversal, and log the resolved target.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/restrict-agent-writes-to-an-allowlisted-path6---78# Restrict agent writes to an allowlisted path910An agent that picks its own filenames can overwrite a config, a credential file, or a sibling project. Resolve every write path, check it against an allowlist, and refuse anything outside.1112## Procedure13141. Declare the writable roots once per run: `out/`, `notes/`, `tmp/`. Everything else is read-only.152. Resolve the target to an absolute real path before checking: `os.path.realpath(p)`, which collapses `..` and symlinks.163. Compare against the resolved roots with a boundary-safe check: `commonpath([root, target]) == root`, not a substring test.174. Reject absolute paths supplied by the model unless they already sit inside a root.185. Disallow symlink targets: a link inside `out/` pointing at `~/.ssh` must fail the realpath check.196. Create parent directories only inside the root, and refuse to create a root that does not already exist.207. Log the resolved path and the allowlist decision: `write=out/report.csv allowed=yes root=out`.218. On rejection, return a clear error naming the attempted path so the model can retarget, not silently drop the write.2223```python24import os25ROOTS = {os.path.realpath(r) for r in ("out", "notes", "tmp")}26def safe_write(p):27 real = os.path.realpath(p)28 if not any(os.path.commonpath([r, real]) == r for r in ROOTS):29 raise PermissionError(f"path outside allowlist: {real}")30 with open(real, "w") as fh:31 fh.write("...")32```3334## Pitfalls3536- Checking with `".." not in path` or a `startswith` prefix, both of which `out/../etc/x` and `outside/` slip past.37- Checking the path before resolving symlinks, so a link inside the root escapes it.38- Allowing the model to widen the allowlist itself in the same turn.39- Creating an allowlist root on demand, turning deny into allow-on-request.40- Dropping a rejected write silently, so the model believes it succeeded.41- Writing a temp file next to the target with a predictable name and racing another child.4243## Verification4445 python3 -c "import os;print(os.path.commonpath([os.path.realpath('out'), os.path.realpath('out/../out/x')]))" # prints the root, not a parent4647Report each write's resolved path, the allowlist decision, and any path refused.