Skip to content

Commit

Permalink
SSH2: make sendEOF() better handle different channel situations
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Jan 16, 2025
1 parent 373586f commit e2cf321
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions phpseclib/Net/SSH2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3346,7 +3346,8 @@ public function reset($channel = null)
}
}

/** Send EOF on a channel
/**
* Send EOF on a channel
*
* Sends an EOF to the stream; this is typically used to close standard
* input, while keeping output and error alive.
Expand All @@ -3360,7 +3361,10 @@ public function sendEOF($channel = null)
$channel = $this->get_interactive_channel();
}

$this->send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$channel]));
$excludeStatuses = [NET_SSH2_MSG_CHANNEL_EOF, NET_SSH2_MSG_CHANNEL_CLOSE];
if (isset($this->channel_status[$channel]) && !in_array($this->channel_status[$channel], $excludeStatuses)) {
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$channel]));
}
}

/**
Expand Down Expand Up @@ -4201,7 +4205,9 @@ protected function get_channel_packet($client_channel, $skip_extended = false)
}

if (isset($this->channel_status[$channel]) && $this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_CLOSE) {
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$channel]));
if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) {
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$channel]));
}
$this->send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));

$this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
Expand Down
16 changes: 16 additions & 0 deletions tests/Functional/Net/SSH2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,22 @@ public static function getCryptoAlgorithms()
return $tests;
}

/**
* @group github2062
*/
public function testSendEOF()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Please close the channel (1) before trying to open it again');

$ssh = $this->getSSH2Login();

$ssh->write("ls -latr; exit\n");
$ssh->read();
$ssh->sendEOF();
$ssh->exec('ls -latr');
}

/**
* @dataProvider getCryptoAlgorithms
* @param string $type
Expand Down

0 comments on commit e2cf321

Please sign in to comment.