From 189062745ed4c637507a2d3701f4063144c7e921 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 28 Jul 2026 19:08:01 +0530 Subject: [PATCH 1/4] fix: restore original session limit and password history caps Session limit and password history policies were switched to APP_LIMIT_COUNT (5000) during the auth-settings refactor, but sessions are backed by an unpaginated subquery, so a limit that high risks degrading the database for the whole project. Restore the previous caps (APP_LIMIT_USER_SESSIONS_MAX = 100, APP_LIMIT_USER_PASSWORD_HISTORY = 20) and drop the unlimited/null option for session limit, which was never supported before. --- app/init/constants.php | 2 ++ .../Policies/PasswordHistory/Update.php | 2 +- .../Project/Policies/SessionLimit/Update.php | 12 +++------- tests/e2e/Services/Project/PoliciesBase.php | 23 +++++-------------- 4 files changed, 12 insertions(+), 27 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index 0a754f1b48..53ae5c9c32 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -31,6 +31,8 @@ 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_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/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/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/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index e92335a61c..e865671cd9 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']); } @@ -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']); } From 5bf39b8fd2bdb2cf1434fedf40f3649f661cf2d8 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 28 Jul 2026 19:48:54 +0530 Subject: [PATCH 2/4] fix: restore remaining auth-policy limits dropped in the refactor Three more accidental regressions from the same auth-settings refactor that swapped policy limits to the generic APP_LIMIT_COUNT: - User limit max was halved: 10,000 (APP_LIMIT_USERS) -> 5,000 (APP_LIMIT_COUNT copy-paste). Restore Range(0, APP_LIMIT_USERS), keeping 0 as the original "unlimited" sentinel. - Default session cap for new projects silently became unlimited (APP_LIMIT_USER_SESSIONS_DEFAULT dropped, default flipped to 0, and the enforcement fallback in shared/api.php followed suit). Restore the constant (10) and use it in both project-create paths and the fallback. - Session duration minimum was loosened from 60 to 5 seconds to speed up an integration test, not as a deliberate product decision. Restore the 60-second floor and adjust the test's polling windows. --- app/controllers/shared/api.php | 2 +- app/init/constants.php | 1 + .../Organization/Http/Projects/Create.php | 2 +- .../Policies/SessionDuration/Update.php | 2 +- .../Project/Policies/UserLimit/Update.php | 2 +- .../Modules/Projects/Http/Projects/Create.php | 2 +- tests/e2e/Services/Project/PoliciesBase.php | 22 +++++++++++++------ ...PoliciesSessionDurationIntegrationTest.php | 12 +++++----- 8 files changed, 27 insertions(+), 18 deletions(-) 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 53ae5c9c32..b12d7997a6 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -33,6 +33,7 @@ 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/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/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/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index e865671cd9..d4b37e8aea 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -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']); } @@ -930,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); @@ -953,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']); From e54192c5f15de4387b06d4e69644e17a8b606e08 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 29 Jul 2026 13:29:52 +0530 Subject: [PATCH 3/4] fix: update remaining tests for restored session-limit/duration bounds PoliciesSessionLimitIntegrationTest relied on disabling the session limit (null -> unlimited), which the policy no longer supports; assert 400 instead. ProjectsConsoleClientTest set the legacy auth/duration alias to 10 seconds, below the restored 60-second floor; bump to 60 and adjust the expiry poll window accordingly. --- .../PoliciesSessionLimitIntegrationTest.php | 20 +++++++++---------- .../Projects/ProjectsConsoleClientTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php index fdce21073b..0fe92c5232 100644 --- a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php @@ -73,6 +73,12 @@ final class PoliciesSessionLimitIntegrationTest extends Scope $this->assertSame(200, $response['headers']['status-code']); }; + $trySetSessionLimit = function (?int $total) use ($serverHeaders): array { + return $this->client->call(Client::METHOD_PATCH, '/project/policies/session-limit', $serverHeaders, [ + 'total' => $total, + ]); + }; + // Step 1: Session limit = 1 $setSessionLimit(1); @@ -109,16 +115,8 @@ 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 = $trySetSessionLimit(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([ From 21047a18e00ef30a6c57249747272634cf7bfe52 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 29 Jul 2026 13:33:44 +0530 Subject: [PATCH 4/4] refactor: inline single-use session-limit test call Drop the trySetSessionLimit closure, only used once, in favor of an inline client call. --- .../Project/PoliciesSessionLimitIntegrationTest.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php index 0fe92c5232..f5f300e4c0 100644 --- a/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php +++ b/tests/e2e/Services/Project/PoliciesSessionLimitIntegrationTest.php @@ -73,12 +73,6 @@ final class PoliciesSessionLimitIntegrationTest extends Scope $this->assertSame(200, $response['headers']['status-code']); }; - $trySetSessionLimit = function (?int $total) use ($serverHeaders): array { - return $this->client->call(Client::METHOD_PATCH, '/project/policies/session-limit', $serverHeaders, [ - 'total' => $total, - ]); - }; - // Step 1: Session limit = 1 $setSessionLimit(1); @@ -116,7 +110,9 @@ final class PoliciesSessionLimitIntegrationTest extends Scope $this->assertSame(401, $getAccount($session2)['headers']['status-code']); // Step 4: Session limit does not support being disabled (unlimited sessions) - $response = $trySetSessionLimit(null); + $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/session-limit', $serverHeaders, [ + 'total' => null, + ]); $this->assertSame(400, $response['headers']['status-code']); } }