Skip to content

Commit

Permalink
Fix possible infinite loop in connect()
Browse files Browse the repository at this point in the history
If the server closes the connection before the server identification is received, the client will hang in an infinite loop.
  • Loading branch information
kelunik committed May 11, 2019
1 parent 4ab2df1 commit f6e3859
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amp\Promise;
use Amp\Ssh\Authentication\Authentication;
use Amp\Ssh\Authentication\AuthenticationFailureException;
use Amp\Ssh\Channel\Dispatcher;
use Amp\Ssh\Transport\LoggerHandler;
use Amp\Ssh\Transport\MessageHandler;
Expand Down Expand Up @@ -31,7 +32,12 @@ function connect(string $uri, Authentication $authentication, LoggerInterface $l
$buffer = '';

while ($serverIdentification === null) {
$buffer .= yield $socket->read();
$chunk = yield $socket->read();
if ($chunk === null) {
throw new AuthenticationFailureException('Could not read server identification: connection closed during read');
}

$buffer .= $chunk;

if (($linePos = \strpos($buffer, "\n")) !== false) {
$line = \substr($buffer, 0, $linePos);
Expand Down

0 comments on commit f6e3859

Please sign in to comment.