Skip to content

Commit bc1183e

Browse files
caseylockerclaudesmarcet
committed
fix: move hyphen to end of password validation regex character class (#102)
* fix: move hyphen to end of password validation regex character class The hyphen character was positioned between * and + in the regex character class [#?!@$%^&*-+], causing it to be interpreted as a range operator (ASCII 42-43) instead of a literal hyphen (ASCII 45). This caused passwords containing hyphens to fail validation while passwords with asterisks passed. Moving the hyphen to the end of the character class [#?!@$%^&*+-] ensures it is treated as a literal character. Regression introduced in 757e964. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore: add regresion test --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: smarcet <smarcet@gmail.com>
1 parent f13ba45 commit bc1183e

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

config/auth.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102
'password_reset_lifetime' => env('AUTH_PASSWORD_RESET_LIFETIME', 1800),
103103
'password_min_length' => env('AUTH_PASSWORD_MIN_LENGTH', 8),
104104
'password_max_length' => env('AUTH_PASSWORD_MAX_LENGTH', 30),
105-
'password_allowed_special_characters' => env('AUTH_PASSWORD_ALLOWED_SPECIAL_CHARACTERS', '[A-Za-z0-9#?!@$%^&*-+]'),
106-
'password_shape_pattern' => env('AUTH_PASSWORD_SHAPE_PATTERN', '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-+])[A-Za-z0-9#?!@$%^&*-+]+$'),
107-
'password_shape_warning' => env('AUTH_PASSWORD_SHAPE_WARNING', 'Password must include at least one uppercase letter, one lowercase letter, one number, and one special character (#?!@$%^&*-+).'),
105+
'password_allowed_special_characters' => env('AUTH_PASSWORD_ALLOWED_SPECIAL_CHARACTERS', '[A-Za-z0-9#?!@$%^&*+-]'),
106+
'password_shape_pattern' => env('AUTH_PASSWORD_SHAPE_PATTERN', '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*+\-])[A-Za-z0-9#?!@$%^&*+\-]+$'),
107+
'password_shape_warning' => env('AUTH_PASSWORD_SHAPE_WARNING', 'Password must include at least one uppercase letter, one lowercase letter, one number, and one special character (#?!@$%^&*+-).'),
108108
'verification_email_lifetime' => env("AUTH_VERIFICATION_EMAIL_LIFETIME", 600),
109109
'allows_native_auth' => env('AUTH_ALLOWS_NATIVE_AUTH', 1),
110110
'allows_native_on_config' => env('AUTH_ALLOWS_NATIVE_AUTH_CONFIG', 1),
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php namespace Tests;
2+
/**
3+
* Copyright 2026 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use App\Jobs\AddUserAction;
15+
use Auth\User;
16+
use Illuminate\Support\Facades\Config;
17+
use Illuminate\Support\Facades\Queue;
18+
use models\exceptions\ValidationException;
19+
20+
class UserPasswordShapeRegressionTest extends TestCase
21+
{
22+
private function setPasswordPolicyForTest(): void
23+
{
24+
// Keep these aligned with your real config defaults (or just read them from config)
25+
Config::set('auth.password_min_length', 8);
26+
Config::set('auth.password_max_length', 128);
27+
Config::set('auth.password_shape_warning', 'Password does not meet complexity requirements.');
28+
29+
// IMPORTANT: hyphen is escaped (or move it to end of the class)
30+
Config::set(
31+
'auth.password_shape_pattern',
32+
'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*+\-])[A-Za-z0-9#?!@$%^&*+\-]+$'
33+
);
34+
}
35+
36+
public static function validPasswordsProvider(): array
37+
{
38+
return [
39+
// has uppercase, lowercase, digit, and "-" special char
40+
['Abcdef1-'],
41+
['Zz9-aaaaa'],
42+
['Aaaaaa1-+'], // multiple specials still ok (if + is allowed)
43+
];
44+
}
45+
46+
/**
47+
* @dataProvider validPasswordsProvider
48+
*/
49+
public function test_set_password_allows_hyphen(string $plainPassword): void
50+
{
51+
$this->setPasswordPolicyForTest();
52+
53+
// Prevent actually queueing anything (setPassword dispatches AddUserAction)
54+
Queue::fake();
55+
56+
$user = new User();
57+
$user->setEmail('test@example.org');
58+
59+
$user->setPassword($plainPassword);
60+
61+
$this->assertTrue($user->hasPasswordSet());
62+
$this->assertNotEmpty($user->getPassword());
63+
64+
}
65+
66+
public function test_password_missing_special_character_is_rejected(): void
67+
{
68+
$this->setPasswordPolicyForTest();
69+
Queue::fake();
70+
71+
$user = new User();
72+
$user->setEmail('test@example.org');
73+
74+
$this->expectException(ValidationException::class);
75+
$user->setPassword('Abcdef12'); // no special char
76+
}
77+
78+
public function test_password_with_hyphen_matches_current_regex(): void
79+
{
80+
$this->setPasswordPolicyForTest();
81+
82+
$pattern = Config::get('auth.password_shape_pattern');
83+
$this->assertSame(1, preg_match("/$pattern/", 'Abcdef1-'));
84+
}
85+
}

0 commit comments

Comments
 (0)