Keep password policy minimum length at 8

This commit is contained in:
Torsten Dittmann committed 2026-06-01 15:01:39 +04:00
1 parent 4015eccc3f
commit 9264fcec13
8 files changed
+19 -20

No files matched your search

+3 -3
View File
@@ -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')
+2 -2
View File
@@ -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')
+3 -5
View File
@@ -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;
}
@@ -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;
@@ -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)
+3 -3
View File
@@ -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']);
@@ -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']);
@@ -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