Skip to content

Policy and output hardening, safe storage clear, and a test-wide delete guard - #70

Open
erfnzdeh wants to merge 6 commits into
mainfrom
fix/policy-hardening
Open

erfnzdeh wants to merge 6 commits into
mainfrom
fix/policy-hardening

Conversation

@erfnzdeh

@erfnzdeh erfnzdeh commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #61

Why this PR is shaped the way it is

While this branch was being written, a regression test for media storage clear --chat / was run against the unfixed code to watch it fail. The old code joined / onto the downloads root, walked the disk and unlinked every file the user owned, with errors suppressed. This PR fixes that bug, adds defense in depth around it, adds a test-wide guard so no test can ever delete outside its temp dirs again, and writes the rules down for every future agent session.

Commits

  1. Add a test-wide guard that refuses deletes outside the run's temp dirs (tests/delete_guard.py, tests/_guard_site/, tests/conftest.py, tests/test_delete_guard.py)
    • Patches os.remove/unlink/rmdir/rename/replace, shutil.rmtree and Path.unlink/rmdir/rename/replace before any test is collected. A target outside pytest's temp area, --basetemp, the in-repo .pytest_cache or a tempfile.mkdtemp/mkstemp path raises DeleteOutsideTestRoots before the disk is touched.
    • The exception is not an OSError, so suppress(OSError) cannot hide it; every refusal is recorded and fails the session even if caught.
    • dir_fd forms are resolved through the descriptor; symlinked parents are resolved.
    • Child Python processes get the guard through a sitecustomize on PYTHONPATH that finds the guard by its own path, so tests that strip TLGR_* from a child's env keep it.
    • Temp roots are forgotten once removed (that only ever removes permission). Without this, a full run accumulated tens of thousands of roots and slowed to a crawl.
    • 27 tests prove it: each guarded call against a real victim file outside the roots, overwrite by rename, suppress(OSError), dir_fd, symlinked dirs, / and $HOME, a caught refusal still recorded, removed roots no longer allowed, and child processes with and without TLGR_*.
  2. Harden the command policy, credential ops, outbound files and output (the Policy and output hardening: fail closed on typos, keep credentials out of *:read, guard uploads and terminal output #61 work: fail-closed [policy] validation, *:credential class, --enable-commands class entries, no uploads from the tlgr home, terminal escape stripping, upload error propagation). This was written by an agent; it passes the full suite but I have not reviewed it line by line yet.
  3. Keep media storage clear inside the downloads directory
    • --chat takes a single directory name and cannot lead out of the root.
    • Refuses to run at all if the downloads dir or tlgr home resolves to /, $HOME or a parent of it, or if the downloads dir is not strictly inside the tlgr home.
    • Resolves every candidate right before unlinking and skips it unless it is inside the resolved root (skipped_outside).
    • Counts and names unlink failures (failed_files, failures) instead of suppressing them.
  4. Add permanent rules for destructive operations to AGENT.md and CLAUDE.md.
  5. Keep a temp root allowed after it is renamed away and 6. Let pytest write its bytecode cache under the delete guard: fixes for the first CI run. On a fresh checkout pytest renames its cache dir into place and then removes the old name, and writes each test module's .pyc as <name>.pyc.<pid> before renaming it. Both were refused. The guard now forgets a temp root only when it is deleted, and allows exactly a .pyc/.pyc.<pid> file directly in a __pycache__ inside the repository. Local runs missed both because the cache already existed and PYTHONDONTWRITEBYTECODE was set.

Testing

All runs used a fresh temp TLGR_HOME with the guard active, and only against the fixed code. The final run also started with no .pytest_cache and with bytecode writing on, as on CI.

  • make lint typecheck: pass
  • make test: 13767 passed, no refusals, 84% coverage
  • make docs parity: no changes

Notes for review

  • tests/conftest.py defines _never_the_real_home twice; the second silently overrides the first. That is already on main and is left alone here.
  • There are other contextlib.suppress(OSError) sites around deletes in the codebase; auditing them against the new rule 6 is left for a separate PR.

tests/delete_guard.py patches os.remove/unlink/rmdir/rename/replace,
shutil.rmtree and the matching Path methods before any test module is
collected. A delete or move whose target is outside pytest's temp area,
--basetemp, the in-repo cache dir or a directory made by tempfile.mkdtemp
raises DeleteOutsideTestRoots before the disk is touched. The exception is
not an OSError, so suppress(OSError) cannot hide it, and every refusal is
recorded and fails the session even if something catches it.

Child Python processes load the same guard through a sitecustomize on
PYTHONPATH, located by its own path so tests that strip TLGR_* variables
keep it. Temp roots are forgotten once removed, which keeps lookups cheap
on a full run.
- Validate every [policy] allow/deny entry against the registry and reject
  unknown [policy] keys, so a typo stops the daemon (or fails reload and
  config validate) instead of silently allowing or denying nothing.
- Tag operations that print or mint access as bearer-credential and move
  them out of *:read into a new *:credential class, with a registry lint.
- Use the daemon's matcher for --enable-commands so class entries work.
- Refuse to upload or send as text any file under the tlgr home (symlinks
  resolved), with downloads/ exempt.
- Strip terminal escape sequences and bidi overrides from text other people
  wrote in human and --plain output.
- Let upload errors propagate through the chat-action broadcast instead of
  surfacing as "generator didn't stop after athrow()".
--chat was joined onto the downloads root, so --chat / or --chat ../..
walked and unlinked files anywhere the user could write. It now takes a
single directory name and refuses anything that leads out of the root,
symlinks included.

Defense in depth on top of that: refuse to run at all when the downloads
directory or the tlgr home resolves to /, the user's home or one of its
parents, or when the downloads directory is not strictly inside the tlgr
home; resolve every candidate immediately before unlinking it and skip it
unless it is inside the resolved root; and count and name unlink failures
(failed_files, failures) instead of suppressing them.
Every agent session reads these: never run delete code against an unfixed
implementation, never check out another branch to confirm red on a
destructive fix, keep delete-path tests under tests/delete_guard.py, never
run destructive tlgr commands on a real machine without approval of the
exact command, ask before writing outside the repo or a temp dir, never
suppress a delete error without recording it, and pass the rules to every
subagent.
pytest creates its cache dir by renaming a mkdtemp dir into place and then
calling rmtree on the old name. The guard forgot the root on the rename, so
that rmtree was refused on every fresh checkout, which failed every CI job.
Roots are now forgotten only when they are deleted, not renamed. A test
repeats pytest's exact pattern.
pytest's assertion rewriter writes each test module's cache as
<name>.pyc.<pid> in __pycache__ and os.replaces it into place. On a fresh
checkout that rename was refused, which failed every CI job; local runs
had PYTHONDONTWRITEBYTECODE set and never wrote one. The guard now allows a
.pyc or .pyc.<pid> file directly in a __pycache__ directory inside the
repository, and nothing else there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Policy and output hardening: fail closed on typos, keep credentials out of *:read, guard uploads and terminal output

1 participant