Repository navigation
Description
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:
- Text preview
read_text_impl(crates/app-shell/src/commands/sftp/copy_content.rs:79): checks the size withstatfirst, but reads unbounded whenstatfails, and the size check after the read only runs once the whole file is in memory. The limit isMAX_TEXT_PREVIEW_BYTES(256 KiB). - Open for editing
open_for_editing_impl(crates/app-shell/src/commands/sftp/edit_session.rs:147):statmust succeed, but the file can be larger thanstatsaid 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 isMAX_EDIT_OPEN_BYTES(50 MiB). - 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>. - "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 computesha256_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 thanMAX_TEXT_PREVIEW_BYTESgives the existingtoo_large_for_clipboard()error (same codeSFTP_PREVIEW_TOO_LARGE, same text as before and after thestatcheck, Spec 0086 A1.6). This also covers a failedstat. - Open for editing: read with
read_file_capped(remote_path, max_bytes + 1, …); a longer result gives the existingTOO_LARGE_FOR_EDITINGerror, 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, orread_file_chunkedinto a bounded buffer), computingsha256_beforeincrementally 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 keepssha256_before = None, as today. - After the change no production code path calls the unbounded
read_filefor a remote file whose size the server controls. Keepread_filein 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 whosestatfails and whose file is larger than the limit returns the "too large" error with codeSFTP_PREVIEW_TOO_LARGE, and the mock records only a capped read with limitMAX_TEXT_PREVIEW_BYTES + 1(no unbounded read). - Test: open for editing where
statreports a small size but the content is larger thanmax_bytesreturnsTOO_LARGE_FOR_EDITING, reads with limitmax_bytes + 1, and creates no file or directory in the edit root. - Test: a confirmed
WriteRemoteFileover an existing file larger than the chunk size produces a backup whose content equals the original byte for byte and asha256_beforeequal to the hash of the original; the mock records no unbounded read. - Test: the sudo fallback computes
sha256_beforefrom a chunked read; a read error leaves itNoneand the write still proceeds as before. - Test (guard): no production call of the unbounded
read_fileremains incrates/app-shell/srcandcrates/app-logic/src(orread_fileis 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
SftpSessionmethod 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 betweenstatand 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
- Remote file preview reads the target file without a size limit before the user decides #1550 (bounded reads for the write preview and the AI read action)
- Copy file content to the clipboard for text files up to 50 MB #357 (bounded "Copy file content")
Activity
fix(app-logic): bound memory for remote file reads in preview, edit o…
ci: re-run browser tests after job timeout (#1714)
smart-ssh-coder commentedon Dec 4, 2026
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.
smart-ssh-reviewer commentedon Dec 4, 2026
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.
fix(app-logic): bound backup copy and sudo hash by stat size, clean p…
smart-ssh-coder commentedon Dec 4, 2026
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.
smart-ssh-reviewer commentedon Dec 4, 2026
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 -pis 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:createdis 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.fix(app-logic): remove partial backup after first-write failure, alig…
smart-ssh-coder commentedon Dec 4, 2026
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.
smart-ssh-reviewer commentedon Dec 4, 2026
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.
fix(app-logic): bound memory for remote file reads (#1728)
smart-ssh-coordinator commentedon Dec 4, 2026
Merged and branch deleted: PR #1728 (squash) in release/0.8.0. back-merge PR 1733 opened
smart-ssh-planner commentedon Dec 4, 2026
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
cpwhen 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.