fix(ssh): handle channel closed exception during shell and privilege checks#1136
fix(ssh): handle channel closed exception during shell and privilege checks#1136Marshall-Hallenbeck wants to merge 3 commits intomainfrom
Conversation
…checks Catch SSHException in check_shell() and check_linux_priv() so that SSH servers that close channels mid-command no longer cause unhandled tracebacks. Instead, the connection is treated as "no shell access". Closes #264
|
This just wraps the potentially failing code in a try to catch the error. |
There was a problem hiding this comment.
Pull request overview
This PR hardens the SSH protocol implementation to avoid unhandled Paramiko SSHException("Channel closed.") errors during post-auth shell/privilege detection, so a channel close is treated as “no shell access” instead of crashing the run.
Changes:
- Wrap
exec_commandcalls incheck_shell()withSSHExceptionhandling to prevent tracebacks when the server closes the channel mid-command. - Wrap the privilege probe in
check_linux_priv()withSSHExceptionhandling to avoid crashes duringsudo -lnchecks.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@Marshall-Hallenbeck here are a few AI responses as well that have to be dealt with in some form |
|
@NeffIsBack OK this is good. I just split up the Linux and Windows exception catches and caught all exceptions and printed it because it could be anything from timeout to any various SSH error and if it errors on these simple commands we should just log and continue. |
Description
Fixes #264 — SSH
check_shell()andcheck_linux_priv()callexec_commandwithout exception handling. When an SSH server closes the channel mid-command (e.g. certain IoT devices, VShell servers, or servers that reject specific commands), paramiko raisesSSHException("Channel closed.")which propagates as an unhandled traceback.This PR wraps the
exec_commandcalls incheck_shell()andcheck_linux_priv()withSSHExceptionhandlers. When a channel close occurs, the connection is treated as "no shell access" (same as if the commands returned empty output) rather than crashing.The upstream paramiko issue (paramiko#2391) remains open with no fix, so handling this in our code is the right approach regardless.
This PR was created with the assistance of AI (Claude Code / claude-opus-4-6 — code review, implementation, and PR drafting).
Type of change
Setup guide for the review
Bug reproduction:
You need an SSH server that closes the channel during command execution. This can happen with:
idorsudo -lncommandsTo trigger the original bug:
If the server closes the channel during
check_shell(), you'll see:After this fix, the connection completes normally and is reported as authenticated (without shell access).
Test environment:
Screenshots (if appropriate):
Checklist:
poetry run ruff check ., use--fixto automatically fix what it can)tests/e2e_commands.txtfile if necessary (new modules or features are required to be added to the e2e tests)