Skip to content

Sudo fallback for a file write: do not copy a file larger than its reported size #1736

In review

Description

@smart-ssh-planner

Goal

The sudo fallback for a confirmed file write never copies a file that is larger than its reported size into a backup, so a root-only endless or fast-growing file cannot fill the server disk.

Context

Follow-up of #1714 (PR #1728): the regular write bounds its backup copy by the stat size, and the SHA-256 "before" hash in the sudo fallback is bounded too (hash_old_content, crates/app-logic/src/orchestration/remote_files.rs, ~line 405-). But the backup itself in write_via_sudo_fallback (~line 544) is sudo -S cp -p <path> <backup>, unbounded. Today hash_old_content returns None both on a read error (e.g. file not readable over SFTP, legitimate for root-only files) and when the file exceeded its stat size, so the caller cannot tell the cases apart.

Requirements

  • hash_old_content reports three outcomes: hash, unreadable (error), exceeded the stat size.
  • When the old file exceeded its stat size, write_via_sudo_fallback aborts with an error before running cp; the target is untouched and no backup is created.
  • On a read error the behaviour stays as today (sha256_before = None, cp still runs).
  • Spec 0020 §4.3.4: replace the "sudo copy is not limited" half-sentence with the new behaviour.

Acceptance Criteria

  • Mock: an old file yielding more bytes than its stat size makes the sudo fallback fail; no sudo cp command is executed and the target is not written.
  • A read error still lets the sudo fallback run with sha256_before = None (existing test passes).
  • A file that matches its stat size is copied and hashed as before.
  • Relevant automated tests pass

Technical Notes

Security-adjacent (privileged command path), but the command text and the confirmation flow stay unchanged; no loosening. Pseudo files with stat size 0 (e.g. under /proc) then fail in the sudo path too, same as in the regular path. A bounded copy via head -c is the alternative (not recommended: changes the privileged command and loses cp -p attributes).

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

Spec impact: update 0020

Open questions

  • Abort (recommended) or bounded copy with head -c <size+1> and a size check afterwards.

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 (hash_old_content, bounded backup from #1714/PR #1728) exists only on release/0.8.0 and is not on develop yet; remote_files.rs on develop still hashes inline. Waiting for the back-merge of release/0.8.0 into develop (#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 #1776. hash_old_content now reports hash/unreadable/exceeded; the sudo fallback aborts before sudo cp when the old file exceeds its stat size, read errors behave as before. Spec 0020 4.3.4 and a changelog fragment updated; tests added with counter-proof; CI green on PR #1776.

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