Skip to content

security: non-atomic file writes in write_file and edit_file tools (Z-075) #921

Closed

Description

@PierrunoYT

Description

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:

  1. Create a temporary file in the same directory as the destination to ensure they reside on the same filesystem volume.
  2. Write the complete content to the temporary file and synchronize it to disk.
  3. 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.

Activity

  1. gnanam1990 commented on Aug 24, 2026

    @gnanam1990
    Collaborator

    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:

    internal/tools/write_file.go:110   os.WriteFile(absolutePath, []byte(content), 0o644)
    internal/tools/edit_file.go:156    os.WriteFile(absolutePath, []byte(updated), 0o644)
    

    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.

  2. added
    issue-approved
    on Aug 24, 2026
  3. added a commit that references this issue on Aug 29, 2026
    0030078
  4. added 3 commits that reference this issue on Sep 12, 2026
    f794fc3
    36e9622
    88efebb
  5. tonydzi commented on Nov 19, 2026

    @tonydzi

    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.

  6. Vasanthdev2004 commented on Nov 19, 2026

    @Vasanthdev2004
    Collaborator

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue-approved

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions