Skip to content

SSH transport: refused or forced shell and exec requests fail fast instead of hanging #1665

Description

@smart-ssh-planner

Goal

Opening a shell or running a command on a server that refuses it, or that forces SFTP on every channel, fails within seconds with a clear error instead of hanging.

Context

  • RusshTransport::open_shell (crates/ssh-transport/src/transport.rs:228-271) waits for the PTY reply (:248-264) but sends request_shell(true) (:267) without waiting for the reply. A refusal arrives later as ChannelMsg::Failure, which RusshShell::read (crates/ssh-transport/src/shell.rs:31-43) skips, so the terminal hangs silently.
  • open_exec (transport.rs:393-416) sends exec(true, cmd) and does not wait for the reply; ExecAccumulator::push (crates/ssh-transport/src/exec.rs:223-240) ignores ChannelMsg::Failure; drain_channel_cancellable (transport.rs:93-140) waits for Close. A refused exec that the server leaves open hangs forever.
  • Without stdin, execute never sends EOF. With OpenSSH ForceCommand internal-sftp every exec starts sftp-server, which waits on stdin, so execute("uname -a") never returns.
  • open_sftp already waits for the subsystem reply (Open the SFTP browser fails fast when the server has no SFTP subsystem #1225, transport.rs:287-307); this issue applies the same pattern to shell and exec.

Requirements

  • open_shell waits for the reply to the shell request. Failure → new error SshError::ShellUnavailable (code SSH_SHELL_UNAVAILABLE); Close/EOF before the reply → SessionClosed as today.
  • open_exec/execute wait for the reply to the exec request. Failure → new error SshError::ExecUnavailable (code SSH_EXEC_UNAVAILABLE).
  • An exec without stdin sends EOF right after the request is accepted (same as ssh host cmd </dev/null). Exec calls that pass stdin keep their behaviour.
  • Both new codes are not in the retryable list of auto-reconnect (crates/app-logic/src/auto_reconnect.rs:54-69).
  • New real-server scenario io-sftponly in crates/ssh-transport/tests/sshd/scenarios/ (image openssh-current, Subsystem sftp internal-sftp, ForceCommand internal-sftp, ChrootDirectory on a root-owned directory created in setup.sh) with tests in a new file crates/ssh-transport/tests/sshd_sftp_only.rs.

Acceptance Criteria

  • Against io-sftponly: execute("uname -a") returns within 5 s with empty stdout (no hang).
  • Against io-sftponly: open_shell returns an error or a shell that reports SessionClosed within 5 s.
  • Against io-sftponly: open_sftp works and lists the chroot root.
  • A refused shell request yields SSH_SHELL_UNAVAILABLE, a refused exec request yields SSH_EXEC_UNAVAILABLE (unit test with a channel that answers Failure).
  • An exec without stdin of cat (no arguments) returns with exit status 0 against openssh-current instead of hanging.
  • All existing real-server tests (sshd_exec_sftp.rs, sshd_auth_policy.rs, …) stay green; normal shell and exec against OpenSSH and Dropbear are unchanged.
  • Auto-reconnect does not retry on the two new codes (unit test).
  • Relevant automated tests pass

Technical Notes

  • Execution path: no change to filter engine, approval or ApprovedCommand; this is transport behaviour only.
  • Error messages follow the existing SshError style; codes are mapped like SSH_CHANNEL_ERROR.
  • No change under .github/ (the runner picks up new scenario directories by itself, see tests/sshd/README.md).

Files: crates/ssh-transport/src/transport.rs, crates/ssh-transport/src/exec.rs, crates/ssh-transport/src/shell.rs, crates/core/src/ssh/ (error type), crates/app-logic/src/auto_reconnect.rs, crates/ssh-transport/tests/sshd/scenarios/ (new io-sftponly), crates/ssh-transport/tests/ (new sshd_sftp_only.rs), docs/specs/0005-ssh-module.md, changelog.d/

Spec impact: update 0005

Open questions

  • Waiting for the shell/exec reply: reuse the open_sftp wait loop (recommended) or a shared helper for all three. Recommendation: a shared helper if it stays small.

Related Work

Activity

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

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1675. Shell and exec now wait for the server reply (SSH_SHELL_UNAVAILABLE / SSH_EXEC_UNAVAILABLE), exec without stdin sends EOF, new io-sftponly scenario and tests, spec 0005 updated. CI green including SSH end-to-end. Note: forced internal-sftp prints a notice on stdout, so execute returns fast but stdout is not empty.

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

    @smart-ssh-reviewer

    Review: 1. Acceptance criterion 4, shell part: there is no test that open_shell turns a Failure reply into SSH_SHELL_UNAVAILABLE. The existing tests only check the helper (Refused) or build SshError::ShellUnavailable by hand, so changing the Refused arm in open_shell (transport.rs:316-330) would not fail any test. Let the in-process test server refuse the shell request (like refused-exec) and add a test that calls open_shell with a timeout and asserts the code. The test must be red without the fix. Note for the maintainer, not blocking: AC 1's 'empty stdout' cannot be met against OpenSSH, because forced internal-sftp prints its own notice 'This service allows sftp connections only.' and exits 1. The intent of the AC (no hang, command not run) is met and tested.

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

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1675. Added an open_shell test against the in-process server refusing the shell request, asserting SSH_SHELL_UNAVAILABLE (red without the mapping). CI green.

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

    @smart-ssh-reviewer

    Review passed – moving on to merge. Approved. The only blocking finding from the first round is fixed: the shell half of AC 4 now has a test. test_refused_shell_request_is_shell_unavailable fails with open_shell from develop and passes with the fix. The whole ssh-transport suite passes locally and CI is green. One note for the maintainer, not blocking: AC 1's 'empty stdout' cannot hold against OpenSSH, because forced internal-sftp prints its own notice. Execute still returns fast and the command does not run. Follow-up (label already set): the request-reply wait drops data that arrives before the reply and has no time limit.

  8. smart-ssh-coordinator commented on Dec 5, 2026

    @smart-ssh-coordinator
    Contributor

    Needs maintainer (merge): PR #1675 still not mergeable after 150 min (state BLOCKED, 0 checks pending).

  9. added
    needs-maintainer
    and removed
    needs-maintainer
    on Dec 5, 2026
  10. smart-ssh-coordinator commented on Dec 5, 2026

    @smart-ssh-coordinator
    Contributor

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

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

    @smart-ssh-planner
    Author

    Follow-ups filed: #1767 Bound the request-reply wait (shell/exec/SFTP) with a timeout and keep channel data that arrives before the reply. Not filed: REFUSE_SHELL_COLS doc-comment placement (style only); the AC 1 'empty stdout' deviation (OpenSSH's own sftp-only notice, no client fix).

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

    bugssh

    Type

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions