diff --git a/AGENTS.md b/AGENTS.md index f5db88cf50..4f8673eb86 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -18,6 +18,8 @@ Self-hosted Backend-as-a-Service platform. Hybrid monolithic-microservice archit | `composer refactor:check` | Rector dry-run over `tests/` (CI "Refactor" check) | | `composer refactor` | Apply Rector fixes | +_Note: When running `composer check` or `composer analyze`, be aware that running this for entire project is very slow, and running for specific files is always recommended during development._ + ## Stack - PHP 8.3+, Swoole 6.x (async runtime, replaces PHP-FPM) diff --git a/app/config/collections/common.php b/app/config/collections/common.php index 2e3df819b7..6f3ca98fe1 100644 --- a/app/config/collections/common.php +++ b/app/config/collections/common.php @@ -762,7 +762,14 @@ return [ 'attributes' => ['userInternalId'], 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], - ] + ], + [ + '$id' => ID::custom('_key_expire'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['expire'], + 'lengths' => [], + 'orders' => [Database::ORDER_ASC], + ], ], ], diff --git a/app/config/events.php b/app/config/events.php index b551e2d6e9..b9eb849688 100644 --- a/app/config/events.php +++ b/app/config/events.php @@ -311,6 +311,152 @@ return [ '$description' => 'This event triggers when a database is deleted.', ], ], + 'documentsdb' => [ + '$model' => Response::MODEL_DATABASE, + '$resource' => true, + '$description' => 'This event triggers on any DocumentsDB database event.', + 'collections' => [ + '$model' => Response::MODEL_COLLECTION, + '$resource' => true, + '$description' => 'This event triggers on any collection event.', + 'documents' => [ + '$model' => Response::MODEL_DOCUMENT, + '$resource' => true, + '$description' => 'This event triggers on any documents event.', + 'create' => [ + '$description' => 'This event triggers when a document is created.', + ], + 'update' => [ + '$description' => 'This event triggers when a document is updated.' + ], + 'upsert' => [ + '$description' => 'This event triggers when a document is upserted.', + ], + 'delete' => [ + '$description' => 'This event triggers when a document is deleted.' + ], + ], + 'indexes' => [ + '$model' => Response::MODEL_INDEX, + '$resource' => true, + '$description' => 'This event triggers on any indexes event.', + 'create' => [ + '$description' => 'This event triggers when an index is created.', + ], + 'delete' => [ + '$description' => 'This event triggers when an index is deleted.' + ], + 'update' => [ + '$description' => 'This event triggers when an index is updated.', + ], + ], + 'attributes' => [ + '$model' => Response::MODEL_ATTRIBUTE, + '$resource' => true, + '$description' => 'This event triggers on any attributes event.', + 'create' => [ + '$description' => 'This event triggers when an attribute is created.', + ], + 'update' => [ + '$description' => 'This event triggers when an attribute is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when an attribute is deleted.' + ] + ], + 'create' => [ + '$description' => 'This event triggers when a collection is created.' + ], + 'update' => [ + '$description' => 'This event triggers when a collection is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when a collection is deleted.', + ], + ], + 'create' => [ + '$description' => 'This event triggers when a database is created.' + ], + 'update' => [ + '$description' => 'This event triggers when a database is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when a database is deleted.', + ], + ], + 'vectorsdb' => [ + '$model' => Response::MODEL_DATABASE, + '$resource' => true, + '$description' => 'This event triggers on any VectorsDB database event.', + 'collections' => [ + '$model' => Response::MODEL_COLLECTION, + '$resource' => true, + '$description' => 'This event triggers on any collection event.', + 'documents' => [ + '$model' => Response::MODEL_DOCUMENT, + '$resource' => true, + '$description' => 'This event triggers on any documents event.', + 'create' => [ + '$description' => 'This event triggers when a document is created.', + ], + 'update' => [ + '$description' => 'This event triggers when a document is updated.' + ], + 'upsert' => [ + '$description' => 'This event triggers when a document is upserted.', + ], + 'delete' => [ + '$description' => 'This event triggers when a document is deleted.' + ], + ], + 'indexes' => [ + '$model' => Response::MODEL_INDEX, + '$resource' => true, + '$description' => 'This event triggers on any indexes event.', + 'create' => [ + '$description' => 'This event triggers when an index is created.', + ], + 'delete' => [ + '$description' => 'This event triggers when an index is deleted.' + ], + 'update' => [ + '$description' => 'This event triggers when an index is updated.', + ], + ], + 'attributes' => [ + '$model' => Response::MODEL_ATTRIBUTE, + '$resource' => true, + '$description' => 'This event triggers on any attributes event.', + 'create' => [ + '$description' => 'This event triggers when an attribute is created.', + ], + 'update' => [ + '$description' => 'This event triggers when an attribute is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when an attribute is deleted.' + ] + ], + 'create' => [ + '$description' => 'This event triggers when a collection is created.' + ], + 'update' => [ + '$description' => 'This event triggers when a collection is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when a collection is deleted.', + ], + ], + 'create' => [ + '$description' => 'This event triggers when a database is created.' + ], + 'update' => [ + '$description' => 'This event triggers when a database is updated.', + ], + 'delete' => [ + '$description' => 'This event triggers when a database is deleted.', + ], + ], 'buckets' => [ '$model' => Response::MODEL_BUCKET, '$resource' => true, diff --git a/app/config/roles.php b/app/config/roles.php index fbed21f0f8..4c09bd369d 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -18,6 +18,7 @@ $member = [ 'documents.write', 'rows.read', 'rows.write', + 'embeddings.write', 'files.read', 'files.write', 'projects.read', @@ -43,6 +44,7 @@ $admins = [ 'documents.write', 'rows.read', 'rows.write', + 'embeddings.write', 'files.read', 'files.write', 'buckets.read', @@ -129,6 +131,7 @@ return [ 'documents.write', 'rows.read', 'rows.write', + 'embeddings.write', 'files.read', 'files.write', 'locale.read', diff --git a/app/config/scopes/project.php b/app/config/scopes/project.php index 30073ccf50..9e5d0a6351 100644 --- a/app/config/scopes/project.php +++ b/app/config/scopes/project.php @@ -163,6 +163,10 @@ return [ 'description' => 'Access to create, update, and delete database table rows', 'category' => 'Databases', ], + 'embeddings.write' => [ + 'description' => 'Access to generate vector embeddings', + 'category' => 'Databases', + ], 'collections.read' => [ 'description' => 'Access to read database collections', 'category' => 'Databases', diff --git a/app/init/models.php b/app/init/models.php index 59da71b356..41651c9262 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -168,6 +168,7 @@ use Appwrite\Utopia\Response\Model\PlatformWeb; use Appwrite\Utopia\Response\Model\PlatformWindows; use Appwrite\Utopia\Response\Model\PolicyList; use Appwrite\Utopia\Response\Model\PolicyMembershipPrivacy; +use Appwrite\Utopia\Response\Model\PolicyMFAFactors; use Appwrite\Utopia\Response\Model\PolicyPasswordDictionary; use Appwrite\Utopia\Response\Model\PolicyPasswordHistory; use Appwrite\Utopia\Response\Model\PolicyPasswordPersonalData; @@ -473,6 +474,7 @@ Response::setModel(new PolicySessionInvalidation()); Response::setModel(new PolicySessionLimit()); Response::setModel(new PolicyUserLimit()); Response::setModel(new PolicyMembershipPrivacy()); +Response::setModel(new PolicyMFAFactors()); Response::setModel(new AuthProvider()); Response::setModel(new PlatformWeb()); Response::setModel(new PlatformApple()); diff --git a/composer.json b/composer.json index 033ebd0515..8aea34b8c2 100644 --- a/composer.json +++ b/composer.json @@ -81,7 +81,7 @@ "utopia-php/dsn": "0.2.1", "utopia-php/http": "^2.0@RC", "utopia-php/fetch": "^1.1", - "utopia-php/validators": "0.3.*", + "utopia-php/validators": "^0.4", "utopia-php/image": "0.8.*", "utopia-php/locale": "0.8.*", "utopia-php/lock": "0.2.*", diff --git a/composer.lock b/composer.lock index 4fa0167fda..fb6418cffd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "47fed84fa9ca6c3d7da20266a4e04587", + "content-hash": "b8f482127f1d48cb4026f35a161b15f5", "packages": [ { "name": "adhocore/jwt", @@ -157,7 +157,6 @@ "issues": "https://github.com/appwrite/php-clamav/issues", "source": "https://github.com/appwrite/php-clamav/tree/2.0.0" }, - "abandoned": true, "time": "2023-02-24T09:50:42+00:00" }, { @@ -3168,16 +3167,16 @@ }, { "name": "utopia-php/audit", - "version": "3.0.0", + "version": "3.0.1", "source": { "type": "git", "url": "https://github.com/utopia-php/audit.git", - "reference": "065b1dc5da8e7eabde4b8bfb4217d0a78d1bf9bd" + "reference": "fd584e0892b636ac3dfb3e6463b96f9ec0a58818" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/audit/zipball/065b1dc5da8e7eabde4b8bfb4217d0a78d1bf9bd", - "reference": "065b1dc5da8e7eabde4b8bfb4217d0a78d1bf9bd", + "url": "https://api.github.com/repos/utopia-php/audit/zipball/fd584e0892b636ac3dfb3e6463b96f9ec0a58818", + "reference": "fd584e0892b636ac3dfb3e6463b96f9ec0a58818", "shasum": "" }, "require": { @@ -3185,7 +3184,7 @@ "utopia-php/database": "^7.0.0", "utopia-php/fetch": "^1.1", "utopia-php/query": "0.1.*", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "type": "library", "autoload": { @@ -3207,9 +3206,9 @@ ], "support": { "issues": "https://github.com/utopia-php/audit/issues", - "source": "https://github.com/utopia-php/audit/tree/3.0.0" + "source": "https://github.com/utopia-php/audit/tree/3.0.1" }, - "time": "2026-07-31T14:52:39+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/auth", @@ -3638,16 +3637,16 @@ }, { "name": "utopia-php/database", - "version": "7.1.1", + "version": "7.1.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "80b5b3d85556d9d17d882e1bb972d239a634ebdd" + "reference": "9eed1f1ed7da154ca7356c5f051bbc192e7b3c73" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/80b5b3d85556d9d17d882e1bb972d239a634ebdd", - "reference": "80b5b3d85556d9d17d882e1bb972d239a634ebdd", + "url": "https://api.github.com/repos/utopia-php/database/zipball/9eed1f1ed7da154ca7356c5f051bbc192e7b3c73", + "reference": "9eed1f1ed7da154ca7356c5f051bbc192e7b3c73", "shasum": "" }, "require": { @@ -3660,7 +3659,7 @@ "utopia-php/console": "0.1.*", "utopia-php/mongo": "1.*", "utopia-php/pools": "2.*", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "require-dev": { "fakerphp/faker": "1.23.*", @@ -3692,9 +3691,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/7.1.1" + "source": "https://github.com/utopia-php/database/tree/7.1.2" }, - "time": "2026-08-09T01:36:59+00:00" + "time": "2026-08-10T04:39:43+00:00" }, { "name": "utopia-php/detector", @@ -3791,16 +3790,16 @@ }, { "name": "utopia-php/dns", - "version": "3.0.0", + "version": "3.0.1", "source": { "type": "git", "url": "https://github.com/utopia-php/dns.git", - "reference": "1de89cfe0c616159b8af94cfcfd8b2f12e42784c" + "reference": "0762b38095c67149b5b1c77392d870c1b24ffe27" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/dns/zipball/1de89cfe0c616159b8af94cfcfd8b2f12e42784c", - "reference": "1de89cfe0c616159b8af94cfcfd8b2f12e42784c", + "url": "https://api.github.com/repos/utopia-php/dns/zipball/0762b38095c67149b5b1c77392d870c1b24ffe27", + "reference": "0762b38095c67149b5b1c77392d870c1b24ffe27", "shasum": "" }, "require": { @@ -3808,7 +3807,7 @@ "php": ">=8.5", "utopia-php/domains": "^3.0", "utopia-php/telemetry": "^0.4", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "require-dev": { "swoole/ide-helper": "5.1.8" @@ -3842,9 +3841,9 @@ ], "support": { "issues": "https://github.com/utopia-php/dns/issues", - "source": "https://github.com/utopia-php/dns/tree/3.0.0" + "source": "https://github.com/utopia-php/dns/tree/3.0.1" }, - "time": "2026-07-31T15:11:37+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/domains", @@ -4058,16 +4057,16 @@ }, { "name": "utopia-php/http", - "version": "2.0.0-rc20", + "version": "2.0.0-rc21", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "6a1c7fe9305fd5adad9ba97bae3f6a6faca7a711" + "reference": "9d8d374926dd2d3c35808deb72199ab943355d64" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/6a1c7fe9305fd5adad9ba97bae3f6a6faca7a711", - "reference": "6a1c7fe9305fd5adad9ba97bae3f6a6faca7a711", + "url": "https://api.github.com/repos/utopia-php/http/zipball/9d8d374926dd2d3c35808deb72199ab943355d64", + "reference": "9d8d374926dd2d3c35808deb72199ab943355d64", "shasum": "" }, "require": { @@ -4078,7 +4077,7 @@ "utopia-php/servers": "^0.4", "utopia-php/system": "^0.10", "utopia-php/telemetry": "^0.4", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "require-dev": { "swoole/ide-helper": "4.8.3" @@ -4105,9 +4104,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/2.0.0-rc20" + "source": "https://github.com/utopia-php/http/tree/2.0.0-rc21" }, - "time": "2026-08-05T18:37:51+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/image", @@ -4725,16 +4724,16 @@ }, { "name": "utopia-php/queue", - "version": "1.1.3", + "version": "1.1.4", "source": { "type": "git", "url": "https://github.com/utopia-php/queue.git", - "reference": "fc32fce2616a56b84923761a77b8f0c8e66e403e" + "reference": "ade5660eadcd042a313d1ed4aa25d260a2737b5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/queue/zipball/fc32fce2616a56b84923761a77b8f0c8e66e403e", - "reference": "fc32fce2616a56b84923761a77b8f0c8e66e403e", + "url": "https://api.github.com/repos/utopia-php/queue/zipball/ade5660eadcd042a313d1ed4aa25d260a2737b5c", + "reference": "ade5660eadcd042a313d1ed4aa25d260a2737b5c", "shasum": "" }, "require": { @@ -4744,7 +4743,7 @@ "utopia-php/pools": "^2.0", "utopia-php/servers": "^0.4", "utopia-php/telemetry": "^0.4", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "require-dev": { "ext-redis": "*", @@ -4783,9 +4782,9 @@ ], "support": { "issues": "https://github.com/utopia-php/queue/issues", - "source": "https://github.com/utopia-php/queue/tree/1.1.3" + "source": "https://github.com/utopia-php/queue/tree/1.1.4" }, - "time": "2026-08-05T12:34:31+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/registry", @@ -4841,22 +4840,22 @@ }, { "name": "utopia-php/servers", - "version": "0.4.6", + "version": "0.4.7", "source": { "type": "git", "url": "https://github.com/utopia-php/servers.git", - "reference": "036c4da1c8b9a5bba1a973270158ba745a5944c9" + "reference": "18572544b1da5af415e5abbd59ba00af29e37de7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/servers/zipball/036c4da1c8b9a5bba1a973270158ba745a5944c9", - "reference": "036c4da1c8b9a5bba1a973270158ba745a5944c9", + "url": "https://api.github.com/repos/utopia-php/servers/zipball/18572544b1da5af415e5abbd59ba00af29e37de7", + "reference": "18572544b1da5af415e5abbd59ba00af29e37de7", "shasum": "" }, "require": { "php": ">=8.5", "utopia-php/di": "^0.3", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "type": "library", "autoload": { @@ -4884,9 +4883,9 @@ ], "support": { "issues": "https://github.com/utopia-php/servers/issues", - "source": "https://github.com/utopia-php/servers/tree/0.4.6" + "source": "https://github.com/utopia-php/servers/tree/0.4.7" }, - "time": "2026-07-13T11:15:40+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/span", @@ -4930,16 +4929,16 @@ }, { "name": "utopia-php/storage", - "version": "4.0.2", + "version": "4.0.3", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "64a391f5dcf043458868322dfc4d402e4d154c4c" + "reference": "f8b122e753c01f30f774d390d7a36f030dfaef46" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/64a391f5dcf043458868322dfc4d402e4d154c4c", - "reference": "64a391f5dcf043458868322dfc4d402e4d154c4c", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/f8b122e753c01f30f774d390d7a36f030dfaef46", + "reference": "f8b122e753c01f30f774d390d7a36f030dfaef46", "shasum": "" }, "require": { @@ -4952,7 +4951,7 @@ "utopia-php/client": "0.2.* || 0.3.*", "utopia-php/psr7": "0.2.*", "utopia-php/telemetry": "^0.4.6", - "utopia-php/validators": "0.3.*" + "utopia-php/validators": "^0.4" }, "type": "library", "autoload": { @@ -4974,9 +4973,9 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/4.0.2" + "source": "https://github.com/utopia-php/storage/tree/4.0.3" }, - "time": "2026-08-05T18:07:20+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/system", @@ -5124,16 +5123,16 @@ }, { "name": "utopia-php/validators", - "version": "0.3.2", + "version": "0.4.1", "source": { "type": "git", "url": "https://github.com/utopia-php/validators.git", - "reference": "444a3dbfcd5f2d167214f07f6e8a9962048def1c" + "reference": "61832b7c863831466ccdeccd4e4f76a43765fbbf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/validators/zipball/444a3dbfcd5f2d167214f07f6e8a9962048def1c", - "reference": "444a3dbfcd5f2d167214f07f6e8a9962048def1c", + "url": "https://api.github.com/repos/utopia-php/validators/zipball/61832b7c863831466ccdeccd4e4f76a43765fbbf", + "reference": "61832b7c863831466ccdeccd4e4f76a43765fbbf", "shasum": "" }, "require": { @@ -5158,9 +5157,9 @@ ], "support": { "issues": "https://github.com/utopia-php/validators/issues", - "source": "https://github.com/utopia-php/validators/tree/0.4.0" + "source": "https://github.com/utopia-php/validators/tree/0.4.1" }, - "time": "2026-08-07T10:53:49+00:00" + "time": "2026-08-10T04:15:37+00:00" }, { "name": "utopia-php/vcs", @@ -5420,16 +5419,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "2.11.1", + "version": "2.10.1", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "4bf377c6b8c4f9620d8e5fd525ab6160bdc95a41" + "reference": "3019e3e939cc5f12a41dab98ded1043423467dd0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/4bf377c6b8c4f9620d8e5fd525ab6160bdc95a41", - "reference": "4bf377c6b8c4f9620d8e5fd525ab6160bdc95a41", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/3019e3e939cc5f12a41dab98ded1043423467dd0", + "reference": "3019e3e939cc5f12a41dab98ded1043423467dd0", "shasum": "" }, "require": { @@ -5466,9 +5465,9 @@ "description": "SDK generator for Appwrite APIs across multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/2.11.1" + "source": "https://github.com/appwrite/sdk-generator/tree/2.10.1" }, - "time": "2026-08-10T11:13:18+00:00" + "time": "2026-08-06T07:56:50+00:00" }, { "name": "brianium/paratest", @@ -5750,16 +5749,16 @@ }, { "name": "laravel/pint", - "version": "v1.30.5", + "version": "v1.29.3", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "fe4148c503a0e266353d61396b79bbf7f35122df" + "reference": "da1d1111a6aa2e082d2a388b194afe1ba0a05d14" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/fe4148c503a0e266353d61396b79bbf7f35122df", - "reference": "fe4148c503a0e266353d61396b79bbf7f35122df", + "url": "https://api.github.com/repos/laravel/pint/zipball/da1d1111a6aa2e082d2a388b194afe1ba0a05d14", + "reference": "da1d1111a6aa2e082d2a388b194afe1ba0a05d14", "shasum": "" }, "require": { @@ -5767,19 +5766,17 @@ "ext-mbstring": "*", "ext-tokenizer": "*", "ext-xml": "*", - "php": "^8.3.0" + "php": "^8.2.0" }, "require-dev": { - "composer/semver": "^3.4.4", - "friendsofphp/php-cs-fixer": "^3.95.18", - "illuminate/view": "^13.24.0", + "friendsofphp/php-cs-fixer": "^3.95.8", + "illuminate/view": "^12.62.0", "larastan/larastan": "^3.10.0", - "laravel-zero/framework": "^13.0.0", + "laravel-zero/framework": "^12.1.0", "laravel/agent-detector": "^2.0.2", - "laravel/prompts": "^0.3.22", "mockery/mockery": "^1.6.12", "nunomaduro/termwind": "^2.4.0", - "pestphp/pest": "^4.7.8" + "pestphp/pest": "^3.8.6" }, "bin": [ "builds/pint" @@ -5816,7 +5813,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2026-08-10T15:35:50+00:00" + "time": "2026-06-16T15:34:04+00:00" }, { "name": "matthiasmullie/minify", @@ -6178,11 +6175,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.2.8", + "version": "2.2.6", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e285254e60f33c21902efef4a926ca0987c06804", - "reference": "e285254e60f33c21902efef4a926ca0987c06804", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/a6e9b5a9420f6109c091e87d82683bd1a80b87ed", + "reference": "a6e9b5a9420f6109c091e87d82683bd1a80b87ed", "shasum": "" }, "require": { @@ -6238,7 +6235,7 @@ "type": "github" } ], - "time": "2026-08-04T22:21:45+00:00" + "time": "2026-07-26T21:22:49+00:00" }, { "name": "phpunit/php-code-coverage", @@ -6587,16 +6584,16 @@ }, { "name": "phpunit/phpunit", - "version": "12.5.33", + "version": "12.5.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "b98e028a26c5c5ba7e4a54be96ccf35f2914d184" + "reference": "c02591dd7840ed124a98d7770753329ad28e9f8f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b98e028a26c5c5ba7e4a54be96ccf35f2914d184", - "reference": "b98e028a26c5c5ba7e4a54be96ccf35f2914d184", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c02591dd7840ed124a98d7770753329ad28e9f8f", + "reference": "c02591dd7840ed124a98d7770753329ad28e9f8f", "shasum": "" }, "require": { @@ -6665,7 +6662,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/12.5.33" + "source": "https://github.com/sebastianbergmann/phpunit/tree/12.5.32" }, "funding": [ { @@ -6673,20 +6670,20 @@ "type": "other" } ], - "time": "2026-07-28T13:58:09+00:00" + "time": "2026-07-25T06:54:13+00:00" }, { "name": "rector/rector", - "version": "2.6.1", + "version": "2.5.8", "source": { "type": "git", "url": "https://github.com/rectorphp/rector.git", - "reference": "b8e68f058bca43e01a2e1caa51ef022d6551ed95" + "reference": "861c77fd2a6d45317a401f4e0b617f947e8b6f96" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/rectorphp/rector/zipball/b8e68f058bca43e01a2e1caa51ef022d6551ed95", - "reference": "b8e68f058bca43e01a2e1caa51ef022d6551ed95", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/861c77fd2a6d45317a401f4e0b617f947e8b6f96", + "reference": "861c77fd2a6d45317a401f4e0b617f947e8b6f96", "shasum": "" }, "require": { @@ -6725,7 +6722,7 @@ ], "support": { "issues": "https://github.com/rectorphp/rector/issues", - "source": "https://github.com/rectorphp/rector/tree/2.6.1" + "source": "https://github.com/rectorphp/rector/tree/2.5.8" }, "funding": [ { @@ -6733,7 +6730,7 @@ "type": "github" } ], - "time": "2026-08-03T17:30:34+00:00" + "time": "2026-07-27T06:19:16+00:00" }, { "name": "sebastian/cli-parser", @@ -7730,16 +7727,16 @@ }, { "name": "symfony/console", - "version": "v8.1.4", + "version": "v8.1.1", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "68efa2ebfd9a362951eb5a8b09fd177c66ddec24" + "reference": "b711a8ab808b6c074c6b8caef70d0fd8d6b6d07d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/68efa2ebfd9a362951eb5a8b09fd177c66ddec24", - "reference": "68efa2ebfd9a362951eb5a8b09fd177c66ddec24", + "url": "https://api.github.com/repos/symfony/console/zipball/b711a8ab808b6c074c6b8caef70d0fd8d6b6d07d", + "reference": "b711a8ab808b6c074c6b8caef70d0fd8d6b6d07d", "shasum": "" }, "require": { @@ -7806,7 +7803,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v8.1.4" + "source": "https://github.com/symfony/console/tree/v8.1.1" }, "funding": [ { @@ -7826,7 +7823,7 @@ "type": "tidelift" } ], - "time": "2026-07-31T12:43:13+00:00" + "time": "2026-06-16T12:55:20+00:00" }, { "name": "symfony/polyfill-ctype", @@ -7913,16 +7910,16 @@ }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.41.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "bb899c1db0aa8127dc3afe8cda4a67eb24915f8d" + "reference": "e9247d281d694a5120554d9afaf54e070e88a603" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/bb899c1db0aa8127dc3afe8cda4a67eb24915f8d", - "reference": "bb899c1db0aa8127dc3afe8cda4a67eb24915f8d", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/e9247d281d694a5120554d9afaf54e070e88a603", + "reference": "e9247d281d694a5120554d9afaf54e070e88a603", "shasum": "" }, "require": { @@ -7971,7 +7968,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.41.0" + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.38.1" }, "funding": [ { @@ -7991,7 +7988,7 @@ "type": "tidelift" } ], - "time": "2026-07-28T08:25:59+00:00" + "time": "2026-05-26T05:58:03+00:00" }, { "name": "symfony/polyfill-intl-normalizer", @@ -8145,16 +8142,16 @@ }, { "name": "symfony/string", - "version": "v8.1.2", + "version": "v8.1.0", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "286a76b7255e5cc4bf0101a0bc5388ecf1c38ccc" + "reference": "afd5944f4005862d961efb85c8bbd5c523c4e3c9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/286a76b7255e5cc4bf0101a0bc5388ecf1c38ccc", - "reference": "286a76b7255e5cc4bf0101a0bc5388ecf1c38ccc", + "url": "https://api.github.com/repos/symfony/string/zipball/afd5944f4005862d961efb85c8bbd5c523c4e3c9", + "reference": "afd5944f4005862d961efb85c8bbd5c523c4e3c9", "shasum": "" }, "require": { @@ -8211,7 +8208,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v8.1.2" + "source": "https://github.com/symfony/string/tree/v8.1.0" }, "funding": [ { @@ -8231,7 +8228,7 @@ "type": "tidelift" } ], - "time": "2026-07-28T07:35:25+00:00" + "time": "2026-05-29T05:06:50+00:00" }, { "name": "textalk/websocket", @@ -8439,5 +8436,5 @@ "platform-dev": { "ext-fileinfo": "*" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.9.0" } diff --git a/docs/references/vcs/create-bitbucket-installation.md b/docs/references/vcs/create-bitbucket-installation.md new file mode 100644 index 0000000000..60da9e01fc --- /dev/null +++ b/docs/references/vcs/create-bitbucket-installation.md @@ -0,0 +1 @@ +Begin Appwrite's Bitbucket OAuth authorization to set up version control integration. This endpoint responds with a redirect URL to Bitbucket's authorization page. The Bitbucket OAuth consumer must be configured in your environment for this endpoint to work. \ No newline at end of file diff --git a/docs/references/vcs/create-gitea-installation.md b/docs/references/vcs/create-gitea-installation.md new file mode 100644 index 0000000000..8912b067fb --- /dev/null +++ b/docs/references/vcs/create-gitea-installation.md @@ -0,0 +1 @@ +Begin Appwrite's Gitea OAuth authorization to set up version control integration. This endpoint responds with a redirect URL to Gitea's authorization page. The Gitea OAuth application must be configured in your environment for this endpoint to work. \ No newline at end of file diff --git a/docs/references/vcs/create-gitlab-installation.md b/docs/references/vcs/create-gitlab-installation.md new file mode 100644 index 0000000000..6058b87ee2 --- /dev/null +++ b/docs/references/vcs/create-gitlab-installation.md @@ -0,0 +1 @@ +Begin Appwrite's GitLab OAuth authorization to set up version control integration. This endpoint responds with a redirect URL to GitLab's authorization page. The GitLab OAuth application must be configured in your environment for this endpoint to work. \ No newline at end of file diff --git a/src/Appwrite/Auth/Validator/Phone.php b/src/Appwrite/Auth/Validator/Phone.php index 667d8e7857..855a2aaa24 100644 --- a/src/Appwrite/Auth/Validator/Phone.php +++ b/src/Appwrite/Auth/Validator/Phone.php @@ -3,22 +3,15 @@ namespace Appwrite\Auth\Validator; use Utopia\Messaging\Adapter\SMS\GEOSMS\CallingCode; -use Utopia\Validator; +use Utopia\Validator\Phone as UtopiaPhone; /** * Phone. * * Validates a number for the E.164 format. */ -class Phone extends Validator +class Phone extends UtopiaPhone { - protected bool $allowEmpty; - - public function __construct(bool $allowEmpty = false) - { - $this->allowEmpty = $allowEmpty; - } - /** * Get Description. * @@ -38,44 +31,18 @@ class Phone extends Validator * * @return bool */ - public function isValid($value): bool + public function isValid(mixed $value): bool { - if (!is_string($value)) { + if (!parent::isValid($value)) { return false; } - if ($this->allowEmpty && \strlen($value) === 0) { + if ($this->allowEmpty && $value === '') { return true; } - if (CallingCode::fromPhoneNumber($value) === null) { - return false; - } + $value = $this->normalize ? self::normalize($value) : $value; - return !!\preg_match('/^\+[1-9]\d{6,14}$/', $value); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; + return CallingCode::fromPhoneNumber($value) !== null; } } diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index c26db65002..f437a3b1c6 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -83,6 +83,15 @@ class V25 extends Migration $this->dbForProject->purgeCachedCollection($id); break; + case 'challenges': + try { + $this->createIndexFromCollection($this->dbForProject, $id, '_key_expire'); + } catch (Throwable $th) { + Console::warning("Failed to create index \"_key_expire\" from {$id}: {$th->getMessage()}"); + } + $this->dbForProject->purgeCachedCollection($id); + break; + case 'databases': if ($collectionType === 'projects') { try { diff --git a/src/Appwrite/Network/Cors.php b/src/Appwrite/Network/Cors.php index 593c82ab24..aa80970721 100644 --- a/src/Appwrite/Network/Cors.php +++ b/src/Appwrite/Network/Cors.php @@ -7,9 +7,9 @@ use Utopia\Validator\Hostname; /** * Generate CORS response headers for an incoming request. * - * Allowed origins are matched by hostname only. Arrays passed to the - * constructor (methods, headers, exposed headers) are formatted into - * comma-separated header strings. + * Allowed origins are matched by hostname only, with loopback hostnames always + * accepted. Arrays passed to the constructor (methods, headers, exposed + * headers) are formatted into comma-separated header strings. */ final class Cors { @@ -77,13 +77,20 @@ final class Cors return $headers; } - // Match only by host + // Match only by host, falling back to the alias the host spells out. + // See Platform::LOOPBACK_ALIASES -- responses here carry credentials. $validator = new Hostname($this->allowedHosts); if (!$validator->isValid($host)) { - return $headers; + $alias = \in_array($host, Platform::LOOPBACK_ALIASES, true) + && $validator->isValid(Platform::LOOPBACK_HOSTNAME); + + if (!$alias) { + return $headers; + } } - // Accepted + // Accepted. Echo the caller's origin exactly, never a wildcard, so the + // browser's own literal-match rule stays the last line of defence. $headers[self::HEADER_ALLOW_ORIGIN] = $origin; return $headers; diff --git a/src/Appwrite/Network/Platform.php b/src/Appwrite/Network/Platform.php index 23fb087010..c20c012862 100644 --- a/src/Appwrite/Network/Platform.php +++ b/src/Appwrite/Network/Platform.php @@ -27,6 +27,31 @@ class Platform public const SCHEME_LINUX = 'appwrite-linux'; public const SCHEME_TAURI = 'tauri'; + public const string LOOPBACK_HOSTNAME = 'localhost'; + + /** + * Other spellings of LOOPBACK_HOSTNAME. They address the same interface, so + * they resolve to it when matching an allow list: a deployment that trusts + * `localhost` trusts these too, and one that does not trusts neither. IPv6 + * is bracketed to match what parse_url() returns for a host. + * + * They are aliases rather than an unconditional allowance on purpose. + * Session cookies are SameSite=None (see `cookieSamesite`) and CORS runs + * with credentials, so any origin accepted here can read authenticated + * responses. Matching is host-only, which means every port on the interface + * counts -- trusting loopback outright would hand that to any local web UI, + * including one with an XSS. Tying it to the configured allow list keeps the + * decision with whoever set _APP_DOMAIN / _APP_CONSOLE_DOMAIN. + * + * Match exactly. A prefix or substring test would wrongly accept remote + * hosts such as `127.0.0.1.example.com`, `xlocalhost` or `[::1].evil.com`. + * Spellings outside this list (`127.0.0.2`, `0:0:0:0:0:0:0:1`, `localhost.`) + * fail closed. + * + * @var array + */ + public const array LOOPBACK_ALIASES = ['127.0.0.1', '[::1]']; + /** * @var array Map scheme types to user-friendly platform names. */ diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index 3c4e8a254a..1d4cd79ea1 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -73,7 +73,13 @@ class Origin extends Validator ]; if (in_array($this->scheme, $webPlatforms, true)) { $validator = new Hostname($this->allowedHostnames); - return $validator->isValid($this->host); + if ($validator->isValid($this->host)) { + return true; + } + + /* Fall back to the alias, see Platform::LOOPBACK_ALIASES */ + return in_array($this->host, Platform::LOOPBACK_ALIASES, true) + && $validator->isValid(Platform::LOOPBACK_HOSTNAME); } if (!empty($this->scheme) && in_array($this->scheme, $this->allowedSchemes, true)) { diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php index 83d0a5112c..e080dfc508 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php @@ -131,6 +131,19 @@ class Create extends Action ProofsToken $proofForToken, ProofsCode $proofForCode ): void { + $mfaFactors = $project->getAttribute('auths', [])['mfaFactors'] ?? []; + $factorEnabled = match ($factor) { + Type::TOTP => $mfaFactors['totp'] ?? true, + Type::EMAIL => $mfaFactors['email'] ?? true, + Type::PHONE => $mfaFactors['phone'] ?? true, + Type::CUSTOM => $mfaFactors['custom'] ?? false, + default => true, // Recovery codes always remain available as a fallback + }; + + if (!$factorEnabled) { + throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'The requested factor is disabled by the MFA factors policy'); + } + $expire = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), TOKEN_EXPIRATION_CONFIRM)); $code = $proofForCode->generate(); diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php index f34deb2ab9..a12b8f9fcf 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php @@ -105,8 +105,26 @@ class Update extends Action throw new Exception(Exception::USER_INVALID_TOKEN); } + if ($challenge->getAttribute('expire') < DateTime::formatTz(DateTime::now())) { + throw new Exception(Exception::USER_INVALID_TOKEN); + } + $type = $challenge->getAttribute('type'); + // Consult the current policy so disabling a factor also invalidates outstanding challenges + $mfaFactors = $project->getAttribute('auths', [])['mfaFactors'] ?? []; + $factorEnabled = match ($type) { + Type::TOTP => $mfaFactors['totp'] ?? true, + Type::EMAIL => $mfaFactors['email'] ?? true, + Type::PHONE => $mfaFactors['phone'] ?? true, + Type::CUSTOM => $mfaFactors['custom'] ?? false, + default => true, // Recovery codes always remain available as a fallback + }; + + if (!$factorEnabled) { + throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'The requested factor is disabled by the MFA factors policy'); + } + $recoveryCodeChallenge = function (Document $challenge, Document $user, string $otp) use ($dbForProject) { if ( $challenge->isSet('type') && diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Factors/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Factors/XList.php index aa8ef7a198..be77f4bd3e 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Factors/XList.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Factors/XList.php @@ -68,21 +68,25 @@ class XList extends Action ]) ->inject('response') ->inject('user') + ->inject('project') ->callback($this->action(...)); } - public function action(Response $response, Document $user): void + public function action(Response $response, Document $user, Document $project): void { $mfaRecoveryCodes = $user->getAttribute('mfaRecoveryCodes', []); $recoveryCodeEnabled = \is_array($mfaRecoveryCodes) && \count($mfaRecoveryCodes) > 0; $totp = TOTP::getAuthenticatorFromUser($user); + $mfaFactors = $project->getAttribute('auths', [])['mfaFactors'] ?? []; + $factors = new Document([ - Type::TOTP => $totp !== null && $totp->getAttribute('verified', false), - Type::EMAIL => $user->getAttribute('email', false) && $user->getAttribute('emailVerification', false), - Type::PHONE => $user->getAttribute('phone', false) && $user->getAttribute('phoneVerification', false), - Type::RECOVERY_CODE => $recoveryCodeEnabled + Type::TOTP => ($mfaFactors['totp'] ?? true) && $totp !== null && $totp->getAttribute('verified', false), + Type::EMAIL => ($mfaFactors['email'] ?? true) && $user->getAttribute('email', false) && $user->getAttribute('emailVerification', false), + Type::PHONE => ($mfaFactors['phone'] ?? true) && $user->getAttribute('phone', false) && $user->getAttribute('phoneVerification', false), + Type::RECOVERY_CODE => $recoveryCodeEnabled, + Type::CUSTOM => $mfaFactors['custom'] ?? false ]); $response->dynamic($factors, Response::MODEL_MFA_FACTORS); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php index 7f11a2a05b..2851ebdf64 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php @@ -9,6 +9,7 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Deprecated; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Utopia\Database\Attribute; use Appwrite\Utopia\Database\Validator\Attributes as AttributesValidator; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Database\Validator\Indexes as IndexesValidator; @@ -81,7 +82,7 @@ class Create extends Action ->param('permissions', null, new Nullable(new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE)), 'An array of permissions strings. By default, no user is granted with any permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).', true) ->param('documentSecurity', false, new Boolean(true), 'Enables configuring permissions for individual documents. A user needs one of document or collection level permissions to access a document. [Learn more about permissions](https://appwrite.io/docs/permissions).', true) ->param('enabled', true, new Boolean(), 'Is collection enabled? When set to \'disabled\', users cannot access the collection but Server SDKs with and API key can still read and write to the collection. No data is lost when this is toggled.', true) - ->param('attributes', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of attribute definitions to create. Each attribute should contain: key (string), type (string: string, integer, float, boolean, datetime), size (integer, required for string type), required (boolean, optional), default (mixed, optional), array (boolean, optional), and type-specific options.', true) + ->param('attributes', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of attribute definitions to create. Each attribute should contain: key (string), type (string: string, varchar, text, mediumtext, longtext, integer, bigint, float, boolean, datetime, email, url, ip, enum), size (integer, required for string and varchar types), required (boolean, optional), default (mixed, optional), array (boolean, optional), and type-specific options.', true) ->param('indexes', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of index definitions to create. Each index should contain: key (string), type (string: key, fulltext, unique, spatial), attributes (array of attribute keys), orders (array of ASC/DESC, optional), and lengths (array of integers, optional).', true) ->inject('response') ->inject('dbForProject') @@ -279,13 +280,12 @@ class Create extends Action Document $collection, array $attribute, ): array { + ['type' => $type, 'format' => $format, 'size' => $size] = Attribute::resolve($attribute); + $key = $attribute['key']; - $type = $attribute['type']; - $size = $attribute['size'] ?? 0; $required = $attribute['required'] ?? false; $signed = $attribute['signed'] ?? true; $array = $attribute['array'] ?? false; - $format = $attribute['format'] ?? ''; $formatOptions = []; $filters = $attribute['filters'] ?? []; $default = $attribute['default'] ?? null; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Embeddings/Text/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Embeddings/Text/Create.php index 2967c29643..2821cb295e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Embeddings/Text/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Embeddings/Text/Create.php @@ -46,7 +46,7 @@ class Create extends CreateDocumentAction ->setHttpPath('/v1/embeddings/text') ->desc('Create text embeddings') ->groups(['api', 'database']) - ->label('scope', 'documents.write') + ->label('scope', 'embeddings.write') ->label('resourceType', RESOURCE_TYPE_EMBEDDINGS_TEXT) ->label('audits.event', 'embedding.create') ->label('audits.resource', 'embeddings/text') diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Create.php index 16aabc877d..2623b7ac94 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Create.php @@ -64,7 +64,7 @@ class Create extends CollectionCreate ->param('permissions', null, new Nullable(new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE)), 'An array of permissions strings. By default, no user is granted with any permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).', true) ->param('rowSecurity', false, new Boolean(true), 'Enables configuring permissions for individual rows. A user needs one of row or table level permissions to access a row. [Learn more about permissions](https://appwrite.io/docs/permissions).', true) ->param('enabled', true, new Boolean(), 'Is table enabled? When set to \'disabled\', users cannot access the table but Server SDKs with and API key can still read and write to the table. No data is lost when this is toggled.', true) - ->param('columns', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of column definitions to create. Each column should contain: key (string), type (string: string, integer, float, boolean, datetime, relationship), size (integer, required for string type), required (boolean, optional), default (mixed, optional), array (boolean, optional), and type-specific options.', true) + ->param('columns', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of column definitions to create. Each column should contain: key (string), type (string: string, varchar, text, mediumtext, longtext, integer, bigint, float, boolean, datetime, email, url, ip, enum), size (integer, required for string and varchar types), required (boolean, optional), default (mixed, optional), array (boolean, optional), and type-specific options.', true) ->param('indexes', [], new ArrayList(new JSONObject(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Array of index definitions to create. Each index should contain: key (string), type (string: key, fulltext, unique, spatial), attributes (array of column keys), orders (array of ASC/DESC, optional), and lengths (array of integers, optional).', true) ->inject('response') ->inject('dbForProject') diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Audits/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Audits/Get.php deleted file mode 100644 index 407490a512..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Audits/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/audits') - ->desc('Get audits queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForAudits') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, Audit $publisherForAudits, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForAudits->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Builds/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Builds/Get.php deleted file mode 100644 index 86514182a0..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Builds/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/builds') - ->desc('Get builds queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForBuilds') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, BuildPublisher $publisherForBuilds, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForBuilds->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Certificates/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Certificates/Get.php deleted file mode 100644 index 412a928f08..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Certificates/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/certificates') - ->desc('Get certificates queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForCertificates') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, Certificate $publisherForCertificates, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForCertificates->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Databases/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Databases/Get.php deleted file mode 100644 index 9b74be9029..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Databases/Get.php +++ /dev/null @@ -1,44 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/databases') - ->desc('Get databases queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('name', 'database_db_main', new Text(256), 'Queue name for which to check the queue size', true) - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForDatabase') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(string $name, int|string $threshold, Database $publisherForDatabase, Response $response): void - { - $threshold = (int) $threshold; - $size = $publisherForDatabase->getSize(queue: new Queue($name)); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Deletes/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Deletes/Get.php deleted file mode 100644 index 296274d67f..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Deletes/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/deletes') - ->desc('Get deletes queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForDeletes') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, Delete $publisherForDeletes, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForDeletes->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Functions/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Functions/Get.php deleted file mode 100644 index 07f8f9afb5..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Functions/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/functions') - ->desc('Get functions queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForFunctions') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, FunctionPublisher $publisherForFunctions, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForFunctions->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Mails/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Mails/Get.php deleted file mode 100644 index ffe8b2c4e0..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Mails/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/mails') - ->desc('Get mails queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForMails') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, MailPublisher $publisherForMails, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForMails->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Messaging/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Messaging/Get.php deleted file mode 100644 index 13e96e3062..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Messaging/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/messaging') - ->desc('Get messaging queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForMessaging') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, MessagingPublisher $publisherForMessaging, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForMessaging->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Migrations/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Migrations/Get.php deleted file mode 100644 index eaff76bcb7..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Migrations/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/migrations') - ->desc('Get migrations queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForMigrations') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, MigrationPublisher $publisherForMigrations, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForMigrations->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php deleted file mode 100644 index 4f3698597e..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/notifications') - ->desc('Get notifications queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForNotifications') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, NotificationPublisher $publisherForNotifications, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForNotifications->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsResources/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsResources/Get.php deleted file mode 100644 index f6e9fcc930..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsResources/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/stats-resources') - ->desc('Get stats resources queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForStatsResources') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, StatsResourcesPublisher $publisherForStatsResources, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForStatsResources->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsUsage/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsUsage/Get.php deleted file mode 100644 index a8fd755bfb..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsUsage/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/stats-usage') - ->desc('Get stats usage queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('publisherForUsage') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, UsagePublisher $publisherForUsage, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForUsage->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Webhooks/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Webhooks/Get.php deleted file mode 100644 index 11422b6f52..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Webhooks/Get.php +++ /dev/null @@ -1,42 +0,0 @@ -setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/health/queue/webhooks') - ->desc('Get webhooks queue') - ->groups(['api', 'health']) - ->label('scope', 'health.read') - ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('queueForWebhooks') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, Webhook $queueForWebhooks, Response $response): void - { - $threshold = (int) $threshold; - - $size = $queueForWebhooks->getSize(); - - $this->assertQueueThreshold($size, $threshold); - - $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); - } -} diff --git a/src/Appwrite/Platform/Modules/Health/Services/Http.php b/src/Appwrite/Platform/Modules/Health/Services/Http.php index 16b7a8fca8..eeadcb4291 100644 --- a/src/Appwrite/Platform/Modules/Health/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Health/Services/Http.php @@ -9,20 +9,7 @@ use Appwrite\Platform\Modules\Health\Http\Health\DB\Get as GetDB; use Appwrite\Platform\Modules\Health\Http\Health\Geo\Get as GetGeo; use Appwrite\Platform\Modules\Health\Http\Health\Get as GetHealth; use Appwrite\Platform\Modules\Health\Http\Health\PubSub\Get as GetPubSub; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Audits\Get as GetQueueAudits; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Builds\Get as GetQueueBuilds; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Certificates\Get as GetQueueCertificates; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Databases\Get as GetQueueDatabases; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Deletes\Get as GetQueueDeletes; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Failed\Get as GetFailedJobs; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Functions\Get as GetQueueFunctions; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Mails\Get as GetQueueMails; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Messaging\Get as GetQueueMessaging; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Migrations\Get as GetQueueMigrations; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Notifications\Get as GetQueueNotifications; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\StatsResources\Get as GetQueueStatsResources; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\StatsUsage\Get as GetQueueUsage; -use Appwrite\Platform\Modules\Health\Http\Health\Queue\Webhooks\Get as GetQueueWebhooks; use Appwrite\Platform\Modules\Health\Http\Health\Stats\Get as GetStats; use Appwrite\Platform\Modules\Health\Http\Health\Storage\Get as GetStorage; use Appwrite\Platform\Modules\Health\Http\Health\Storage\Local\Get as GetStorageLocal; @@ -48,19 +35,6 @@ class Http extends Service $this->addAction(GetAntivirus::getName(), new GetAntivirus()); $this->addAction(GetGeo::getName(), new GetGeo()); - $this->addAction(GetQueueAudits::getName(), new GetQueueAudits()); - $this->addAction(GetQueueWebhooks::getName(), new GetQueueWebhooks()); - $this->addAction(GetQueueCertificates::getName(), new GetQueueCertificates()); - $this->addAction(GetQueueBuilds::getName(), new GetQueueBuilds()); - $this->addAction(GetQueueDatabases::getName(), new GetQueueDatabases()); - $this->addAction(GetQueueDeletes::getName(), new GetQueueDeletes()); - $this->addAction(GetQueueMails::getName(), new GetQueueMails()); - $this->addAction(GetQueueMessaging::getName(), new GetQueueMessaging()); - $this->addAction(GetQueueMigrations::getName(), new GetQueueMigrations()); - $this->addAction(GetQueueNotifications::getName(), new GetQueueNotifications()); - $this->addAction(GetQueueFunctions::getName(), new GetQueueFunctions()); - $this->addAction(GetQueueStatsResources::getName(), new GetQueueStatsResources()); - $this->addAction(GetQueueUsage::getName(), new GetQueueUsage()); $this->addAction(GetFailedJobs::getName(), new GetFailedJobs()); $this->addAction(GetStats::getName(), new GetStats()); diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php index a57548a71b..94e246a7ee 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php @@ -102,7 +102,13 @@ class Create extends Action 'membershipsMfa' => false, 'membershipsUserId' => false, 'membershipsUserPhone' => false, - 'invalidateSessions' => true + 'invalidateSessions' => true, + 'mfaFactors' => [ + 'totp' => true, + 'email' => true, + 'phone' => true, + 'custom' => false, + ], ]; foreach ($auth as $method) { diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index f65a955b3d..eef9bcb896 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -52,7 +52,7 @@ class Delete extends Action ], contentType: ContentType::NONE )) - ->param('number', null, new Phone(), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') + ->param('number', null, new Phone(normalize: true), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') ->inject('response') ->inject('queueForEvents') ->inject('project') @@ -69,6 +69,7 @@ class Delete extends Action Database $dbForPlatform, Authorization $authorization, ) { + $number = Phone::normalize($number); $auths = $project->getAttribute('auths', []); $mockNumbers = $auths['mockNumbers'] ?? []; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Get.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Get.php index a51095b368..6344144713 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Get.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Get.php @@ -44,7 +44,7 @@ class Get extends Action ) ] )) - ->param('number', null, new Phone(), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') + ->param('number', null, new Phone(normalize: true), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') ->inject('response') ->inject('project') ->callback($this->action(...)); @@ -55,6 +55,7 @@ class Get extends Action Response $response, Document $project ) { + $number = Phone::normalize($number); $auths = $project->getAttribute('auths', []); $mockNumbers = $auths['mockNumbers'] ?? []; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index f2b618c39e..ff670170de 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -52,7 +52,7 @@ class Update extends Action ) ] )) - ->param('number', null, new Phone(), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') + ->param('number', null, new Phone(normalize: true), 'Phone number associated with the mock phone. Must be a valid E.164 formatted phone number.') ->param('otp', '', new Text(6, 6, Text::NUMBERS), 'One-time password (OTP) to associate with the mock phone. Must be a 6-digit numeric code.') ->inject('response') ->inject('queueForEvents') @@ -71,6 +71,7 @@ class Update extends Action Database $dbForPlatform, Authorization $authorization, ) { + $number = Phone::normalize($number); $auths = $project->getAttribute('auths', []); $mockNumbers = $auths['mockNumbers'] ?? []; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php index 3ac3c9823a..1743ce94f8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php @@ -85,6 +85,7 @@ class Get extends Action 'session-limit' => Response::MODEL_POLICY_SESSION_LIMIT, 'user-limit' => Response::MODEL_POLICY_USER_LIMIT, 'membership-privacy' => Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, + 'mfa-factors' => Response::MODEL_POLICY_MFA_FACTORS, ]; } @@ -176,6 +177,17 @@ class Get extends Action ]), Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, ], + 'mfa-factors' => [ + new Document(\array_merge([ + 'totp' => true, + 'email' => true, + 'phone' => true, + 'custom' => false, + ], $auths['mfaFactors'] ?? [], [ + '$id' => 'mfa-factors', + ])), + Response::MODEL_POLICY_MFA_FACTORS, + ], default => null, }; } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php new file mode 100644 index 0000000000..d84f0c995a --- /dev/null +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php @@ -0,0 +1,103 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) + ->setHttpPath('/v1/project/policies/mfa-factors') + ->desc('Update MFA factors policy') + ->groups(['api', 'project']) + ->label('scope', ['policies.write', 'project.policies.write']) + ->label('event', 'projects.[projectId].policies.[policy].update') + ->label('audits.event', 'projects.[projectId].policies.[policy].update') + ->label('audits.resource', 'project/{response.$id}') + ->label('sdk', new Method( + namespace: 'project', + group: 'policies', + name: 'updateMFAFactorsPolicy', + description: <<param('totp', null, new Boolean(), 'Set to true to allow TOTP to complete an MFA challenge, or false to disable it.', optional: true) + ->param('email', null, new Boolean(), 'Set to true to allow email to complete an MFA challenge, or false to disable it.', optional: true) + ->param('phone', null, new Boolean(), 'Set to true to allow phone (SMS) to complete an MFA challenge, or false to disable it.', optional: true) + ->param('custom', null, new Boolean(), 'Set to true to allow the custom factor to complete an MFA challenge, or false to disable it.', optional: true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('project') + ->inject('queueForEvents') + ->callback($this->action(...)); + } + + public function action( + ?bool $totp, + ?bool $email, + ?bool $phone, + ?bool $custom, + Response $response, + Database $dbForPlatform, + Document $project, + Event $queueForEvents, + ): void { + $auths = $project->getAttribute('auths', []); + $factors = $auths['mfaFactors'] ?? []; + + if ($totp !== null) { + $factors['totp'] = $totp; + } + if ($email !== null) { + $factors['email'] = $email; + } + if ($phone !== null) { + $factors['phone'] = $phone; + } + if ($custom !== null) { + $factors['custom'] = $custom; + } + + $auths['mfaFactors'] = $factors; + + $updates = new Document([ + 'auths' => $auths, + ]); + + $project = $dbForPlatform->updateDocument('projects', $project->getId(), $updates); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); + + $queueForEvents + ->setParam('projectId', $project->getId()) + ->setParam('policy', 'mfa-factors'); + + $response->dynamic($project, Response::MODEL_PROJECT); + } +} diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php index 464047e213..8e6000bd9e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php @@ -145,6 +145,14 @@ class XList extends Action 'userMFA' => $auths['membershipsMfa'] ?? false, 'userAccessedAt' => $auths['membershipsUserAccessedAt'] ?? false, ]), + new Document(\array_merge([ + 'totp' => true, + 'email' => true, + 'phone' => true, + 'custom' => false, + ], $auths['mfaFactors'] ?? [], [ + '$id' => 'mfa-factors', + ])), ]; } } diff --git a/src/Appwrite/Platform/Modules/Project/Services/Http.php b/src/Appwrite/Platform/Modules/Project/Services/Http.php index 2ffa9eed8b..2c322c0ad6 100644 --- a/src/Appwrite/Platform/Modules/Project/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Project/Services/Http.php @@ -78,6 +78,7 @@ use Appwrite\Platform\Modules\Project\Http\Project\Platforms\Windows\Update as U use Appwrite\Platform\Modules\Project\Http\Project\Platforms\XList as ListPlatforms; use Appwrite\Platform\Modules\Project\Http\Project\Policies\Get as GetPolicy; use Appwrite\Platform\Modules\Project\Http\Project\Policies\MembershipPrivacy\Update as UpdateMembershipPrivacyPolicy; +use Appwrite\Platform\Modules\Project\Http\Project\Policies\MFAFactors\Update as UpdateMFAFactorsPolicy; use Appwrite\Platform\Modules\Project\Http\Project\Policies\PasswordDictionary\Update as UpdatePasswordDictionaryPolicy; use Appwrite\Platform\Modules\Project\Http\Project\Policies\PasswordHistory\Update as UpdatePasswordHistoryPolicy; use Appwrite\Platform\Modules\Project\Http\Project\Policies\PasswordPersonalData\Update as UpdatePasswordPersonalDataPolicy; @@ -168,6 +169,7 @@ class Http extends Service $this->addAction(ListPolicies::getName(), new ListPolicies()); $this->addAction(GetPolicy::getName(), new GetPolicy()); $this->addAction(UpdateMembershipPrivacyPolicy::getName(), new UpdateMembershipPrivacyPolicy()); + $this->addAction(UpdateMFAFactorsPolicy::getName(), new UpdateMFAFactorsPolicy()); $this->addAction(UpdatePasswordDictionaryPolicy::getName(), new UpdatePasswordDictionaryPolicy()); $this->addAction(UpdatePasswordHistoryPolicy::getName(), new UpdatePasswordHistoryPolicy()); $this->addAction(UpdatePasswordStrengthPolicy::getName(), new UpdatePasswordStrengthPolicy()); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index 82279824be..d8f85a64d5 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -105,7 +105,13 @@ class Create extends Action 'membershipsMfa' => false, 'membershipsUserId' => false, 'membershipsUserPhone' => false, - 'invalidateSessions' => true + 'invalidateSessions' => true, + 'mfaFactors' => [ + 'totp' => true, + 'email' => true, + 'phone' => true, + 'custom' => false, + ], ]; foreach ($auth as $method) { diff --git a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php index e1d40b548e..ce51074f28 100644 --- a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php +++ b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Challenges/Get.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\DateTime; use Utopia\Database\Validator\UID; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; @@ -68,6 +69,7 @@ class Get extends Action $challenge->isEmpty() || $challenge->getAttribute('userId') !== $userId || $challenge->getAttribute('type') !== Type::CUSTOM + || $challenge->getAttribute('expire') < DateTime::formatTz(DateTime::now()) ) { throw new Exception(Exception::USER_INVALID_TOKEN); } diff --git a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Factors/XList.php b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Factors/XList.php index de8e1ba406..c6df02a79b 100644 --- a/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Factors/XList.php +++ b/src/Appwrite/Platform/Modules/Users/Http/Users/MFA/Factors/XList.php @@ -70,10 +70,11 @@ class XList extends Action ->param('userId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'User ID.', false, ['dbForProject']) ->inject('response') ->inject('dbForProject') + ->inject('project') ->callback($this->action(...)); } - public function action(string $userId, Response $response, Database $dbForProject): void + public function action(string $userId, Response $response, Database $dbForProject, Document $project): void { $user = $dbForProject->getDocument('users', $userId); @@ -83,10 +84,13 @@ class XList extends Action $totp = TOTP::getAuthenticatorFromUser($user); + $mfaFactors = $project->getAttribute('auths', [])['mfaFactors'] ?? []; + $factors = new Document([ - Type::TOTP => $totp !== null && $totp->getAttribute('verified', false), - Type::EMAIL => $user->getAttribute('email', false) && $user->getAttribute('emailVerification', false), - Type::PHONE => $user->getAttribute('phone', false) && $user->getAttribute('phoneVerification', false) + Type::TOTP => ($mfaFactors['totp'] ?? true) && $totp !== null && $totp->getAttribute('verified', false), + Type::EMAIL => ($mfaFactors['email'] ?? true) && $user->getAttribute('email', false) && $user->getAttribute('emailVerification', false), + Type::PHONE => ($mfaFactors['phone'] ?? true) && $user->getAttribute('phone', false) && $user->getAttribute('phoneVerification', false), + Type::CUSTOM => $mfaFactors['custom'] ?? false ]); $response->dynamic($factors, Response::MODEL_MFA_FACTORS); diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index efa9a0f8eb..3b71cf044b 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -36,6 +36,18 @@ abstract class ScheduleBase extends Action abstract public static function getSupportedResource(): string; abstract public static function getCollectionId(): string; + /** + * Deterministic per-resource offset, in seconds, within [0, $window). + * + * Schedules that share a cron slot are spread across the window instead + * of all being enqueued in the same second, while each resource keeps a + * stable slot so run intervals stay exact. + */ + public static function spreadOffset(string $resourceId, int $window): int + { + return $window <= 1 ? 0 : \abs(\crc32($resourceId)) % $window; + } + protected function loadResource(Document $project, callable $getProjectDB, array $schedule): Document { return $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); @@ -237,10 +249,19 @@ abstract class ScheduleBase extends Action foreach ($batches as $batch) { $dbStart = microtime(true); - $documents = $dbForPlatform->find('projects', [ - Query::equal('$id', $batch), - Query::limit(count($batch)), - ]); + // The project's subquery attributes cost one query each, per project — five + // thousand extra queries for a batch of a thousand projects — and no + // schedule task reads them. The only attributes used here are accessedAt, + // teamId, database and the sequence, and the documents enqueued for the + // workers are reloaded there by id, so the stripped arrays never reach the + // code that needs them. Same group Action::$filters marks as Project. + $documents = $dbForPlatform->skipFilters( + fn () => $dbForPlatform->find('projects', [ + Query::equal('$id', $batch), + Query::limit(count($batch)), + ]), + ['subQueryKeys', 'subQueryWebhooks', 'subQueryPlatforms', 'subQueryBlocks', 'subQueryDevKeys'] + ); $dbQueryDuration += microtime(true) - $dbStart; $transformStart = microtime(true); diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index e6ea71204d..c991df852d 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -37,18 +37,6 @@ class ScheduleFunctions extends ScheduleBase return RESOURCE_TYPE_FUNCTIONS; } - /** - * Deterministic per-function offset, in seconds, within [0, $window). - * - * Schedules that share a cron slot (everyone runs `0 * * * *`) are spread - * across the window instead of all being enqueued in the same second, - * while each function keeps a stable slot so run intervals stay exact. - */ - public static function spreadOffset(string $resourceId, int $window): int - { - return $window <= 1 ? 0 : \abs(\crc32($resourceId)) % $window; - } - /** * Spread window, in seconds, for a given schedule. The default applies * _APP_FUNCTIONS_SCHEDULE_SPREAD to every schedule; override to scope diff --git a/src/Appwrite/Platform/Tasks/Specs.php b/src/Appwrite/Platform/Tasks/Specs.php index 45208d234d..0bbb9dcd02 100644 --- a/src/Appwrite/Platform/Tasks/Specs.php +++ b/src/Appwrite/Platform/Tasks/Specs.php @@ -779,7 +779,10 @@ class Specs extends Action $parsedSpecs = $specs->parse(); $this->verifyParsedSpec($parsedSpecs); } catch (\RuntimeException $e) { - throw new \RuntimeException("Spec generation failed for {$platform} ({$format}): " . $e->getMessage(), 0, $e); + // A throw is reported and carried on from, so stop here + Console::error("Spec generation failed for {$platform} ({$format}): " . $e->getMessage()); + Console::exit(1); + return; } $encodedSpecs = \json_encode($parsedSpecs, JSON_PRETTY_PRINT); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 5a3bedc268..0b64907ca1 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -211,6 +211,7 @@ class Deletes extends Action $this->deleteUsageStats($project, $getProjectDB, $getLogsDB, $hourlyUsageRetentionDatetime); $this->deleteExpiredSessions($project, $getProjectDB); $this->deleteExpiredOAuth2Grants($project, $getProjectDB); + $this->deleteExpiredChallenges($project, $getProjectDB); $this->deleteExpiredTransactions($project, $getProjectDB); $this->deleteExpiredPresences($project, $getProjectDB, $publisherForUsage); $this->deleteOldDeployments($publisherForDeletes, $project, $getProjectDB); @@ -1166,6 +1167,24 @@ class Deletes extends Action ], $dbForProject); } + /** + * @param Document $project + * @param callable $getProjectDB + * @return void + * @throws Exception|Throwable + */ + private function deleteExpiredChallenges(Document $project, callable $getProjectDB): void + { + Console::info('Delete expired challenges'); + + $this->deleteByGroup('challenges', [ + Query::select([...$this->selects, 'expire']), + Query::lessThan('expire', DateTime::format(new \DateTime())), + Query::orderAsc('expire'), + Query::orderAsc(), + ], $getProjectDB($project)); + } + /** * @param Database $dbForPlatform * @param Device $deviceForFiles diff --git a/src/Appwrite/Utopia/Database/Attribute.php b/src/Appwrite/Utopia/Database/Attribute.php new file mode 100644 index 0000000000..b4dba3433c --- /dev/null +++ b/src/Appwrite/Utopia/Database/Attribute.php @@ -0,0 +1,100 @@ + + */ + public const SIZES = [ + Database::VAR_TEXT => 65535, + Database::VAR_MEDIUMTEXT => 16777215, + Database::VAR_LONGTEXT => 2147483647, + ]; + + /** + * String formats, mapped to the size their dedicated endpoint uses. Each + * format is also accepted as a shorthand type, mirroring the endpoints + * (`createEmailColumn` and friends) that create a string with that format. + * + * @var array + */ + public const FORMAT_SIZES = [ + APP_DATABASE_ATTRIBUTE_EMAIL => 254, + APP_DATABASE_ATTRIBUTE_ENUM => Database::LENGTH_KEY, + APP_DATABASE_ATTRIBUTE_IP => 39, + APP_DATABASE_ATTRIBUTE_URL => 2000, + ]; + + /** + * Types accepted in an inline definition, including the format shorthands. + * + * @return array + */ + public static function types(): array + { + return [ + Database::VAR_STRING, + Database::VAR_VARCHAR, + Database::VAR_TEXT, + Database::VAR_MEDIUMTEXT, + Database::VAR_LONGTEXT, + Database::VAR_INTEGER, + Database::VAR_BIGINT, + Database::VAR_FLOAT, + Database::VAR_BOOLEAN, + Database::VAR_DATETIME, + Database::VAR_POINT, + Database::VAR_LINESTRING, + Database::VAR_POLYGON, + ...\array_keys(self::FORMAT_SIZES), + ]; + } + + /** + * Resolve an inline definition into the type, format and size stored on the + * attribute document. A format shorthand becomes a string of that format, + * and a size is filled in from the type or format when the caller omits it. + * + * @param array $attribute + * @return array{type: string, format: string, size: int} + */ + public static function resolve(array $attribute): array + { + $type = $attribute['type'] ?? ''; + $format = $attribute['format'] ?? ''; + + if (isset(self::FORMAT_SIZES[$type])) { + $format = $type; + $type = Database::VAR_STRING; + } + + $size = $attribute['size'] ?? 0; + $size = \is_int($size) ? $size : 0; + + if (isset(self::SIZES[$type])) { + // Fixed width types ignore any size the caller sent. + $size = self::SIZES[$type]; + } elseif ($size < 1) { + $size = self::FORMAT_SIZES[$format] ?? $size; + } + + return [ + 'type' => $type, + 'format' => $format, + 'size' => $size, + ]; + } +} diff --git a/src/Appwrite/Utopia/Database/Validator/Attributes.php b/src/Appwrite/Utopia/Database/Validator/Attributes.php index 54aaf135f9..4eefb9ba2d 100644 --- a/src/Appwrite/Utopia/Database/Validator/Attributes.php +++ b/src/Appwrite/Utopia/Database/Validator/Attributes.php @@ -2,6 +2,7 @@ namespace Appwrite\Utopia\Database\Validator; +use Appwrite\Utopia\Database\Attribute; use Utopia\Database\Database; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\Key; @@ -17,21 +18,6 @@ class Attributes extends Validator protected int $maxAttributes; protected string $message = 'Invalid attributes'; - /** - * @var array Supported attribute types - */ - protected array $supportedTypes = [ - Database::VAR_STRING, - Database::VAR_INTEGER, - Database::VAR_BIGINT, - Database::VAR_FLOAT, - Database::VAR_BOOLEAN, - Database::VAR_DATETIME, - Database::VAR_POINT, - Database::VAR_LINESTRING, - Database::VAR_POLYGON, - ]; - /** * @var array Supported formats for string attributes */ @@ -92,6 +78,7 @@ class Attributes extends Validator } $keyValidator = new Key(); + $supportedTypes = Attribute::types(); $keys = []; foreach ($value as $index => $attribute) { @@ -132,34 +119,44 @@ class Attributes extends Validator } // Validate type - if (!\in_array($attribute['type'], $this->supportedTypes)) { + if (!\in_array($attribute['type'], $supportedTypes)) { $this->message = "Invalid type for attribute '" . $attribute['key'] . "': " . $attribute['type']; return false; } + // Resolve format shorthands and implicit sizes the same way the + // attribute document is built, so both agree on what was asked for. + ['type' => $type, 'format' => $format, 'size' => $size] = Attribute::resolve($attribute); + // Validate spatial type support - if (\in_array($attribute['type'], Database::SPATIAL_TYPES) && !$this->supportForSpatialAttributes) { + if (\in_array($type, Database::SPATIAL_TYPES) && !$this->supportForSpatialAttributes) { $this->message = "Spatial attributes are not supported by the current database"; return false; } - // Validate size for string types - if ($attribute['type'] === Database::VAR_STRING) { - if (!isset($attribute['size']) || !is_int($attribute['size']) || $attribute['size'] < 1 || $attribute['size'] > APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH) { + if (isset($attribute['size']) && !\is_int($attribute['size'])) { + $this->message = "Invalid size for attribute '" . $attribute['key'] . "': must be an integer"; + return false; + } + + // Validate size for the types that take one. The remaining string + // types (text, mediumtext, longtext) are fixed width. + if (\in_array($type, [Database::VAR_STRING, Database::VAR_VARCHAR])) { + if ($size < 1 || $size > APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH) { $this->message = "Invalid or missing size for string attribute '" . $attribute['key'] . "'. Size must be between 1 and " . APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH; return false; } } // Validate format if provided - if (isset($attribute['format']) && $attribute['format'] !== '') { - // Format is only allowed for string type - if ($attribute['type'] !== Database::VAR_STRING) { + if ($format !== '') { + // Format is only allowed for sized string types + if (!\in_array($type, [Database::VAR_STRING, Database::VAR_VARCHAR])) { $this->message = "Format is only allowed for string type for attribute '" . $attribute['key'] . "'"; return false; } - if (!in_array($attribute['format'], $this->supportedFormats)) { - $this->message = "Invalid format for attribute '" . $attribute['key'] . "': " . $attribute['format']; + if (!in_array($format, $this->supportedFormats)) { + $this->message = "Invalid format for attribute '" . $attribute['key'] . "': " . $format; return false; } } @@ -183,7 +180,7 @@ class Attributes extends Validator } // Validate signed only for integer/bigint/float types - if (isset($attribute['signed']) && !in_array($attribute['type'], [Database::VAR_INTEGER, Database::VAR_BIGINT, Database::VAR_FLOAT])) { + if (isset($attribute['signed']) && !in_array($type, [Database::VAR_INTEGER, Database::VAR_BIGINT, Database::VAR_FLOAT])) { $this->message = "Attribute '" . $attribute['key'] . "': 'signed' can only be used with integer, bigint or float types"; return false; } @@ -202,7 +199,7 @@ class Attributes extends Validator // Validate min/max range for integer/bigint/float if (isset($attribute['min']) || isset($attribute['max'])) { - if (!in_array($attribute['type'], [Database::VAR_INTEGER, Database::VAR_BIGINT, Database::VAR_FLOAT])) { + if (!in_array($type, [Database::VAR_INTEGER, Database::VAR_BIGINT, Database::VAR_FLOAT])) { $this->message = "Attribute '" . $attribute['key'] . "': min/max can only be used with integer, bigint or float types"; return false; } @@ -216,15 +213,18 @@ class Attributes extends Validator // Validate default value matches attribute type if (isset($attribute['default'])) { - switch ($attribute['type']) { + switch ($type) { case Database::VAR_STRING: + case Database::VAR_VARCHAR: + case Database::VAR_TEXT: + case Database::VAR_MEDIUMTEXT: + case Database::VAR_LONGTEXT: if (!is_string($attribute['default'])) { $this->message = "Default value for string attribute '" . $attribute['key'] . "' must be a string"; return false; } // Validate string size - $size = $attribute['size'] ?? 0; if ($size > 0) { $textValidator = new Text($size, 0); if (!$textValidator->isValid($attribute['default'])) { @@ -234,7 +234,6 @@ class Attributes extends Validator } // Validate format-specific defaults - $format = $attribute['format'] ?? ''; if ($format === APP_DATABASE_ATTRIBUTE_EMAIL) { $emailValidator = new Email(); if (!$emailValidator->isValid($attribute['default'])) { @@ -330,7 +329,7 @@ class Attributes extends Validator } // Validate enum elements if format is enum - if (isset($attribute['format']) && $attribute['format'] === APP_DATABASE_ATTRIBUTE_ENUM) { + if ($format === APP_DATABASE_ATTRIBUTE_ENUM) { if (!isset($attribute['elements']) || !is_array($attribute['elements']) || empty($attribute['elements'])) { $this->message = "Attribute '" . $attribute['key'] . "' with enum format must have 'elements' array"; return false; diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php index 9d9bbde00b..dffb1f6b48 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php @@ -91,7 +91,11 @@ class Base extends Queries new Limit(), new Offset(), new Cursor(), - new Filter($attributes, APP_DATABASE_QUERY_MAX_VALUES), + new Filter( + attributes: $attributes, + idAttributeType: Database::VAR_INTEGER, + maxValuesCount: APP_DATABASE_QUERY_MAX_VALUES + ), new Order($attributes), ]; diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index ec2a4ac978..aa261a320b 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -277,6 +277,7 @@ class Response extends SwooleResponse public const MODEL_POLICY_SESSION_LIMIT = 'policySessionLimit'; public const MODEL_POLICY_USER_LIMIT = 'policyUserLimit'; public const MODEL_POLICY_MEMBERSHIP_PRIVACY = 'policyMembershipPrivacy'; + public const MODEL_POLICY_MFA_FACTORS = 'policyMfaFactors'; public const MODEL_AUTH_PROVIDER = 'authProvider'; public const MODEL_AUTH_PROVIDER_LIST = 'authProviderList'; public const MODEL_PLATFORM_APPLE = 'platformApple'; diff --git a/src/Appwrite/Utopia/Response/Model/MFAFactors.php b/src/Appwrite/Utopia/Response/Model/MFAFactors.php index 47938ac07c..6ff74ce19f 100644 --- a/src/Appwrite/Utopia/Response/Model/MFAFactors.php +++ b/src/Appwrite/Utopia/Response/Model/MFAFactors.php @@ -35,6 +35,12 @@ class MFAFactors extends Model 'default' => false, 'example' => true ]) + ->addRule(Type::CUSTOM, [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Can custom factor be used for MFA challenge for this account.', + 'default' => false, + 'example' => true + ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/PolicyList.php b/src/Appwrite/Utopia/Response/Model/PolicyList.php index 0c61f15097..ff8d7cbe37 100644 --- a/src/Appwrite/Utopia/Response/Model/PolicyList.php +++ b/src/Appwrite/Utopia/Response/Model/PolicyList.php @@ -40,6 +40,7 @@ class PolicyList extends Model Response::MODEL_POLICY_SESSION_LIMIT, Response::MODEL_POLICY_USER_LIMIT, Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, + Response::MODEL_POLICY_MFA_FACTORS, ]; } diff --git a/src/Appwrite/Utopia/Response/Model/PolicyMFAFactors.php b/src/Appwrite/Utopia/Response/Model/PolicyMFAFactors.php new file mode 100644 index 0000000000..d2a3684edd --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/PolicyMFAFactors.php @@ -0,0 +1,54 @@ + 'mfa-factors', + ]; + + public function __construct() + { + parent::__construct(); + + $this + ->addRule(Type::TOTP, [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Whether TOTP can be used to complete an MFA challenge.', + 'default' => true, + 'example' => true, + ]) + ->addRule(Type::EMAIL, [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Whether email can be used to complete an MFA challenge.', + 'default' => true, + 'example' => true, + ]) + ->addRule(Type::PHONE, [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Whether phone (SMS) can be used to complete an MFA challenge.', + 'default' => true, + 'example' => true, + ]) + ->addRule(Type::CUSTOM, [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Whether the custom factor can be used to complete an MFA challenge.', + 'default' => false, + 'example' => true, + ]); + } + + public function getName(): string + { + return 'Policy MFA Factors'; + } + + public function getType(): string + { + return Response::MODEL_POLICY_MFA_FACTORS; + } +} diff --git a/tests/e2e/General/HTTPTest.php b/tests/e2e/General/HTTPTest.php index 68331ae47f..42d49bbabc 100644 --- a/tests/e2e/General/HTTPTest.php +++ b/tests/e2e/General/HTTPTest.php @@ -156,6 +156,64 @@ final class HTTPTest extends Scope $this->assertNull($response['headers']['access-control-allow-origin'] ?? null); } + public function testCorsLoopback() + { + $endpoint = '/v1/projects'; // Can be any non-404 route + + /** + * Test for SUCCESS + * + * Loopback origins are allowed without a registered platform. + */ + $origins = [ + 'http://localhost', + 'http://localhost:3000', + 'http://127.0.0.1', + 'https://127.0.0.1:5173', + 'http://[::1]', + 'http://[::1]:3000', + ]; + + foreach ($origins as $origin) { + $response = $this->client->call(Client::METHOD_GET, $endpoint, [ + 'origin' => $origin, + ]); + $this->assertEquals($origin, $response['headers']['access-control-allow-origin'] ?? null, 'Origin ' . $origin . ' was not allowed'); + + // Trusting loopback with credentials relies on the origin being + // echoed verbatim, so the browser's literal match still applies + $this->assertNotEquals('*', $response['headers']['access-control-allow-origin']); + $this->assertEquals('true', $response['headers']['access-control-allow-credentials']); + } + + /** + * Test for FAILURE + * + * Hostnames that only look like loopback must not be allowed. + */ + $origins = [ + 'http://127.0.0.1.example.com', + 'http://localhost.example.com', + 'http://128.0.0.1', + 'http://[2001:db8::1]', + // A prefix or substring match would wrongly accept these + 'http://xlocalhost', + 'http://127.0.0.1x.example.com', + 'http://[::1].evil.com', + // Only the exact loopback spellings are hardcoded + 'http://127.0.0.2', + 'http://[0:0:0:0:0:0:0:1]', + 'http://localhost.', + ]; + + foreach ($origins as $origin) { + $response = $this->client->call(Client::METHOD_GET, $endpoint, [ + 'origin' => $origin, + ]); + $this->assertNull($response['headers']['access-control-allow-origin'] ?? null, 'Origin ' . $origin . ' was unexpectedly allowed'); + } + } + public function testPreflight() { diff --git a/tests/e2e/Scopes/ProjectCustom.php b/tests/e2e/Scopes/ProjectCustom.php index a336660a43..e814894df3 100644 --- a/tests/e2e/Scopes/ProjectCustom.php +++ b/tests/e2e/Scopes/ProjectCustom.php @@ -126,6 +126,7 @@ trait ProjectCustom 'documents.write', 'rows.read', 'rows.write', + 'embeddings.write', 'files.read', 'files.write', 'buckets.read', diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 1dfcd1f965..306eacb893 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -2228,6 +2228,131 @@ final class AccountCustomClientTest extends Scope $this->assertEquals(412, $response['headers']['status-code']); } + public function testCreateOAuth2AccountSessionLoopback(): void + { + $this->setupAccountWithSession(); + + $provider = 'mock'; + + $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $this->getProject()['$id'] . '/oauth2', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]), [ + 'provider' => $provider, + 'appId' => '1', + 'secret' => '123456', + 'enabled' => true, + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test for SUCCESS + * + * Loopback redirects are allowed without a registered platform. + */ + $hosts = [ + 'localhost', + 'localhost:3000', + '127.0.0.1', + '127.0.0.1:5173', + '[::1]', + '[::1]:3000', + ]; + + foreach ($hosts as $host) { + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/oauth2/' . $provider, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://' . $host . '/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://' . $host . '/v1/mock/tests/general/oauth2/failure', + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code'], 'Host ' . $host . ' was not allowed'); + $this->assertStringStartsWith('http://localhost/v1/mock/tests/general/oauth2', $response['headers']['location']); + } + + /** + * Test for FAILURE + * + * Hostnames that only look like loopback must not be allowed. + */ + $hosts = [ + '127.0.0.1.example.com', + 'localhost.example.com', + '128.0.0.1', + '[2001:db8::1]', + // Only the exact loopback spellings are hardcoded + '127.0.0.2', + '[0:0:0:0:0:0:0:1]', + ]; + + foreach ($hosts as $host) { + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/oauth2/' . $provider, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://' . $host . '/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://' . $host . '/v1/mock/tests/general/oauth2/failure', + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code'], 'Host ' . $host . ' was unexpectedly allowed'); + } + + /** + * Walk the whole token flow to prove the loopback success URL survives + * the provider round trip and the callback validation. + */ + $response = $this->client->call(Client::METHOD_GET, '/account/tokens/oauth2/' . $provider, array_merge([ + 'origin' => 'http://127.0.0.1', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://127.0.0.1/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://127.0.0.1/v1/mock/tests/general/oauth2/failure', + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + + $oauthClient = new Client(); + $oauthClient->setEndpoint(''); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://appwrite:/v1/account/sessions/oauth2/callback/mock/' . $this->getProject()['$id'] . '?code=', $response['headers']['location']); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://appwrite:/v1/account/sessions/oauth2/mock/redirect?code=', $response['headers']['location']); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://127.0.0.1/v1/mock/tests/general/oauth2/success?secret=', $response['headers']['location']); + + $oauthParams = []; + \parse_str((string) \parse_url($response['headers']['location'], PHP_URL_QUERY), $oauthParams); + + $this->assertNotEmpty($oauthParams['secret']); + $this->assertNotEmpty($oauthParams['userId']); + + $response = $this->client->call(Client::METHOD_POST, '/account/sessions/token', [ + 'origin' => 'http://127.0.0.1', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'userId' => $oauthParams['userId'], + 'secret' => $oauthParams['secret'], + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertEquals('mock', $response['body']['provider']); + } + public function testOAuth2TokenSessionProviderAccessToken(): void { // Just ensure we have a session set up @@ -3981,6 +4106,89 @@ final class AccountCustomClientTest extends Scope $this->assertStringContainsStringIgnoringCase($phrase, $lastEmail['text']); } + public function testCreateMagicUrlLoopback(): void + { + /** + * Test for SUCCESS + * + * Loopback redirect URLs are allowed without a registered platform. + */ + $hosts = [ + 'localhost', + 'localhost:3000', + '127.0.0.1', + '127.0.0.1:5173', + '[::1]', + '[::1]:3000', + ]; + + $email = 'magic-loopback-' . uniqid() . '-' . \time() . '@appwrite.io'; + + foreach ($hosts as $host) { + $response = $this->client->call(Client::METHOD_POST, '/account/tokens/magic-url', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'userId' => ID::unique(), + 'email' => $email, + 'url' => 'http://' . $host . '/magiclogin', + ]); + + $this->assertEquals(201, $response['headers']['status-code'], 'Host ' . $host . ' was not allowed'); + } + + /** + * The emailed link must keep pointing at the loopback host. + */ + $email = 'magic-loopback-' . uniqid() . '-' . \time() . '@appwrite.io'; + + $response = $this->client->call(Client::METHOD_POST, '/account/tokens/magic-url', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'userId' => ID::unique(), + 'email' => $email, + 'url' => 'http://127.0.0.1:5173/magiclogin', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + $lastEmail = $this->getLastEmailByAddress($email); + $this->assertNotEmpty($lastEmail, 'Email not found for address: ' . $email); + $this->assertStringContainsString('http://127.0.0.1:5173/magiclogin?', (string) $lastEmail['text']); + + /** + * Test for FAILURE + * + * Hostnames that only look like loopback must not be allowed. + */ + $hosts = [ + '127.0.0.1.example.com', + 'localhost.example.com', + '128.0.0.1', + '[2001:db8::1]', + // Only the exact loopback spellings are hardcoded + '127.0.0.2', + '[0:0:0:0:0:0:0:1]', + ]; + + foreach ($hosts as $host) { + $response = $this->client->call(Client::METHOD_POST, '/account/tokens/magic-url', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'userId' => ID::unique(), + 'email' => 'magic-loopback-' . uniqid() . '-' . \time() . '@appwrite.io', + 'url' => 'http://' . $host . '/magiclogin', + ]); + + $this->assertEquals(400, $response['headers']['status-code'], 'Host ' . $host . ' was unexpectedly allowed'); + } + } + public function testCreateSessionWithMagicUrl(): void { $projectId = $this->getProject()['$id']; @@ -4276,6 +4484,26 @@ final class AccountCustomClientTest extends Scope $projectId = $this->getProject()['$id']; $apiKey = $this->getProject()['apiKey']; + // Enable the custom factor, which the MFA factors policy disables by default + $policy = $this->client->call(Client::METHOD_PATCH, '/project/policies/mfa-factors', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-key' => $apiKey, + ], [ + 'custom' => true, + ]); + + $this->assertEquals(200, $policy['headers']['status-code']); + + // Custom factor becomes visible when the policy enables it + $factors = $this->client->call(Client::METHOD_GET, '/account/mfa/factors', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders())); + + $this->assertEquals(200, $factors['headers']['status-code']); + $this->assertTrue($factors['body']['custom']); + // Create custom factor challenge using existing authenticated session $challenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenge', array_merge([ 'content-type' => 'application/json', diff --git a/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php new file mode 100644 index 0000000000..6235a8bfcb --- /dev/null +++ b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php @@ -0,0 +1,144 @@ + 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + + $database = $this->client->call(Client::METHOD_POST, '/tablesdb', $headers, [ + 'databaseId' => ID::unique(), + 'name' => 'Inline Columns', + ]); + + $this->assertEquals(201, $database['headers']['status-code']); + $databaseId = $database['body']['$id']; + + $table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $headers, [ + 'tableId' => ID::unique(), + 'name' => 'Modules', + 'permissions' => [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ], + 'columns' => [ + ['key' => 'title', 'type' => 'string', 'size' => 128, 'required' => true], + ['key' => 'slug', 'type' => 'varchar', 'size' => 64], + ['key' => 'modulePath', 'type' => 'text'], + ['key' => 'summary', 'type' => 'mediumtext'], + ['key' => 'archive', 'type' => 'longtext'], + ['key' => 'email', 'type' => 'email'], + ['key' => 'website', 'type' => 'url'], + ['key' => 'address', 'type' => 'ip'], + ['key' => 'status', 'type' => 'enum', 'elements' => ['on', 'off'], 'default' => 'on'], + ], + ]); + + $this->assertEquals(201, $table['headers']['status-code']); + $tableId = $table['body']['$id']; + + $columns = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns', $headers); + + $this->assertEquals(200, $columns['headers']['status-code']); + $this->assertEquals(9, $columns['body']['total']); + + $byKey = []; + foreach ($columns['body']['columns'] as $column) { + $this->assertEquals('available', $column['status']); + $byKey[$column['key']] = $column; + } + + // Sizes must match the dedicated per-column endpoints. The fixed width + // types do not expose a size, it is implied by the type. + $this->assertEquals('string', $byKey['title']['type']); + $this->assertEquals(128, $byKey['title']['size']); + $this->assertEquals('varchar', $byKey['slug']['type']); + $this->assertEquals(64, $byKey['slug']['size']); + $this->assertEquals('text', $byKey['modulePath']['type']); + $this->assertEquals('mediumtext', $byKey['summary']['type']); + $this->assertEquals('longtext', $byKey['archive']['type']); + + // Format shorthands become a string of that format + $this->assertEquals('string', $byKey['email']['type']); + $this->assertEquals('email', $byKey['email']['format']); + $this->assertEquals('string', $byKey['website']['type']); + $this->assertEquals('url', $byKey['website']['format']); + $this->assertEquals('string', $byKey['address']['type']); + $this->assertEquals('ip', $byKey['address']['format']); + $this->assertEquals('string', $byKey['status']['type']); + $this->assertEquals('enum', $byKey['status']['format']); + $this->assertEquals(['on', 'off'], $byKey['status']['elements']); + $this->assertEquals('on', $byKey['status']['default']); + + // Longer than any of the sized string types allow, so it only fits if + // the text column really got its 65535 default + $modulePath = \str_repeat('src/Appwrite/Platform/Modules/Databases/', 200); + + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $headers, [ + 'rowId' => ID::unique(), + 'data' => [ + 'title' => 'Appwrite', + 'modulePath' => $modulePath, + 'email' => 'team@appwrite.io', + 'website' => 'https://appwrite.io', + 'address' => '127.0.0.1', + 'status' => 'off', + ], + ]); + + $this->assertEquals(201, $row['headers']['status-code']); + $this->assertEquals($modulePath, $row['body']['modulePath']); + $this->assertEquals('off', $row['body']['status']); + } + + public function testCreateTableUnsupportedColumnType(): void + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + + $database = $this->client->call(Client::METHOD_POST, '/tablesdb', $headers, [ + 'databaseId' => ID::unique(), + 'name' => 'Inline Columns Invalid', + ]); + + $this->assertEquals(201, $database['headers']['status-code']); + $databaseId = $database['body']['$id']; + + $table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $headers, [ + 'tableId' => ID::unique(), + 'name' => 'Invalid', + 'columns' => [ + ['key' => 'unknown', 'type' => 'blob'], + ], + ]); + + $this->assertEquals(400, $table['headers']['status-code']); + $this->assertStringContainsString("Invalid type for attribute 'unknown': blob", (string) $table['body']['message']); + } +} diff --git a/tests/e2e/Services/Health/AuditsQueueTest.php b/tests/e2e/Services/Health/AuditsQueueTest.php deleted file mode 100644 index a5d5ff055e..0000000000 --- a/tests/e2e/Services/Health/AuditsQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/audits'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/audits', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/BuildsQueueTest.php b/tests/e2e/Services/Health/BuildsQueueTest.php deleted file mode 100644 index aa750274e6..0000000000 --- a/tests/e2e/Services/Health/BuildsQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/builds'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/builds', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/CertificatesQueueTest.php b/tests/e2e/Services/Health/CertificatesQueueTest.php deleted file mode 100644 index fa5c430e63..0000000000 --- a/tests/e2e/Services/Health/CertificatesQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/certificates'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/certificates', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/DatabasesQueueTest.php b/tests/e2e/Services/Health/DatabasesQueueTest.php deleted file mode 100644 index 25a866788d..0000000000 --- a/tests/e2e/Services/Health/DatabasesQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/databases', ['name' => 'database_db_main']); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/databases', ['name' => 'database_db_main', 'threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/DeletesQueueTest.php b/tests/e2e/Services/Health/DeletesQueueTest.php deleted file mode 100644 index 85754abd0a..0000000000 --- a/tests/e2e/Services/Health/DeletesQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/deletes'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/deletes', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/FunctionsQueueTest.php b/tests/e2e/Services/Health/FunctionsQueueTest.php deleted file mode 100644 index 334a89edd6..0000000000 --- a/tests/e2e/Services/Health/FunctionsQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/functions'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/functions', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/MailsQueueTest.php b/tests/e2e/Services/Health/MailsQueueTest.php deleted file mode 100644 index d9b2cda431..0000000000 --- a/tests/e2e/Services/Health/MailsQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/mails'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/mails', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/MessagingQueueTest.php b/tests/e2e/Services/Health/MessagingQueueTest.php deleted file mode 100644 index 8fbb02189e..0000000000 --- a/tests/e2e/Services/Health/MessagingQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/messaging'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/messaging', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/MigrationsQueueTest.php b/tests/e2e/Services/Health/MigrationsQueueTest.php deleted file mode 100644 index 8655926018..0000000000 --- a/tests/e2e/Services/Health/MigrationsQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/migrations'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/migrations', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/StatsResourcesQueueTest.php b/tests/e2e/Services/Health/StatsResourcesQueueTest.php deleted file mode 100644 index e251926179..0000000000 --- a/tests/e2e/Services/Health/StatsResourcesQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/stats-resources'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/stats-resources', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/StatsUsageQueueTest.php b/tests/e2e/Services/Health/StatsUsageQueueTest.php deleted file mode 100644 index fda30c9bb5..0000000000 --- a/tests/e2e/Services/Health/StatsUsageQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/stats-usage'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/stats-usage', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Health/WebhooksQueueTest.php b/tests/e2e/Services/Health/WebhooksQueueTest.php deleted file mode 100644 index 1171686069..0000000000 --- a/tests/e2e/Services/Health/WebhooksQueueTest.php +++ /dev/null @@ -1,19 +0,0 @@ -callGet('/health/queue/webhooks'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertLessThan(100, $response['body']['size']); - - $failure = $this->callGet('/health/queue/webhooks', ['threshold' => '0']); - $this->assertEquals(503, $failure['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index e6c8babe9b..534fa4125a 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -8,16 +8,14 @@ use Utopia\Database\Helpers\ID; use Utopia\System\System; /** - * End-to-end coverage for the notifications queue health surface and the + * End-to-end coverage for the notifications failed-jobs health surface and the * notifications user-facing API. * * The notification worker itself is exercised in unit tests with a pinned * queue payload; the server side cannot deterministically inject a - * Notification onto the live queue without an admin endpoint, so the health - * portion validates the public contract that ops and KEDA scale on: + * Notification onto the live queue without an admin endpoint. This suite keeps + * coverage for the failed-jobs health surface and the user-facing API: * - * - GET /v1/health/queue/notifications returns the live queue depth - * - the threshold guard returns 503 when the depth exceeds the budget * - the failed-jobs surface accepts the notifications queue name * * The notifications portion exercises the full webhook-paused fanout end-to-end: @@ -37,28 +35,6 @@ use Utopia\System\System; */ trait NotificationsBase { - public function testHealthQueueNotificationsReportsSize(): void - { - $response = $this->client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertGreaterThanOrEqual(0, $response['body']['size']); - } - - public function testHealthQueueNotificationsThresholdGuard(): void - { - $response = $this->client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), ['threshold' => '0']); - - $this->assertContains($response['headers']['status-code'], [200, 503]); - } - public function testHealthQueueFailedAcceptsNotifications(): void { $response = $this->client->call(Client::METHOD_GET, '/health/queue/failed/v1-notifications', \array_merge([ diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index d417888e23..43cbe1a53e 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -23,6 +23,7 @@ trait PoliciesBase 'session-limit' => ['total'], 'user-limit' => ['total'], 'membership-privacy' => ['userId', 'userEmail', 'userPhone', 'userName', 'userMFA', 'userAccessedAt'], + 'mfa-factors' => ['totp', 'email', 'phone', 'custom'], ]; } diff --git a/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php b/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php new file mode 100644 index 0000000000..d0facbf59e --- /dev/null +++ b/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php @@ -0,0 +1,189 @@ +getProject(true); + $projectId = $project['$id']; + + $serverHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-key' => $project['apiKey'], + ]; + + $policy = $this->client->call(Client::METHOD_GET, '/project/policies/mfa-factors', $serverHeaders); + $this->assertSame(200, $policy['headers']['status-code']); + $this->assertTrue($policy['body']['totp']); + $this->assertTrue($policy['body']['email']); + $this->assertTrue($policy['body']['phone']); + $this->assertFalse($policy['body']['custom']); + + $list = $this->client->call(Client::METHOD_GET, '/project/policies', $serverHeaders); + $this->assertSame(200, $list['headers']['status-code']); + + $byId = []; + foreach ($list['body']['policies'] as $listed) { + $byId[$listed['$id']] = $listed; + } + $this->assertArrayHasKey('mfa-factors', $byId); + $this->assertSame($policy['body'], $byId['mfa-factors']); + + $member = $this->createUserWithSession($serverHeaders); + + $factors = $this->client->call(Client::METHOD_GET, '/account/mfa/factors', $member['sessionHeaders']); + $this->assertSame(200, $factors['headers']['status-code']); + $this->assertFalse($factors['body']['custom']); + + $challenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'factor' => 'custom', + ]); + $this->assertSame(501, $challenge['headers']['status-code']); + $this->assertSame('user_auth_method_unsupported', $challenge['body']['type']); + } + + public function testMFAFactorsPolicyIntegration(): void + { + $project = $this->getProject(true); + $projectId = $project['$id']; + + $serverHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-key' => $project['apiKey'], + ]; + + // Step 1: Enable the custom factor and disable email + $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/mfa-factors', $serverHeaders, [ + 'custom' => true, + 'email' => false, + ]); + $this->assertSame(200, $response['headers']['status-code']); + + $policy = $this->client->call(Client::METHOD_GET, '/project/policies/mfa-factors', $serverHeaders); + $this->assertSame(200, $policy['headers']['status-code']); + $this->assertTrue($policy['body']['totp']); + $this->assertFalse($policy['body']['email']); + $this->assertTrue($policy['body']['phone']); + $this->assertTrue($policy['body']['custom']); + + // Step 2: A user with a verified email sees the policy reflected in factors + $member = $this->createUserWithSession($serverHeaders, verifiedEmail: true); + + $factors = $this->client->call(Client::METHOD_GET, '/account/mfa/factors', $member['sessionHeaders']); + $this->assertSame(200, $factors['headers']['status-code']); + $this->assertTrue($factors['body']['custom']); + $this->assertFalse($factors['body']['email']); + + // Step 3: Challenges for a disabled factor are rejected + $challenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'factor' => 'email', + ]); + $this->assertSame(501, $challenge['headers']['status-code']); + $this->assertSame('user_auth_method_unsupported', $challenge['body']['type']); + + // Step 4: The custom factor completes the full challenge flow + $challenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'factor' => 'custom', + ]); + $this->assertSame(201, $challenge['headers']['status-code']); + $this->assertArrayNotHasKey('code', $challenge['body']); + $challengeId = $challenge['body']['$id']; + + $secret = $this->client->call(Client::METHOD_GET, '/users/' . $member['userId'] . '/mfa/challenges/' . $challengeId, $serverHeaders); + $this->assertSame(200, $secret['headers']['status-code']); + $this->assertNotEmpty($secret['body']['code']); + + $verification = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'challengeId' => $challengeId, + 'otp' => $secret['body']['code'], + ]); + $this->assertSame(200, $verification['headers']['status-code']); + $this->assertContains('custom', $verification['body']['factors']); + + // Step 5: Leave a custom challenge pending, then restore email and disable custom again + $pending = $this->client->call(Client::METHOD_POST, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'factor' => 'custom', + ]); + $this->assertSame(201, $pending['headers']['status-code']); + + $pendingSecret = $this->client->call(Client::METHOD_GET, '/users/' . $member['userId'] . '/mfa/challenges/' . $pending['body']['$id'], $serverHeaders); + $this->assertSame(200, $pendingSecret['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/mfa-factors', $serverHeaders, [ + 'custom' => false, + 'email' => true, + ]); + $this->assertSame(200, $response['headers']['status-code']); + + // Disabling a factor also invalidates challenges issued while it was enabled + $verification = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'challengeId' => $pending['body']['$id'], + 'otp' => $pendingSecret['body']['code'], + ]); + $this->assertSame(501, $verification['headers']['status-code']); + $this->assertSame('user_auth_method_unsupported', $verification['body']['type']); + + $factors = $this->client->call(Client::METHOD_GET, '/account/mfa/factors', $member['sessionHeaders']); + $this->assertSame(200, $factors['headers']['status-code']); + $this->assertFalse($factors['body']['custom']); + $this->assertTrue($factors['body']['email']); + + $challenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenges', $member['sessionHeaders'], [ + 'factor' => 'custom', + ]); + $this->assertSame(501, $challenge['headers']['status-code']); + } + + /** + * Create a user with a server-issued session. + * + * @param array $serverHeaders + * @return array{userId: string, sessionHeaders: array} + */ + private function createUserWithSession(array $serverHeaders, bool $verifiedEmail = false): array + { + $user = $this->client->call(Client::METHOD_POST, '/users', $serverHeaders, [ + 'userId' => ID::unique(), + 'email' => 'mfa_' . \uniqid() . '@localhost.test', + 'password' => 'password1234', + 'name' => 'Morgan Miller', + ]); + $this->assertSame(201, $user['headers']['status-code']); + $userId = $user['body']['$id']; + + if ($verifiedEmail) { + $response = $this->client->call(Client::METHOD_PATCH, '/users/' . $userId . '/verification', $serverHeaders, [ + 'emailVerification' => true, + ]); + $this->assertSame(200, $response['headers']['status-code']); + } + + $session = $this->client->call(Client::METHOD_POST, '/users/' . $userId . '/sessions', $serverHeaders); + $this->assertSame(201, $session['headers']['status-code']); + + return [ + 'userId' => $userId, + 'sessionHeaders' => [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $serverHeaders['x-appwrite-project'], + 'x-appwrite-session' => $session['body']['secret'], + ], + ]; + } +} diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index d63d4fda9d..57baf4b6f2 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -633,6 +633,16 @@ trait UsersBase { $projectId = $this->getProject()['$id']; + // Enable the custom factor, which the MFA factors policy disables by default + $policy = $this->client->call(Client::METHOD_PATCH, '/project/policies/mfa-factors', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'custom' => true, + ]); + + $this->assertEquals(200, $policy['headers']['status-code']); + $user = $this->client->call(Client::METHOD_POST, '/users', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, diff --git a/tests/unit/Auth/Validator/PhoneTest.php b/tests/unit/Auth/Validator/PhoneTest.php index 4392d960e6..05e5b2e259 100644 --- a/tests/unit/Auth/Validator/PhoneTest.php +++ b/tests/unit/Auth/Validator/PhoneTest.php @@ -45,4 +45,29 @@ final class PhoneTest extends TestCase $this->assertTrue($this->object->isValid('+55115525632534')); $this->assertTrue($this->object->isValid('+919367788755111')); } + + public function testNormalizeRecoversPathEncoding(): void + { + $this->assertSame('+15550102680', Phone::normalize('%2B15550102680')); + $this->assertSame('+15550102680', Phone::normalize(' 15550102680')); + $this->assertSame(' 15550102680', Phone::normalize(' 15550102680')); + $this->assertSame('+15550102680', Phone::normalize('15550102680')); + $this->assertSame('+15550102680', Phone::normalize('+15550102680')); + } + + public function testNormalizeFlagAcceptsPathEncoding(): void + { + $phone = new Phone(normalize: true); + + $this->assertTrue($phone->isValid('%2B15550102680')); + $this->assertTrue($phone->isValid(' 15550102680')); + $this->assertTrue($phone->isValid('15550102680')); + $this->assertTrue($phone->isValid('+15550102680')); + $this->assertFalse($phone->isValid(' 15550102680')); + + // Default validator still rejects path-damaged values. + $this->assertFalse($this->object->isValid('%2B15550102680')); + $this->assertFalse($this->object->isValid(' 15550102680')); + $this->assertFalse($this->object->isValid('15550102680')); + } } diff --git a/tests/unit/Event/EventTest.php b/tests/unit/Event/EventTest.php index 6f15c80b9a..04f3f7749f 100644 --- a/tests/unit/Event/EventTest.php +++ b/tests/unit/Event/EventTest.php @@ -216,5 +216,15 @@ final class EventTest extends TestCase $this->assertContains('documentsdb.factory-db.collections.assembly.documents.doc-123.update', $documentsDbEvents); $this->assertNotContains('documentsdb.factory-db.tables.assembly.rows.doc-123.update', $documentsDbEvents); $this->assertNotContains('databases.factory-db.collections.assembly.documents.doc-123.update', $documentsDbEvents); + + $vectorsDb = new Document(['type' => 'vectorsdb']); + $vectorsDbEvents = Event::generateEvents('databases.[databaseId].collections.[collectionId].documents.[documentId].update', [ + 'databaseId' => 'factory-db', + 'collectionId' => 'assembly', + 'documentId' => 'doc-123', + ], $vectorsDb); + $this->assertContains('vectorsdb.factory-db.collections.assembly.documents.doc-123.update', $vectorsDbEvents); + $this->assertNotContains('vectorsdb.factory-db.tables.assembly.rows.doc-123.update', $vectorsDbEvents); + $this->assertNotContains('databases.factory-db.collections.assembly.documents.doc-123.update', $vectorsDbEvents); } } diff --git a/tests/unit/Event/Validator/EventValidatorTest.php b/tests/unit/Event/Validator/EventValidatorTest.php index 3af99bc3c2..ab95e1c7b8 100644 --- a/tests/unit/Event/Validator/EventValidatorTest.php +++ b/tests/unit/Event/Validator/EventValidatorTest.php @@ -79,6 +79,50 @@ final class EventValidatorTest extends TestCase $this->assertTrue($this->object->isValid('tablesdb.books.tables.chapters')); $this->assertTrue($this->object->isValid('tablesdb.books.tables.*')); $this->assertTrue($this->object->isValid('tablesdb.books.tables.chapters.columns.name.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.prolog')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.*')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.*.documents.prolog.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.*.documents.prolog')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.*.documents.*')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.chapters.documents.prolog')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.chapters.documents.*')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*.documents.prolog.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*.documents.prolog')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*.documents.*')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*')); + $this->assertTrue($this->object->isValid('documentsdb.*')); + $this->assertTrue($this->object->isValid('documentsdb.books')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.*')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.attributes.name.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.prolog')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.*')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.*.documents.prolog.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.*.documents.prolog')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.*.documents.*')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.chapters.documents.prolog')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.chapters.documents.*')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*.documents.prolog.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*.documents.prolog')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*.documents.*')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*')); + $this->assertTrue($this->object->isValid('vectorsdb.*')); + $this->assertTrue($this->object->isValid('vectorsdb.books')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.*')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.attributes.name.create')); $this->assertTrue($this->object->isValid('functions.*')); $this->assertTrue($this->object->isValid('buckets.*')); $this->assertTrue($this->object->isValid('teams.*')); @@ -97,6 +141,10 @@ final class EventValidatorTest extends TestCase $this->assertFalse($this->object->isValid('tables.*.rows.*.unknown')); $this->assertFalse($this->object->isValid('tablesdb.books.collections.chapters.documents.prolog.create')); $this->assertFalse($this->object->isValid('tablesdb.books.tables.chapters.rows.prolog.unknown')); + $this->assertFalse($this->object->isValid('documentsdb.books.tables.chapters.rows.prolog.create')); + $this->assertFalse($this->object->isValid('documentsdb.books.collections.chapters.documents.prolog.unknown')); + $this->assertFalse($this->object->isValid('vectorsdb.books.tables.chapters.rows.prolog.create')); + $this->assertFalse($this->object->isValid('vectorsdb.books.collections.chapters.documents.prolog.unknown')); $this->assertFalse($this->object->isValid('users.torsten.unknown')); $this->assertFalse($this->object->isValid('users.torsten.delete.email')); $this->assertFalse($this->object->isValid('teams.*.memberships.*.update.unknown')); diff --git a/tests/unit/Event/Validator/FunctionEventValidatorTest.php b/tests/unit/Event/Validator/FunctionEventValidatorTest.php index 3fbbbb905b..59e08eb23a 100644 --- a/tests/unit/Event/Validator/FunctionEventValidatorTest.php +++ b/tests/unit/Event/Validator/FunctionEventValidatorTest.php @@ -62,6 +62,16 @@ final class FunctionEventValidatorTest extends TestCase $this->assertTrue($this->object->isValid('tablesdb.*.tables.*.rows.*.create')); $this->assertTrue($this->object->isValid('tablesdb.*.tables.*')); $this->assertTrue($this->object->isValid('tablesdb.*')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('documentsdb.books.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('documentsdb.*.collections.*')); + $this->assertTrue($this->object->isValid('documentsdb.*')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.prolog.create')); + $this->assertTrue($this->object->isValid('vectorsdb.books.collections.chapters.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*.documents.*.create')); + $this->assertTrue($this->object->isValid('vectorsdb.*.collections.*')); + $this->assertTrue($this->object->isValid('vectorsdb.*')); $this->assertTrue($this->object->isValid('buckets.*')); $this->assertTrue($this->object->isValid('teams.*')); $this->assertTrue($this->object->isValid('users.*')); @@ -78,6 +88,8 @@ final class FunctionEventValidatorTest extends TestCase $this->assertFalse($this->object->isValid('tables.*.unknown')); $this->assertFalse($this->object->isValid('tables.*.rows.*.unknown')); $this->assertFalse($this->object->isValid('tablesdb.books.collections.chapters.documents.prolog.create')); + $this->assertFalse($this->object->isValid('documentsdb.books.tables.chapters.rows.prolog.create')); + $this->assertFalse($this->object->isValid('vectorsdb.books.tables.chapters.rows.prolog.create')); $this->assertFalse($this->object->isValid('users.torsten.unknown')); $this->assertFalse($this->object->isValid('users.torsten.delete.email')); $this->assertFalse($this->object->isValid('teams.*.memberships.*.update.unknown')); diff --git a/tests/unit/Network/CorsTest.php b/tests/unit/Network/CorsTest.php index c406b51bfa..ab94ba5bbd 100644 --- a/tests/unit/Network/CorsTest.php +++ b/tests/unit/Network/CorsTest.php @@ -131,6 +131,112 @@ final class CorsTest extends TestCase $this->assertSame('https://example.com', $result[Cors::HEADER_ALLOW_ORIGIN]); } + public function testLoopbackOriginIsAllowed(): void + { + $cors = new Cors( + allowedHosts: ['example.com', 'localhost'], + allowedMethods: ['GET'], + allowedHeaders: ['X-Test'], + exposedHeaders: [], + allowCredentials: true + ); + + foreach ( + [ + 'http://localhost', + 'http://localhost:3000', + 'http://127.0.0.1', + 'https://127.0.0.1:5173', + 'http://[::1]', + 'http://[::1]:3000', + ] as $origin + ) { + $this->assertSame($origin, $cors->headers($origin)[Cors::HEADER_ALLOW_ORIGIN] ?? null); + } + } + + public function testLoopbackOriginIsEchoedExactly(): void + { + $cors = new Cors( + allowedHosts: ['example.com', 'localhost'], + allowedMethods: ['GET'], + allowedHeaders: ['X-Test'], + exposedHeaders: [], + allowCredentials: true + ); + + /** + * Trusting loopback with credentials is only safe while the origin is + * echoed back verbatim, since the browser requires a literal match. + * A wildcard here would hand the allowance to every remote page. + */ + $result = $cors->headers('http://127.0.0.1:5173'); + + $this->assertSame('http://127.0.0.1:5173', $result[Cors::HEADER_ALLOW_ORIGIN]); + $this->assertNotSame('*', $result[Cors::HEADER_ALLOW_ORIGIN]); + $this->assertSame('true', $result[Cors::HEADER_ALLOW_CREDENTIALS]); + } + + public function testLoopbackLookalikeOriginIsRejected(): void + { + $cors = new Cors( + allowedHosts: ['example.com', 'localhost'], + allowedMethods: ['GET'], + allowedHeaders: ['X-Test'], + exposedHeaders: [], + allowCredentials: true + ); + + foreach ( + [ + 'http://127.0.0.1.evil.com', + 'http://localhost.evil.com', + 'http://128.0.0.1', + 'http://[2001:db8::1]', + /* A prefix or substring match would wrongly accept these */ + 'http://xlocalhost', + 'http://127.0.0.1x.example.com', + 'http://[::1].evil.com', + 'http://[::1]evil.com', + /* Only the exact alias spellings resolve to localhost */ + 'http://127.0.0.2', + 'http://[0:0:0:0:0:0:0:1]', + 'http://localhost.', + ] as $origin + ) { + $this->assertArrayNotHasKey(Cors::HEADER_ALLOW_ORIGIN, $cors->headers($origin)); + } + } + + public function testLoopbackRequiresLocalhostToBeAllowed(): void + { + /** + * Credentialed CORS plus SameSite=None cookies means an accepted origin + * can read authenticated responses, so a deployment that does not trust + * localhost must not have any spelling of it reflected back. + */ + $cors = new Cors( + allowedHosts: ['example.com'], + allowedMethods: ['GET'], + allowedHeaders: ['X-Test'], + exposedHeaders: [], + allowCredentials: true + ); + + foreach ( + [ + 'http://localhost', + 'http://localhost:3000', + 'http://127.0.0.1', + 'https://127.0.0.1:5173', + 'http://[::1]', + 'http://[::1]:3000', + ] as $origin + ) { + $this->assertArrayNotHasKey(Cors::HEADER_ALLOW_ORIGIN, $cors->headers($origin), 'Origin ' . $origin . ' was unexpectedly allowed'); + } + } + public function testHeaderFormatting(): void { $cors = new Cors( diff --git a/tests/unit/Network/Validators/OriginTest.php b/tests/unit/Network/Validators/OriginTest.php index 6c5d7a878c..ececb2294d 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -83,6 +83,72 @@ final class OriginTest extends TestCase $this->assertSame('Invalid Scheme. The scheme used (random-scheme) in the Origin (random-scheme://localhost) is not supported. If you are using a custom scheme, please change it to `appwrite-callback-`', $validator->getDescription()); } + public function testLoopback(): void + { + $validator = new Origin( + allowedHostnames: ['appwrite.io', 'localhost'], + allowedSchemes: ['exp'] + ); + + /* Allowing localhost allows its other spellings too */ + $this->assertEquals(true, $validator->isValid('http://localhost')); + $this->assertEquals(true, $validator->isValid('http://localhost:3000')); + $this->assertEquals(true, $validator->isValid('http://127.0.0.1')); + $this->assertEquals(true, $validator->isValid('https://127.0.0.1:5173')); + $this->assertEquals(true, $validator->isValid('http://[::1]')); + $this->assertEquals(true, $validator->isValid('http://[::1]:3000')); + + /* Hostnames that only look like loopback are still rejected */ + $this->assertEquals(false, $validator->isValid('http://127.0.0.1.example.com')); + $this->assertEquals(false, $validator->isValid('http://localhost.example.com')); + $this->assertEquals(false, $validator->isValid('http://127.0.0.1@example.com')); + $this->assertEquals(false, $validator->isValid('http://128.0.0.1')); + $this->assertEquals(false, $validator->isValid('http://[2001:db8::1]')); + + /* A prefix or substring match would wrongly accept these */ + $this->assertEquals(false, $validator->isValid('http://xlocalhost')); + $this->assertEquals(false, $validator->isValid('http://127.0.0.1x.example.com')); + $this->assertEquals(false, $validator->isValid('http://[::1].evil.com')); + $this->assertEquals(false, $validator->isValid('http://[::1]evil.com')); + + /* Only the exact loopback spellings are hardcoded */ + $this->assertEquals(false, $validator->isValid('http://127.0.0.2')); + $this->assertEquals(false, $validator->isValid('http://[0:0:0:0:0:0:0:1]')); + $this->assertEquals(false, $validator->isValid('http://localhost.')); + + /* Loopback does not bypass the scheme allow list */ + $this->assertEquals(false, $validator->isValid('random-scheme://127.0.0.1')); + } + + public function testLoopbackRequiresLocalhostToBeAllowed(): void + { + /* A deployment that does not trust localhost trusts no spelling of it */ + $validator = new Origin( + allowedHostnames: ['appwrite.io'], + allowedSchemes: ['exp'] + ); + + $this->assertEquals(false, $validator->isValid('http://localhost')); + $this->assertEquals(false, $validator->isValid('http://localhost:3000')); + $this->assertEquals(false, $validator->isValid('http://127.0.0.1')); + $this->assertEquals(false, $validator->isValid('https://127.0.0.1:5173')); + $this->assertEquals(false, $validator->isValid('http://[::1]')); + $this->assertEquals(false, $validator->isValid('http://[::1]:3000')); + } + + public function testLoopbackAliasHonoursAnExplicitEntry(): void + { + /* An explicitly allowed literal still matches on its own */ + $validator = new Origin( + allowedHostnames: ['127.0.0.1'], + allowedSchemes: ['exp'] + ); + + $this->assertEquals(true, $validator->isValid('http://127.0.0.1:5173')); + $this->assertEquals(false, $validator->isValid('http://localhost')); + $this->assertEquals(false, $validator->isValid('http://[::1]')); + } + public function testGetAllowedHostnames(): void { $validator = new Origin( diff --git a/tests/unit/Platform/Tasks/ScheduleBaseTest.php b/tests/unit/Platform/Tasks/ScheduleBaseTest.php new file mode 100644 index 0000000000..907939873a --- /dev/null +++ b/tests/unit/Platform/Tasks/ScheduleBaseTest.php @@ -0,0 +1,61 @@ +assertSame( + ScheduleBase::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 60), + ScheduleBase::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 60) + ); + } + + public function testSpreadOffsetIsBounded(): void + { + foreach ($this->sampleIds() as $id) { + foreach ([2, 30, 60, 300] as $window) { + $offset = ScheduleBase::spreadOffset($id, $window); + $this->assertGreaterThanOrEqual(0, $offset); + $this->assertLessThan($window, $offset); + } + } + } + + public function testSpreadOffsetIsZeroWhenDisabled(): void + { + $this->assertSame(0, ScheduleBase::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 1)); + $this->assertSame(0, ScheduleBase::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 0)); + $this->assertSame(0, ScheduleBase::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', -5)); + } + + public function testSpreadOffsetDispersesDistinctResources(): void + { + $offsets = []; + foreach ($this->sampleIds() as $id) { + $offsets[] = ScheduleBase::spreadOffset($id, 60); + } + + // Resources sharing a cron slot must not all land in the same second. + $this->assertGreaterThan(10, count(array_unique($offsets))); + } + + /** + * @return string[] + */ + private function sampleIds(): array + { + $ids = []; + for ($i = 0; $i < 50; $i++) { + $ids[] = md5("resource-{$i}"); + } + + return $ids; + } +} diff --git a/tests/unit/Platform/Tasks/ScheduleFunctionsTest.php b/tests/unit/Platform/Tasks/ScheduleFunctionsTest.php index 0fc88fd0d4..1d4a34a796 100644 --- a/tests/unit/Platform/Tasks/ScheduleFunctionsTest.php +++ b/tests/unit/Platform/Tasks/ScheduleFunctionsTest.php @@ -13,44 +13,6 @@ use Utopia\Database\Database; final class ScheduleFunctionsTest extends TestCase { - public function testSpreadOffsetIsDeterministic(): void - { - $this->assertSame( - ScheduleFunctions::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 60), - ScheduleFunctions::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 60) - ); - } - - public function testSpreadOffsetIsBounded(): void - { - foreach ($this->sampleIds() as $id) { - foreach ([2, 30, 60, 300] as $window) { - $offset = ScheduleFunctions::spreadOffset($id, $window); - $this->assertGreaterThanOrEqual(0, $offset); - $this->assertLessThan($window, $offset); - } - } - } - - public function testSpreadOffsetIsZeroWhenDisabled(): void - { - $this->assertSame(0, ScheduleFunctions::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 1)); - $this->assertSame(0, ScheduleFunctions::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', 0)); - $this->assertSame(0, ScheduleFunctions::spreadOffset('64f5c8e2a1b3d4e5f6a7b8c9', -5)); - } - - public function testSpreadOffsetDispersesDistinctFunctions(): void - { - $offsets = []; - foreach ($this->sampleIds() as $id) { - $offsets[] = ScheduleFunctions::spreadOffset($id, 60); - } - - // The whole point: functions sharing a cron slot must not all land - // in the same second. Expect real dispersion across the window. - $this->assertGreaterThan(10, count(array_unique($offsets))); - } - public function testSpreadWindowDefaultsToEnv(): void { $task = new class () extends ScheduleFunctions { @@ -74,17 +36,4 @@ final class ScheduleFunctionsTest extends TestCase : putenv("_APP_FUNCTIONS_SCHEDULE_SPREAD={$previous}"); } } - - /** - * @return string[] - */ - private function sampleIds(): array - { - $ids = []; - for ($i = 0; $i < 50; $i++) { - $ids[] = md5("function-{$i}"); - } - - return $ids; - } } diff --git a/tests/unit/Utopia/Database/Validator/AttributesTest.php b/tests/unit/Utopia/Database/Validator/AttributesTest.php new file mode 100644 index 0000000000..6c02e5bbe7 --- /dev/null +++ b/tests/unit/Utopia/Database/Validator/AttributesTest.php @@ -0,0 +1,137 @@ +object = new Attributes(); + } + + public function testStringTypes(): void + { + $this->assertTrue($this->object->isValid([ + ['key' => 'title', 'type' => Database::VAR_STRING, 'size' => 128], + ['key' => 'slug', 'type' => Database::VAR_VARCHAR, 'size' => 128], + ['key' => 'body', 'type' => Database::VAR_TEXT], + ['key' => 'summary', 'type' => Database::VAR_MEDIUMTEXT], + ['key' => 'archive', 'type' => Database::VAR_LONGTEXT], + ]), $this->object->getDescription()); + } + + public function testStringTypeDefaults(): void + { + $this->assertTrue($this->object->isValid([ + ['key' => 'body', 'type' => Database::VAR_TEXT, 'default' => 'hello'], + ]), $this->object->getDescription()); + + $this->assertFalse($this->object->isValid([ + ['key' => 'body', 'type' => Database::VAR_TEXT, 'default' => 1], + ])); + + $this->assertFalse($this->object->isValid([ + ['key' => 'slug', 'type' => Database::VAR_VARCHAR, 'size' => 4, 'default' => 'toolong'], + ])); + } + + public function testVarcharRequiresSize(): void + { + $this->assertFalse($this->object->isValid([ + ['key' => 'slug', 'type' => Database::VAR_VARCHAR], + ])); + } + + public function testFormatTypes(): void + { + $this->assertTrue($this->object->isValid([ + ['key' => 'email', 'type' => APP_DATABASE_ATTRIBUTE_EMAIL, 'required' => true], + ['key' => 'website', 'type' => APP_DATABASE_ATTRIBUTE_URL], + ['key' => 'address', 'type' => APP_DATABASE_ATTRIBUTE_IP], + ['key' => 'status', 'type' => APP_DATABASE_ATTRIBUTE_ENUM, 'elements' => ['on', 'off'], 'default' => 'on'], + ]), $this->object->getDescription()); + + $this->assertFalse($this->object->isValid([ + ['key' => 'email', 'type' => APP_DATABASE_ATTRIBUTE_EMAIL, 'default' => 'not-an-email'], + ])); + + $this->assertFalse($this->object->isValid([ + ['key' => 'status', 'type' => APP_DATABASE_ATTRIBUTE_ENUM], + ])); + } + + public function testEmailWithoutSize(): void + { + $this->assertTrue($this->object->isValid([ + ['key' => 'email', 'type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_EMAIL], + ]), $this->object->getDescription()); + } + + public function testUnsupportedType(): void + { + $this->assertFalse($this->object->isValid([ + ['key' => 'rel', 'type' => Database::VAR_RELATIONSHIP], + ])); + $this->assertSame("Invalid type for attribute 'rel': relationship", $this->object->getDescription()); + } + + public function testResolveMatchesDedicatedEndpoints(): void + { + $this->assertEquals( + ['type' => Database::VAR_TEXT, 'format' => '', 'size' => 65535], + Attribute::resolve(['key' => 'body', 'type' => Database::VAR_TEXT]) + ); + + $this->assertEquals( + ['type' => Database::VAR_MEDIUMTEXT, 'format' => '', 'size' => 16777215], + Attribute::resolve(['key' => 'body', 'type' => Database::VAR_MEDIUMTEXT]) + ); + + $this->assertEquals( + ['type' => Database::VAR_LONGTEXT, 'format' => '', 'size' => 2147483647], + Attribute::resolve(['key' => 'body', 'type' => Database::VAR_LONGTEXT]) + ); + + $this->assertEquals( + ['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_EMAIL, 'size' => 254], + Attribute::resolve(['key' => 'email', 'type' => APP_DATABASE_ATTRIBUTE_EMAIL]) + ); + + $this->assertEquals( + ['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_IP, 'size' => 39], + Attribute::resolve(['key' => 'ip', 'type' => APP_DATABASE_ATTRIBUTE_IP]) + ); + + $this->assertEquals( + ['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_URL, 'size' => 2000], + Attribute::resolve(['key' => 'url', 'type' => APP_DATABASE_ATTRIBUTE_URL]) + ); + + $this->assertEquals( + ['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_ENUM, 'size' => Database::LENGTH_KEY], + Attribute::resolve(['key' => 'enum', 'type' => APP_DATABASE_ATTRIBUTE_ENUM]) + ); + } + + public function testResolveKeepsExplicitSize(): void + { + $this->assertEquals( + ['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_EMAIL, 'size' => 512], + Attribute::resolve(['key' => 'email', 'type' => APP_DATABASE_ATTRIBUTE_EMAIL, 'size' => 512]) + ); + + $this->assertEquals( + ['type' => Database::VAR_VARCHAR, 'format' => '', 'size' => 128], + Attribute::resolve(['key' => 'slug', 'type' => Database::VAR_VARCHAR, 'size' => 128]) + ); + } +} diff --git a/tests/unit/Utopia/Database/Validator/Queries/BaseTest.php b/tests/unit/Utopia/Database/Validator/Queries/BaseTest.php new file mode 100644 index 0000000000..88223b7e4e --- /dev/null +++ b/tests/unit/Utopia/Database/Validator/Queries/BaseTest.php @@ -0,0 +1,53 @@ +assertTrue( + $validator->isValid([Query::equal('userId', $values)]), + $validator->getDescription() + ); + } + + public function testQueryOverMaxValuesIsInvalid(): void + { + $validator = new Presences(); + $values = \array_fill(0, APP_DATABASE_QUERY_MAX_VALUES + 1, 'user'); + + $this->assertFalse( + $validator->isValid([Query::equal('userId', $values)]), + 'Query with ' . \count($values) . ' values must exceed the ' . APP_DATABASE_QUERY_MAX_VALUES . ' value cap' + ); + $this->assertStringContainsString( + 'greater than ' . APP_DATABASE_QUERY_MAX_VALUES . ' values', + $validator->getDescription(), + 'Rejection must be reported against the configured cap, not the validator default' + ); + } + + public function testSequenceTypedAttributeIsValidated(): void + { + $validator = new Presences(); + + $this->assertTrue( + $validator->isValid([Query::equal('userInternalId', [1])]), + $validator->getDescription() + ); + $this->assertFalse( + $validator->isValid([Query::equal('userInternalId', [1.5])]), + 'Fractional value must not validate against a sequence attribute' + ); + } +}