Repository navigation
fix(app-logic): bound memory for remote file reads - #1728
smart-ssh-coordinator[bot] merged 4 commits into
Conversation
…pen and write backup (#1714) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Review
Verdict: changes requested
Review level: elevated
Blocking
docs/specs/0086-sftp-limits-and-session-pairing.md:24-27(also §1 l.13, A1.2, A1.6, T3c/T5): the spec update is missing, and the spec now contradicts the code. The issue setsSpec impact: update 0086, but the PR body saysSpec: none. §2 still lists as a non-goal "Kein begrenztes Lesen für „Lokal öffnen“ und die Vorschau des Upload-Vergleichs: Eine Datei darf dort kurz ganz im Speicher liegen". After this PR both read at most limit + 1 byte (copy_content.rs:81-83,edit_session.rs:147-150). Fix:- Remove or rewrite that non-goal.
- A1.2: "Open locally" reads at most 50 MB + 1 byte, also when the file grew after the size query.
- A1.6: the upload-comparison preview reads at most 256 KB + 1 byte, also when the size query fails, with the same "too large" message.
- Adjust §1 ("sonst nach dem Lesen") accordingly.
- Add test cases for a failed or understated size query (T3c twin; T5 reads only limit + 1).
- Also add one sentence to Spec 0020 §4.2 point 4 on what happens when the backup copy fails or the old file does not end (see the next findings).
crates/app-logic/src/orchestration/remote_files.rs:352-377(copy_to_backup) and:471-482(sudo-fallback hash): the copy and the hash have no end condition other than EOF. The issue's goal names "a huge or growing file, a special file, or a server whosestatanswer does not match the content". Memory is now bounded, but a target that never ends still makes the action run forever. Examples: a confirmedWriteRemoteFileon a path that is a symlink to/dev/zero, a FIFO or device thatstatfinds (existed = true), a log that grows faster than it is copied, or a server that never reports EOF. Each turns "client out of memory" into "backup file grows until the server disk is full". The action hangs and holds the session's SFTP lock, so the file browser blocks too. This also applies to the local pseudo-server, where it fills the user's own disk. The sudo fallback'sread_file_chunked(..., Continue)hangs the same way. The caller already knows thestatsize (bytes_before,remote_files.rs:601). Fix:- Pass
bytes_beforeintocopy_to_backupand fail the write with an error once the copy reads past it. The target stays untouched. - In the sudo fallback, count the bytes in the closure and return
ChunkControl::Stoppast that size, sosha256_beforestaysNoneforStopped. - Add a test with a mock that keeps delivering data past the reported size.
- Pass
crates/app-logic/src/orchestration/remote_files.rs:362-372: a failed copy leaves a truncated backup under a name that says "backup". If a read or write fails on a later chunk (I/O error, disk full, a server failing the secondread_range),?returns after earlierwrite_rangecalls already went through. A partial<path>.smartssh-backup-<ts>stays on the server. On the base, a read error happened before any backup write, so no backup file existed. A user restoring from it gets a truncated file. The target is still untouched, which is correct. Fix:- On any error after the first
write_range, remove the backup as best effort (also on the abort from the previous finding). Alternatively, write to a temporary name and rename it once the copy is complete. - Add a test whose read fails on the second chunk. The existing
test_backup_read_error_leaves_the_target_untouchedonly fails at offset 0.
- On any error after the first
crates/app-shell/src/commands/sftp/size_limit_tests.rs:501-535(test_t5_…): AC 2 ("creates no file or directory in the edit root") is not proven for the grown-file case. The test callscreate_dir_all(&dir)before the call, so it cannot show that no directory is created. It also does not assert that no new file appeared there. It only checks that the earlier copy is unchanged. T4 checks!dir.exists(), but only whenstatalready reports too large. The production order is correct (the check runs beforeedit_session_dir_in), but the criterion asks for the test. Fix: add a variant without the pre-created directory that asserts!dir.exists()whenstatreports a small size and the content is overmax_bytes. Or assert that the directory holds only the earlier copy.crates/app-shell/src/commands/sftp/copy_content.rs:38-42,:70-73,:86-92, andcrates/app-shell/src/commands/sftp/edit_session.rs:156-159: the comments contradict the code. They still talk aboutread_file("die eigentlicheread_file-Fehlermeldung", "zwischenstatundread_file"). The comment atcopy_content.rs:89also says a failedstatleaves the file "bewusst ganz ungeprüft", but the read is now capped. Fix: nameread_file_capped. Say instead that a failedstatno longer matters for memory, because at most limit + 1 byte is read, and the check after the read decides.
Non-blocking
- PR body:
Spec: none (…)→Spec: update 0086(and 0020). changelog.d/issue-1714-bounded-remote-file-reads.md:2: "Text preview" is not a UI name. Suggested: "The upload comparison preview, "Open locally" and the backup before a confirmed file write no longer load a huge or growing remote file into memory."crates/app-logic/tests/no_unbounded_read_file.rs:1-4:#![allow(clippy::unwrap_used)]splits the module doc in two. The text search for.read_file(misses a call written asSftpSession::read_file(sftp, p). Adding::read_file(to the search would close that gap. Acceptable for a guard test.remote_files.rs:360-375: the loop checkschunk.is_empty()twice. "Write the first chunk, then break on empty" would read more easily. This is optional on a release branch.remote_files.rs:360:read_rangereopens the file for every chunk. If the file is replaced atomically (rename) during the copy, the backup mixes old and new content. The oldread_fileread from one handle. This is rare (see Follow-ups).- The empty commit
18167853(CI re-run) can be dropped at squash-merge.
Verified
- counter-proof: production files reset to
origin/release/0.8.0→ red:test_backup_of_a_multi_chunk_file…,test_backup_read_error…,test_sudo_fallback_hashes…,test_sudo_fallback_read_error…,size_limit_tests::test_t1/t2/t5,no_unbounded_read_file(reports exactly the four call sites the issue names); green again with the fix (executed).test_backup_of_an_empty_file…is green on the base too, as expected. - AC1 → met (
size_limit_tests.rs:test_t2_a_failing_stat_…, ops[stat, read_file_capped limit=MAX_TEXT_PREVIEW_BYTES+1]) (executed) - AC2 → partly met: error code and
limit+1read are checked; "no directory" is not covered for this case (see Blocking) (read + executed) - AC3 → met (2×256 KiB + 123 bytes, byte-exact backup,
sha256_before, noread_file) (executed) - AC4 → met (chunked hash; read error →
None, write proceeds) (executed) - AC5 → met; the only
.read_file(left outside tests in all crates is the elevated wrapperelevation/sftp_impl.rs:24(executed + read) - AC6/AC7 → met;
revocation_tests.rsunchanged and green. - edge cases: empty file → ok (empty backup) | exactly at the limit → ok (T3/T6) | chunk boundaries → ok |
max_bytes = u64::MAX→saturating_add, ok | revocation betweenstatand read on the elevated channel → ok (wrapper checks each op, T6b) | read error at a later chunk → partial backup (Blocking) | endless/growing/special file → endless copy (Blocking) - callers checked:
read_text_impl,open_for_editing_impl,write_via_sftp_with_backup,write_via_sudo_fallback(one caller each) → ok. ASftpPermissionDeniedfromread_rangestill leads to the sudo fallback. (read) - tests run locally:
cargo test --locked -q -p app-logic→ 1425 + 37 passed;-p app-shell→ 516 + 4 passed;-p ssh-manager-core --lib ssh::→ 80 passed (executed) - UI: not a UI change
- data: not touched
Timing analysis
- 3 test files analysed (5 new tests in
tests_activity.rs, T1/T2/T5 insize_limit_tests.rs, the guard test): no timing dependency found (mock-only, no sleeps/clocks/ports; path comparison is separator-safe).
Coherence
- comments: 4 places checked →
copy_content.rs:38-42, 70-73, 86-92,edit_session.rs:156-159blocking (see above); all other comments match. - fit: the change fits (256 KiB matches
TRANSFER_CHUNK_BYTES,read_range/write_rangeis the right pair for a copy, thelimit + 1pattern matchesremote_files.rs); loop readability is optional.
Spec and boundary
- visible behaviour changed: the preview and "Open locally" now read with a bound; the backup is copied in chunks (a partial backup can remain on failure); the sudo hash is chunked; messages, codes and limits are unchanged.
- spec searched (
smartssh-backup,Lokal öffnen,Upload-Vergleich,Speicher), spec covering it: 0086 §1, §2, A1.2, A1.3, A1.6, T3c, T5; 0020 §4.2 point 4, §4.3. - verdict: missing (blocking): update 0086, plus one sentence in 0020 §4.2.4.
- boundary, CI changes, vendor: ok; no
.github/changes; scope stays within the issue on the release branch; threat-model note "no change" holds.
Security checks
- Checked: filter/confirmation path unchanged, per-operation revocation (Spec 0085 A1.1) incl. each
read_range/write_rangechunk, no content reaches the AI context/logs (hash only), capped reads bounded in both transports (take),read_rangenever returns more thanlen. - security review (subagent): 8 bypass attempts; preview and edit-open blocked in all of them →
- endless backup copy / sudo hash on a never-ending target: blocking (confirmed by reading the code; no regular-file or size check in the caller)
- partial backup after a mid-copy error: blocking (confirmed)
- guard test only matches
.read_file(: non-blocking, unsure (no such call exists today)
UI review
- not a UI change
Follow-ups
- Backup copy via
read_rangereopens the file per chunk; an atomic replace during the copy yields a mixed backup. Consider reading the backup from one open handle (needs a trait method that copies with a single handle, or a size/mtime re-check after the copy). (no existing issue)
…artial backups (#1714) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Review points: 1 fixed in 3d4b57e (Spec 0086 and 0020 updated). 2 fixed in 3d4b57e (backup copy and sudo hash bounded by stat size, tests added). 3 fixed in 3d4b57e (partial backup removed best-effort, test on second chunk). 4 fixed in 3d4b57e (test_t5_a_grown_file_creates_no_edit_directory). 5 fixed in 3d4b57e (comments updated). |
There was a problem hiding this comment.
Review
Verdict: changes requested
Review level: elevated
Follow-up round: delta 18167853..3d4b57ed. Most of the earlier points are fixed. Four small items remain. Two are in the spec text, one is a gap in the new cleanup, and one is a comment.
Blocking
docs/specs/0020-sftp-file-transfer.md:114-117together with §4.3 point 4: the new sentence promises a size limit for every backup, but the sudo backup does not have one. §4.3 point 4 says the backup "aus 4.2, Punkt 4" is also made withsudo -S cp -pin the sudo case. On that pathremote_files.rs:538-544runscpon the server. Nothing limits that copy by the size checked before the write, and nothing removes a partial copy. Only the "before" hash is limited. Fix:- Limit the 4.2.4 sentence to the regular write, e.g. "Beim regulären Schreiben ist die Kopie …".
- Add a half-sentence to §4.3 point 4: the sudo copy is made on the server and is not limited by that size; only the stored SHA-256 "vorher" is, and it is missing when the file returns more bytes.
crates/app-logic/src/orchestration/remote_files.rs:398-403(copy_to_backup_inner): a failure on the first chunk still leaves a partial backup.createdis set only after the firstwrite_range(…, true)succeeds. The realwrite_range(crates/ssh-transport/src/sftp.rs:396-409) first callscreate(CREATE|TRUNC) and thenwrite_all. Scenario: the server disk is nearly full andcreatesucceeds, but writing the first 256 KiB chunk fails with ENOSPC. The action then fails and the target stays untouched, which is correct. But a truncated<path>.smartssh-backup-<ts>stays on the server and is not removed. That contradicts the new Spec 0020 sentence ("eine angefangene Sicherungskopie wird entfernt") and the earlier request ("remove the backup on any error"). The same happens with thewrite_range(backup, 0, &[], true)for an empty file. Fix:- Set
*created = truebefore each firstwrite_range(…, true)call, not after it. The name carries a timestamp, so removing it after a failed create only touches a file this action would have truncated anyway. - Add a test where the first backup
write_rangefails. For example, the mock could get a write-failure switch for a path prefix.
- Set
crates/app-logic/src/orchestration/remote_files.rs:527-528: the comment contradicts the code. It says the "before" hash exists whenever the old content is readable, and isNoneonly when the read is denied or fails. Since this change,hash_old_contentalso returnsNonewhen the file returns more bytes thanstatreported. Fixed comment:// "Before" hash: only when the old content is readable over SFTP (e.g. a 0644 root file) and no larger than its stat size; None otherwise.docs/specs/0086-sftp-limits-and-session-pairing.md:13-15(§1) and:165(T3c): these were requested last round and are still open.- §1 still says „Lokal öffnen“ rejects "vor dem Lesen, wenn die Größe schon bekannt ist, sonst nach dem Lesen". A1.2, however, says a failed size query aborts the command, and the read stops at 50 MB + 1 byte. Suggested: "… vor dem Lesen nach der gemeldeten Größe, spätestens nach 50 MB + 1 gelesenem Byte, falls die Datei größer ist als gemeldet."
- T3c needs the twin for a failed or too-small size query in the upload-comparison preview: "lehnt über 256 KB ab, auch wenn
statscheitert oder zu wenig meldet; gelesen werden höchstens 256 KB + 1 Byte". The code andtest_t2_a_failing_stat_…already cover it. - Optionally, name A1.6 next to A1.1 and A1.2 in the §2 non-goal.
Non-blocking
changelog.d/issue-1714-bounded-remote-file-reads.md:2: "Text preview" is not a UI name (raised last round). Suggested: "The upload comparison preview, "Open locally" and the backup before a confirmed file write no longer load a huge or growing remote file into memory."tests_activity.rs:test_backup_of_a_file_larger_than_its_stat_size_…uses a reported size of 0. The overflow is therefore caught before any backup exists. A variant withwith_reported_size(path, 256 * 1024)would cover "partial copy, then the probe finds more bytes, then the backup is removed". The code handles that case already (read).- Behaviour change: a file whose
statsize is 0 but which returns content (procfs/sysfs/some FUSE files) can no longer be written by the AI write action over SFTP. The backup now fails with "larger than its reported size". This is fail-closed and matches the issue goal. The new Spec 0020 sentence covers it. remote_files.rs:511: the#[allow(clippy::too_many_arguments)]has no reason. Next to it,execute_write_remote_fileuses#[expect(…, reason = …)].- PR body: once the spec fix above is in, also name Spec 0020 §4.3.
Verified
- counter-proof (executed, then reverted, worktree clean):
- Disabling the probe check turns
test_backup_of_a_file_larger_than_its_stat_size…red. - Removing
sftp.remove(backup)turnstest_backup_failing_on_a_later_chunk…red. - Disabling the
total > sizecheck turnshash_tests::test_hash_old_content_is_bounded_by_the_stat_sizered. - Creating the edit dir before the post-read check turns
test_t5_a_grown_file_creates_no_edit_directoryred. - All four are green with the fix.
- Disabling the probe check turns
- AC2 → met:
size_limit_tests.rs:test_t5_a_grown_file_creates_no_edit_directoryasserts!dir.exists()(executed). - AC3/AC4 → met, unchanged since last round. The new stat-size bound is added on top (executed).
- edge cases (read + executed):
- exactly at the stat size → ok
- file shrinks after
stat→ ok (backup and hash cover what is there) - empty file → ok (empty backup)
- stat size 0 with content → fails closed
bytes_beforeNonewhileexisted→ impossible (samestatmatch),unwrap_or(0)is never reached- probe read error after a partial copy → backup removed
- first-chunk write failure → partial backup left (Blocking)
- callers checked:
copy_to_backup,write_via_sftp_with_backup,write_via_sudo_fallback,hash_old_content(1 call site each) → ok. ASftpPermissionDeniedon a later chunk still leads to the sudo fallback. The overflow error is aChannelError, so it does not (read). - tests run locally (executed):
cargo test --locked -q -p app-logic→ 1428 + 37 passed-p app-shell→ 517 + 4 passed-p ssh-manager-core ssh::→ 80 passed- clippy
-D warnings,cargo fmt --all --checkandcheck-file-size→ clean
- UI: not a UI change
- data: not touched
- CI: all checks green on
3d4b57ed.
Timing analysis
- 3 test files analysed (two new backup tests, the new T5 variant,
hash_tests, mock helpers): no timing dependency found (mock-only, counters under a mutex, no sleeps or clocks).
Coherence
- comments:
- The earlier findings in
copy_content.rsandedit_session.rsare fixed. - The new comments on
copy_to_backup,hash_old_contentand the mock helpers match the code. remote_files.rs:527-528is blocking (see above).
- The earlier findings in
- fit: the double
is_emptycheck is gone. Threadingold_sizenext toexisted(both come from the samestat) needs the clippy allow and the impossibleunwrap_or(0)→ follow-up (release base).
Spec and boundary
- visible behaviour changed:
- The preview and "Open locally" read at most limit + 1 byte.
- The backup fails the write when the file returns more than its
statsize, and a partial backup is removed. - The sudo "before" hash is missing for such a file.
- spec searched (
Lokal öffnen,Upload-Vergleich,Sicherungskopie,smartssh-backup,Vorschau), spec covering it: 0086 §1, §2, A1.2, A1.6, T3c, T5; 0020 §4.2.4, §4.3.4; 0057 (activity entry; optionally note that the SHA-256 "vorher" can be missing). - verdict:
- Mostly present: the §2 non-goal, A1.2, A1.6, T5 and 0020 §4.2.4 are updated.
- 0020 overclaims for the sudo path (blocking).
- 0086 §1 and T3c are still open (blocking).
- boundary, CI changes, vendor: ok. No
.github/changes, and the scope stays within the issue on the release branch. The threat-model note "no change" holds.
Security checks
- Checked:
- The regular write path fails closed: no target write after any backup error.
- The bound on the backup copy and on the sudo hash.
- The cleanup scope:
removeonly unlinks the timestamped name, never a symlink's target. - No new hang while the SFTP lock is held.
- Per-operation revocation: not touched, because this path uses the normal channel.
- security review (subagent): 8 bypass attempts (/dev/zero symlink, growing log, FIFO, shrinking file, mid-copy error, oversized file in the sudo path, existing file or symlink at the backup path) →
- first-chunk create-then-fail leaves a partial backup: blocking (confirmed by reading
sftp.rs:396-409) - the sudo fallback still runs an unbounded
sudo cp -pfor a root-only, never-ending file: non-blocking, unsure (pre-existing, not new in this PR) → Follow-ups - the guard test only matches
.read_file(: non-blocking, unsure (unchanged)
- first-chunk create-then-fail leaves a partial backup: blocking (confirmed by reading
UI review
- not a UI change
Previous findings
- Spec 0086/0020 not updated → mostly fixed in
3d4b57ed; §1 and T3c still open, and 0020 overclaims for sudo (see Blocking). - Endless backup copy / sudo hash → fixed in
3d4b57ed. - Partial backup after a mid-copy error → fixed for later chunks in
3d4b57ed; still open for the first chunk (see Blocking). - AC2 test without the pre-created dir → fixed in
3d4b57ed. - Stale comments in
copy_content.rs/edit_session.rs→ fixed in3d4b57ed.
Follow-ups
- The sudo fallback copies the old file with an unbounded
sudo -S cp -p. A root-only target that never ends (for example a symlink to an endless source) can fill the server disk. Possible fixes: abort beforecpwhen the hash stopped because the file exceeded itsstatsize, or use a bounded copy. (no existing issue) - The backup copy via
read_rangereopens the file for each chunk. An atomic replace during the copy yields a mixed backup. Fix: read the backup from one open handle, or re-check size and mtime afterwards. (from last round; no existing issue) write_via_sftp_with_backup/write_via_sudo_fallback: pass onlyold_size: Option<u64>(or anOldFile { mode, size }from the stat) instead ofexistedplusold_size. This drops the clippy allow and the impossibleunwrap_or(0).- The guard test
no_unbounded_read_file.rscould also match::read_file(. - Writing to pseudo files with a
statsize of 0 through the AI write action now fails at the backup step. Consider a clearer user-facing message.
…n specs (#1714) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Review points: 1. fixed in f301cc0 (§4.2.4 limited to regular write; §4.3.4 notes the unbounded sudo copy). 2. fixed in f301cc0 ( |
There was a problem hiding this comment.
Review
Verdict: approved
Review level: elevated
Follow-up round: delta 3d4b57ed..f301cc02. All blocking points from the last round are fixed. What remains is non-blocking.
Blocking
- None.
Non-blocking
docs/specs/0086-sftp-limits-and-session-pairing.md:12-14(§1): the spec review flagged „sonst“ as blocking. I am overruling that, and here is why. The sentence reads "vor dem Lesen, wenn die Größe bekannt ist; sonst oder bei zu klein gemeldeter Größe werden höchstens 50 MB + 1 Byte gelesen". „Sonst“ refers to a case where "Open locally" never reads anything: a failed size query aborts the command (edit_session.rs:138, A1.2). "Höchstens 50 MB + 1 Byte" therefore still holds. The normative A1.2 a few lines below states the abort correctly. Still, the summary is misleading. Clearer wording: „„Lokal öffnen“ lehnt Dateien über 50 MB vor dem Lesen anhand der Größenabfrage ab; meldet sie zu wenig, wird nach höchstens 50 MB + 1 Byte abgelehnt. Scheitert die Größenabfrage, bricht der Befehl ab.“crates/core/src/ssh/mock.rs:119-120: the doc says a matchingwrite_range"creates or truncates the file … and then fails". It only does that whentruncateis true; a later-chunk write just fails. Suggested: "fails (e.g. ENOSPC) after the truncating open has already created/truncated the file, as a real server does." The newInnerfieldwrite_range_fails_containing(:64) has no doc comment, unlike its neighbours.- Tests: no test covers a failing empty-file backup write (
remote_files.rs:408-412). The code is correct, sincecreatedis set first. The oversize test still uses a reported size of 0, so "partial multi-chunk copy, then the probe finds more bytes, then the backup is removed" has no test (the code handles it). Both were left open without a stated reason. remote_files.rs:401-405:creatednow means "a (partial) backup may exist", sobackup_may_existwould be a clearer name. Optional on the release branch.- PR body: the
Spec:line should also name 0086 §1/T3d and 0020 §4.3 point 4. The Summary still says "Text preview". - Spec 0086 §2 non-goal: it could name A1.6 next to A1.1/A1.2 (optional, raised before).
Verified
- previous blocking finding (partial backup after a first-write failure) → fixed.
*created = trueis now set before every truncatingwrite_range, in both the chunk loop and the empty-file branch (read:remote_files.rs:401-412). - mock realism:
write_range(.., truncate=true)inserts the empty file before the new failure switch applies, so the new test is meaningful. The field defaults toNone, so other mock users are unchanged (read:crates/core/src/ssh/mock.rs:393-411). - counter-proof:
remote_files.rsrestored from3d4b57ed→test_backup_failing_on_the_first_write_removes_the_partial_backupred (backup path left behind). Green with the fix; worktree clean afterwards (executed). - edge cases (read):
- create fails before the file exists →
removeof a missing path, error ignored, original error returned → ok - empty file with a failing write → cleanup runs → ok (no test)
- failure on a later chunk → ok (existing test)
- file larger than its stat size → ok
SftpPermissionDeniedstill reaches the sudo fallback; the returned error is unchanged → ok
- create fails before the file exists →
- callers checked:
copy_to_backup(1 call site) → ok;with_write_range_failure_containing(1 call site, the new test) → ok (read) - tests run locally (executed):
cargo test --locked -q -p app-logic→ 1429 passed, plus integration and doc tests, 0 failedcargo test --locked -q -p ssh-manager-core→ 935 passed, plus integration tests, 0 failed
- UI: not a UI change
- data: not touched
- CI: all checks green on
f301cc02, including Test on all three OSes, Browser Tests, Dependency Audit and threat-model-note.
Timing analysis
- 1 test file (plus the mock) analysed: no timing dependency found.
approve_first_proposed_actionwaits for the proposed event, and the confirmation is registered before that event. The mock is deterministic, and the asserts run afterpropose(including cleanup) has finished.
Coherence
- comments: 3 checked.
remote_files.rs:401-402and:531-533match the code; the earlier finding at:527-528is fixed.mock.rs:119-120is imprecise → non-blocking. It is test-helper documentation and correct for the first-chunk case it was written for. - fit: the change fits. The
created→backup_may_existrename is a follow-up (release base).
Spec and boundary
- visible behaviour changed:
- The preview and "Open locally" read at most limit + 1 byte.
- The regular backup is bounded by the
statsize, and a partial backup is now also removed after a first-chunk failure. - The sudo "before" hash is missing for a file larger than its reported size.
- spec searched (
Lokal öffnen,Sicherungskopie,Backup,Upload-Vergleich,50 MB), spec covering it: 0086 §1, §2, A1.2, A1.6, T3d, T5; 0020 §4.2 point 4, §4.3 point 4. - verdict: spec change present and matches the code. Details:
- 0020 §4.2.4 is now limited to the regular write.
- 0020 §4.3.4 states that the sudo copy is unbounded.
- 0086 T3d is added.
- 0086 §1 is imprecise, see Non-blocking (spec review's blocking finding overruled with the reason given there).
- boundary, CI changes, vendor: ok. No
.github/changes, and the scope stays within the issue on the release branch. The changelog fragment is fixed ("upload comparison preview"). The threat-model note "no change" holds.
Security checks
- Checked:
- The regular write fails closed before the target is written: a backup error returns before
write_file(path, ..). - Non-permission errors do not reach the sudo fallback.
- Cleanup removes a symlink at the backup name, never its target.
- No unredacted content in errors or logs.
- Nothing was loosened against
3d4b57edor the base, apart from the narrow possible case below.
- The regular write fails closed before the target is written: a backup error returns before
- security review (subagent): 7 bypass attempts (ENOSPC on the first chunk, EACCES on an existing backup name, planted symlink, directory at the backup name, empty file plus write failure, oversized source, permission denied leading to the sudo fallback) →
- first-chunk partial backup: fixed (confirmed)
remote_files.rs:401-405: a foreign, non-writable file that already sits at exactly<path>.smartssh-backup-<same second>in a writable, non-sticky directory makescreatefail, butcreatedis already true, so the cleanup deletes that file. The base left it alone. This needs a planted file or a same-second collision → non-blocking, unsure → Follow-ups- pre-existing, not new: a symlink planted at the predictable backup name makes
create()follow it and overwrite its target. The base behaved the same → non-blocking, unsure → Follow-ups - earlier notes (unbounded
sudo cp -p, the guard test matching only.read_file() are unchanged → Follow-ups
UI review
- not a UI change
Previous findings
- Spec 0020 overclaims for the sudo path → fixed in
f301cc02(§4.2.4 limited to the regular write, §4.3.4 half-sentence added). - Partial backup after a first-chunk or empty-file write failure → fixed in
f301cc02(new test with counter-proof). - Sudo "before" hash comment at
remote_files.rs:527-528→ fixed inf301cc02. - Spec 0086 §1 and T3c twin → T3d added (fixed). §1 is rewritten but still imprecise → accepted as non-blocking (see above).
- Changelog "Text preview" → fixed.
Follow-ups
- Backup creation: open the backup with
CREATE|EXCLUDE(already used inssh-transport/src/sftp.rs:488) and mark it for cleanup only after that open succeeds. This closes both the deletion of a pre-existing foreign file at the backup name and writing through a planted symlink at the predictable name. (no existing issue) - The sudo fallback copies the old file with an unbounded
sudo -S cp -p; a root-only target that never ends can fill the server disk. Abort beforecpwhen the hash stopped because the file exceeded itsstatsize, or use a bounded copy. (no existing issue) - The backup copy via
read_rangereopens the file for each chunk; an atomic replace during the copy yields a mixed backup. Read from one open handle, or re-check size and mtime afterwards. (no existing issue) - Spec 0086 §1: wording as suggested under Non-blocking.
- Tests: a failing empty-file backup write; an oversize case with a reported size of 256 KiB (partial copy, then probe, then removal).
write_via_sftp_with_backup/write_via_sudo_fallback: passold_size: Option<u64>instead ofexistedplusold_size, and renamecreatedtobackup_may_exist.- The guard test
no_unbounded_read_file.rscould also match::read_file(. - Writing to pseudo files with a
statsize of 0 through the AI write action now fails at the backup step. Consider a clearer user-facing message.
Closes #1714
Summary
read_file_capped(limit + 1); the existing "too large" errors/codes are returned for a longer result (also whenstatfails or lied).read_range+write_range), hashing incrementally. The copy is bounded by thestatsize (a file yielding more bytes fails the write); any read/write error fails the write before the target is touched and removes the partial backup best-effort.read_file_chunked, bounded by thestatsize; a read error or a larger file keepssha256_before = None(remote_files.rs:hash_tests::test_hash_old_content_is_bounded_by_the_stat_size).tests_activity.rs:test_backup_of_a_file_larger_than_its_stat_size_fails_and_leaves_no_backup,test_backup_failing_on_a_later_chunk_removes_the_partial_backup(counter-proof: with the removal disabled it fails),size_limit_tests.rs:test_t5_a_grown_file_creates_no_edit_directory..read_file(call in production code of app-logic / app-shell (only the elevated wrapper forwards it).Acceptance Criteria
stat: met —size_limit_tests.rs:test_t2_a_failing_stat_no_longer_lets_an_oversized_file_through(asserts only a capped read with limitMAX_TEXT_PREVIEW_BYTES + 1).size_limit_tests.rs:test_t5_a_file_that_grew_between_stat_and_read_leaves_no_local_copy(limit + 1, existing local copy untouched, no dir created).tests_activity.rs:test_backup_of_a_multi_chunk_file_is_exact_and_read_in_bounded_pieces(plus empty file and read-error tests).tests_activity.rs:test_sudo_fallback_hashes_the_old_file_from_a_chunked_read,test_sudo_fallback_read_error_leaves_the_hash_none_and_still_writes.crates/app-logic/tests/no_unbounded_read_file.rs.read_fileto the capped read.Tests
cargo test -p app-logic,-p app-shell,-p ssh-manager-core, clippy all green locally. No counter-proof run (the old code path called unboundedread_file, which the updated mock expectations and the guard test would flag).Assumptions
read_filestays in the trait: it is still used by transport tests and the local/elevated wrappers; removing it would touch many mocks.with_read_failure(permission-style error) to test the read-error cases.Spec: updated 0086 (§2 non-goal, A1.2, A1.6, T5) and 0020 (§4.2 point 4: bounded backup copy).
Threat model: no change – only how existing file content is read changes, no new boundary.
Manual test steps
None needed.
Follow-ups
None.
🤖 Generated with Claude Code