Skip to content

Backup before a file write: create the backup exclusively and never remove a foreign file #1735

In review

Description

@smart-ssh-planner

Goal

The safety backup made before a confirmed remote file write never overwrites or deletes a file that already exists at the backup name, and never writes through a symlink placed there.

Context

Follow-up of #1714 (PR #1728): the review noted that copy_to_backup_inner (crates/app-logic/src/orchestration/remote_files.rs:370-413) creates the backup via write_range(backup, .., true), which in the SFTP implementation uses create() (crates/ssh-transport/src/sftp.rs:388-410, truncate-or-create, follows symlinks). Two consequences:

SftpSession::create_file_exclusive (crates/core/src/ssh/transport.rs, implemented with CREATE|EXCLUDE at crates/ssh-transport/src/sftp.rs:487-) already exists.

Requirements

  • Create the backup file with create_file_exclusive first; only when that succeeds is the backup marked as created (eligible for cleanup). Subsequent chunk writes use truncate = false.
  • If the exclusive create fails (name taken, symlink, permission), the write fails before the target is touched; nothing at the backup name is removed or modified.
  • A write failure after the exclusive create still removes the partial backup (existing behaviour, keep tests).
  • Spec 0020 §4.2.4: one sentence that an existing file at the backup name is never overwritten.

Acceptance Criteria

  • A pre-existing file at the backup name makes the write fail with an error; the pre-existing file is unchanged and not removed; the target is untouched.
  • A symlink at the backup name (mock/local transport) is not followed: its target is unchanged, and the symlink is not removed.
  • A failing first chunk write after a successful exclusive create removes the partial backup (existing test still passes).
  • An empty source file still gets an empty backup, created exclusively.
  • The mock SftpSession supports the new call sequence; existing backup tests are adapted without weakening them.
  • Relevant automated tests pass

Technical Notes

Security-adjacent file handling, but no filter/confirmation/redaction path changes. Threat model: no change expected (note it in the PR). Backup name collision within the same second now fails the write instead of overwriting; a clear error message is enough (no retry).

Files: crates/app-logic/src/orchestration/remote_files.rs, crates/app-logic/src/orchestration/action_exec/tests_activity.rs, crates/core/src/ssh/mock.rs, docs/specs/0020-sftp-file-transfer.md, changelog.d

Spec impact: update 0020

Open questions

  • On a name collision: fail the write (recommended, simplest and safe) or pick another timestamp/suffix and retry.

Related Work

Activity

  1. added
    follow-up
    on Dec 4, 2026
  2. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Needs maintainer: The issue waits for #1733, but "blocked by" could not be set. Set it on the issue (Relationships), then remove needs-maintainer: The code this issue changes (copy_to_backup_inner in remote_files.rs, from #1714/PR #1728) exists only on release/0.8.0. It is not on develop yet. It arrives with the open back-merge PR #1733.

  3. added
    needs-maintainer
    question-triage
    and removed
    question-triage
    needs-maintainer
    on Dec 4, 2026
  4. smart-ssh-coder commented on Dec 5, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1771. Backup is now created via create_file_exclusive before any write; collisions/symlinks fail the write without touching or removing the foreign file. Tests with counter-proof added, spec 0020 and changelog updated, CI green on PR #1771.

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

    follow-upsecurityssh

    Type

    Fields

    Priority

    P3 - Low

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions