diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 864719a674a..5ebe7afedec 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6436,8 +6436,10 @@ function send_password_change_info($user) { function email_is_not_allowed($email) { global $CFG; + // Comparing lowercase domains. + $email = strtolower($email); if (!empty($CFG->allowemailaddresses)) { - $allowed = explode(' ', $CFG->allowemailaddresses); + $allowed = explode(' ', strtolower($CFG->allowemailaddresses)); foreach ($allowed as $allowedpattern) { $allowedpattern = trim($allowedpattern); if (!$allowedpattern) { @@ -6456,7 +6458,7 @@ function email_is_not_allowed($email) { return get_string('emailonlyallowed', '', $CFG->allowemailaddresses); } else if (!empty($CFG->denyemailaddresses)) { - $denied = explode(' ', $CFG->denyemailaddresses); + $denied = explode(' ', strtolower($CFG->denyemailaddresses)); foreach ($denied as $deniedpattern) { $deniedpattern = trim($deniedpattern); if (!$deniedpattern) { diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 5f3eab48d97..451893f4fca 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -3928,6 +3928,163 @@ class core_moodlelib_testcase extends advanced_testcase { $this->assertTrue(validate_email(generate_email_processing_address(23, $modargs))); } + /** + * Test allowemailaddresses setting. + * + * @param string $email Email address for the from user. + * @param string $config The CFG->allowemailaddresses config values + * @param false/string $result The expected result. + * + * @dataProvider data_email_is_not_allowed_for_allowemailaddresses + */ + public function test_email_is_not_allowed_for_allowemailaddresses($email, $config, $result) { + $this->resetAfterTest(); + + set_config('allowemailaddresses', $config); + $this->assertEquals($result, email_is_not_allowed($email)); + } + + /** + * Data provider for data_email_is_not_allowed_for_allowemailaddresses. + * + * @return array Returns an array of test data for the above function. + */ + public function data_email_is_not_allowed_for_allowemailaddresses() { + return [ + // Test allowed domain empty list. + [ + 'email' => 'fromuser@example.com', + 'config' => '', + 'result' => false + ], + // Test from email is in allowed domain. + [ + 'email' => 'fromuser@example.com', + 'config' => 'example.com test.com', + 'result' => false + ], + // Test from email is in allowed domain but uppercase config. + [ + 'email' => 'fromuser@example.com', + 'config' => 'EXAMPLE.com test.com', + 'result' => false + ], + // Test from email is in allowed domain but uppercase email. + [ + 'email' => 'fromuser@EXAMPLE.com', + 'config' => 'example.com test.com', + 'result' => false + ], + // Test from email is in allowed subdomain. + [ + 'email' => 'fromuser@something.example.com', + 'config' => '.example.com test.com', + 'result' => false + ], + // Test from email is in allowed subdomain but uppercase config. + [ + 'email' => 'fromuser@something.example.com', + 'config' => '.EXAMPLE.com test.com', + 'result' => false + ], + // Test from email is in allowed subdomain but uppercase email. + [ + 'email' => 'fromuser@something.EXAMPLE.com', + 'config' => '.example.com test.com', + 'result' => false + ], + // Test from email is not in allowed domain. + [ 'email' => 'fromuser@moodle.com', + 'config' => 'example.com test.com', + 'result' => get_string('emailonlyallowed', '', 'example.com test.com') + ], + // Test from email is not in allowed subdomain. + [ 'email' => 'fromuser@something.example.com', + 'config' => 'example.com test.com', + 'result' => get_string('emailonlyallowed', '', 'example.com test.com') + ], + ]; + } + + /** + * Test denyemailaddresses setting. + * + * @param string $email Email address for the from user. + * @param string $config The CFG->denyemailaddresses config values + * @param false/string $result The expected result. + * + * @dataProvider data_email_is_not_allowed_for_denyemailaddresses + */ + public function test_email_is_not_allowed_for_denyemailaddresses($email, $config, $result) { + $this->resetAfterTest(); + + set_config('denyemailaddresses', $config); + $this->assertEquals($result, email_is_not_allowed($email)); + } + + + /** + * Data provider for test_email_is_not_allowed_for_denyemailaddresses. + * + * @return array Returns an array of test data for the above function. + */ + public function data_email_is_not_allowed_for_denyemailaddresses() { + return [ + // Test denied domain empty list. + [ + 'email' => 'fromuser@example.com', + 'config' => '', + 'result' => false + ], + // Test from email is in denied domain. + [ + 'email' => 'fromuser@example.com', + 'config' => 'example.com test.com', + 'result' => get_string('emailnotallowed', '', 'example.com test.com') + ], + // Test from email is in denied domain but uppercase config. + [ + 'email' => 'fromuser@example.com', + 'config' => 'EXAMPLE.com test.com', + 'result' => get_string('emailnotallowed', '', 'EXAMPLE.com test.com') + ], + // Test from email is in denied domain but uppercase email. + [ + 'email' => 'fromuser@EXAMPLE.com', + 'config' => 'example.com test.com', + 'result' => get_string('emailnotallowed', '', 'example.com test.com') + ], + // Test from email is in denied subdomain. + [ + 'email' => 'fromuser@something.example.com', + 'config' => '.example.com test.com', + 'result' => get_string('emailnotallowed', '', '.example.com test.com') + ], + // Test from email is in denied subdomain but uppercase config. + [ + 'email' => 'fromuser@something.example.com', + 'config' => '.EXAMPLE.com test.com', + 'result' => get_string('emailnotallowed', '', '.EXAMPLE.com test.com') + ], + // Test from email is in denied subdomain but uppercase email. + [ + 'email' => 'fromuser@something.EXAMPLE.com', + 'config' => '.example.com test.com', + 'result' => get_string('emailnotallowed', '', '.example.com test.com') + ], + // Test from email is not in denied domain. + [ 'email' => 'fromuser@moodle.com', + 'config' => 'example.com test.com', + 'result' => false + ], + // Test from email is not in denied subdomain. + [ 'email' => 'fromuser@something.example.com', + 'config' => 'example.com test.com', + 'result' => false + ], + ]; + } + /** * Test safe method unserialize_array(). */