BF close connections the legacy WebSocketHandler refuses

Server\WebSocketHandler can still be selected through
websockets.handlers.websocket. On an unknown app key verifyAppKey()
rejected its promise and then went on to set $connection->app = null and
resolve it anyway. onError() sent the 4001 frame but never closed the
socket, and a disallowed origin (4009) was left open the same way.

- verifyAppKey() returns after the rejection and passes a rejected lookup
  on.
- onOpen() refuses through refuseConnection(): websocket.error via
  onError(), then close with the exception's 4xxx code (1011 for anything
  else). A Throwable from verifyAppKey() itself is refused the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-04 12:29:49 +01:00
parent f04ace1eea
commit 88ed045899
2 changed files with 122 additions and 3 deletions

View File

@ -50,7 +50,15 @@ class WebSocketHandler implements MessageComponentInterface
return $connection->close(); return $connection->close();
} }
$this->verifyAppKey($connection) try {
$verified = $this->verifyAppKey($connection);
} catch (\Throwable $exception) {
$this->refuseConnection($connection, $exception);
return;
}
$verified
->then(function () use ($connection) { ->then(function () use ($connection) {
try { try {
$this->verifyOrigin($connection) $this->verifyOrigin($connection)
@ -71,13 +79,43 @@ class WebSocketHandler implements MessageComponentInterface
NewConnection::dispatch($connection->app->id, $connection->socketId); NewConnection::dispatch($connection->app->id, $connection->socketId);
} }
} catch (WebSocketException $exception) { } catch (WebSocketException $exception) {
$this->onError($connection, $exception); // Disallowed origin (4009): refuse, don't leave it open.
$this->refuseConnection($connection, $exception);
} }
}, function ($exception) use ($connection) { }, function ($exception) use ($connection) {
$this->onError($connection, $exception); $this->refuseConnection($connection, $exception);
}); });
} }
/**
* Report why the connection is refused (websocket.error for a
* WebSocketException) and close it: with the exception's 4xxx code, or
* 1011 for anything else. A socket that failed to open joined nothing
* and must not stay open.
*
* @param \Ratchet\ConnectionInterface $connection
* @param mixed $exception
* @return void
*/
protected function refuseConnection(ConnectionInterface $connection, $exception): void
{
if (! $exception instanceof Exception) {
$exception = new \RuntimeException(
$exception instanceof \Throwable ? $exception->getMessage() : 'Opening the connection failed.'
);
}
$this->onError($connection, $exception);
$code = $exception instanceof WebSocketException ? (int) $exception->getCode() : 0;
try {
$connection->close($code >= 4000 && $code <= 4999 ? $code : 1011);
} catch (\Throwable) {
// The peer is already gone.
}
}
/** /**
* Handle the incoming message. * Handle the incoming message.
* *
@ -220,11 +258,15 @@ class WebSocketHandler implements MessageComponentInterface
if (! $app) { if (! $app) {
$this->wsLog('error', "Unknown app key: '{$appKey}'. Check that PUSHER_APP_KEY in .env matches the key used by the frontend. Configured apps: " . implode(', ', array_map(fn($a) => $a['key'] ?? 'null', config('websockets.apps', [])))); $this->wsLog('error', "Unknown app key: '{$appKey}'. Check that PUSHER_APP_KEY in .env matches the key used by the frontend. Configured apps: " . implode(', ', array_map(fn($a) => $a['key'] ?? 'null', config('websockets.apps', []))));
$deferred->reject(new Exceptions\UnknownAppKey($appKey)); $deferred->reject(new Exceptions\UnknownAppKey($appKey));
return;
} }
$connection->app = $app; $connection->app = $app;
$deferred->resolve(); $deferred->resolve();
}, function ($reason) use ($deferred) {
$deferred->reject($reason);
}); });
return $deferred->promise(); return $deferred->promise();

View File

@ -0,0 +1,77 @@
<?php
namespace BlaxSoftware\LaravelWebSockets\Test;
use BlaxSoftware\LaravelWebSockets\Server\WebSocketHandler;
use GuzzleHttp\Psr7\Request;
/**
* The legacy Server\WebSocketHandler can still be selected through
* websockets.handlers.websocket. A connection it refuses must be closed.
*/
class LegacyWebSocketHandlerTest extends TestCase
{
private WebSocketHandler $legacy;
public function setUp(): void
{
parent::setUp();
$this->legacy = new WebSocketHandler($this->channelManager);
}
public function test_unknown_app_key_gets_4001_and_is_closed()
{
$connection = $this->newConnection('NonWorkingKey');
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', [
'data' => ['message' => 'Could not find app key `NonWorkingKey`.', 'code' => 4001],
])
->assertNotSentEvent('websocket.connection_established')
->assertClosed();
$this->assertSame(4001, $connection->closeCode);
$this->assertFalse(isset($connection->app));
}
public function test_array_app_key_gets_4001_and_is_closed()
{
$connection = new Mocks\Connection;
$connection->httpRequest = new Request('GET', '/?appKey[]=x');
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', ['data' => ['message' => 'Could not find app key ``.', 'code' => 4001]])
->assertClosed();
$this->assertSame(4001, $connection->closeCode);
}
public function test_disallowed_origin_gets_4009_and_is_closed()
{
$connection = $this->newConnection('TestOrigin', ['Origin' => 'https://google.ro']);
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', [
'data' => ['message' => 'The origin is not allowed for `TestOrigin`.', 'code' => 4009],
])
->assertNotSentEvent('websocket.connection_established')
->assertClosed();
$this->assertSame(4009, $connection->closeCode);
}
public function test_known_app_key_is_established_and_stays_open()
{
$connection = $this->newConnection();
$this->legacy->onOpen($connection);
$connection->assertSentEvent('websocket.connection_established');
$this->assertFalse($connection->closed);
$this->assertSame('TestKey', $connection->app->key);
}
}