Repository navigation
fix(security): keep edit backups out of the user's working tree - #3992
Conversation
Verdict: Approve with suggestionsEdit backups now land in GAIA's own cache instead of beside the file, so the agent stops littering The fix doesn't cover every path. Two of the edit tools still drop a Nothing ever cleans the new location. Backups used to pile up where the user could see and delete them; now they pile up invisibly in the cache directory, forever. 🔒 SECURITY CONCERN (hardening, not an exploit): @kovtcharov-amd — copies of edited files are now aggregated in the home cache under default directory permissions, and they outlive the project they came from. A file edited inside a private directory gets a long-lived copy in a more permissive one. Creating the backups root restrictively would close it. Real-world evidenceNo evidence bundle was produced for this run ( 🔍 Technical detailsIssues🟡 The no-validator fallback still writes beside the file (
🟡 No retention or cleanup for Every edit adds a file and nothing prunes them. 🔒 🟡 Backups inherit the cache directory's permissions (
🟢 The 🟢 Deeply nested paths on Windows ( Mirroring the full absolute path under Strengths
|
|
No edit tool puts a backup in the user's tree now, even on an agent without a path validator. Backups no longer pile up: each file keeps its five newest. The backup directory is now owner-only. The replace_function spec is updated. I left the Windows long-path fallback out; the reason is below. 🔍 Technical detailsAll changes are in 98cc7bc.
Also fixed. Moving backups to Tests: guardrails, security edge cases, The manual "edit a file in a repo, then |
Every write_file and edit_file left name.<timestamp>.bak beside the edited file and nothing removed it, so every repository the agent touched ended up littered with backups. The backup is still made; it now goes under the validator's state directory (~/.gaia/cache/backups, beside the audit log that records it) at the original's absolute path, e.g. ~/.gaia/cache/backups/Users/me/repo/src/app.py.20260918_101500.bak. ".bak" stays last (amd#3747). A drive letter or UNC share becomes one folder name. Only a filesystem error now skips the backup, with a warning naming both paths; any other exception propagates instead of being swallowed. The file tools only report backup_path, so none of their callers changed. - Backups are made by one function, backup_file, used with or without a validator, so no edit tool (edit_python_file and replace_function included) writes a backup into the user's tree. - Only the five newest backups of each file are kept; older ones are removed when a new one lands. - The backups directory and every directory below it is owner-only. - edit_python_file reports backup_created from whether a backup was made. - Tests that edit files now use a temporary home, so the suite no longer writes backups into the developer's real ~/.gaia.
5fbf778 to
a61b34a
Compare
|
Rebased onto current main as one commit. First in the file-edit stack, nothing to merge before it; #4029 builds on this. |
|
🟡 The new doc text makes a claim that contradicts the code. Both Drop "with or without a 🔍 Technical details
The behaviour for no-validator callers is already documented in the The |
|
Refreshed this branch against current The update was a clean merge: nothing was resolved by hand, and no commit on this branch was rewritten. CI now runs against an up-to-date base. |
"never beside the file, with or without a PathValidator" implied a backup is always created in the cache dir. The code returns an error before any I/O when path_validator is None (_missing_path_validator_write_error) -- no backup is written at all in that case. The guarantee that holds is "never beside the file"; dropped the rest. The other three findings from the same review (no-validator fallback still writing beside the file, missing backup retention, default cache directory permissions) are already fixed in the current code -- verified against security.py's backup_file(), which already creates the backups root at mode 0o700 and prunes to BACKUP_GENERATIONS.
Every
write_fileandedit_fileleft aname.<timestamp>.bakbeside the file it changed, and nothing ever removed it, so every repository the agent worked in slowly filled with backup files the user never asked for. Backups are still made, but now under~/.gaia/cache/backups/, mirroring the original's full path, so the working tree stays clean. That holds for every edit tool, with or without a path validator. The backup directory is owner-only, and only the five newest backups of each file are kept. On 8 benchmark runs this left zero.bakfiles in any workspace while 86 backups landed in the cache. A backup that fails for a filesystem reason is skipped with a warning naming both paths; any other error now surfaces instead of being silently swallowed.Test plan
pytest tests/unit/test_file_write_guardrails.py tests/unit/test_security_edge_cases.py tests/unit/agents/ tests/unit/test_replace_function_spans.py tests/unit/test_file_io_directory_path_errors.py: 1066 passed, 57 skipped. Covers the no-validator path, the per-file cap and the directory permissions.~/.gaia/cache/backups: none appeared after a run.git statusshows no.bakand the backup exists under~/.gaia/cache/backups/<repo path>/gaia eval agenthas not been run on this change (it touches the edit tools' behaviour)