From 6b97e71af27f60b2953c5fa3734948fdb2a86e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 28 Jul 2026 11:25:40 +0200 Subject: [PATCH 01/19] Fix request denied from loopback url --- AGENTS.md | 2 + src/Appwrite/Network/Cors.php | 17 +- src/Appwrite/Network/Platform.php | 9 + src/Appwrite/Network/Validator/Origin.php | 5 + tests/e2e/General/HTTPTest.php | 48 ++++ .../Account/AccountCustomClientTest.php | 208 ++++++++++++++++++ tests/unit/Network/CorsTest.php | 49 +++++ tests/unit/Network/Validators/OriginTest.php | 30 +++ 8 files changed, 361 insertions(+), 7 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a54bb22dfb..0a0b00c500 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/src/Appwrite/Network/Cors.php b/src/Appwrite/Network/Cors.php index 593c82ab24..6719e7835b 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,10 +77,13 @@ final class Cors return $headers; } - // Match only by host - $validator = new Hostname($this->allowedHosts); - if (!$validator->isValid($host)) { - return $headers; + // Match only by host. Loopback clients are always allowed, so local + // development needs no platform registration. + if (!\in_array($host, Platform::LOOPBACK_HOSTNAMES, true)) { + $validator = new Hostname($this->allowedHosts); + if (!$validator->isValid($host)) { + return $headers; + } } // Accepted diff --git a/src/Appwrite/Network/Platform.php b/src/Appwrite/Network/Platform.php index 23fb087010..b95f609cf8 100644 --- a/src/Appwrite/Network/Platform.php +++ b/src/Appwrite/Network/Platform.php @@ -27,6 +27,15 @@ class Platform public const SCHEME_LINUX = 'appwrite-linux'; public const SCHEME_TAURI = 'tauri'; + /** + * Hostnames that address the client's own machine. Always allowed, so local + * development works without registering a platform. IPv6 is bracketed to + * match what parse_url() returns for a host. + * + * @var array + */ + public const array LOOPBACK_HOSTNAMES = ['localhost', '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..48ed75395f 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -72,6 +72,11 @@ class Origin extends Validator Platform::SCHEME_TAURI, ]; if (in_array($this->scheme, $webPlatforms, true)) { + /* Loopback clients are always allowed, so local development needs no platform */ + if (in_array($this->host, Platform::LOOPBACK_HOSTNAMES, true)) { + return true; + } + $validator = new Hostname($this->allowedHostnames); return $validator->isValid($this->host); } diff --git a/tests/e2e/General/HTTPTest.php b/tests/e2e/General/HTTPTest.php index 68331ae47f..0f393815ad 100644 --- a/tests/e2e/General/HTTPTest.php +++ b/tests/e2e/General/HTTPTest.php @@ -156,6 +156,54 @@ 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'); + } + + /** + * 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]', + // Only the exact loopback spellings are hardcoded + 'http://127.0.0.2', + 'http://[0:0:0:0:0:0:0:1]', + ]; + + 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/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index a11cdd9b63..421722efbe 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']; diff --git a/tests/unit/Network/CorsTest.php b/tests/unit/Network/CorsTest.php index c406b51bfa..962a5cf7d8 100644 --- a/tests/unit/Network/CorsTest.php +++ b/tests/unit/Network/CorsTest.php @@ -131,6 +131,55 @@ 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'], + 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 testLoopbackLookalikeOriginIsRejected(): void + { + $cors = new Cors( + allowedHosts: ['example.com'], + 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]', + /* Only the exact loopback spellings are hardcoded */ + 'http://127.0.0.2', + 'http://[0:0:0:0:0:0:0:1]', + ] as $origin + ) { + $this->assertArrayNotHasKey(Cors::HEADER_ALLOW_ORIGIN, $cors->headers($origin)); + } + } + 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..2e750a8e47 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -83,6 +83,36 @@ 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'], + allowedSchemes: ['exp'] + ); + + /* Loopback is allowed even though it is not in the allow list */ + $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]')); + + /* 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]')); + + /* Loopback does not bypass the scheme allow list */ + $this->assertEquals(false, $validator->isValid('random-scheme://127.0.0.1')); + } + public function testGetAllowedHostnames(): void { $validator = new Origin( From 3b825429ccaafcbbaef8d88014a7b175b0ed0add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 28 Jul 2026 11:44:58 +0200 Subject: [PATCH 02/19] Improve pr review clarity --- src/Appwrite/Network/Cors.php | 8 +++--- src/Appwrite/Network/Platform.php | 15 +++++++++++ src/Appwrite/Network/Validator/Origin.php | 2 +- tests/e2e/General/HTTPTest.php | 10 +++++++ tests/unit/Network/CorsTest.php | 28 ++++++++++++++++++++ tests/unit/Network/Validators/OriginTest.php | 7 +++++ 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Network/Cors.php b/src/Appwrite/Network/Cors.php index 6719e7835b..467da91690 100644 --- a/src/Appwrite/Network/Cors.php +++ b/src/Appwrite/Network/Cors.php @@ -77,8 +77,9 @@ final class Cors return $headers; } - // Match only by host. Loopback clients are always allowed, so local - // development needs no platform registration. + // Match only by host. Loopback clients are always allowed so local + // development needs no platform registration; see + // Platform::LOOPBACK_HOSTNAMES for why that is safe with credentials. if (!\in_array($host, Platform::LOOPBACK_HOSTNAMES, true)) { $validator = new Hostname($this->allowedHosts); if (!$validator->isValid($host)) { @@ -86,7 +87,8 @@ final class Cors } } - // 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 b95f609cf8..5e4a031722 100644 --- a/src/Appwrite/Network/Platform.php +++ b/src/Appwrite/Network/Platform.php @@ -32,6 +32,21 @@ class Platform * development works without registering a platform. IPv6 is bracketed to * match what parse_url() returns for a host. * + * Trusting these for credentialed CORS is deliberate. It cannot be turned + * against a remote page: a credentialed response must carry an + * Access-Control-Allow-Origin that literally equals the caller's own origin + * (the wildcard is illegal with credentials), so the only way to reach an + * API this way is to genuinely serve the page from a loopback port on the + * user's own machine. `localhost` was already trusted like this wherever + * _APP_CONSOLE_DOMAIN keeps its default, and 127.0.0.1 and [::1] address + * the very same interface. + * + * Match exactly, and on the host only. 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 and fall through to the + * configured allow list. + * * @var array */ public const array LOOPBACK_HOSTNAMES = ['localhost', '127.0.0.1', '[::1]']; diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index 48ed75395f..2ceec2348d 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -72,7 +72,7 @@ class Origin extends Validator Platform::SCHEME_TAURI, ]; if (in_array($this->scheme, $webPlatforms, true)) { - /* Loopback clients are always allowed, so local development needs no platform */ + /* Loopback needs no platform, see Platform::LOOPBACK_HOSTNAMES */ if (in_array($this->host, Platform::LOOPBACK_HOSTNAMES, true)) { return true; } diff --git a/tests/e2e/General/HTTPTest.php b/tests/e2e/General/HTTPTest.php index 0f393815ad..42d49bbabc 100644 --- a/tests/e2e/General/HTTPTest.php +++ b/tests/e2e/General/HTTPTest.php @@ -179,6 +179,11 @@ final class HTTPTest extends Scope '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']); } /** @@ -191,9 +196,14 @@ final class HTTPTest extends Scope '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) { diff --git a/tests/unit/Network/CorsTest.php b/tests/unit/Network/CorsTest.php index 962a5cf7d8..27d51adb90 100644 --- a/tests/unit/Network/CorsTest.php +++ b/tests/unit/Network/CorsTest.php @@ -155,6 +155,28 @@ final class CorsTest extends TestCase } } + public function testLoopbackOriginIsEchoedExactly(): void + { + $cors = new Cors( + allowedHosts: ['example.com'], + 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( @@ -171,9 +193,15 @@ final class CorsTest extends TestCase '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 loopback spellings are hardcoded */ '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)); diff --git a/tests/unit/Network/Validators/OriginTest.php b/tests/unit/Network/Validators/OriginTest.php index 2e750a8e47..2bdaf6aabe 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -105,9 +105,16 @@ final class OriginTest extends TestCase $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')); From 87b4c9ceeab0dd17fa60337cfc09ffe6019b4305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 28 Jul 2026 11:53:42 +0200 Subject: [PATCH 03/19] Improve implementatio nclarity for localhost --- src/Appwrite/Network/Cors.php | 14 ++++---- src/Appwrite/Network/Platform.php | 35 +++++++++--------- src/Appwrite/Network/Validator/Origin.php | 9 ++--- tests/unit/Network/CorsTest.php | 37 +++++++++++++++++--- tests/unit/Network/Validators/OriginTest.php | 33 +++++++++++++++-- 5 files changed, 95 insertions(+), 33 deletions(-) diff --git a/src/Appwrite/Network/Cors.php b/src/Appwrite/Network/Cors.php index 467da91690..aa80970721 100644 --- a/src/Appwrite/Network/Cors.php +++ b/src/Appwrite/Network/Cors.php @@ -77,12 +77,14 @@ final class Cors return $headers; } - // Match only by host. Loopback clients are always allowed so local - // development needs no platform registration; see - // Platform::LOOPBACK_HOSTNAMES for why that is safe with credentials. - if (!\in_array($host, Platform::LOOPBACK_HOSTNAMES, true)) { - $validator = new Hostname($this->allowedHosts); - if (!$validator->isValid($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)) { + $alias = \in_array($host, Platform::LOOPBACK_ALIASES, true) + && $validator->isValid(Platform::LOOPBACK_HOSTNAME); + + if (!$alias) { return $headers; } } diff --git a/src/Appwrite/Network/Platform.php b/src/Appwrite/Network/Platform.php index 5e4a031722..c20c012862 100644 --- a/src/Appwrite/Network/Platform.php +++ b/src/Appwrite/Network/Platform.php @@ -27,29 +27,30 @@ class Platform public const SCHEME_LINUX = 'appwrite-linux'; public const SCHEME_TAURI = 'tauri'; + public const string LOOPBACK_HOSTNAME = 'localhost'; + /** - * Hostnames that address the client's own machine. Always allowed, so local - * development works without registering a platform. IPv6 is bracketed to - * match what parse_url() returns for a host. + * 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. * - * Trusting these for credentialed CORS is deliberate. It cannot be turned - * against a remote page: a credentialed response must carry an - * Access-Control-Allow-Origin that literally equals the caller's own origin - * (the wildcard is illegal with credentials), so the only way to reach an - * API this way is to genuinely serve the page from a loopback port on the - * user's own machine. `localhost` was already trusted like this wherever - * _APP_CONSOLE_DOMAIN keeps its default, and 127.0.0.1 and [::1] address - * the very same interface. + * 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, and on the host only. 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 and fall through to the - * configured allow list. + * 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_HOSTNAMES = ['localhost', '127.0.0.1', '[::1]']; + 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 2ceec2348d..1d4cd79ea1 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -72,13 +72,14 @@ class Origin extends Validator Platform::SCHEME_TAURI, ]; if (in_array($this->scheme, $webPlatforms, true)) { - /* Loopback needs no platform, see Platform::LOOPBACK_HOSTNAMES */ - if (in_array($this->host, Platform::LOOPBACK_HOSTNAMES, true)) { + $validator = new Hostname($this->allowedHostnames); + if ($validator->isValid($this->host)) { return true; } - $validator = new Hostname($this->allowedHostnames); - return $validator->isValid($this->host); + /* 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/tests/unit/Network/CorsTest.php b/tests/unit/Network/CorsTest.php index 27d51adb90..ab94ba5bbd 100644 --- a/tests/unit/Network/CorsTest.php +++ b/tests/unit/Network/CorsTest.php @@ -134,7 +134,7 @@ final class CorsTest extends TestCase public function testLoopbackOriginIsAllowed(): void { $cors = new Cors( - allowedHosts: ['example.com'], + allowedHosts: ['example.com', 'localhost'], allowedMethods: ['GET'], allowedHeaders: ['X-Test'], exposedHeaders: [], @@ -158,7 +158,7 @@ final class CorsTest extends TestCase public function testLoopbackOriginIsEchoedExactly(): void { $cors = new Cors( - allowedHosts: ['example.com'], + allowedHosts: ['example.com', 'localhost'], allowedMethods: ['GET'], allowedHeaders: ['X-Test'], exposedHeaders: [], @@ -180,7 +180,7 @@ final class CorsTest extends TestCase public function testLoopbackLookalikeOriginIsRejected(): void { $cors = new Cors( - allowedHosts: ['example.com'], + allowedHosts: ['example.com', 'localhost'], allowedMethods: ['GET'], allowedHeaders: ['X-Test'], exposedHeaders: [], @@ -198,7 +198,7 @@ final class CorsTest extends TestCase 'http://127.0.0.1x.example.com', 'http://[::1].evil.com', 'http://[::1]evil.com', - /* Only the exact loopback spellings are hardcoded */ + /* Only the exact alias spellings resolve to localhost */ 'http://127.0.0.2', 'http://[0:0:0:0:0:0:0:1]', 'http://localhost.', @@ -208,6 +208,35 @@ final class CorsTest extends TestCase } } + 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 2bdaf6aabe..ececb2294d 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -86,11 +86,11 @@ final class OriginTest extends TestCase public function testLoopback(): void { $validator = new Origin( - allowedHostnames: ['appwrite.io'], + allowedHostnames: ['appwrite.io', 'localhost'], allowedSchemes: ['exp'] ); - /* Loopback is allowed even though it is not in the allow list */ + /* 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')); @@ -120,6 +120,35 @@ final class OriginTest extends TestCase $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( From d1e324044216a0099984abce995e7bdbc4457a53 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 9 Aug 2026 10:01:06 +1200 Subject: [PATCH 04/19] fix(queries): pass Filter arguments in the correct positions Filter's second parameter is $idAttributeType and $maxValuesCount is third, but Base passed APP_DATABASE_QUERY_MAX_VALUES second. Neither file declares strict_types, so the int 500 was silently coerced to the string "500" for $idAttributeType and $maxValuesCount kept its 5000 default, letting every console-collection query carry ten times the intended number of values. Named arguments so a future signature change fails loudly instead of sliding into the wrong slot, matching how the utopia Documents validator and cloud's ActivityEvents already build Filter. $idAttributeType is Database::VAR_INTEGER because the console database is SQL, whose adapters all report that type, and Base already declares its own $sequence attribute as VAR_INTEGER. Co-Authored-By: Claude Opus 5 --- .../Database/Validator/Queries/Base.php | 6 ++- .../Database/Validator/Queries/BaseTest.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Utopia/Database/Validator/Queries/BaseTest.php 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/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' + ); + } +} From 486e90cc3f29bd6e492e4f9863a85cbfb769e8bb Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 9 Aug 2026 03:33:42 +0000 Subject: [PATCH 05/19] fix(events): allow documentsdb and vectorsdb event triggers Event::generateEvents already emits documentsdb.* and vectorsdb.* prefixes for those database types, but the public allowlist only accepted databases/tablesdb. Mirror the tablesdb config so functions and webhooks can subscribe to DocumentsDB and VectorsDB events. Co-authored-by: Chirag Aggarwal --- app/config/events.php | 146 ++++++++++++++++++ tests/unit/Event/EventTest.php | 10 ++ .../Event/Validator/EventValidatorTest.php | 48 ++++++ .../Validator/FunctionEventValidatorTest.php | 12 ++ 4 files changed, 216 insertions(+) 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/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')); From a65fa6896b3e6a6c8d6e055655fde78154ed6707 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 9 Aug 2026 15:55:37 +0300 Subject: [PATCH 06/19] skip sunqueries --- src/Appwrite/Platform/Tasks/ScheduleBase.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index efa9a0f8eb..78c120faf2 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -237,10 +237,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); From 1ff7d2ea67d8f8101444cb1b550aaf6489359f90 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 10 Aug 2026 10:16:54 +0530 Subject: [PATCH 07/19] docs(vcs): describe the installation each provider starts The VCS authorize endpoints name a description file per provider, and only GitHub's was ever written. Hiding them from the SDK kept that from mattering until preview builds began showing the whole surface, where generating the console spec now stops at the first provider whose file is missing. GitHub installs an app; the rest authorize through OAuth, so each says what it actually starts rather than sharing one description. --- docs/references/vcs/create-bitbucket-installation.md | 1 + docs/references/vcs/create-gitea-installation.md | 1 + docs/references/vcs/create-gitlab-installation.md | 1 + 3 files changed, 3 insertions(+) create mode 100644 docs/references/vcs/create-bitbucket-installation.md create mode 100644 docs/references/vcs/create-gitea-installation.md create mode 100644 docs/references/vcs/create-gitlab-installation.md 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 From 477cc61548388a2891135447832116e14a75357a Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 10 Aug 2026 10:25:28 +0530 Subject: [PATCH 08/19] fix(specs): fail the task when a spec cannot be generated The CLI reports a throw through its error hooks and then carries on, so a spec that failed to generate left the task reporting success. The missing file was met further down the pipeline, by the SDK build, which reads a spec that isn't there as specs never having been run -- pointing at the wrong step and hiding the reason in one marked green. A platform that cannot be generated now says so and stops, which is what lets the SDK preview workflow catch it here rather than in cloud. --- src/Appwrite/Platform/Tasks/Specs.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Tasks/Specs.php b/src/Appwrite/Platform/Tasks/Specs.php index 45208d234d..0957537a47 100644 --- a/src/Appwrite/Platform/Tasks/Specs.php +++ b/src/Appwrite/Platform/Tasks/Specs.php @@ -779,7 +779,12 @@ 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); + // The CLI reports a throw and carries on, so a spec that + // failed to generate leaves the task reporting success and + // the missing file is met further down the pipeline, where + // it reads as specs never having been run. + Console::error("Spec generation failed for {$platform} ({$format}): " . $e->getMessage()); + Console::exit(1); } $encodedSpecs = \json_encode($parsedSpecs, JSON_PRETTY_PRINT); From 78f48bb9748202180d39f53b9f0ec7f0c08f5ece Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 10 Aug 2026 10:26:39 +0530 Subject: [PATCH 09/19] style(specs): keep the note to one line --- src/Appwrite/Platform/Tasks/Specs.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/Specs.php b/src/Appwrite/Platform/Tasks/Specs.php index 0957537a47..dbeced4a24 100644 --- a/src/Appwrite/Platform/Tasks/Specs.php +++ b/src/Appwrite/Platform/Tasks/Specs.php @@ -779,10 +779,7 @@ class Specs extends Action $parsedSpecs = $specs->parse(); $this->verifyParsedSpec($parsedSpecs); } catch (\RuntimeException $e) { - // The CLI reports a throw and carries on, so a spec that - // failed to generate leaves the task reporting success and - // the missing file is met further down the pipeline, where - // it reads as specs never having been run. + // A throw is reported and carried on from, so stop here Console::error("Spec generation failed for {$platform} ({$format}): " . $e->getMessage()); Console::exit(1); } From 9103230f5aabac15d04a5de389c33bc5a9d5c67f Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 10 Aug 2026 10:32:14 +0530 Subject: [PATCH 10/19] fix(specs): return after exiting so the analyzer sees the stop --- src/Appwrite/Platform/Tasks/Specs.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Appwrite/Platform/Tasks/Specs.php b/src/Appwrite/Platform/Tasks/Specs.php index dbeced4a24..0bbb9dcd02 100644 --- a/src/Appwrite/Platform/Tasks/Specs.php +++ b/src/Appwrite/Platform/Tasks/Specs.php @@ -782,6 +782,7 @@ class Specs extends Action // 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); From a1c163a443c1d88b2540bdae552fec4f32f48047 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Mon, 10 Aug 2026 11:42:42 +0530 Subject: [PATCH 11/19] feat(embeddings): use embeddings.write scope for text embeddings endpoint --- app/config/roles.php | 3 +++ app/config/scopes/project.php | 4 ++++ .../Modules/Databases/Http/Embeddings/Text/Create.php | 2 +- tests/e2e/Scopes/ProjectCustom.php | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) 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/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/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', From a0a7a2208ebf6b049c22dd323a87aa378f8e6632 Mon Sep 17 00:00:00 2001 From: "cursor[bot]" <206951365+cursor[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 12:07:53 +0530 Subject: [PATCH 12/19] fix(project): recover E.164 mock phone numbers from path encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mock phone get/update/delete take `:number` in the URL path. Routers may leave a leading '+' as `%2B`, and HTTP stacks often turn a bare '+' into a space — so create succeeds while later lookups fail Phone validation. Normalize path numbers before validate and lookup. Co-authored-by: Cursor Agent Co-authored-by: Chirag Aggarwal --- composer.json | 2 +- composer.lock | 248 +++++++++--------- src/Appwrite/Auth/Validator/Phone.php | 47 +--- .../Project/Http/Project/MockPhone/Delete.php | 3 +- .../Project/Http/Project/MockPhone/Get.php | 3 +- .../Project/Http/Project/MockPhone/Update.php | 3 +- tests/unit/Auth/Validator/PhoneTest.php | 25 ++ 7 files changed, 164 insertions(+), 167 deletions(-) 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 662ebc6504..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", @@ -2368,16 +2368,16 @@ }, { "name": "symfony/http-client", - "version": "v7.4.15", + "version": "v7.4.16", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "817bb83ef06717f67ab72a3f03e3b63fbe138de1" + "reference": "c513ed0ba5d1784a6b55fc84190dbe4451b12f41" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/817bb83ef06717f67ab72a3f03e3b63fbe138de1", - "reference": "817bb83ef06717f67ab72a3f03e3b63fbe138de1", + "url": "https://api.github.com/repos/symfony/http-client/zipball/c513ed0ba5d1784a6b55fc84190dbe4451b12f41", + "reference": "c513ed0ba5d1784a6b55fc84190dbe4451b12f41", "shasum": "" }, "require": { @@ -2445,7 +2445,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.4.15" + "source": "https://github.com/symfony/http-client/tree/v7.4.16" }, "funding": [ { @@ -2465,7 +2465,7 @@ "type": "tidelift" } ], - "time": "2026-07-29T07:12:33+00:00" + "time": "2026-07-29T16:20:51+00:00" }, { "name": "symfony/http-client-contracts", @@ -3167,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": { @@ -3184,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": { @@ -3206,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", @@ -3271,33 +3271,32 @@ }, { "name": "utopia-php/cache", - "version": "4.0.0", + "version": "4.0.1", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "2ab50440114a7699b8a017ee2ecd8bcd9d54ac5e" + "reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/2ab50440114a7699b8a017ee2ecd8bcd9d54ac5e", - "reference": "2ab50440114a7699b8a017ee2ecd8bcd9d54ac5e", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/6f8cb03a5cb76e95b95a05103cd14cf782b8060f", + "reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f", "shasum": "" }, "require": { "ext-json": "*", - "ext-memcached": "*", - "ext-redis": "*", - "php": ">=8.3", - "utopia-php/circuit-breaker": "0.3.*", - "utopia-php/pools": "2.*", - "utopia-php/telemetry": "*" + "php": ">=8.4", + "utopia-php/circuit-breaker": "^0.3", + "utopia-php/pools": "^2.0", + "utopia-php/telemetry": "^0.4" }, "require-dev": { - "laravel/pint": "1.2.*", - "phpstan/phpstan": "^1.12", - "phpunit/phpunit": "^9.3", - "swoole/ide-helper": "^6.0", - "vimeo/psalm": "4.13.1" + "swoole/ide-helper": "^6.0" + }, + "suggest": { + "ext-memcached": "Required for the Memcached and Hazelcast adapters.", + "ext-redis": "Required for the Redis, RedisCluster and Sharding adapters.", + "ext-swoole": "Required for the multiplexing Redis adapter (>=6.0)." }, "type": "library", "autoload": { @@ -3309,6 +3308,12 @@ "license": [ "MIT" ], + "authors": [ + { + "name": "Team Appwrite", + "email": "team@appwrite.io" + } + ], "description": "A simple cache library to manage application cache storing, loading and purging", "keywords": [ "cache", @@ -3319,32 +3324,29 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/4.0.0" + "source": "https://github.com/utopia-php/cache/tree/4.0.1" }, - "time": "2026-07-31T13:04:45+00:00" + "time": "2026-08-04T00:06:02+00:00" }, { "name": "utopia-php/circuit-breaker", - "version": "0.3.1", + "version": "0.3.2", "source": { "type": "git", "url": "https://github.com/utopia-php/circuit-breaker.git", - "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828" + "reference": "5fbc3802471b0d1b4260bd9f5544514e6929b481" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", - "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", + "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/5fbc3802471b0d1b4260bd9f5544514e6929b481", + "reference": "5fbc3802471b0d1b4260bd9f5544514e6929b481", "shasum": "" }, "require": { "php": ">=8.2" }, "require-dev": { - "laravel/pint": "^1.29", - "phpstan/phpstan": "^2.1", - "phpunit/phpunit": "^10.0", - "utopia-php/telemetry": "^0.4" + "utopia-php/telemetry": "^0.4.6" }, "suggest": { "ext-opentelemetry": "Required by utopia-php/telemetry when using OpenTelemetry metrics.", @@ -3356,7 +3358,7 @@ "type": "library", "autoload": { "psr-4": { - "Utopia\\CircuitBreaker\\": "src/CircuitBreaker" + "Utopia\\CircuitBreaker\\": "src/CircuitBreaker/" } }, "notification-url": "https://packagist.org/downloads/", @@ -3381,9 +3383,9 @@ ], "support": { "issues": "https://github.com/utopia-php/circuit-breaker/issues", - "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.1" + "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.2" }, - "time": "2026-05-29T12:12:23+00:00" + "time": "2026-08-05T18:07:20+00:00" }, { "name": "utopia-php/cli", @@ -3635,16 +3637,16 @@ }, { "name": "utopia-php/database", - "version": "7.0.0", + "version": "7.1.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "40185b100f92c402a189d6f4f0962c63bbe5726b" + "reference": "9eed1f1ed7da154ca7356c5f051bbc192e7b3c73" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/40185b100f92c402a189d6f4f0962c63bbe5726b", - "reference": "40185b100f92c402a189d6f4f0962c63bbe5726b", + "url": "https://api.github.com/repos/utopia-php/database/zipball/9eed1f1ed7da154ca7356c5f051bbc192e7b3c73", + "reference": "9eed1f1ed7da154ca7356c5f051bbc192e7b3c73", "shasum": "" }, "require": { @@ -3657,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.*", @@ -3689,9 +3691,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/7.0.0" + "source": "https://github.com/utopia-php/database/tree/7.1.2" }, - "time": "2026-07-31T13:33:10+00:00" + "time": "2026-08-10T04:39:43+00:00" }, { "name": "utopia-php/detector", @@ -3788,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": { @@ -3805,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" @@ -3839,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", @@ -4055,16 +4057,16 @@ }, { "name": "utopia-php/http", - "version": "2.0.0-rc19", + "version": "2.0.0-rc21", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "7a9419858c6c612a8d783024ad46740f857e4430" + "reference": "9d8d374926dd2d3c35808deb72199ab943355d64" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/7a9419858c6c612a8d783024ad46740f857e4430", - "reference": "7a9419858c6c612a8d783024ad46740f857e4430", + "url": "https://api.github.com/repos/utopia-php/http/zipball/9d8d374926dd2d3c35808deb72199ab943355d64", + "reference": "9d8d374926dd2d3c35808deb72199ab943355d64", "shasum": "" }, "require": { @@ -4075,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" @@ -4102,9 +4104,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/2.0.0-rc19" + "source": "https://github.com/utopia-php/http/tree/2.0.0-rc21" }, - "time": "2026-07-27T07:23:46+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/image", @@ -4201,16 +4203,16 @@ }, { "name": "utopia-php/lock", - "version": "0.2.3", + "version": "0.2.4", "source": { "type": "git", "url": "https://github.com/utopia-php/lock.git", - "reference": "4eca3784d4c112655faf77adcf0fdbb186650487" + "reference": "49352091b93cc0400eeb2464c2aceb4203da14a3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/lock/zipball/4eca3784d4c112655faf77adcf0fdbb186650487", - "reference": "4eca3784d4c112655faf77adcf0fdbb186650487", + "url": "https://api.github.com/repos/utopia-php/lock/zipball/49352091b93cc0400eeb2464c2aceb4203da14a3", + "reference": "49352091b93cc0400eeb2464c2aceb4203da14a3", "shasum": "" }, "require": { @@ -4243,9 +4245,9 @@ "description": "A simple lock library to coordinate access to shared resources across coroutines, processes and hosts", "support": { "issues": "https://github.com/utopia-php/lock/issues", - "source": "https://github.com/utopia-php/lock/tree/0.2.3" + "source": "https://github.com/utopia-php/lock/tree/0.2.4" }, - "time": "2026-06-26T10:48:18+00:00" + "time": "2026-08-05T09:16:12+00:00" }, { "name": "utopia-php/logger", @@ -4413,16 +4415,16 @@ }, { "name": "utopia-php/mongo", - "version": "1.4.0", + "version": "1.5.1", "source": { "type": "git", "url": "https://github.com/utopia-php/mongo.git", - "reference": "78818fd295f2829aaad5b74c9094e8c6f603550d" + "reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/mongo/zipball/78818fd295f2829aaad5b74c9094e8c6f603550d", - "reference": "78818fd295f2829aaad5b74c9094e8c6f603550d", + "url": "https://api.github.com/repos/utopia-php/mongo/zipball/3ece830d1d72d2c9f68c656738e285629b5c72a9", + "reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9", "shasum": "" }, "require": { @@ -4468,22 +4470,22 @@ ], "support": { "issues": "https://github.com/utopia-php/mongo/issues", - "source": "https://github.com/utopia-php/mongo/tree/1.4.0" + "source": "https://github.com/utopia-php/mongo/tree/1.5.1" }, - "time": "2026-07-30T05:24:58+00:00" + "time": "2026-08-02T00:46:11+00:00" }, { "name": "utopia-php/platform", - "version": "1.0.0-rc16", + "version": "1.0.0-rc17", "source": { "type": "git", "url": "https://github.com/utopia-php/platform.git", - "reference": "0d270c05fdeef59b6404addd403dbe0eb0b1c0e1" + "reference": "d05e530794fdc655ed940fa8ab518a66e5cd0924" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/platform/zipball/0d270c05fdeef59b6404addd403dbe0eb0b1c0e1", - "reference": "0d270c05fdeef59b6404addd403dbe0eb0b1c0e1", + "url": "https://api.github.com/repos/utopia-php/platform/zipball/d05e530794fdc655ed940fa8ab518a66e5cd0924", + "reference": "d05e530794fdc655ed940fa8ab518a66e5cd0924", "shasum": "" }, "require": { @@ -4492,7 +4494,7 @@ "php": ">=8.5", "utopia-php/cli": "^0.24", "utopia-php/http": "^2.0@RC", - "utopia-php/queue": "0.23.* || 0.24.* || 1.0.*", + "utopia-php/queue": "0.23.* || 0.24.* || ^1.0.0", "utopia-php/servers": "^0.4" }, "type": "library", @@ -4515,27 +4517,27 @@ ], "support": { "issues": "https://github.com/utopia-php/platform/issues", - "source": "https://github.com/utopia-php/platform/tree/1.0.0-rc16" + "source": "https://github.com/utopia-php/platform/tree/1.0.0-rc17" }, - "time": "2026-08-04T06:38:05+00:00" + "time": "2026-08-04T06:47:02+00:00" }, { "name": "utopia-php/pools", - "version": "2.0.1", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/utopia-php/pools.git", - "reference": "48905dac1b8f8050c2e07bdda32a018ca33982fc" + "reference": "86d43fcacd125232c0743e8c22695faf97285cf3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/pools/zipball/48905dac1b8f8050c2e07bdda32a018ca33982fc", - "reference": "48905dac1b8f8050c2e07bdda32a018ca33982fc", + "url": "https://api.github.com/repos/utopia-php/pools/zipball/86d43fcacd125232c0743e8c22695faf97285cf3", + "reference": "86d43fcacd125232c0743e8c22695faf97285cf3", "shasum": "" }, "require": { "php": ">=8.4", - "utopia-php/telemetry": "^0.4" + "utopia-php/telemetry": "^0.4.6" }, "require-dev": { "swoole/ide-helper": "6.*" @@ -4571,9 +4573,9 @@ ], "support": { "issues": "https://github.com/utopia-php/pools/issues", - "source": "https://github.com/utopia-php/pools/tree/2.0.1" + "source": "https://github.com/utopia-php/pools/tree/2.0.2" }, - "time": "2026-07-31T12:19:18+00:00" + "time": "2026-08-05T18:07:20+00:00" }, { "name": "utopia-php/preloader", @@ -4722,16 +4724,16 @@ }, { "name": "utopia-php/queue", - "version": "1.0.1", + "version": "1.1.4", "source": { "type": "git", "url": "https://github.com/utopia-php/queue.git", - "reference": "d7bd6d4b3b17d6d0a46aaf7449285705467efd37" + "reference": "ade5660eadcd042a313d1ed4aa25d260a2737b5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/queue/zipball/d7bd6d4b3b17d6d0a46aaf7449285705467efd37", - "reference": "d7bd6d4b3b17d6d0a46aaf7449285705467efd37", + "url": "https://api.github.com/repos/utopia-php/queue/zipball/ade5660eadcd042a313d1ed4aa25d260a2737b5c", + "reference": "ade5660eadcd042a313d1ed4aa25d260a2737b5c", "shasum": "" }, "require": { @@ -4741,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": "*", @@ -4780,9 +4782,9 @@ ], "support": { "issues": "https://github.com/utopia-php/queue/issues", - "source": "https://github.com/utopia-php/queue/tree/1.0.1" + "source": "https://github.com/utopia-php/queue/tree/1.1.4" }, - "time": "2026-08-04T08:52:40+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/registry", @@ -4838,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": { @@ -4881,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", @@ -4927,16 +4929,16 @@ }, { "name": "utopia-php/storage", - "version": "4.0.1", + "version": "4.0.3", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "9684da5ab161ae9375d4f47541ef825451d69f2a" + "reference": "f8b122e753c01f30f774d390d7a36f030dfaef46" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/9684da5ab161ae9375d4f47541ef825451d69f2a", - "reference": "9684da5ab161ae9375d4f47541ef825451d69f2a", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/f8b122e753c01f30f774d390d7a36f030dfaef46", + "reference": "f8b122e753c01f30f774d390d7a36f030dfaef46", "shasum": "" }, "require": { @@ -4948,8 +4950,8 @@ "psr/http-message": "^1.1 || ^2.0", "utopia-php/client": "0.2.* || 0.3.*", "utopia-php/psr7": "0.2.*", - "utopia-php/telemetry": "^0.4", - "utopia-php/validators": "0.3.*" + "utopia-php/telemetry": "^0.4.6", + "utopia-php/validators": "^0.4" }, "type": "library", "autoload": { @@ -4971,9 +4973,9 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/4.0.1" + "source": "https://github.com/utopia-php/storage/tree/4.0.3" }, - "time": "2026-07-31T09:20:57+00:00" + "time": "2026-08-10T04:51:03+00:00" }, { "name": "utopia-php/system", @@ -5028,16 +5030,16 @@ }, { "name": "utopia-php/telemetry", - "version": "0.4.5", + "version": "0.4.6", "source": { "type": "git", "url": "https://github.com/utopia-php/telemetry.git", - "reference": "139943bffcd4f6dd8fb9ed247f946a1d151b006a" + "reference": "f96778a01792c32df0876fe1f38a79b9588445d8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/139943bffcd4f6dd8fb9ed247f946a1d151b006a", - "reference": "139943bffcd4f6dd8fb9ed247f946a1d151b006a", + "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/f96778a01792c32df0876fe1f38a79b9588445d8", + "reference": "f96778a01792c32df0876fe1f38a79b9588445d8", "shasum": "" }, "require": { @@ -5073,9 +5075,9 @@ ], "support": { "issues": "https://github.com/utopia-php/telemetry/issues", - "source": "https://github.com/utopia-php/telemetry/tree/0.4.5" + "source": "https://github.com/utopia-php/telemetry/tree/0.4.6" }, - "time": "2026-07-08T11:07:25+00:00" + "time": "2026-08-05T17:56:48+00:00" }, { "name": "utopia-php/user-agent", @@ -5121,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": { @@ -5155,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", 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/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/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')); + } } From 664c13e9692cfa1f3b0c02a488e551b62626a2ca Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 10 Aug 2026 13:10:58 +0530 Subject: [PATCH 13/19] Remove queue depth health endpoints (#13168) * Remove queue depth health endpoints * Unregister removed queue health actions --- .../Health/Http/Health/Queue/Audits/Get.php | 42 ------------------ .../Health/Http/Health/Queue/Builds/Get.php | 42 ------------------ .../Http/Health/Queue/Certificates/Get.php | 42 ------------------ .../Http/Health/Queue/Databases/Get.php | 44 ------------------- .../Health/Http/Health/Queue/Deletes/Get.php | 42 ------------------ .../Http/Health/Queue/Functions/Get.php | 42 ------------------ .../Health/Http/Health/Queue/Mails/Get.php | 42 ------------------ .../Http/Health/Queue/Messaging/Get.php | 42 ------------------ .../Http/Health/Queue/Migrations/Get.php | 42 ------------------ .../Http/Health/Queue/Notifications/Get.php | 42 ------------------ .../Http/Health/Queue/StatsResources/Get.php | 42 ------------------ .../Http/Health/Queue/StatsUsage/Get.php | 42 ------------------ .../Health/Http/Health/Queue/Webhooks/Get.php | 42 ------------------ .../Platform/Modules/Health/Services/Http.php | 26 ----------- tests/e2e/Services/Health/AuditsQueueTest.php | 19 -------- tests/e2e/Services/Health/BuildsQueueTest.php | 19 -------- .../Services/Health/CertificatesQueueTest.php | 19 -------- .../Services/Health/DatabasesQueueTest.php | 19 -------- .../e2e/Services/Health/DeletesQueueTest.php | 19 -------- .../Services/Health/FunctionsQueueTest.php | 19 -------- tests/e2e/Services/Health/MailsQueueTest.php | 19 -------- .../Services/Health/MessagingQueueTest.php | 19 -------- .../Services/Health/MigrationsQueueTest.php | 19 -------- .../Health/StatsResourcesQueueTest.php | 19 -------- .../Services/Health/StatsUsageQueueTest.php | 19 -------- .../e2e/Services/Health/WebhooksQueueTest.php | 19 -------- .../Notifications/NotificationsBase.php | 30 ++----------- 27 files changed, 3 insertions(+), 829 deletions(-) delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Audits/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Builds/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Certificates/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Databases/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Deletes/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Functions/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Mails/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Messaging/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Migrations/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsResources/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/StatsUsage/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Webhooks/Get.php delete mode 100644 tests/e2e/Services/Health/AuditsQueueTest.php delete mode 100644 tests/e2e/Services/Health/BuildsQueueTest.php delete mode 100644 tests/e2e/Services/Health/CertificatesQueueTest.php delete mode 100644 tests/e2e/Services/Health/DatabasesQueueTest.php delete mode 100644 tests/e2e/Services/Health/DeletesQueueTest.php delete mode 100644 tests/e2e/Services/Health/FunctionsQueueTest.php delete mode 100644 tests/e2e/Services/Health/MailsQueueTest.php delete mode 100644 tests/e2e/Services/Health/MessagingQueueTest.php delete mode 100644 tests/e2e/Services/Health/MigrationsQueueTest.php delete mode 100644 tests/e2e/Services/Health/StatsResourcesQueueTest.php delete mode 100644 tests/e2e/Services/Health/StatsUsageQueueTest.php delete mode 100644 tests/e2e/Services/Health/WebhooksQueueTest.php 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/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([ From d45162c12acbd351f49e8c02f60fb76f0fd90cc4 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 10 Aug 2026 23:09:17 +1200 Subject: [PATCH 14/19] (refactor): share schedule spread offset --- src/Appwrite/Platform/Tasks/ScheduleBase.php | 12 ++++ .../Platform/Tasks/ScheduleFunctions.php | 12 ---- .../unit/Platform/Tasks/ScheduleBaseTest.php | 61 +++++++++++++++++++ .../Platform/Tasks/ScheduleFunctionsTest.php | 51 ---------------- 4 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 tests/unit/Platform/Tasks/ScheduleBaseTest.php diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 78c120faf2..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']); 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/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; - } } From ac04af99d13f4cb35bda7c1dd556e42119f34bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 10 Aug 2026 13:42:09 +0200 Subject: [PATCH 15/19] Add integration test and policy for mfat types; fix expiration --- app/init/models.php | 2 + .../Http/Account/MFA/Challenges/Create.php | 13 ++ .../Http/Account/MFA/Challenges/Update.php | 4 + .../Http/Account/MFA/Factors/XList.php | 14 +- .../Organization/Http/Projects/Create.php | 8 +- .../Project/Http/Project/Policies/Get.php | 12 ++ .../Project/Policies/MFAFactors/Update.php | 106 +++++++++++ .../Project/Http/Project/Policies/XList.php | 8 + .../Modules/Project/Services/Http.php | 2 + .../Modules/Projects/Http/Projects/Create.php | 8 +- .../Users/Http/Users/MFA/Challenges/Get.php | 2 + .../Users/Http/Users/MFA/Factors/XList.php | 12 +- src/Appwrite/Platform/Workers/Deletes.php | 19 ++ src/Appwrite/Utopia/Response.php | 1 + .../Utopia/Response/Model/MFAFactors.php | 6 + .../Utopia/Response/Model/PolicyList.php | 1 + .../Response/Model/PolicyMFAFactors.php | 54 ++++++ .../Account/AccountCustomClientTest.php | 20 ++ tests/e2e/Services/Project/PoliciesBase.php | 1 + .../PoliciesMFAFactorsIntegrationTest.php | 173 ++++++++++++++++++ tests/e2e/Services/Users/UsersBase.php | 10 + 21 files changed, 465 insertions(+), 11 deletions(-) create mode 100644 src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php create mode 100644 src/Appwrite/Utopia/Response/Model/PolicyMFAFactors.php create mode 100644 tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php 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/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..31392d7d19 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,6 +105,10 @@ 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'); $recoveryCodeChallenge = function (Document $challenge, Document $user, string $otp) use ($dbForProject) { 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/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/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..9318d8850f --- /dev/null +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php @@ -0,0 +1,106 @@ +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('authorization') + ->inject('queueForEvents') + ->callback($this->action(...)); + } + + public function action( + ?bool $totp, + ?bool $email, + ?bool $phone, + ?bool $custom, + Response $response, + Database $dbForPlatform, + Document $project, + Authorization $authorization, + 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 = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $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/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/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/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 1dfcd1f965..28da13179b 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -4276,6 +4276,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/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..c107f9fb1a --- /dev/null +++ b/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php @@ -0,0 +1,173 @@ +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: Restore email and disable custom again + $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/mfa-factors', $serverHeaders, [ + 'custom' => false, + 'email' => true, + ]); + $this->assertSame(200, $response['headers']['status-code']); + + $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, From 135b943105f7643c3353fea82c62df16b1e842e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 10 Aug 2026 13:57:00 +0200 Subject: [PATCH 16/19] Fix edge case security --- .../Http/Account/MFA/Challenges/Update.php | 14 ++++++++++++++ .../PoliciesMFAFactorsIntegrationTest.php | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) 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 31392d7d19..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 @@ -111,6 +111,20 @@ class Update extends Action $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/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php b/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php index c107f9fb1a..d0facbf59e 100644 --- a/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesMFAFactorsIntegrationTest.php @@ -116,13 +116,29 @@ final class PoliciesMFAFactorsIntegrationTest extends Scope $this->assertSame(200, $verification['headers']['status-code']); $this->assertContains('custom', $verification['body']['factors']); - // Step 5: Restore email and disable custom again + // 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']); From 90dda4bf6091cc707c047e5c505188d5decaea0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 10 Aug 2026 14:28:10 +0200 Subject: [PATCH 17/19] Add challenge expiry index; fix unnessessary auth skip --- app/config/collections/common.php | 9 ++++++++- src/Appwrite/Migration/Version/V25.php | 9 +++++++++ .../Project/Http/Project/Policies/MFAFactors/Update.php | 7 ++----- 3 files changed, 19 insertions(+), 6 deletions(-) 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/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/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php index 9318d8850f..d84f0c995a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MFAFactors/Update.php @@ -10,7 +10,6 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -56,7 +55,6 @@ class Update extends Action ->inject('response') ->inject('dbForPlatform') ->inject('project') - ->inject('authorization') ->inject('queueForEvents') ->callback($this->action(...)); } @@ -69,7 +67,6 @@ class Update extends Action Response $response, Database $dbForPlatform, Document $project, - Authorization $authorization, Event $queueForEvents, ): void { $auths = $project->getAttribute('auths', []); @@ -94,8 +91,8 @@ class Update extends Action 'auths' => $auths, ]); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); + $project = $dbForPlatform->updateDocument('projects', $project->getId(), $updates); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $queueForEvents ->setParam('projectId', $project->getId()) From 7130cb40ac499c7857bab6e3f3a58685ef32a13d Mon Sep 17 00:00:00 2001 From: Claudear <262350598+claudear@users.noreply.github.com> Date: Mon, 10 Aug 2026 13:06:46 +0000 Subject: [PATCH 18/19] fix(databases): support text, varchar and format column types inline Creating a table/collection with its schema in one call rejected every non-legacy string type. The shared Attributes validator's supported-type list was never updated when the text/varchar family was introduced, so `text`, `varchar`, `mediumtext` and `longtext` all failed with "Invalid type for attribute '': text" even though the dedicated per-column endpoints create those exact types. The metadata row is written before validation runs, so callers got a partially executed request and had to fall back to createTable + N column calls. The size defaults that make those types work only lived in the dedicated endpoints, so widening the type list alone was not enough. Move the type/format to size mapping into Appwrite\Utopia\Database\Attribute and consume it from both the validator and the inline attribute builder, so the two surfaces produce identical attribute documents and cannot drift again. The format shorthands (email, url, ip, enum) are now accepted as a `type` too, mirroring createEmailColumn and friends, and a string with an explicit `format` no longer needs a `size`. Co-Authored-By: Claude Opus 5 --- .../Http/Databases/Collections/Create.php | 8 +- .../Databases/Http/TablesDB/Tables/Create.php | 2 +- src/Appwrite/Utopia/Database/Attribute.php | 100 ++++++++++++ .../Utopia/Database/Validator/Attributes.php | 61 ++++---- .../TablesDB/TablesDBColumnsTest.php | 142 ++++++++++++++++++ .../Database/Validator/AttributesTest.php | 137 +++++++++++++++++ 6 files changed, 414 insertions(+), 36 deletions(-) create mode 100644 src/Appwrite/Utopia/Database/Attribute.php create mode 100644 tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php create mode 100644 tests/unit/Utopia/Database/Validator/AttributesTest.php 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/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/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/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php new file mode 100644 index 0000000000..d25c08de7c --- /dev/null +++ b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php @@ -0,0 +1,142 @@ + '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 + $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(65535, $byKey['modulePath']['size']); + $this->assertEquals('mediumtext', $byKey['summary']['type']); + $this->assertEquals(16777215, $byKey['summary']['size']); + $this->assertEquals('longtext', $byKey['archive']['type']); + $this->assertEquals(2147483647, $byKey['archive']['size']); + + // 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']); + + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $headers, [ + 'rowId' => ID::unique(), + 'data' => [ + 'title' => 'Appwrite', + 'modulePath' => 'src/Appwrite/Platform/Modules/Databases', + 'email' => 'team@appwrite.io', + 'website' => 'https://appwrite.io', + 'address' => '127.0.0.1', + 'status' => 'off', + ], + ]); + + $this->assertEquals(201, $row['headers']['status-code']); + $this->assertEquals('src/Appwrite/Platform/Modules/Databases', $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/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]) + ); + } +} From f4a48d55cf38b9839922cb97345d06d18a02178e Mon Sep 17 00:00:00 2001 From: Claudear <262350598+claudear@users.noreply.github.com> Date: Mon, 10 Aug 2026 13:22:28 +0000 Subject: [PATCH 19/19] test(databases): drop size assertions on fixed width column types ColumnText/ColumnMediumtext/ColumnLongtext deliberately omit `size` from the response, the type implies it. Prove the text column got its real size by round-tripping a value too long for any sized string type instead. Co-Authored-By: Claude Opus 5 --- .../Databases/TablesDB/TablesDBColumnsTest.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php index d25c08de7c..6235a8bfcb 100644 --- a/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php +++ b/tests/e2e/Services/Databases/TablesDB/TablesDBColumnsTest.php @@ -71,17 +71,15 @@ final class TablesDBColumnsTest extends Scope $byKey[$column['key']] = $column; } - // Sizes must match the dedicated per-column endpoints + // 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(65535, $byKey['modulePath']['size']); $this->assertEquals('mediumtext', $byKey['summary']['type']); - $this->assertEquals(16777215, $byKey['summary']['size']); $this->assertEquals('longtext', $byKey['archive']['type']); - $this->assertEquals(2147483647, $byKey['archive']['size']); // Format shorthands become a string of that format $this->assertEquals('string', $byKey['email']['type']); @@ -95,11 +93,15 @@ final class TablesDBColumnsTest extends Scope $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' => 'src/Appwrite/Platform/Modules/Databases', + 'modulePath' => $modulePath, 'email' => 'team@appwrite.io', 'website' => 'https://appwrite.io', 'address' => '127.0.0.1', @@ -108,7 +110,7 @@ final class TablesDBColumnsTest extends Scope ]); $this->assertEquals(201, $row['headers']['status-code']); - $this->assertEquals('src/Appwrite/Platform/Modules/Databases', $row['body']['modulePath']); + $this->assertEquals($modulePath, $row['body']['modulePath']); $this->assertEquals('off', $row['body']['status']); }