Address password policy review feedback

This commit is contained in:
Torsten Dittmann committed 2026-06-01 18:45:34 +04:00
1 parent 9264fcec13
commit 7cd0230987
14 files changed
+213 -134

No files matched your search

+1 -1
View File
@@ -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 8 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, allowEmpty: true)], Validator::TYPE_STRING), 'New user password. Must be at least 8 chars.', false, ['project', 'passwordsDictionary'])
->inject('response')
->inject('project')
->inject('dbForProject')
@@ -1 +0,0 @@
Update the authentication password strength requirements.
@@ -2,20 +2,19 @@
namespace Appwrite\Auth\Validator;
use Utopia\Validator;
/**
* Password.
*
* Validates user password string
*/
class PasswordDictionary extends Validator
class PasswordDictionary extends Password
{
protected array $dictionary;
protected bool $enabled;
public function __construct(array $dictionary, bool $enabled = false)
public function __construct(array $dictionary, bool $enabled = false, bool $allowEmpty = false)
{
parent::__construct($allowEmpty);
$this->dictionary = $dictionary;
$this->enabled = $enabled;
}
@@ -29,7 +28,7 @@ class PasswordDictionary extends Validator
*/
public function getDescription(): string
{
return 'Password should not be one of the commonly used passwords.';
return 'Password must be between 8 and 265 characters long, and should not be one of the commonly used password.';
}
/**
@@ -41,7 +40,11 @@ class PasswordDictionary extends Validator
*/
public function isValid($value): bool
{
if ($this->enabled && \is_string($value) && array_key_exists($value, $this->dictionary)) {
if (!parent::isValid($value)) {
return false;
}
if ($this->enabled && array_key_exists($value, $this->dictionary)) {
return false;
}
return true;
@@ -10,20 +10,20 @@ namespace Appwrite\Auth\Validator;
class PasswordStrength extends Password
{
protected int $minLength;
protected bool $requireUppercase;
protected bool $requireLowercase;
protected bool $requireNumber;
protected bool $requireSpecialChar;
protected bool $uppercase;
protected bool $lowercase;
protected bool $number;
protected bool $symbols;
public function __construct(array $policy = [], bool $allowEmpty = false)
{
parent::__construct($allowEmpty);
$this->minLength = $policy['minLength'] ?? 8;
$this->requireUppercase = $policy['requireUppercase'] ?? false;
$this->requireLowercase = $policy['requireLowercase'] ?? false;
$this->requireNumber = $policy['requireNumber'] ?? false;
$this->requireSpecialChar = $policy['requireSpecialChar'] ?? false;
$this->uppercase = $policy['uppercase'] ?? false;
$this->lowercase = $policy['lowercase'] ?? false;
$this->number = $policy['number'] ?? false;
$this->symbols = $policy['symbols'] ?? false;
}
public function getDescription(): string
@@ -32,20 +32,20 @@ class PasswordStrength extends Password
'between ' . $this->minLength . ' and 256 characters long',
];
if ($this->requireUppercase) {
if ($this->uppercase) {
$requirements[] = 'include an uppercase letter';
}
if ($this->requireLowercase) {
if ($this->lowercase) {
$requirements[] = 'include a lowercase letter';
}
if ($this->requireNumber) {
if ($this->number) {
$requirements[] = 'include a number';
}
if ($this->requireSpecialChar) {
$requirements[] = 'include a special character';
if ($this->symbols) {
$requirements[] = 'include a symbol';
}
return 'Password must be ' . \implode(', ', $requirements) . '.';
@@ -68,19 +68,19 @@ class PasswordStrength extends Password
return false;
}
if ($this->requireUppercase && !\preg_match('/[A-Z]/', $value)) {
if ($this->uppercase && !\preg_match('/[A-Z]/', $value)) {
return false;
}
if ($this->requireLowercase && !\preg_match('/[a-z]/', $value)) {
if ($this->lowercase && !\preg_match('/[a-z]/', $value)) {
return false;
}
if ($this->requireNumber && !\preg_match('/\d/', $value)) {
if ($this->number && !\preg_match('/\d/', $value)) {
return false;
}
if ($this->requireSpecialChar && !\preg_match('/[^\p{L}\p{N}\s]/u', $value)) {
if ($this->symbols && !\preg_match('/[^\p{L}\p{N}\s]/u', $value)) {
return false;
}
@@ -113,10 +113,10 @@ class Get extends Action
'password-strength' => [
new Document(\array_merge([
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
], $auths['passwordStrength'] ?? [], [
'$id' => 'password-strength',
])),
@@ -51,10 +51,10 @@ class Update extends Action
],
))
->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)
->param('requireSpecialChar', null, new Boolean(), 'Whether passwords must include at least one special character.', optional: true)
->param('uppercase', null, new Boolean(), 'Whether passwords must include at least one uppercase letter.', optional: true)
->param('lowercase', null, new Boolean(), 'Whether passwords must include at least one lowercase letter.', optional: true)
->param('number', null, new Boolean(), 'Whether passwords must include at least one number.', optional: true)
->param('symbols', null, new Boolean(), 'Whether passwords must include at least one symbol.', optional: true)
->inject('response')
->inject('dbForPlatform')
->inject('project')
@@ -65,10 +65,10 @@ class Update extends Action
public function action(
?int $minLength,
?bool $requireUppercase,
?bool $requireLowercase,
?bool $requireNumber,
?bool $requireSpecialChar,
?bool $uppercase,
?bool $lowercase,
?bool $number,
?bool $symbols,
Response $response,
Database $dbForPlatform,
Document $project,
@@ -78,26 +78,26 @@ class Update extends Action
$auths = $project->getAttribute('auths', []);
$auths['passwordStrength'] = \array_merge([
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
], $auths['passwordStrength'] ?? []);
if ($minLength !== null) {
$auths['passwordStrength']['minLength'] = $minLength;
}
if ($requireUppercase !== null) {
$auths['passwordStrength']['requireUppercase'] = $requireUppercase;
if ($uppercase !== null) {
$auths['passwordStrength']['uppercase'] = $uppercase;
}
if ($requireLowercase !== null) {
$auths['passwordStrength']['requireLowercase'] = $requireLowercase;
if ($lowercase !== null) {
$auths['passwordStrength']['lowercase'] = $lowercase;
}
if ($requireNumber !== null) {
$auths['passwordStrength']['requireNumber'] = $requireNumber;
if ($number !== null) {
$auths['passwordStrength']['number'] = $number;
}
if ($requireSpecialChar !== null) {
$auths['passwordStrength']['requireSpecialChar'] = $requireSpecialChar;
if ($symbols !== null) {
$auths['passwordStrength']['symbols'] = $symbols;
}
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
@@ -105,10 +105,10 @@ class XList extends Action
]),
new Document(\array_merge([
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
], $auths['passwordStrength'] ?? [], [
'$id' => 'password-strength',
])),
@@ -85,10 +85,10 @@ class Create extends Action
'maxSessions' => 0,
'passwordStrength' => [
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
],
'passwordHistory' => 0,
'passwordDictionary' => false,
@@ -21,27 +21,27 @@ class PolicyPasswordStrength extends PolicyBase
'default' => 8,
'example' => 12,
])
->addRule('requireUppercase', [
->addRule('uppercase', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether passwords must include at least one uppercase letter.',
'default' => false,
'example' => true,
])
->addRule('requireLowercase', [
->addRule('lowercase', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether passwords must include at least one lowercase letter.',
'default' => false,
'example' => true,
])
->addRule('requireNumber', [
->addRule('number', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether passwords must include at least one number.',
'default' => false,
'example' => true,
])
->addRule('requireSpecialChar', [
->addRule('symbols', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Whether passwords must include at least one special character.',
'description' => 'Whether passwords must include at least one symbol.',
'default' => false,
'example' => true,
]);
+46 -46
View File
@@ -15,7 +15,7 @@ trait PoliciesBase
return [
'password-dictionary' => ['enabled'],
'password-history' => ['total'],
'password-strength' => ['minLength', 'requireUppercase', 'requireLowercase', 'requireNumber', 'requireSpecialChar'],
'password-strength' => ['minLength', 'uppercase', 'lowercase', 'number', 'symbols'],
'password-personal-data' => ['enabled'],
'session-alert' => ['enabled'],
'session-duration' => ['duration'],
@@ -69,10 +69,10 @@ trait PoliciesBase
$this->updatePasswordHistoryPolicy(5);
$this->updatePasswordStrengthPolicy([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->updateSessionDurationPolicy(3600);
$this->updateMembershipPrivacyPolicy([
@@ -97,10 +97,10 @@ trait PoliciesBase
$this->assertSame(200, $passwordStrength['headers']['status-code']);
$this->assertSame(12, $passwordStrength['body']['minLength']);
$this->assertSame(true, $passwordStrength['body']['requireUppercase']);
$this->assertSame(true, $passwordStrength['body']['requireLowercase']);
$this->assertSame(true, $passwordStrength['body']['requireNumber']);
$this->assertSame(true, $passwordStrength['body']['requireSpecialChar']);
$this->assertSame(true, $passwordStrength['body']['uppercase']);
$this->assertSame(true, $passwordStrength['body']['lowercase']);
$this->assertSame(true, $passwordStrength['body']['number']);
$this->assertSame(true, $passwordStrength['body']['symbols']);
$this->assertSame(200, $sessionDuration['headers']['status-code']);
$this->assertSame(3600, $sessionDuration['body']['duration']);
@@ -199,10 +199,10 @@ trait PoliciesBase
$this->updatePasswordHistoryPolicy(5);
$this->updatePasswordStrengthPolicy([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->updateSessionDurationPolicy(3600);
$this->updateMembershipPrivacyPolicy([
@@ -225,10 +225,10 @@ trait PoliciesBase
$this->assertSame(true, $byId['password-dictionary']['enabled']);
$this->assertSame(5, $byId['password-history']['total']);
$this->assertSame(12, $byId['password-strength']['minLength']);
$this->assertSame(true, $byId['password-strength']['requireUppercase']);
$this->assertSame(true, $byId['password-strength']['requireLowercase']);
$this->assertSame(true, $byId['password-strength']['requireNumber']);
$this->assertSame(true, $byId['password-strength']['requireSpecialChar']);
$this->assertSame(true, $byId['password-strength']['uppercase']);
$this->assertSame(true, $byId['password-strength']['lowercase']);
$this->assertSame(true, $byId['password-strength']['number']);
$this->assertSame(true, $byId['password-strength']['symbols']);
$this->assertSame(3600, $byId['session-duration']['duration']);
$this->assertSame(true, $byId['membership-privacy']['userId']);
$this->assertSame(true, $byId['membership-privacy']['userEmail']);
@@ -473,27 +473,27 @@ trait PoliciesBase
{
$response = $this->updatePasswordStrengthPolicy([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->assertSame(200, $response['headers']['status-code']);
$this->assertSame('password-strength', $response['body']['$id']);
$this->assertSame(12, $response['body']['minLength']);
$this->assertSame(true, $response['body']['requireUppercase']);
$this->assertSame(true, $response['body']['requireLowercase']);
$this->assertSame(true, $response['body']['requireNumber']);
$this->assertSame(true, $response['body']['requireSpecialChar']);
$this->assertSame(true, $response['body']['uppercase']);
$this->assertSame(true, $response['body']['lowercase']);
$this->assertSame(true, $response['body']['number']);
$this->assertSame(true, $response['body']['symbols']);
$policy = $this->getPolicy('password-strength');
$this->assertSame(200, $policy['headers']['status-code']);
$this->assertSame(12, $policy['body']['minLength']);
$this->assertSame(true, $policy['body']['requireUppercase']);
$this->assertSame(true, $policy['body']['requireLowercase']);
$this->assertSame(true, $policy['body']['requireNumber']);
$this->assertSame(true, $policy['body']['requireSpecialChar']);
$this->assertSame(true, $policy['body']['uppercase']);
$this->assertSame(true, $policy['body']['lowercase']);
$this->assertSame(true, $policy['body']['number']);
$this->assertSame(true, $policy['body']['symbols']);
// Cleanup
$this->resetPasswordStrengthPolicy();
@@ -503,24 +503,24 @@ trait PoliciesBase
{
$response = $this->updatePasswordStrengthPolicy([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->assertSame(200, $response['headers']['status-code']);
$response = $this->updatePasswordStrengthPolicy([
'requireLowercase' => false,
'lowercase' => false,
]);
$this->assertSame(200, $response['headers']['status-code']);
$this->assertSame(12, $response['body']['minLength']);
$this->assertSame(true, $response['body']['requireUppercase']);
$this->assertSame(false, $response['body']['requireLowercase']);
$this->assertSame(true, $response['body']['requireNumber']);
$this->assertSame(true, $response['body']['requireSpecialChar']);
$this->assertSame(true, $response['body']['uppercase']);
$this->assertSame(false, $response['body']['lowercase']);
$this->assertSame(true, $response['body']['number']);
$this->assertSame(true, $response['body']['symbols']);
// Cleanup
$this->resetPasswordStrengthPolicy();
@@ -556,10 +556,10 @@ trait PoliciesBase
$this->assertSame(200, $response['headers']['status-code']);
$this->assertSame(8, $response['body']['minLength']);
$this->assertSame(false, $response['body']['requireUppercase']);
$this->assertSame(false, $response['body']['requireLowercase']);
$this->assertSame(false, $response['body']['requireNumber']);
$this->assertSame(false, $response['body']['requireSpecialChar']);
$this->assertSame(false, $response['body']['uppercase']);
$this->assertSame(false, $response['body']['lowercase']);
$this->assertSame(false, $response['body']['number']);
$this->assertSame(false, $response['body']['symbols']);
}
public function testUpdatePasswordStrengthPolicyWithoutAuth(): void
@@ -1292,10 +1292,10 @@ trait PoliciesBase
{
$this->updatePasswordStrengthPolicy([
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
]);
}
@@ -0,0 +1,77 @@
<?php
namespace Tests\E2E\Services\Project;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
use Utopia\Database\Helpers\ID;
class PoliciesPasswordStrengthIntegrationTest extends Scope
{
use ProjectCustom;
use SideServer;
public function testPasswordStrengthIntegration(): void
{
$projectId = $this->getProject()['$id'];
$apiKey = $this->getProject()['apiKey'];
$serverHeaders = [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apiKey,
'x-appwrite-response-format' => '1.9.4',
];
$signupHeaders = [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
];
$response = $this->client->call(Client::METHOD_PATCH, '/project/policies/password-strength', $serverHeaders, [
'minLength' => 12,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->assertSame(200, $response['headers']['status-code']);
$this->assertSame(12, $response['body']['minLength']);
$this->assertTrue($response['body']['uppercase']);
$this->assertTrue($response['body']['lowercase']);
$this->assertTrue($response['body']['number']);
$this->assertTrue($response['body']['symbols']);
$weak = $this->client->call(Client::METHOD_POST, '/account', $signupHeaders, [
'userId' => ID::unique(),
'email' => 'strength_weak_' . uniqid() . '@localhost.test',
'password' => 'password123!',
'name' => 'Weak Password User',
]);
$this->assertSame(400, $weak['headers']['status-code']);
$valid = $this->client->call(Client::METHOD_POST, '/account', $signupHeaders, [
'userId' => ID::unique(),
'email' => 'strength_valid_' . uniqid() . '@localhost.test',
'password' => 'Password123!',
'name' => 'Valid Password User',
]);
$this->assertSame(201, $valid['headers']['status-code']);
$response = $this->client->call(Client::METHOD_PATCH, '/project/policies/password-strength', $serverHeaders, [
'minLength' => 8,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
]);
$this->assertSame(200, $response['headers']['status-code']);
}
}
@@ -2936,18 +2936,18 @@ class ProjectsConsoleClientTest extends Scope
'x-appwrite-project' => $id,
'x-appwrite-mode' => 'admin',
], $this->getHeaders()), [
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(12, $response['body']['minLength']);
$this->assertEquals(true, $response['body']['requireUppercase']);
$this->assertEquals(true, $response['body']['requireLowercase']);
$this->assertEquals(true, $response['body']['requireNumber']);
$this->assertEquals(true, $response['body']['requireSpecialChar']);
$this->assertEquals(true, $response['body']['uppercase']);
$this->assertEquals(true, $response['body']['lowercase']);
$this->assertEquals(true, $response['body']['number']);
$this->assertEquals(true, $response['body']['symbols']);
$weakPassword = 'password123!';
$validPassword = 'Password123!';
@@ -3063,18 +3063,18 @@ class ProjectsConsoleClientTest extends Scope
'x-appwrite-mode' => 'admin',
], $this->getHeaders()), [
'minLength' => 8,
'requireUppercase' => false,
'requireLowercase' => false,
'requireNumber' => false,
'requireSpecialChar' => false,
'uppercase' => false,
'lowercase' => false,
'number' => false,
'symbols' => false,
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(8, $response['body']['minLength']);
$this->assertEquals(false, $response['body']['requireUppercase']);
$this->assertEquals(false, $response['body']['requireLowercase']);
$this->assertEquals(false, $response['body']['requireNumber']);
$this->assertEquals(false, $response['body']['requireSpecialChar']);
$this->assertEquals(false, $response['body']['uppercase']);
$this->assertEquals(false, $response['body']['lowercase']);
$this->assertEquals(false, $response['body']['number']);
$this->assertEquals(false, $response['body']['symbols']);
}
#[Group('smtpAndTemplates')]
@@ -19,7 +19,7 @@ class PasswordDictionaryTest extends TestCase
public function testValues(): void
{
$this->assertEquals($this->object->isValid('1'), true);
$this->assertEquals($this->object->isValid('1'), false); // to check parent is being called
$this->assertEquals($this->object->isValid('123456'), false);
$this->assertEquals($this->object->isValid('password'), false);
$this->assertEquals($this->object->isValid('myPasswordIsRight'), true);
@@ -33,6 +33,6 @@ class PasswordDictionaryTest extends TestCase
$pass .= 'p'; // 257 chars
$this->assertEquals($this->object->isValid($pass), true);
$this->assertEquals($this->object->isValid($pass), false);
}
}
@@ -19,10 +19,10 @@ class PasswordStrengthTest extends TestCase
{
$validator = new PasswordStrength([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
]);
$this->assertFalse($validator->isValid('Password1!'));
@@ -48,10 +48,10 @@ class PasswordStrengthTest extends TestCase
{
$validator = new PasswordStrength([
'minLength' => 12,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSpecialChar' => true,
'uppercase' => true,
'lowercase' => true,
'number' => true,
'symbols' => true,
], true);
$this->assertTrue($validator->isValid(''));