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.
This commit is contained in:
ArnabChatterjee20k committed 2026-09-17 22:24:50 +05:30
1 parent e2496d8895
commit 417f0efe8d
2 files changed
+15 -18

No files matched your search

+11 -14
View File
@@ -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);
}
+4 -4
View File
@@ -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();
}