From 9264fcec13d95cdabb846988f887d661ded59ccd Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 1 Jun 2026 15:01:39 +0400 Subject: [PATCH] Keep password policy minimum length at 8 --- app/controllers/api/account.php | 6 +++--- app/controllers/api/users.php | 4 ++-- src/Appwrite/Auth/Validator/Password.php | 8 +++----- src/Appwrite/Auth/Validator/PasswordStrength.php | 3 ++- .../Http/Project/Policies/PasswordStrength/Update.php | 2 +- tests/e2e/Services/Project/PoliciesBase.php | 6 +++--- tests/e2e/Services/Projects/ProjectsConsoleClientTest.php | 2 +- tests/unit/Auth/Validator/PasswordStrengthTest.php | 8 ++++---- 8 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 69ad1fc2f2..114d9f4a17 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -257,7 +257,7 @@ Http::post('/v1/account') ->label('abuse-limit', 10) ->param('userId', '', fn (Database $dbForProject) => new CustomId(false, $dbForProject->getAdapter()->getMaxUIDLength()), 'User ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', false, ['dbForProject']) ->param('email', '', new EmailValidator(), 'User email.') - ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be between 6 and 256 chars.', false, ['project', 'passwordsDictionary']) + ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be between 8 and 256 chars.', false, ['project', 'passwordsDictionary']) ->param('name', '', new Text(128), 'User name. Max length: 128 chars.', true) ->inject('request') ->inject('response') @@ -3273,7 +3273,7 @@ Http::patch('/v1/account/password') contentType: ContentType::JSON )) ->label('abuse-limit', 10) - ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be at least 6 chars.', false, ['project', 'passwordsDictionary']) + ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be at least 8 chars.', false, ['project', 'passwordsDictionary']) ->param('oldPassword', '', new Password(), 'Current user password. Must be at least 8 chars.', true) ->inject('response') ->inject('user') @@ -3890,7 +3890,7 @@ Http::put('/v1/account/recovery') ->label('abuse-key', 'url:{url},userId:{param-userId}') ->param('userId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'User ID.', false, ['dbForProject']) ->param('secret', '', new Text(256), 'Valid reset token.') - ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be between 6 and 256 chars.', false, ['project', 'passwordsDictionary']) + ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be between 8 and 256 chars.', false, ['project', 'passwordsDictionary']) ->inject('response') ->inject('user') ->inject('dbForProject') diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index baed5fa63a..fb5514a6fe 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -283,7 +283,7 @@ Http::post('/v1/users') ->param('userId', '', fn (Database $dbForProject) => new CustomId(false, $dbForProject->getAdapter()->getMaxUIDLength()), 'User ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', false, ['dbForProject']) ->param('email', null, new Nullable(new EmailValidator()), 'User email.', true) ->param('phone', null, new Nullable(new Phone()), 'Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.', true) - ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'Plain text user password. Must be at least 6 chars.', true, ['project', 'passwordsDictionary']) + ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? []), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'Plain text user password. Must be at least 8 chars.', true, ['project', 'passwordsDictionary']) ->param('name', '', new Text(128), 'User name. Max length: 128 chars.', true) ->inject('response') ->inject('project') @@ -1434,7 +1434,7 @@ Http::patch('/v1/users/:userId/password') ] )) ->param('userId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'User ID.', false, ['dbForProject']) - ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? [], allowEmpty: true), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be at least 6 chars.', false, ['project', 'passwordsDictionary']) + ->param('password', '', fn ($project, $passwordsDictionary) => new AllOf([new PasswordStrength($project->getAttribute('auths', [])['passwordStrength'] ?? [], allowEmpty: true), new PasswordDictionary($passwordsDictionary, enabled: $project->getAttribute('auths', [])['passwordDictionary'] ?? false)], Validator::TYPE_STRING), 'New user password. Must be at least 8 chars.', false, ['project', 'passwordsDictionary']) ->inject('response') ->inject('project') ->inject('dbForProject') diff --git a/src/Appwrite/Auth/Validator/Password.php b/src/Appwrite/Auth/Validator/Password.php index d1c72d686e..bfe5577889 100644 --- a/src/Appwrite/Auth/Validator/Password.php +++ b/src/Appwrite/Auth/Validator/Password.php @@ -12,12 +12,10 @@ use Utopia\Validator; class Password extends Validator { protected bool $allowEmpty; - protected int $minLength; - public function __construct(bool $allowEmpty = false, int $minLength = 8) + public function __construct(bool $allowEmpty = false) { $this->allowEmpty = $allowEmpty; - $this->minLength = $minLength; } /** @@ -29,7 +27,7 @@ class Password extends Validator */ public function getDescription(): string { - return 'Password must be between ' . $this->minLength . ' and 256 characters long.'; + return 'Password must be between 8 and 256 characters long.'; } /** @@ -49,7 +47,7 @@ class Password extends Validator return true; } - if (\strlen($value) < $this->minLength) { + if (\strlen($value) < 8) { return false; } diff --git a/src/Appwrite/Auth/Validator/PasswordStrength.php b/src/Appwrite/Auth/Validator/PasswordStrength.php index 73ad4b8d58..fc4d3cfd82 100644 --- a/src/Appwrite/Auth/Validator/PasswordStrength.php +++ b/src/Appwrite/Auth/Validator/PasswordStrength.php @@ -17,8 +17,9 @@ class PasswordStrength extends Password public function __construct(array $policy = [], bool $allowEmpty = false) { - parent::__construct($allowEmpty, $policy['minLength'] ?? 8); + parent::__construct($allowEmpty); + $this->minLength = $policy['minLength'] ?? 8; $this->requireUppercase = $policy['requireUppercase'] ?? false; $this->requireLowercase = $policy['requireLowercase'] ?? false; $this->requireNumber = $policy['requireNumber'] ?? false; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php index 453cd9ca78..a131cacbfd 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php @@ -50,7 +50,7 @@ class Update extends Action ), ], )) - ->param('minLength', null, new Range(6, 256), 'Minimum password length. Value must be between 6 and 256. Default is 8.', optional: true) + ->param('minLength', null, new Range(8, 256), 'Minimum password length. Value must be between 8 and 256. Default is 8.', optional: true) ->param('requireUppercase', null, new Boolean(), 'Whether passwords must include at least one uppercase letter.', optional: true) ->param('requireLowercase', null, new Boolean(), 'Whether passwords must include at least one lowercase letter.', optional: true) ->param('requireNumber', null, new Boolean(), 'Whether passwords must include at least one number.', optional: true) diff --git a/tests/e2e/Services/Project/PoliciesBase.php b/tests/e2e/Services/Project/PoliciesBase.php index 66517fb5af..217ba7eba5 100644 --- a/tests/e2e/Services/Project/PoliciesBase.php +++ b/tests/e2e/Services/Project/PoliciesBase.php @@ -529,11 +529,11 @@ trait PoliciesBase public function testUpdatePasswordStrengthPolicyMinLength(): void { $response = $this->updatePasswordStrengthPolicy([ - 'minLength' => 6, + 'minLength' => 8, ]); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame(6, $response['body']['minLength']); + $this->assertSame(8, $response['body']['minLength']); // Cleanup $this->resetPasswordStrengthPolicy(); @@ -542,7 +542,7 @@ trait PoliciesBase public function testUpdatePasswordStrengthPolicyBelowMinLength(): void { $response = $this->client->call(Client::METHOD_PATCH, '/project/policies/password-strength', $this->buildHeaders(), [ - 'minLength' => 5, + 'minLength' => 7, ]); $this->assertSame(400, $response['headers']['status-code']); diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index ca7ccb18e3..490f25153f 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -2916,7 +2916,7 @@ class ProjectsConsoleClientTest extends Scope 'x-appwrite-project' => $id, 'x-appwrite-mode' => 'admin', ], $this->getHeaders()), [ - 'minLength' => 5, + 'minLength' => 7, ]); $this->assertEquals(400, $response['headers']['status-code']); diff --git a/tests/unit/Auth/Validator/PasswordStrengthTest.php b/tests/unit/Auth/Validator/PasswordStrengthTest.php index 49896f4e1f..44062bbf91 100644 --- a/tests/unit/Auth/Validator/PasswordStrengthTest.php +++ b/tests/unit/Auth/Validator/PasswordStrengthTest.php @@ -34,14 +34,14 @@ class PasswordStrengthTest extends TestCase $this->assertTrue($validator->isValid('Password123€')); } - public function testMinLengthCanBeSix(): void + public function testMinLengthCanBeEight(): void { $validator = new PasswordStrength([ - 'minLength' => 6, + 'minLength' => 8, ]); - $this->assertFalse($validator->isValid('12345')); - $this->assertTrue($validator->isValid('123456')); + $this->assertFalse($validator->isValid('1234567')); + $this->assertTrue($validator->isValid('12345678')); } public function testAllowEmpty(): void