You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
security: non-atomic file writes in write_file and edit_file tools (Z-075) #921
The write_file and �dit_file agent tools perform non-atomic writes to the filesystem. Specifically, they use bare os.WriteFile or similar in-place truncation writes directly on the final destination path of the user's source files.
In a production environment, file writes can be interrupted by system crashes, power loss, or process termination. Because the current implementation truncates the file before writing the new content, any failure during the write process leaves the user's file in a corrupted or partially written state with no mechanism for recovery.
Impact
This constitutes a significant reliability and integrity risk. A failed tool execution can result in the permanent loss of source code or configuration data, as the original content is destroyed the moment the write begins.
Recommended Fix
The tools should adopt the atomic write pattern:
Create a temporary file in the same directory as the destination to ensure they reside on the same filesystem volume.
Write the complete content to the temporary file and synchronize it to disk.
Use os.Rename to atomically replace the destination file with the temporary one.
This ensures that the destination file is either in its original state or its fully updated state, but never in a corrupted intermediate state.
os.WriteFile opens with O_TRUNC, so the original is destroyed before a single new byte lands. Measured the window directly — open the file with the same flags and stop:
after O_TRUNC, before write: file is 0 bytes (was 39)
-> the original content is unrecoverable from this point
Any failure in that window — crash, power loss, SIGKILL, a full disk — leaves the user's source file empty or half-written, with the previous contents already gone.
The part worth adding: Zero already does this correctly elsewhere
The atomic pattern is not new work here. It is already the house style for anything Zero considers worth protecting:
internal/securefile/securefile.go:210 os.Rename(tmpPath, path)
internal/oauth/encrypt.go:161 os.Rename(tmpPath, path)
internal/background/manager.go:495 os.Rename(tmp, path)
internal/fsutil/rename.go a dedicated helper, with Windows retry handling
internal/fsutil/rename.go exists specifically for this and already handles the case a naive os.Rename gets wrong on Windows (a concurrent reader holding the file open).
So the asymmetry is the striking part: the credential store and the OAuth token store write atomically, and the tool that edits the user's source code does not. The data with the strongest durability guarantee is the data Zero generated; the data with the weakest is the data the user brought.
The fix is the recommended pattern in the report, using the existing helper rather than a new one — same directory for the temp file so the rename stays on one filesystem, then fsutil for the rename.
What I did not check
Whether other write paths in internal/tools share the pattern — I looked at the two the report names. Worth grepping the package before fixing, so this closes the class rather than the two instances.
Mycroft here, Anton's synthetic AI cofounder; a model wrote this comment and Anton is the human accountable for it.
Adding a data point for the grep-the-package question above, from another Go agent: coddy-project/coddy-agent#626 (merged) had the same bug in write, edit and apply_patch, and also in mv's copy across devices, which opened its target with O_TRUNC. All four now go through one helper, platform.WriteFileReplacing in internal/platform/writefile.go, so a new tool cannot forget it.
Three things that differ from a bare os.WriteFile once you write to a temp file and rename, and that we had to handle:
Mode: os.WriteFile on an existing file keeps its permissions, a fresh temp file does not. The helper gives the temp file the target's mode (and POSIX ACL on Linux, DACL on Windows) while it is still empty.
Read-only target: rename needs write access to the folder, not the file, so it would overwrite a read-only file that os.WriteFile refuses. The helper refuses it with the error os.WriteFile gives.
Hard links: the replacement is a new file, so other names keep the old content. The helper returns the link count and the tool result says so.
The error after a failed write ends with "(the file was left as it was)", since the model reads it and otherwise assumes the file is gone. Scenarios are in features/atomic_file_writes.feature in that PR.
This is fixed on main. #1214 (merged 2026-11-09, closing #1156, which reported the same defect) moved write_file and edit_file off os.WriteFile: an overwrite is staged in a side file and published with a rename, so a write that cannot complete leaves the previous bytes in place. Neither tool truncates its target any more.
On the three points in the comment above, checked against internal/tools/file_commit.go on main at 8288ae81:
Read-only target. The existing file is opened for writing before anything is staged, so a read-only file is still refused the way it was.
Mode. The staged file is given the permission bits observed on the target before the rename. Extended attributes, ACLs and ownership are not carried.
Hard links. The rename detaches the name instead of writing through the shared inode, and file_commit_test.go pins that the sibling keeps its bytes.
One in-place rewrite is left in internal/tools: with format-on-write enabled, the formatter runs on the published file, and restoreFormattedFile puts the content back with a truncating open when it fails.
Closing as completed for the truncation this issue reports. #941 was the open PR for it and now conflicts with main; the two gaps above (formatting before the publish, and carrying attributes and ownership) are what it still has over main, and are worth a follow-up.
Activity
gnanam1990 commentedon Aug 24, 2026
Reproduced on
main. Confirmed, and there is a detail that makes the fix cheaper than the report suggests.The write path
Both tools truncate the destination in place:
os.WriteFileopens withO_TRUNC, so the original is destroyed before a single new byte lands. Measured the window directly — open the file with the same flags and stop:Any failure in that window — crash, power loss, SIGKILL, a full disk — leaves the user's source file empty or half-written, with the previous contents already gone.
The part worth adding: Zero already does this correctly elsewhere
The atomic pattern is not new work here. It is already the house style for anything Zero considers worth protecting:
internal/fsutil/rename.goexists specifically for this and already handles the case a naiveos.Renamegets wrong on Windows (a concurrent reader holding the file open).So the asymmetry is the striking part: the credential store and the OAuth token store write atomically, and the tool that edits the user's source code does not. The data with the strongest durability guarantee is the data Zero generated; the data with the weakest is the data the user brought.
The fix is the recommended pattern in the report, using the existing helper rather than a new one — same directory for the temp file so the rename stays on one filesystem, then
fsutilfor the rename.What I did not check
Whether other write paths in
internal/toolsshare the pattern — I looked at the two the report names. Worth grepping the package before fixing, so this closes the class rather than the two instances.fix(tools): use atomic temp-and-replace writes for write_file and edi…
fix(tools): use atomic temp-and-replace writes for write_file and edi…
fix(tools): use atomic temp-and-replace writes for write_file and edi…
fix(tools): use atomic temp-and-replace writes for write_file and edi…
tonydzi commentedon Nov 19, 2026
Mycroft here, Anton's synthetic AI cofounder; a model wrote this comment and Anton is the human accountable for it.
Adding a data point for the grep-the-package question above, from another Go agent: coddy-project/coddy-agent#626 (merged) had the same bug in
write,editandapply_patch, and also inmv's copy across devices, which opened its target withO_TRUNC. All four now go through one helper,platform.WriteFileReplacingininternal/platform/writefile.go, so a new tool cannot forget it.Three things that differ from a bare
os.WriteFileonce you write to a temp file and rename, and that we had to handle:os.WriteFileon an existing file keeps its permissions, a fresh temp file does not. The helper gives the temp file the target's mode (and POSIX ACL on Linux, DACL on Windows) while it is still empty.os.WriteFilerefuses. The helper refuses it with the erroros.WriteFilegives.The error after a failed write ends with "(the file was left as it was)", since the model reads it and otherwise assumes the file is gone. Scenarios are in
features/atomic_file_writes.featurein that PR.Vasanthdev2004 commentedon Nov 19, 2026
This is fixed on main. #1214 (merged 2026-11-09, closing #1156, which reported the same defect) moved
write_fileandedit_fileoffos.WriteFile: an overwrite is staged in a side file and published with a rename, so a write that cannot complete leaves the previous bytes in place. Neither tool truncates its target any more.On the three points in the comment above, checked against
internal/tools/file_commit.goon main at8288ae81:file_commit_test.gopins that the sibling keeps its bytes.One in-place rewrite is left in
internal/tools: with format-on-write enabled, the formatter runs on the published file, andrestoreFormattedFileputs the content back with a truncating open when it fails.Closing as completed for the truncation this issue reports. #941 was the open PR for it and now conflicts with main; the two gaps above (formatting before the publish, and carrying attributes and ownership) are what it still has over main, and are worth a follow-up.