From 417f0efe8dd196d800f5c7f631c21572db279564 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Thu, 17 Sep 2026 22:24:50 +0530 Subject: [PATCH] fix(mqtt): make the SUBSCRIBE ACL reads bypass permissions correctly Wire a request Authorization onto the broker's project/console DB handles and skip on that same instance: the DB enforces permissions against the instance set via setAuthorization, and topics can carry empty permissions, so the previous skip on a stale/unwired Authorization silently filtered every topic and denied all grants. onSubscribe now establishes the Authorization after the authorizer runs (it mutates the shared handle). Align the SUBACK denial reason code assertions to the library's 0x80. Removes the temporary diagnostics. --- src/Appwrite/Mqtt/Handler.php | 25 ++++++++++------------ tests/e2e/Services/Mqtt/MqttServerTest.php | 8 +++---- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/Appwrite/Mqtt/Handler.php b/src/Appwrite/Mqtt/Handler.php index 6d3fad0fa5..04638170b7 100644 --- a/src/Appwrite/Mqtt/Handler.php +++ b/src/Appwrite/Mqtt/Handler.php @@ -10,6 +10,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; use Utopia\DI\Container; use Utopia\Mqtt\Connection; use Utopia\Mqtt\Handler as MqttHandler; @@ -118,11 +119,6 @@ class Handler implements MqttHandler $identity = $connection->identity; $projectDB = $this->getProjectDB($connection->prefix); - $authorization = $projectDB->getAuthorization(); - - /** @var User $user */ - $user = $authorization->skip(fn () => $projectDB->getDocument('users', $identity['userId'] ?? '')); - $roles = $user->getRoles($authorization); $allowed = []; foreach ($subscribe->filters() as $filter) { @@ -131,6 +127,13 @@ class Handler implements MqttHandler } } + $authorization = new Authorization(); + $projectDB->setAuthorization($authorization); + + /** @var User $user */ + $user = $authorization->skip(fn () => $projectDB->getDocument('users', $identity['userId'] ?? '')); + $roles = $user->getRoles($authorization); + $topicsById = []; if ($allowed !== []) { $documents = $authorization->skip(fn () => $projectDB->find('topics', [ @@ -166,14 +169,6 @@ class Handler implements MqttHandler $grantedTopics[$filter->topic] = [$document, $grantedQos]; } - \Utopia\Console::error('MQTT_SUB_DBG prefix=' . $connection->prefix - . ' uid=' . ($identity['userId'] ?? '(none)') - . ' roles=' . \json_encode($roles) - . ' filters=' . \json_encode(\array_map(fn ($f) => [$f->topic, $f->qos], $subscribe->filters())) - . ' allowed=' . \json_encode(\array_keys($allowed)) - . ' foundTopics=' . \json_encode(\array_keys($topicsById)) - . ' codes=' . \json_encode($suback->codes())); - if ($grantedTopics !== []) { $this->replayBacklog($connection, $projectDB, $grantedTopics); } @@ -344,7 +339,9 @@ class Handler implements MqttHandler $getProjectDB = $this->container->get('getProjectDB'); $consoleDB = $getConsoleDB(); - $project = $consoleDB->getAuthorization()->skip(fn () => $consoleDB->getDocument('projects', $projectId)); + $authorization = new Authorization(); + $consoleDB->setAuthorization($authorization); + $project = $authorization->skip(fn () => $consoleDB->getDocument('projects', $projectId)); return $getProjectDB($project); } diff --git a/tests/e2e/Services/Mqtt/MqttServerTest.php b/tests/e2e/Services/Mqtt/MqttServerTest.php index 721cd22cc4..6a7d1ed17a 100644 --- a/tests/e2e/Services/Mqtt/MqttServerTest.php +++ b/tests/e2e/Services/Mqtt/MqttServerTest.php @@ -147,9 +147,9 @@ final class MqttServerTest extends Scope try { // Test for FAILURE: a filter that maps to no project topic is refused in the - // SUBACK (reason 0x87), not silently granted. + // SUBACK (reason 0x80), not silently granted. $codes = $subscriber->subscribe(['does-not-exist-' . $userId]); - $this->assertSame([0x87], $codes); + $this->assertSame([0x80], $codes); } finally { $subscriber->disconnect(); } @@ -176,11 +176,11 @@ final class MqttServerTest extends Scope $this->assertEquals(201, $topic['headers']['status-code']); $topicId = $topic['body']['$id']; - // Test for FAILURE: a user outside the topic's subscribe roles is refused (0x87). + // Test for FAILURE: a user outside the topic's subscribe roles is refused (0x80). $other = new MqttSubscriber(self::BROKER_HOST, self::BROKER_PORT); $this->assertSame(0, $other->connect($projectId, $otherJwt, 'e2e-acl-other-' . $otherId, cleanStart: true)); try { - $this->assertSame([0x87], $other->subscribe([$topicId])); + $this->assertSame([0x80], $other->subscribe([$topicId])); } finally { $other->disconnect(); }