From 88ed045899ccdf43a13ed10cc859dd7f5f424b8e Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 4 Nov 2026 12:29:49 +0100 Subject: [PATCH] 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 --- src/Server/WebSocketHandler.php | 48 +++++++++++++++-- tests/LegacyWebSocketHandlerTest.php | 77 ++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 tests/LegacyWebSocketHandlerTest.php diff --git a/src/Server/WebSocketHandler.php b/src/Server/WebSocketHandler.php index 3e5b28a..fd4b588 100644 --- a/src/Server/WebSocketHandler.php +++ b/src/Server/WebSocketHandler.php @@ -50,7 +50,15 @@ class WebSocketHandler implements MessageComponentInterface return $connection->close(); } - $this->verifyAppKey($connection) + try { + $verified = $this->verifyAppKey($connection); + } catch (\Throwable $exception) { + $this->refuseConnection($connection, $exception); + + return; + } + + $verified ->then(function () use ($connection) { try { $this->verifyOrigin($connection) @@ -71,13 +79,43 @@ class WebSocketHandler implements MessageComponentInterface NewConnection::dispatch($connection->app->id, $connection->socketId); } } catch (WebSocketException $exception) { - $this->onError($connection, $exception); + // Disallowed origin (4009): refuse, don't leave it open. + $this->refuseConnection($connection, $exception); } }, 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. * @@ -220,11 +258,15 @@ class WebSocketHandler implements MessageComponentInterface 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', [])))); $deferred->reject(new Exceptions\UnknownAppKey($appKey)); + + return; } $connection->app = $app; $deferred->resolve(); + }, function ($reason) use ($deferred) { + $deferred->reject($reason); }); return $deferred->promise(); diff --git a/tests/LegacyWebSocketHandlerTest.php b/tests/LegacyWebSocketHandlerTest.php new file mode 100644 index 0000000..be672f9 --- /dev/null +++ b/tests/LegacyWebSocketHandlerTest.php @@ -0,0 +1,77 @@ +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); + } +}