Skip to content

Commit f0d201a

Browse files
committed
Add email address denylist regexp matching
1 parent cf07e03 commit f0d201a

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

etc/config.sample.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
"user_password_pepper": "bnetdocs-INSERTRANDOMVALUEHERE",
4343
"user_register_disabled": false,
4444
"user_register_requirements": {
45+
"email_enable_denylist": true,
4546
"email_validate_quick": true,
4647
"password_allow_email": false,
4748
"password_allow_username": false,
4849
"password_length_max": null,
4950
"password_length_min": 6,
50-
"username_length_max": 64,
51+
"username_length_max": 28,
5152
"username_length_min": 3
5253
}
5354
},
@@ -67,6 +68,9 @@
6768
"server_id": 405789880749260820
6869
},
6970
"email": {
71+
"recipient_denylist_regexp": [
72+
"/@mailinator\\.com$/i"
73+
],
7074
"recipient_from": [
7175
"root@localhost",
7276
"BNETDocs"

src/controllers/User/Register.php

+9
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,20 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
9191
$pwlen = strlen($pw1);
9292
$usernamelen = strlen($username);
9393
$req = &Common::$config->bnetdocs->user_register_requirements;
94+
$email_denylist = &Common::$config->email->recipient_denylist_regexp;
9495
if ($req->email_validate_quick
9596
&& !filter_var($email, FILTER_VALIDATE_EMAIL)) {
9697
$model->error = 'INVALID_EMAIL';
9798
return;
9899
}
100+
if ($req->email_enable_denylist) {
101+
foreach ($email_denylist as $_bad_email) {
102+
if (preg_match($_bad_email, $email)) {
103+
$model->error = 'EMAIL_NOT_ALLOWED';
104+
return;
105+
}
106+
}
107+
}
99108
if (!$req->password_allow_email && stripos($pw1, $email)) {
100109
$model->error = 'PASSWORD_CONTAINS_EMAIL';
101110
return;

src/controllers/User/Update.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ public function &run(Router &$router, View &$view, array &$args) {
199199

200200
// email change request
201201

202+
// email denylist check
203+
$email_not_allowed = false;
204+
if ($req->email_enable_denylist) {
205+
$email_denylist = &Common::$config->email->recipient_denylist_regexp;
206+
foreach ($email_denylist as $_bad_email) {
207+
if (preg_match($_bad_email, $email)) {
208+
$email_not_allowed = true;
209+
break;
210+
}
211+
}
212+
}
213+
202214
if (strtolower($model->email_1) !== strtolower($model->email_2)) {
203215

204216
// email mismatch
@@ -209,11 +221,17 @@ public function &run(Router &$router, View &$view, array &$args) {
209221
// email is empty
210222
$model->email_error = ['red', 'EMPTY'];
211223

212-
} else if (!filter_var($model->email_2, FILTER_VALIDATE_EMAIL)) {
224+
} else if ($req->email_validate_quick
225+
&& !filter_var($model->email_2, FILTER_VALIDATE_EMAIL)) {
213226

214227
// email is invalid; it doesn't meet RFC 822 requirements
215228
$model->email_error = ['red', 'INVALID'];
216229

230+
} else if ($email_not_allowed) {
231+
232+
// email is not allowed; it matches a denylist regular expression
233+
$model->email_error = ['red', 'NOT_ALLOWED'];
234+
217235
} else {
218236

219237
// initiate email change

src/templates/User/Register.phtml

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ switch ($this->getContext()->error) {
3636
$af = "email";
3737
$message = "The email address is invalid.";
3838
break;
39+
case "EMAIL_NOT_ALLOWED":
40+
$af = "email";
41+
$message = "The email address is not allowed, use a different one.";
42+
break;
3943
case "PASSWORD_CONTAINS_EMAIL":
4044
$af = "pw1";
4145
$message = "The password contains the email address, "

src/templates/User/Update.phtml

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ switch ($this->getContext()->email_error[1]) {
3434
$email_error = 'Your new email address cannot be blank.'; break;
3535
case 'INVALID':
3636
$email_error = 'Your new email address uses invalid formatting.'; break;
37+
case 'NOT_ALLOWED':
38+
$email_error = 'Your new email address is not allowed, use a different '
39+
. 'one.'; break;
3740
case 'CHANGE_FAILED':
3841
$email_error = 'Failed to change email due to an internal error.'; break;
3942
case 'CHANGE_SUCCESS':

0 commit comments

Comments
 (0)