Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
tests/delete_guard.py,tests/_guard_site/,tests/conftest.py,tests/test_delete_guard.py)os.remove/unlink/rmdir/rename/replace,shutil.rmtreeandPath.unlink/rmdir/rename/replacebefore any test is collected. A target outside pytest's temp area,--basetemp, the in-repo.pytest_cacheor atempfile.mkdtemp/mkstemppath raisesDeleteOutsideTestRootsbefore the disk is touched.OSError, sosuppress(OSError)cannot hide it; every refusal is recorded and fails the session even if caught.dir_fdforms are resolved through the descriptor; symlinked parents are resolved.sitecustomizeonPYTHONPATHthat finds the guard by its own path, so tests that stripTLGR_*from a child's env keep it.suppress(OSError),dir_fd, symlinked dirs,/and$HOME, a caught refusal still recorded, removed roots no longer allowed, and child processes with and withoutTLGR_*.[policy]validation,*:credentialclass,--enable-commandsclass 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.--chattakes a single directory name and cannot lead out of the root./,$HOMEor a parent of it, or if the downloads dir is not strictly inside the tlgr home.skipped_outside).failed_files,failures) instead of suppressing them..pycas<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 andPYTHONDONTWRITEBYTECODEwas set.Testing
All runs used a fresh temp
TLGR_HOMEwith the guard active, and only against the fixed code. The final run also started with no.pytest_cacheand with bytecode writing on, as on CI.make lint typecheck: passmake test: 13767 passed, no refusals, 84% coveragemake docs parity: no changesNotes for review
tests/conftest.pydefines_never_the_real_hometwice; the second silently overrides the first. That is already on main and is left alone here.contextlib.suppress(OSError)sites around deletes in the codebase; auditing them against the new rule 6 is left for a separate PR.