Skip to content

Commit aaa2820

Browse files
committed
Add password blacklist
1 parent e010ad8 commit aaa2820

File tree

7 files changed

+56
-7
lines changed

7 files changed

+56
-7
lines changed

etc/config.sample.json

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
},
2828
"server_update_job_token": null,
2929
"user_login_disabled": false,
30+
"user_password_blacklist": [
31+
{
32+
"password": "123456",
33+
"reason": "This password is too simple and well known."
34+
},
35+
{
36+
"password": "correcthorsebatterystaple",
37+
"reason": "This is a bad password because it's well known. Don't take advice from a web comic too seriously."
38+
},
39+
{
40+
"password": "password",
41+
"reason": "This password is too simple and well known."
42+
}
43+
],
3044
"user_password_pepper": "bnetdocs-INSERTRANDOMVALUEHERE",
3145
"user_register_disabled": false,
3246
"user_register_requirements": {

src/controllers/User/ChangePassword.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ protected function tryChangePassword(
6464
$model->error = "PASSWORD_INCORRECT";
6565
return;
6666
}
67+
$blacklist = Common::$config->bnetdocs->user_password_blacklist;
68+
foreach ($blacklist as $blacklist_pw) {
69+
if (strtolower($blacklist_pw->password) == strtolower($pw2)) {
70+
$model->error = "PASSWORD_BLACKLIST";
71+
$model->error_extra = $blacklist_pw->reason;
72+
return;
73+
}
74+
}
6775
$old_password_hash = Authentication::$user->getPasswordHash();
6876
$old_password_salt = Authentication::$user->getPasswordSalt();
6977
try {

src/controllers/User/Register.php

+16-7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
131131
$model->error = "PASSWORD_TOO_SHORT";
132132
return;
133133
}
134+
$blacklist = Common::$config->bnetdocs->user_password_blacklist;
135+
foreach ($blacklist as $blacklist_pw) {
136+
if (strtolower($blacklist_pw->password) == strtolower($pw1)) {
137+
$model->error = "PASSWORD_BLACKLIST";
138+
$model->error_extra = $blacklist_pw->reason;
139+
return;
140+
}
141+
}
134142
if (Common::$config->bnetdocs->user_register_disabled) {
135143
$model->error = "REGISTER_DISABLED";
136144
return;
@@ -180,14 +188,15 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
180188
Logger::logEvent(
181189
EventTypes::USER_CREATED,
182190
$user_id,
183-
getenv("REMOTE_ADDR"),
191+
getenv('REMOTE_ADDR'),
184192
json_encode([
185-
"error" => $model->error,
186-
"requirements" => $req,
187-
"email" => $email,
188-
"username" => $username,
189-
"display_name" => null,
190-
"options_bitmask" => 0,
193+
'error' => $model->error,
194+
'error_extra' => $model->error_extra,
195+
'requirements' => $req,
196+
'email' => $email,
197+
'username' => $username,
198+
'display_name' => null,
199+
'options_bitmask' => 0,
191200
])
192201
);
193202

src/models/User/ChangePassword.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66

77
class ChangePassword extends Model {
88

9+
public $csrf_id;
10+
public $csrf_token;
11+
public $error;
12+
public $error_extra;
13+
914
}

src/models/User/Register.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
class Register extends Model {
88

9+
public $csrf_id;
10+
public $csrf_token;
911
public $email;
12+
public $error;
13+
public $error_extra;
1014
public $recaptcha;
1115
public $username;
1216
public $username_max_len;

src/templates/User/ChangePassword.phtml

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ switch ($this->getContext()->error) {
2323
case "PASSWORD_INCORRECT":
2424
$message = "You did not enter your correct current password.";
2525
break;
26+
case "PASSWORD_BLACKLIST":
27+
$message = $this->getContext()->error_extra;
28+
if (empty($message)) $message = "The new password is blacklisted.";
29+
break;
2630
case "INTERNAL_ERROR":
2731
$message = "An internal error occurred while processing your request. "
2832
. "Our staff have been notified of the issue. Try again later.";

src/templates/User/Register.phtml

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ switch ($this->getContext()->error) {
6161
$af = "pw1";
6262
$message = "The password is too short, use a better password.";
6363
break;
64+
case "PASSWORD_BLACKLIST":
65+
$af = "pw1";
66+
$message = $this->getContext()->error_extra;
67+
if (empty($message)) $message = "The password is blacklisted.";
68+
break;
6469
case "REGISTER_DISABLED":
6570
$af = null;
6671
$message = "Creating accounts has been administratively disabled "

0 commit comments

Comments
 (0)