From efd386b5e082aaf231eade161170b2dc8f832b1c Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 30 May 2025 04:51:38 +0200 Subject: [PATCH] Reduce username limit to 16 characters --- common/tests/unit/validators/UsernameValidatorTest.php | 6 +++--- common/validators/UsernameValidator.php | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/common/tests/unit/validators/UsernameValidatorTest.php b/common/tests/unit/validators/UsernameValidatorTest.php index d0a1af6..38b162e 100644 --- a/common/tests/unit/validators/UsernameValidatorTest.php +++ b/common/tests/unit/validators/UsernameValidatorTest.php @@ -37,7 +37,7 @@ class UsernameValidatorTest extends TestCase { $this->validator->validateAttribute($model, 'field'); $this->assertSame(['error.username_too_short'], $model->getErrors('field')); - $model = $this->createModel('erickskrauch_erickskrauch'); + $model = $this->createModel('mcmodder_mcmodder'); $this->validator->validateAttribute($model, 'field'); $this->assertSame(['error.username_too_long'], $model->getErrors('field')); @@ -50,8 +50,8 @@ class UsernameValidatorTest extends TestCase { // TODO: rewrite this test with @provider usage public function testValidateAttributePattern(): void { $shouldBeValid = [ - 'русский_ник', 'русский_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!$%^&*()[]', - '[ESP]Эрик', 'Свят_помидор;', 'зроблена_ў_беларусі:)', + 'русский_ник', 'by_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!$%^&*()[]', + '[ESP]Эрик', 'Свят_помидор;', ]; foreach ($shouldBeValid as $nickname) { $model = $this->createModel($nickname); diff --git a/common/validators/UsernameValidator.php b/common/validators/UsernameValidator.php index 1a018d2..fa42a1f 100644 --- a/common/validators/UsernameValidator.php +++ b/common/validators/UsernameValidator.php @@ -1,4 +1,6 @@ message = E::USERNAME_REQUIRED; + // Using username longer than 16 characters causes the "Failed to encode client:hello" error + // during server authentication on Minecraft 1.20 and above $length = new validators\StringValidator(); $length->min = 3; - $length->max = 21; + $length->max = 16; $length->tooShort = E::USERNAME_TOO_SHORT; $length->tooLong = E::USERNAME_TOO_LONG; @@ -54,7 +58,7 @@ class UsernameValidator extends Validator { return null; } - protected function executeValidation(Validator $validator, Model $model, string $attribute) { + protected function executeValidation(Validator $validator, Model $model, string $attribute): bool { $validator->validateAttribute($model, $attribute); return !$model->hasErrors($attribute);