From c1e1c98263f82f0c3105b0a7302eb118ccb9bb76 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 24 Jun 2026 10:18:59 +0530 Subject: [PATCH 1/3] added fallback to read query params for jwt auth --- app/init/realtime/connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/init/realtime/connection.php b/app/init/realtime/connection.php index 397d077aa7..571b5dfc77 100644 --- a/app/init/realtime/connection.php +++ b/app/init/realtime/connection.php @@ -271,7 +271,7 @@ return function (Container $container): void { $user = new User([]); } - $authJWT = $request->getHeaderLine('x-appwrite-jwt', ''); + $authJWT = $request->getHeaderLine('x-appwrite-jwt', (string)($request->getParam('jwt', ''))); if (!empty($authJWT) && !$project->isEmpty()) { if (!$user->isEmpty()) { throw new Exception(Exception::USER_JWT_AND_COOKIE_SET); From cee3857c4eecb21f115f03e7762420838f2a92ee Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 24 Jun 2026 10:19:06 +0530 Subject: [PATCH 2/3] added message tests --- ...altimeCustomClientQueryTestWithMessage.php | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientQueryTestWithMessage.php b/tests/e2e/Services/Realtime/RealtimeCustomClientQueryTestWithMessage.php index 13a7d549dc..2a0d0e132c 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientQueryTestWithMessage.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientQueryTestWithMessage.php @@ -13,6 +13,7 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; use WebSocket\Client as WebSocketClient; +use WebSocket\ConnectionException; use WebSocket\TimeoutException; final class RealtimeCustomClientQueryTestWithMessage extends Scope @@ -657,6 +658,90 @@ final class RealtimeCustomClientQueryTestWithMessage extends Scope ); } + public function testConnectionJWT(): void + { + $user = $this->getUser(); + $userId = $user['$id'] ?? ''; + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + // Mint a JWT for the authenticated user. + $response = $this->client->call(Client::METHOD_POST, '/account/jwt', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_' . $projectId . '=' . $session, + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['jwt']); + $jwt = $response['body']['jwt']; + + /** + * Test for SUCCESS - JWT via x-appwrite-jwt header. + * Connection authenticates at handshake time; the `connected` payload carries the user. + */ + $client = $this->getWebsocketWithCustomQuery([ + 'project' => $projectId, + ], [ + 'origin' => 'http://localhost', + 'x-appwrite-jwt' => $jwt, + ]); + $connected = \json_decode($client->receive(), true); + + $this->assertEquals('connected', $connected['type'] ?? null); + $this->assertNotEmpty($connected['data']['user']); + $this->assertEquals($userId, $connected['data']['user']['$id']); + + // The authenticated user can subscribe to their account channel. + $subscribe = $this->sendSubscribeMessage($client, [[ + 'channels' => ['account'], + ]]); + $this->assertContains('account', $subscribe['data']['subscriptions'][0]['channels']); + + $client->close(); + + /** + * Test for SUCCESS - JWT via ?jwt= query string. + */ + $client = $this->getWebsocketWithCustomQuery([ + 'project' => $projectId, + 'jwt' => $jwt, + ], [ + 'origin' => 'http://localhost', + ]); + $connected = \json_decode($client->receive(), true); + + $this->assertEquals('connected', $connected['type'] ?? null); + $this->assertNotEmpty($connected['data']['user']); + $this->assertEquals($userId, $connected['data']['user']['$id']); + + $subscribe = $this->sendSubscribeMessage($client, [[ + 'channels' => ['account'], + ]]); + $this->assertContains('account', $subscribe['data']['subscriptions'][0]['channels']); + + $client->close(); + + /** + * Test for FAILURE - invalid JWT via query string is rejected at handshake. + */ + $client = $this->getWebsocketWithCustomQuery([ + 'project' => $projectId, + 'jwt' => 'invalid-token', + ], [ + 'origin' => 'http://localhost', + ]); + $error = \json_decode($client->receive(), true); + + $this->assertEquals('error', $error['type'] ?? null); + $this->assertEquals(401, $error['data']['code']); // USER_JWT_INVALID + + \usleep(250000); // 250ms + $this->expectException(ConnectionException::class); // server should disconnect + $client->close(); + } + public function testProjectChannelWithHeaderOnly(): void { $user = $this->getUser(); From 1daa36ffb4f0a65532048d0ae653f227d75ee3e0 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 24 Jun 2026 10:19:14 +0530 Subject: [PATCH 3/3] added query tests --- .../Realtime/RealtimeCustomClientTest.php | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index c623bdfd90..ce241ab91c 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -276,6 +276,89 @@ final class RealtimeCustomClientTest extends Scope $client->close(); } + public function testConnectionJWT() + { + $user = $this->getUser(); + $userId = $user['$id'] ?? ''; + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + // Mint a JWT for the authenticated user. + $response = $this->client->call(Client::METHOD_POST, '/account/jwt', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'cookie' => 'a_session_' . $projectId . '=' . $session, + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['jwt']); + $jwt = $response['body']['jwt']; + + /** + * Test for SUCCESS - JWT via x-appwrite-jwt header + */ + $client = $this->getWebsocket(['account'], [ + 'origin' => 'http://localhost', + 'x-appwrite-jwt' => $jwt, + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + + $client->close(); + + /** + * Test for SUCCESS - JWT via ?jwt= query string + */ + $client = $this->getWebsocketWithCustomQuery([ + 'project' => $projectId, + 'channels' => ['account'], + 'jwt' => $jwt, + ], [ + 'origin' => 'http://localhost', + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + $this->assertContains('account', $response['data']['channels']); + $this->assertContains('account.' . $userId, $response['data']['channels']); + + $client->close(); + + /** + * Test for FAILURE - invalid JWT via query string + */ + $client = $this->getWebsocketWithCustomQuery([ + 'project' => $projectId, + 'channels' => ['account'], + 'jwt' => 'invalid-token', + ], [ + 'origin' => 'http://localhost', + ]); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertEquals('error', $response['type']); + $this->assertEquals(401, $response['data']['code']); // USER_JWT_INVALID + + \usleep(250000); // 250ms + $this->expectException(ConnectionException::class); // server should disconnect + $client->close(); + } + public function testConnectionPlatform() { /**