diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 211128c499..21c4c7397b 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -774,7 +774,7 @@ Http::shutdown() ->inject('project') ->inject('dbForProject') ->action(function (Request $request, Response $response, Document $project, Database $dbForProject) { - $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? 0; + $sessionLimit = $project->getAttribute('auths', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS_DEFAULT; if ($sessionLimit === 0) { return; diff --git a/app/init/constants.php b/app/init/constants.php index 0a754f1b48..b12d7997a6 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -31,6 +31,9 @@ const APP_MODE_ADMIN = 'admin'; const APP_PAGING_LIMIT = 12; const APP_LIMIT_COUNT = 5000; const APP_LIMIT_USERS = 10_000; +const APP_LIMIT_USER_PASSWORD_HISTORY = 20; +const APP_LIMIT_USER_SESSIONS_MAX = 100; +const APP_LIMIT_USER_SESSIONS_DEFAULT = 10; const APP_LIMIT_ANTIVIRUS = 20_000_000; //20MB const APP_LIMIT_ENCRYPTION = 20_000_000; //20MB const APP_LIMIT_COMPRESSION = 20_000_000; //20MB diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php index 90c3781701..c2eeaa7f30 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php @@ -86,7 +86,7 @@ class Create extends Action $auth = Config::getParam('auth', []); $auths = [ 'limit' => 0, - 'maxSessions' => 0, + 'maxSessions' => APP_LIMIT_USER_SESSIONS_DEFAULT, 'passwordHistory' => 0, 'passwordDictionary' => false, 'duration' => TOKEN_EXPIRATION_LOGIN_LONG, diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index be611e299f..dcfde09aaa 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -53,7 +53,7 @@ class Update extends Action ) ], )) - ->param('total', null, new Nullable(new Range(1, APP_LIMIT_COUNT)), 'Set the password history length per user. Value can be between 1 and ' . APP_LIMIT_COUNT . ', or null to disable the limit.') + ->param('total', null, new Nullable(new Range(1, APP_LIMIT_USER_PASSWORD_HISTORY)), 'Set the password history length per user. Value can be between 1 and ' . APP_LIMIT_USER_PASSWORD_HISTORY . ', or null to disable the limit.') ->inject('response') ->inject('dbForPlatform') ->inject('project') diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index 10eabb57cd..8b34dafe81 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -50,7 +50,7 @@ class Update extends Action ) ], )) - ->param('duration', null, new Range(5, 31536000), 'Maximum session length in seconds. Minium allowed value is 5 second, and maximum is 1 year, which is 31536000 seconds.') + ->param('duration', null, new Range(60, 31536000), 'Maximum session length in seconds. Minium allowed value is 60 seconds, and maximum is 1 year, which is 31536000 seconds.') ->inject('response') ->inject('dbForPlatform') ->inject('project') diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 93ae339b79..92ca2bccb7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -12,7 +12,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; -use Utopia\Validator\Nullable; use Utopia\Validator\Range; class Update extends Action @@ -51,7 +50,7 @@ class Update extends Action ) ], )) - ->param('total', null, new Nullable(new Range(1, APP_LIMIT_COUNT)), 'Set the maximum number of sessions allowed per user. Value can be between 1 and ' . APP_LIMIT_COUNT . ', or null to disable the limit.') + ->param('total', null, new Range(1, APP_LIMIT_USER_SESSIONS_MAX), 'Set the maximum number of sessions allowed per user. Value can be between 1 and ' . APP_LIMIT_USER_SESSIONS_MAX . '.') ->inject('response') ->inject('dbForPlatform') ->inject('project') @@ -61,7 +60,7 @@ class Update extends Action } public function action( - ?int $total, + int $total, Response $response, Database $dbForPlatform, Document $project, @@ -69,12 +68,7 @@ class Update extends Action Event $queueForEvents, ): void { $auths = $project->getAttribute('auths', []); - - if (\is_null($total)) { - $auths['maxSessions'] = 0; - } else { - $auths['maxSessions'] = $total; - } + $auths['maxSessions'] = $total; $updates = new Document([ 'auths' => $auths, diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index b3f430b779..5419c93e3e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -51,7 +51,7 @@ class Update extends Action ) ], )) - ->param('total', null, new Nullable(new Range(1, APP_LIMIT_COUNT)), 'Set the maximum number of users allowed in the project. Value can be between 1 and ' . APP_LIMIT_COUNT . ', or null to disable the limit.') + ->param('total', null, new Nullable(new Range(0, APP_LIMIT_USERS)), 'Set the maximum number of users allowed in the project. Value can be between 0 and ' . APP_LIMIT_USERS . '. Use 0 or null to disable the limit.') ->inject('response') ->inject('dbForPlatform') ->inject('project') diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index 28405fe1da..82279824be 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -82,7 +82,7 @@ class Create extends Action $auth = Config::getParam('auth', []); $auths = [ 'limit' => 0, - 'maxSessions' => 0, + 'maxSessions' => APP_LIMIT_USER_SESSIONS_DEFAULT, 'passwordStrength' => [ 'min' => 8, 'uppercase' => false, diff --git a/src/Appwrite/Platform/Tasks/Doctor.php b/src/Appwrite/Platform/Tasks/Doctor.php index db42c7a4b6..d5e2c29f24 100644 --- a/src/Appwrite/Platform/Tasks/Doctor.php +++ b/src/Appwrite/Platform/Tasks/Doctor.php @@ -14,8 +14,6 @@ use Utopia\Domains\Domain; use Utopia\DSN\DSN; use Utopia\Http\Http; use Utopia\Logger\Logger; -use Utopia\Messaging\Adapter\Email as EmailAdapter; -use Utopia\Messaging\Messages\Email as EmailMessage; use Utopia\Platform\Action; use Utopia\Pools\Group; use Utopia\Queue\Broker\Pool as BrokerPool; @@ -222,20 +220,23 @@ class Doctor extends Action } } + // Probe SMTP reachability only; do not send a live message. try { - /** @var EmailAdapter $smtp */ - $smtp = $register->get('smtp'); + $smtpHost = System::getEnv('_APP_SMTP_HOST', ''); + $smtpPort = (int) System::getEnv('_APP_SMTP_PORT', '25'); - $emailMessage = new EmailMessage( - to: ['demo@example.com'], - subject: 'Test SMTP Connection', - content: 'Hello World', - fromName: \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')), - fromEmail: System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), - ); + if ($smtpHost === '') { + Console::log('⚪ ' . str_pad("SMTP", 50, '.') . 'not configured'); + } else { + $connection = @\fsockopen($smtpHost, $smtpPort > 0 ? $smtpPort : 25, $errno, $errstr, 3); - $smtp->send($emailMessage); - Console::success('🟢 ' . str_pad("SMTP", 50, '.') . 'connected'); + if ($connection !== false) { + \fclose($connection); + Console::success('🟢 ' . str_pad("SMTP", 50, '.') . 'connected'); + } else { + Console::error('🔴 ' . str_pad("SMTP", 47, '.') . 'disconnected'); + } + } } catch (\Throwable) { Console::error('🔴 ' . str_pad("SMTP", 47, '.') . 'disconnected'); } diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index e92335a61c..d4b37e8aea 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -405,10 +405,10 @@ trait PoliciesBase public function testUpdatePasswordHistoryPolicyMax(): void { - $response = $this->updatePasswordHistoryPolicy(5000); + $response = $this->updatePasswordHistoryPolicy(20); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(5000, $response['body']['authPasswordHistory']); + $this->assertSame(20, $response['body']['authPasswordHistory']); // Cleanup $this->updatePasswordHistoryPolicy(null); @@ -437,7 +437,7 @@ trait PoliciesBase public function testUpdatePasswordHistoryPolicyAboveMax(): void { - $response = $this->updatePasswordHistoryPolicy(5001); + $response = $this->updatePasswordHistoryPolicy(21); $this->assertSame(400, $response['headers']['status-code']); } @@ -707,10 +707,10 @@ trait PoliciesBase public function testUpdateSessionDurationPolicyMin(): void { - $response = $this->updateSessionDurationPolicy(5); + $response = $this->updateSessionDurationPolicy(60); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(5, $response['body']['authDuration']); + $this->assertSame(60, $response['body']['authDuration']); // Cleanup $this->updateSessionDurationPolicy(31536000); @@ -726,7 +726,7 @@ trait PoliciesBase public function testUpdateSessionDurationPolicyBelowMin(): void { - $response = $this->updateSessionDurationPolicy(4); + $response = $this->updateSessionDurationPolicy(59); $this->assertSame(400, $response['headers']['status-code']); } @@ -851,21 +851,10 @@ trait PoliciesBase public function testUpdateSessionLimitPolicyMax(): void { - $response = $this->updateSessionLimitPolicy(5000); + $response = $this->updateSessionLimitPolicy(100); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(5000, $response['body']['authSessionsLimit']); - - // Cleanup - $this->updateSessionLimitPolicy(10); - } - - public function testUpdateSessionLimitPolicyDisable(): void - { - $response = $this->updateSessionLimitPolicy(null); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(0, $response['body']['authSessionsLimit']); + $this->assertSame(100, $response['body']['authSessionsLimit']); // Cleanup $this->updateSessionLimitPolicy(10); @@ -880,7 +869,7 @@ trait PoliciesBase public function testUpdateSessionLimitPolicyAboveMax(): void { - $response = $this->updateSessionLimitPolicy(5001); + $response = $this->updateSessionLimitPolicy(101); $this->assertSame(400, $response['headers']['status-code']); } @@ -941,10 +930,10 @@ trait PoliciesBase public function testUpdateUserLimitPolicyMax(): void { - $response = $this->updateUserLimitPolicy(5000); + $response = $this->updateUserLimitPolicy(10000); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(5000, $response['body']['authLimit']); + $this->assertSame(10000, $response['body']['authLimit']); // Cleanup $this->updateUserLimitPolicy(null); @@ -964,16 +953,24 @@ trait PoliciesBase $this->assertSame(0, $project['body']['authLimit']); } - public function testUpdateUserLimitPolicyBelowMin(): void + public function testUpdateUserLimitPolicyZeroMeansUnlimited(): void { $response = $this->updateUserLimitPolicy(0); + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame(0, $response['body']['authLimit']); + } + + public function testUpdateUserLimitPolicyBelowMin(): void + { + $response = $this->updateUserLimitPolicy(-1); + $this->assertSame(400, $response['headers']['status-code']); } public function testUpdateUserLimitPolicyAboveMax(): void { - $response = $this->updateUserLimitPolicy(5001); + $response = $this->updateUserLimitPolicy(10001); $this->assertSame(400, $response['headers']['status-code']); } diff --git a/tests/e2e/Services/Project/PoliciesSessionDurationIntegrationTest.php b/tests/e2e/Services/Project/PoliciesSessionDurationIntegrationTest.php index 84ffad1850..69356d221a 100644 --- a/tests/e2e/Services/Project/PoliciesSessionDurationIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesSessionDurationIntegrationTest.php @@ -41,8 +41,8 @@ final class PoliciesSessionDurationIntegrationTest extends Scope $this->assertSame($seconds, $response['body']['authDuration']); }; - // Step 1: Set session duration to 5 seconds - $setDuration(5); + // Step 1: Set session duration to 60 seconds (the policy minimum) + $setDuration(60); // Step 2: Create user and a session $email = 'duration_' . uniqid() . '@localhost.test'; @@ -73,14 +73,14 @@ final class PoliciesSessionDurationIntegrationTest extends Scope $response = $this->client->call(Client::METHOD_GET, '/account', $accountHeaders); $this->assertSame(200, $response['headers']['status-code']); - // Step 3: Poll until the 5s TTL elapses - session should expire + // Step 3: Poll until the 60s TTL elapses - session should expire $this->assertEventually(function () use ($accountHeaders) { $response = $this->client->call(Client::METHOD_GET, '/account', $accountHeaders); $this->assertSame(401, $response['headers']['status-code']); - }, 15_000, 500); + }, 75_000, 500); - // Step 4: Raise duration to 10s - same session should still not be usable - $setDuration(10); + // Step 4: Raise duration to 120s - same session should still not be usable + $setDuration(120); $response = $this->client->call(Client::METHOD_GET, '/account', $accountHeaders); $this->assertSame(401, $response['headers']['status-code']); diff --git a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php index fdce21073b..f5f300e4c0 100644 --- a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php @@ -109,16 +109,10 @@ final class PoliciesSessionLimitIntegrationTest extends Scope $this->assertSame(200, $getAccount($session3)['headers']['status-code']); $this->assertSame(401, $getAccount($session2)['headers']['status-code']); - // Step 4: Disable session limit, create 5 new sessions, all should remain usable - $setSessionLimit(null); - - $newSessions = []; - for ($i = 0; $i < 5; $i++) { - $newSessions[] = $login(); - } - - foreach ($newSessions as $index => $sessionCookie) { - $this->assertSame(200, $getAccount($sessionCookie)['headers']['status-code'], 'Session #' . ($index + 1) . ' should remain valid when limit is disabled'); - } + // Step 4: Session limit does not support being disabled (unlimited sessions) + $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/session-limit', $serverHeaders, [ + 'total' => null, + ]); + $this->assertSame(400, $response['headers']['status-code']); } } diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 82eb9c5bba..22d04d45d7 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -2092,7 +2092,7 @@ final class ProjectsConsoleClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-response-format' => '1.9.1', ], $this->getHeaders()), [ - 'duration' => 10, // Set session duration to 10 seconds + 'duration' => 60, // Set session duration to 60 seconds (the policy minimum) ]); $this->assertEquals(200, $response['headers']['status-code']); @@ -2101,7 +2101,7 @@ final class ProjectsConsoleClientTest extends Scope $this->assertArrayHasKey('platforms', $response['body']); $this->assertArrayHasKey('webhooks', $response['body']); $this->assertArrayHasKey('keys', $response['body']); - $this->assertEquals(10, $response['body']['authDuration']); + $this->assertEquals(60, $response['body']['authDuration']); $projectId = $response['body']['$id']; @@ -2142,7 +2142,7 @@ final class ProjectsConsoleClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); - // Eventually session expires, within 15 seconds (10+variance) + // Eventually session expires, within 75 seconds (60+variance) $this->assertEventually(function () use ($projectId, $sessionCookie) { // Get User $response = $this->client->call(Client::METHOD_GET, '/account', array_merge([ @@ -2152,7 +2152,7 @@ final class ProjectsConsoleClientTest extends Scope ])); $this->assertEquals(401, $response['headers']['status-code']); - }, timeoutMs: 15 * 1000); + }, timeoutMs: 75 * 1000); // Set session duration to 10min $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/duration', array_merge([