fix(databases): 404 (not 500) for a wrong-type database id on product-DB APIs

Product-DB routes (tablesdb/documentsdb/vectorsdb) resolved a database by id
with only an isEmpty() check, so a database of the wrong type (e.g. a
documentsdb id on the tablesdb path) proceeded to a type-mismatched backend op
that surfaced as an opaque 500 instead of a clean 404.

Centralise the per-path "allowed database types" in one source
(Action::getAllowedDatabaseTypes), used by BOTH the list filter
(getDatabaseTypeQueryFilters) and the by-id guard (isDatabaseTypeMismatch), so
the two cannot diverge. TablesDB keeps legacy compatibility (mirrors
listTablesDatabases); documentsdb/vectorsdb are scoped to their own type; the
legacy /v1/databases API is not type-guarded. Every database carries a non-null
type (set on create, backfilled to 'legacy' by migration V23), so the filter is
a plain Query::equal and the redundant TablesDB list-filter override is removed.

Apply the guard at every database-resolution site in the module, and make the
remaining handlers type-aware so they can use it: re-base Databases
Get/Update/Delete and the Databases/TablesDB log lists onto the type-aware
base, and re-base the Collections Attributes/Indexes Action bases and Usage/Get
onto it as well (with covariant setHttpPath signatures).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jake BarnbyandClaude Opus 4.8 committed 2026-06-20 15:05:55 +12:00
1 parent 0cf4722597
commit 283bd1c999
41 files changed
+192 -65

No files matched your search

@@ -21,6 +21,41 @@ class Action extends AppwriteAction
return $this->context;
}
/**
* Database types this path may operate on. Single source of truth shared by
* the list filter ({@see getDatabaseTypeQueryFilters()}) and the by-id guard
* ({@see isDatabaseTypeMismatch()}) so they cannot diverge.
*
* TablesDB is the compatibility successor to the legacy databases API, so it
* also serves legacy-typed databases; the other product APIs are scoped to
* their own type. Every database carries a non-null `type` (set on create
* and backfilled to 'legacy' by migration V23), so null is not represented.
*
* @return string[]
*/
protected function getAllowedDatabaseTypes(): array
{
return match ($this->getDatabaseType()) {
DATABASE_TYPE_TABLESDB => [DATABASE_TYPE_TABLESDB, DATABASE_TYPE_LEGACY],
default => [$this->getDatabaseType()],
};
}
/**
* A database resolved by id on a product-DB path must be one of the path's
* allowed types; otherwise it is treated as not-found rather than proceeding
* to a type-mismatched backend operation that surfaces as an opaque 500. The
* legacy /v1/databases API is intentionally not type-guarded.
*/
protected function isDatabaseTypeMismatch(Document $database): bool
{
if ($this->getDatabaseType() === DATABASE_TYPE_LEGACY) {
return false;
}
return !in_array($database->getAttribute('type', ''), $this->getAllowedDatabaseTypes(), true);
}
public function setHttpPath(string $path): self
{
if (\str_contains($path, '/tablesdb')) {
@@ -6,6 +6,7 @@ use Appwrite\Event\Event;
use Appwrite\Event\Message\Database as DatabaseMessage;
use Appwrite\Event\Publisher\Database as DatabasePublisher;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action as DatabasesAction;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response as UtopiaResponse;
use Throwable;
@@ -21,10 +22,9 @@ use Utopia\Database\Helpers\ID;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Structure;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Platform\Action as UtopiaAction;
use Utopia\Validator\Range;
abstract class Action extends UtopiaAction
abstract class Action extends DatabasesAction
{
/**
* @var string The current context (either 'column' or 'attribute')
@@ -36,12 +36,14 @@ abstract class Action extends UtopiaAction
*/
abstract protected function getResponseModel(): string|array;
public function setHttpPath(string $path): UtopiaAction
public function setHttpPath(string $path): DatabasesAction
{
if (\str_contains($path, '/tablesdb')) {
$this->context = COLUMNS;
}
return parent::setHttpPath($path);
parent::setHttpPath($path);
return $this;
}
/**
@@ -333,7 +335,7 @@ abstract class Action extends UtopiaAction
$db = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($db->isEmpty()) {
if ($db->isEmpty() || $this->isDatabaseTypeMismatch($db)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -495,7 +497,7 @@ abstract class Action extends UtopiaAction
{
$db = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($db->isEmpty()) {
if ($db->isEmpty() || $this->isDatabaseTypeMismatch($db)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -76,7 +76,7 @@ class Delete extends Action
public function action(string $databaseId, string $collectionId, string $key, UtopiaResponse $response, Database $dbForProject, DatabasePublisher $publisherForDatabase, Event $queueForEvents, Authorization $authorization): void
{
$db = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($db->isEmpty()) {
if ($db->isEmpty() || $this->isDatabaseTypeMismatch($db)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -75,7 +75,7 @@ class Get extends Action
public function action(string $databaseId, string $collectionId, string $key, UtopiaResponse $response, Database $dbForProject, Authorization $authorization): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -99,7 +99,7 @@ class Create extends Action
$twoWayKey ??= $collectionId;
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -71,7 +71,7 @@ class XList extends Action
public function action(string $databaseId, string $collectionId, array $queries, bool $includeTotal, UtopiaResponse $response, Database $dbForProject, Authorization $authorization): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -94,7 +94,7 @@ class Create extends Action
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -73,7 +73,7 @@ class Delete extends Action
public function action(string $databaseId, string $collectionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, DatabasePublisher $publisherForDatabase, Event $queueForEvents, Authorization $authorization): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -97,7 +97,7 @@ class Decrement extends Action
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -97,7 +97,7 @@ class Increment extends Action
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -91,7 +91,7 @@ class Delete extends Action
public function action(string $databaseId, string $collectionId, array $queries, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Context $usage, Event $queueForEvents, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, EventProcessor $eventProcessor): void
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -103,7 +103,7 @@ class Update extends Action
}
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -93,7 +93,7 @@ class Upsert extends Action
public function action(string $databaseId, string $collectionId, array $documents, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Context $usage, Event $queueForEvents, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, EventProcessor $eventProcessor): void
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -209,7 +209,7 @@ class Create extends Action
}
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -110,7 +110,7 @@ class Delete extends Action
$isAPIKey = $user->isKey($authorization->getRoles());
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -82,7 +82,7 @@ class Get extends Action
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -81,7 +81,7 @@ class XList extends Action
public function action(string $databaseId, string $collectionId, string $documentId, array $queries, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Locale $locale, Reader $geodb, Authorization $authorization, Audit $audit): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -106,7 +106,7 @@ class Update extends Action
$isAPIKey = $user->isKey($authorization->getRoles());
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -112,7 +112,7 @@ class Upsert extends Action
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -92,7 +92,7 @@ class XList extends Action
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -65,7 +65,7 @@ class Get extends Action
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -3,9 +3,9 @@
namespace Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Indexes;
use Appwrite\Extend\Exception;
use Utopia\Platform\Action as UtopiaAction;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action as DatabasesAction;
abstract class Action extends UtopiaAction
abstract class Action extends DatabasesAction
{
/**
* The current API context (either 'columnIndex' or 'index').
@@ -17,12 +17,14 @@ abstract class Action extends UtopiaAction
*/
abstract protected function getResponseModel(): string;
public function setHttpPath(string $path): UtopiaAction
public function setHttpPath(string $path): DatabasesAction
{
if (\str_contains($path, '/tablesdb')) {
$this->context = COLUMN_INDEX;
}
return parent::setHttpPath($path);
parent::setHttpPath($path);
return $this;
}
/**
@@ -90,7 +90,7 @@ class Create extends Action
{
$db = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($db->isEmpty()) {
if ($db->isEmpty() || $this->isDatabaseTypeMismatch($db)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -80,7 +80,7 @@ class Delete extends Action
{
$db = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($db->isEmpty()) {
if ($db->isEmpty() || $this->isDatabaseTypeMismatch($db)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
$collection = $dbForProject->getDocument('database_' . $db->getSequence(), $collectionId);
@@ -67,7 +67,7 @@ class Get extends Action
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
@@ -75,7 +75,7 @@ class XList extends Action
/** @var Document $database */
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -80,7 +80,7 @@ class XList extends Action
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -80,7 +80,7 @@ class Update extends Action
public function action(string $databaseId, string $collectionId, ?string $name, ?array $permissions, bool $documentSecurity, bool $enabled, bool $purge, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Authorization $authorization): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -75,7 +75,7 @@ class XList extends Action
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -15,7 +15,6 @@ use Appwrite\Utopia\Response as UtopiaResponse;
use Utopia\Database\Database;
use Utopia\Database\Validator\UID;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Platform\Action;
class Delete extends Action
{
@@ -68,7 +67,7 @@ class Delete extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -12,7 +12,6 @@ use Appwrite\Utopia\Response as UtopiaResponse;
use Utopia\Database\Database;
use Utopia\Database\Validator\UID;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Platform\Action;
class Get extends Action
{
@@ -60,7 +59,7 @@ class Get extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -4,6 +4,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\Databases\Logs;
use Appwrite\Detector\Detector;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Deprecated;
@@ -23,7 +24,6 @@ use Utopia\Database\Validator\Query\Offset;
use Utopia\Database\Validator\UID;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Locale\Locale;
use Utopia\Platform\Action;
class XList extends Action
{
@@ -75,7 +75,7 @@ class XList extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -122,7 +122,7 @@ class Create extends Action
}
$database = $databases[$operation['databaseId']] ??= $authorization->skip(fn () => $dbForProject->getDocument('databases', $operation['databaseId']));
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database) || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$operation['databaseId']]);
}
@@ -13,7 +13,6 @@ use Appwrite\Utopia\Response as UtopiaResponse;
use Utopia\Database\Database;
use Utopia\Database\Validator\UID;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Platform\Action;
use Utopia\Validator\Boolean;
use Utopia\Validator\Text;
@@ -69,7 +68,7 @@ class Update extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -3,7 +3,7 @@
namespace Appwrite\Platform\Modules\Databases\Http\Databases\Usage;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Action;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action as DatabasesAction;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Deprecated;
@@ -20,7 +20,7 @@ use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Platform\Enum;
use Utopia\Validator\WhiteList;
class Get extends Action
class Get extends DatabasesAction
{
public static function getName(): string
{
@@ -29,7 +29,7 @@ class Get extends Action
protected $databaseType = DATABASE_TYPE_LEGACY;
public function setHttpPath(string $path): Action
public function setHttpPath(string $path): DatabasesAction
{
$this->databaseType = match (true) {
str_contains($path, '/documentsdb') => DATABASE_TYPE_DOCUMENTSDB,
@@ -114,7 +114,7 @@ class Get extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -29,7 +29,7 @@ class XList extends Action
protected function getDatabaseTypeQueryFilters(): array
{
return [Query::equal('type', [$this->getDatabaseType()])];
return [Query::equal('type', $this->getAllowedDatabaseTypes())];
}
public function __construct()
@@ -4,6 +4,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\TablesDB\Logs;
use Appwrite\Detector\Detector;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
@@ -22,7 +23,6 @@ use Utopia\Database\Validator\Query\Offset;
use Utopia\Database\Validator\UID;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Locale\Locale;
use Utopia\Platform\Action;
class XList extends Action
{
@@ -70,7 +70,7 @@ class XList extends Action
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
}
@@ -9,7 +9,6 @@ use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\Queries\Databases;
use Appwrite\Utopia\Response as UtopiaResponse;
use Utopia\Database\Query;
use Utopia\Http\Adapter\Swoole\Response as SwooleResponse;
use Utopia\Validator\Boolean;
use Utopia\Validator\Text;
@@ -21,16 +20,6 @@ class XList extends DatabaseXList
return 'listTablesDatabases';
}
protected function getDatabaseTypeQueryFilters(): array
{
return [
Query::or([
Query::equal('type', [DATABASE_TYPE_TABLESDB, DATABASE_TYPE_LEGACY]),
Query::isNull('type'),
]),
];
}
public function __construct()
{
$this
@@ -85,7 +85,7 @@ class Create extends CollectionAction
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
@@ -76,7 +76,7 @@ class Update extends CollectionAction
public function action(string $databaseId, string $collectionId, ?string $name, ?int $dimensions, ?array $permissions, bool $documentSecurity, ?bool $enabled, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Authorization $authorization): void
{
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
if ($database->isEmpty() || $this->isDatabaseTypeMismatch($database)) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Platform\Modules\Databases;
use Appwrite\Platform\Modules\Databases\Http\Databases\Action;
use PHPUnit\Framework\TestCase;
use Utopia\Database\Document;
final class DatabaseTypeMismatchTest extends TestCase
{
public function testTablesdbAllowsTablesdbAndLegacyButRejectsOtherProducts(): void
{
$action = new class () extends Action {
public static function getName(): string
{
return 'testTablesdbTypeGuard';
}
public function exposeMismatch(Document $database): bool
{
return $this->isDatabaseTypeMismatch($database);
}
/** @return string[] */
public function exposeAllowedTypes(): array
{
return $this->getAllowedDatabaseTypes();
}
};
$action->setHttpPath('/v1/tablesdb/:databaseId/tables');
// TablesDB is the compatibility successor to the legacy databases API:
// the allowed set must equal what listTablesDatabases advertises.
$this->assertSame([DATABASE_TYPE_TABLESDB, DATABASE_TYPE_LEGACY], $action->exposeAllowedTypes());
$this->assertFalse($action->exposeMismatch(new Document(['type' => DATABASE_TYPE_TABLESDB])));
$this->assertFalse($action->exposeMismatch(new Document(['type' => DATABASE_TYPE_LEGACY])));
$this->assertTrue($action->exposeMismatch(new Document(['type' => DATABASE_TYPE_DOCUMENTSDB])));
$this->assertTrue($action->exposeMismatch(new Document(['type' => DATABASE_TYPE_VECTORSDB])));
// Databases always carry a non-null type (V23 backfilled old rows to
// legacy); an unexpected empty type is excluded by both the list and guard.
$this->assertTrue($action->exposeMismatch(new Document([])));
}
public function testDocumentsAndVectorsPathsScopeStrictlyToTheirOwnType(): void
{
$documents = new class () extends Action {
public static function getName(): string
{
return 'testDocumentsdbTypeGuard';
}
public function exposeMismatch(Document $database): bool
{
return $this->isDatabaseTypeMismatch($database);
}
};
$documents->setHttpPath('/v1/documentsdb/:databaseId/collections');
$this->assertFalse($documents->exposeMismatch(new Document(['type' => DATABASE_TYPE_DOCUMENTSDB])));
$this->assertTrue($documents->exposeMismatch(new Document(['type' => DATABASE_TYPE_VECTORSDB])));
$this->assertTrue($documents->exposeMismatch(new Document(['type' => DATABASE_TYPE_LEGACY])));
$this->assertTrue($documents->exposeMismatch(new Document([])));
$vectors = new class () extends Action {
public static function getName(): string
{
return 'testVectorsdbTypeGuard';
}
public function exposeMismatch(Document $database): bool
{
return $this->isDatabaseTypeMismatch($database);
}
};
$vectors->setHttpPath('/v1/vectorsdb/:databaseId/collections');
$this->assertFalse($vectors->exposeMismatch(new Document(['type' => DATABASE_TYPE_VECTORSDB])));
$this->assertTrue($vectors->exposeMismatch(new Document(['type' => DATABASE_TYPE_DOCUMENTSDB])));
$this->assertTrue($vectors->exposeMismatch(new Document(['type' => DATABASE_TYPE_LEGACY])));
}
public function testLegacyDatabasesPathIsNeverTypeGuarded(): void
{
$legacy = new class () extends Action {
public static function getName(): string
{
return 'testLegacyTypeGuard';
}
public function exposeMismatch(Document $database): bool
{
return $this->isDatabaseTypeMismatch($database);
}
};
$legacy->setHttpPath('/v1/databases/:databaseId/collections');
$this->assertFalse($legacy->exposeMismatch(new Document(['type' => DATABASE_TYPE_LEGACY])));
$this->assertFalse($legacy->exposeMismatch(new Document([])));
$this->assertFalse($legacy->exposeMismatch(new Document(['type' => DATABASE_TYPE_TABLESDB])));
$this->assertFalse($legacy->exposeMismatch(new Document(['type' => DATABASE_TYPE_DOCUMENTSDB])));
}
}