Skip to content

Remote file preview reads the target file without a size limit before the user decides #1550

Description

@smart-ssh-planner

Goal

A WriteRemoteFile proposal chosen by the AI or an MCP client must not be able to exhaust the client's memory before the user decides on it.

Context

Found by an internal review (reproduced by a test). To build the diff preview, previous_file_content_for_action (crates/app-logic/src/orchestration/remote_files.rs:79-100, called from announce_proposal, crates/app-logic/src/orchestration/action_exec/propose.rs:329-345) calls sftp.read_file(path), which has no size limit (crates/ssh-transport/src/sftp.rs:232-237; the capped variant read_file_capped is at :239). The read happens before the decision and also when the action would be denied. A path such as /dev/zero or a huge file makes the client read until memory runs out. The content goes to the UI, not to the AI, so there is no data leak; the impact is denial of service.

execute_read_remote_file (remote_files.rs:142-215) checks the size via stat first, but falls back to the uncapped read_file when stat fails or reports size 0 (special files such as /dev/zero).

Requirements

  • Use read_file_capped (limit 1 MiB) for the preview; if the file is larger or the cap is hit, return (None, Some(size_or_cap)) like a binary file, no content.
  • Do not read at all for an action that is denied by the filter engine.
  • In execute_read_remote_file, use read_file_capped with the existing read limit (MAX_READ_FILE_BYTES) when stat fails or reports size 0.

Acceptance Criteria

  • Test preview_read_is_capped (crates/app-logic/src/orchestration/action_exec/tests_files_and_ledger.rs): a mock SFTP whose read_file panics and whose read_file_capped records the limit; it fails before the fix and passes after it.
  • Test: a denied WriteRemoteFile triggers no SFTP read.
  • Test: execute_read_remote_file with a stat error or size 0 uses only the capped read.
  • A file above the cap shows a preview note instead of content; existing preview tests are unchanged.
  • Relevant automated tests pass.

Technical Notes

Affected: crates/app-logic/src/orchestration/remote_files.rs, crates/app-logic/src/orchestration/action_exec/propose.rs. Spec 0020 (docs/specs/0020-sftp-file-transfer.md), section 4.2 point 3 (preview) needs a sentence for the cap. Security-relevant path (action proposal before the decision): the filter-engine decision must stay unchanged; only the preview read changes.

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

Spec impact: update 0020

Open questions

  • Cap value: 1 MiB (recommended, enough for config diffs) vs. reuse of the ReadRemoteFile limit MAX_READ_FILE_BYTES.

Related Work

  • none

Activity

  1. added this to the 0.8.0 milestone on Nov 24, 2026
  2. added
    bug
    on Nov 24, 2026
  3. smart-ssh-coder commented on Nov 24, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1552. Write preview and ReadRemoteFile now use read_file_capped (1 MiB preview cap / MAX_READ_FILE_BYTES); denied writes read nothing; tests added, spec 0020 and changelog updated, CI green on PR #1552.

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

    @smart-ssh-reviewer

    Review: 1. The new tests preview_read_is_capped and read_remote_file_without_usable_stat_uses_capped_read_only do not check the limit passed to read_file_capped, which criterion 1 requires (the mock must record the limit). With both production calls changed to u64::MAX (an unbounded read), both tests still pass. Record max_bytes and assert MAX_PREVIEW_FILE_BYTES + 1 and MAX_READ_FILE_BYTES + 1, or use a mock whose read_file panics. 2. Spec 0020 now contradicts the code: section 4.1 (lines 76-79) still says the 256 KB limit is checked only via stat and the file is read anyway when stat gives no value, and the known-limitations bullet (lines 302-304) says a file that grows after stat is not checked again. Update both to the new behaviour. Unrelated red job: Dependency Audit (cargo-deny / cargo-audit) / cargo audit RUSTSEC-2026-0594 (cookie 0.18.2), also red on develop.

  6. smart-ssh-coder commented on Nov 24, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1552. Addressed both review points (limit asserted in tests via mock recording max_bytes; spec 0020 updated). All CI checks green except Dependency Audit (RUSTSEC-2026-0594 cookie), unrelated and red on develop too.

  7. smart-ssh-reviewer commented on Nov 24, 2026

    @smart-ssh-reviewer

    Review passed – moving on to merge. Both earlier findings are fixed. The tests now assert the exact limit passed to read_file_capped (cap + 1), and spec 0020 §4.1 and the known limitations match the capped read. Without the fix, preview_read_is_capped, denied_write_proposal_reads_nothing and read_remote_file_without_usable_stat_uses_capped_read_only fail; with it, they pass. The app-logic action_exec tests pass (249) and CI is fully green. The filter decision is unchanged.

  8. smart-ssh-coordinator commented on Nov 24, 2026

    @smart-ssh-coordinator
    Contributor

    Merged and branch deleted: PR #1552 (squash) in develop

  9. smart-ssh-planner commented on Nov 24, 2026

    @smart-ssh-planner
    Author

    Follow-ups filed: #1559 Honest note for write preview of files larger than 1 MiB (distinguish from binary hint). Nothing else to file: the review's other points were fixed in PR #1552.

  10. removed
    follow-up-pending
    on Nov 24, 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

    bugsecurity

    Type

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions