Skip to content

Commit

Permalink
Reject connection when invalid message is received during login
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed May 30, 2018
1 parent 0220547 commit cee51e9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function awaitLogin(Client $client, $user, $pass)
return;
}

// reject if core rejects initialization
if ($type === 'ClientInitReject') {
$reject(new \RuntimeException('Connection rejected by Quassel core: ' . $data['Error']));
return $client->close();
}

// reject promise if login is rejected
if ($type === 'ClientLoginReject') {
$reject(new \RuntimeException('Unable to log in: ' . $data['Error']));
Expand All @@ -125,6 +131,10 @@ public function awaitLogin(Client $client, $user, $pass)

return;
}

// otherwise reject if we receive an unexpected message
$reject(new \RuntimeException('Received unexpected "' . $type . '" message during login'));
$client->close();
});

// reject promise if client emits error
Expand Down
58 changes: 58 additions & 0 deletions tests/FactoryIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,64 @@ public function testCreateClientWithAuthRejectsIfServerClosesAfterClientInit()
Block\await($promise, $loop, 10.0);
}

/**
* @expectedException RuntimeException
*/
public function testCreateClientWithAuthRejectsIfServerSendsClientInitRejectAfterClientInit()
{
$loop = LoopFactory::create();
$server = new Server(0, $loop);

$server->on('connection', function (ConnectionInterface $conn) {
$conn->once('data', function () use ($conn) {
$conn->write("\x00\x00\x00\x02");

$conn->on('data', function () use ($conn) {
// respond with rejection
$conn->write(FactoryIntegrationTest::encode(array(
'MsgType' => 'ClientInitReject',
'Error' => 'Too old'
)));
});
});
});

$uri = str_replace('tcp://', '', $server->getAddress());
$factory = new Factory($loop);
$promise = $factory->createClient('user:pass@' . $uri);

Block\await($promise, $loop, 10.0);
}

/**
* @expectedException RuntimeException
*/
public function testCreateClientWithAuthRejectsIfServerSendsUnknownMessageAfterClientInit()
{
$loop = LoopFactory::create();
$server = new Server(0, $loop);

$server->on('connection', function (ConnectionInterface $conn) {
$conn->once('data', function () use ($conn) {
$conn->write("\x00\x00\x00\x02");

$conn->on('data', function () use ($conn) {
// respond with unknown message
$conn->write(FactoryIntegrationTest::encode(array(
'MsgType' => 'Unknown',
'Error' => 'Ignored'
)));
});
});
});

$uri = str_replace('tcp://', '', $server->getAddress());
$factory = new Factory($loop);
$promise = $factory->createClient('user:pass@' . $uri);

Block\await($promise, $loop, 10.0);
}

/**
* @expectedException RuntimeException
*/
Expand Down

0 comments on commit cee51e9

Please sign in to comment.