Skip to content

Bound memory for remote file reads in text preview, open for editing and the backup before a file write #1714

Done

Description

@smart-ssh-planner

Goal

No remote file read keeps more than a fixed amount of the file in memory, whatever size the server reports or delivers. A huge or growing file, a special file, or a server whose stat answer does not match the content can no longer make the client read until memory runs out.

Context

read_file (crates/ssh-transport/src/sftp.rs:232) reads a whole file into one buffer without a limit. Most callers already use the bounded read_file_capped (crates/core/src/ssh/transport.rs:273-285) or the chunked reads, e.g. "Copy file content" (crates/app-shell/src/commands/sftp/copy_content.rs:256), the write preview and the AI read action (crates/app-logic/src/orchestration/remote_files.rs:104,228, issue #1550). Four production paths still call the unbounded read_file:

  1. Text preview read_text_impl (crates/app-shell/src/commands/sftp/copy_content.rs:79): checks the size with stat first, but reads unbounded when stat fails, and the size check after the read only runs once the whole file is in memory. The limit is MAX_TEXT_PREVIEW_BYTES (256 KiB).
  2. Open for editing open_for_editing_impl (crates/app-shell/src/commands/sftp/edit_session.rs:147): stat must succeed, but the file can be larger than stat said by the time it is read (the channel lock is taken per operation, Spec 0086 A1.2/A1.3), and the check after the read again comes after the full read. The limit is MAX_EDIT_OPEN_BYTES (50 MiB).
  3. Backup before a confirmed file write write_via_sftp_with_backup (crates/app-logic/src/orchestration/remote_files.rs:366): reads the whole existing file into memory to hash it and write it to <path>.smartssh-backup-<timestamp>.
  4. "Before" hash in the sudo fallback write_via_sudo_fallback (crates/app-logic/src/orchestration/remote_files.rs:439): reads the whole existing file only to compute sha256_before.

The trait already offers bounded alternatives: read_file_capped, read_file_chunked and read_range / write_range (crates/core/src/ssh/transport.rs:280-330).

Specs: Spec 0086 (A1.1–A1.6, size limits in the file browser), Spec 0020 §4.2 point 4 (backup before overwrite) and §4.3 (sudo fallback).

Requirements

  • Text preview: read with read_file_capped(path, MAX_TEXT_PREVIEW_BYTES + 1, …); a result longer than MAX_TEXT_PREVIEW_BYTES gives the existing too_large_for_clipboard() error (same code SFTP_PREVIEW_TOO_LARGE, same text as before and after the stat check, Spec 0086 A1.6). This also covers a failed stat.
  • Open for editing: read with read_file_capped(remote_path, max_bytes + 1, …); a longer result gives the existing TOO_LARGE_FOR_EDITING error, and as before no local file or directory is created (Spec 0086 A1.3).
  • Backup before a write: copy the old file to the backup path in bounded chunks (e.g. read_range + write_range, or read_file_chunked into a bounded buffer), computing sha256_before incrementally over the same bytes. Memory use is bounded by the chunk size, not the file size. The backup content and the backup path naming are unchanged; a read or write error during the copy fails the write action before the target is touched, exactly as today.
  • Sudo fallback "before" hash: compute it incrementally from a chunked read (read_file_chunked); a read error keeps sha256_before = None, as today.
  • After the change no production code path calls the unbounded read_file for a remote file whose size the server controls. Keep read_file in the trait only if tests or local-only callers still need it; otherwise remove it from the trait, the elevated wrapper (crates/app-shell/src/commands/elevation/sftp_impl.rs:23) and the mocks.

Acceptance Criteria

  • Test (in crates/app-shell/src/commands/sftp/size_limit_tests.rs): text preview with a mock whose stat fails and whose file is larger than the limit returns the "too large" error with code SFTP_PREVIEW_TOO_LARGE, and the mock records only a capped read with limit MAX_TEXT_PREVIEW_BYTES + 1 (no unbounded read).
  • Test: open for editing where stat reports a small size but the content is larger than max_bytes returns TOO_LARGE_FOR_EDITING, reads with limit max_bytes + 1, and creates no file or directory in the edit root.
  • Test: a confirmed WriteRemoteFile over an existing file larger than the chunk size produces a backup whose content equals the original byte for byte and a sha256_before equal to the hash of the original; the mock records no unbounded read.
  • Test: the sudo fallback computes sha256_before from a chunked read; a read error leaves it None and the write still proceeds as before.
  • Test (guard): no production call of the unbounded read_file remains in crates/app-shell/src and crates/app-logic/src (or read_file is removed from the trait).
  • Existing size limit, revocation and file write tests pass unchanged.
  • Relevant automated tests pass.

Technical Notes

  • File browser commands are manual user actions and must not run through AI or filter code (Spec 0054 security model, see the doc comment on MAX_TEXT_PREVIEW_BYTES); keep the limits as separate constants as today.
  • The elevated file browser channel wraps every SftpSession method with a revocation check (crates/app-shell/src/commands/elevation/sftp_impl.rs); the bounded methods are already wrapped. Keep the per-operation revocation behaviour of Spec 0085 A1.1 (a revocation between stat and the read leaves the file unread).
  • The file write path runs after the user confirmed the action; the filter engine decision and the confirmation flow stay unchanged. Only how the old content is read for backup and hash changes.
  • No data model change.

Files: crates/app-shell/src/commands/sftp/copy_content.rs, crates/app-shell/src/commands/sftp/edit_session.rs, crates/app-shell/src/commands/sftp/size_limit_tests.rs, crates/app-logic/src/orchestration/remote_files.rs, crates/app-logic/src/orchestration/action_exec/, crates/core/src/ssh/transport.rs, crates/core/src/ssh/mock.rs, crates/app-shell/src/commands/elevation/sftp_impl.rs, docs/specs/0086-sftp-limits-and-session-pairing.md, docs/specs/0020-sftp-file-transfer.md

Spec impact: update 0086

Open questions

  • Chunk size for the backup copy: reuse an existing transfer chunk constant (recommended) vs. a new constant. Pick whichever the download/upload code already uses.

Related Work

Activity

  1. added this to the 0.8.1 milestone on Dec 4, 2026
  2. added
    bug
    hotfix
    on Dec 4, 2026
  3. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1728. The red Browser Tests job was a runner timeout, not a test failure: the job was cancelled at its 30-minute limit with no failing test (run 47388404864, 876 of 936 tests passed), the PR touches no frontend code, and the same job took 28 min on the base (run 47368547038). Re-triggered with an empty commit; CI is now fully green on the new head.

  4. added
    follow-up-pending
    on Dec 4, 2026
  5. smart-ssh-reviewer commented on Dec 4, 2026

    @smart-ssh-reviewer

    Review: 1. Spec 0086 not updated (issue: Spec impact update 0086): §2 non-goal 'no bounded read for Open locally / preview' now contradicts the code; update §1, §2, A1.2, A1.6 and test cases, plus one sentence in Spec 0020 §4.2.4 on a failed backup copy. 2. copy_to_backup and the sudo-fallback hash run until EOF with no other bound: a symlink to /dev/zero, a FIFO/device, a fast-growing log or a server that never reports EOF fills the server disk and hangs the action while holding the SFTP lock. Bound both by the stat size (bytes_before): fail the write, or stop the hash and leave it None; add a test. 3. A read/write error on a later chunk leaves a truncated .smartssh-backup-: remove it best-effort (or write to a temp name and rename); add a test failing on the second chunk. 4. AC2: test_t5 pre-creates the edit dir, so 'no file or directory in the edit root' is not proven for the grown-file case; add a variant asserting !dir.exists(). 5. Stale comments still name read_file / say a failed stat leaves the file unchecked: copy_content.rs:38-42, 70-73, 86-92; edit_session.rs:156-159.

  6. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1728. Addressed all review points: backup copy and sudo hash are now bounded by the stat size (grown/endless files fail the write or leave the hash None), partial backups are removed best-effort, Specs 0086 and 0020 updated, extra tests added (incl. no edit dir for a grown file), stale comments fixed. CI green.

  7. smart-ssh-reviewer commented on Dec 4, 2026

    @smart-ssh-reviewer

    Review: 1. Spec 0020 §4.2.4: the new sentence promises a size-limited backup with cleanup for every write, but the sudo fallback's sudo cp -p is unbounded. Limit the sentence to the regular write and add a half-sentence to §4.3.4: the sudo copy is not limited, and only the SHA-256 'before' is bounded and is missing for a larger file. 2. remote_files.rs copy_to_backup_inner: created is set only after the first write_range succeeds, but write_range creates/truncates the file before writing. A failure on the first chunk (e.g. ENOSPC), or on the empty-file write, leaves a partial backup. Set created = true before the first write_range(…, true) and add a test where the first backup write fails. 3. remote_files.rs:527-528: the comment says the sudo 'before' hash is None only on a read error; it is now also None when the file is larger than its stat size. Fix the comment. 4. Spec 0086, still open from last round: fix §1 ('sonst nach dem Lesen' contradicts A1.2; say at most 50 MB + 1 byte is read) and add the T3c twin (failed or understated size query in the upload-comparison preview, at most 256 KB + 1 byte read). Non-blocking: rename 'Text preview' in the changelog fragment to the upload comparison preview.

  8. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1728. Review round fixed: backup marked created before first write (new test with counter-proof), spec 0020/0086 wording and T3d, comment and changelog fixes. CI green.

  9. smart-ssh-reviewer commented on Dec 4, 2026

    @smart-ssh-reviewer

    Review passed – moving on to merge. Approved. All blocking points from the last round are fixed: a partial backup is now also removed when the first backup write fails (new test, counter-proof run), Spec 0020 §4.2.4/§4.3.4 match the sudo path, Spec 0086 has T3d, and the comment and changelog are corrected. CI is green. Non-blocking: Spec 0086 §1 wording is still imprecise for a failed size query (A1.2 is correct). Follow-ups, kept under follow-up-pending: open the backup with CREATE|EXCLUDE (a planted symlink or a pre-existing file at the backup name), the unbounded sudo cp -p, per-chunk reopen during the backup copy, two missing edge-case tests, and small refactors.

  10. smart-ssh-coordinator commented on Dec 4, 2026

    @smart-ssh-coordinator
    Contributor

    Merged and branch deleted: PR #1728 (squash) in release/0.8.0. back-merge PR 1733 opened

  11. smart-ssh-planner commented on Dec 4, 2026

    @smart-ssh-planner
    Author

    Follow-ups filed: #1735 Create the backup exclusively (CREATE|EXCLUDE) so a foreign file or symlink at the backup name is never removed or written through; #1736 Sudo fallback: abort before cp when the old file exceeds its stat size; #1737 Fail the backup when the source changes during the chunked copy; #1738 Remaining tests, guard-test and Spec 0086 wording gaps, plus a clearer message for pseudo files with stat size 0. The optional refactor (old_size/created rename) is folded into #1738 as optional.

  12. removed
    follow-up-pending
    on Dec 4, 2026
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

    bughotfixsecurityssh

    Type

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions