Skip to content

Commit c1661b6

Browse files
authored
Merge pull request #45570 from nextcloud/fix/strict-email-verification
fix(Mailer): Allow to enforce strict email format
2 parents dbf0d93 + 1a27314 commit c1661b6

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

lib/private/Mail/Mailer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OC\Mail;
1010

1111
use Egulias\EmailValidator\EmailValidator;
12+
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
1213
use Egulias\EmailValidator\Validation\RFCValidation;
1314
use OCP\Defaults;
1415
use OCP\EventDispatcher\IEventDispatcher;
@@ -206,8 +207,10 @@ public function validateMailAddress(string $email): bool {
206207
// Shortcut: empty addresses are never valid
207208
return false;
208209
}
210+
211+
$strictMailCheck = $this->config->getAppValue('core', 'enforce_strict_email_check', 'yes') === 'yes';
209212
$validator = new EmailValidator();
210-
$validation = new RFCValidation();
213+
$validation = $strictMailCheck ? new NoRFCWarningsValidation() : new RFCValidation();
211214

212215
return $validator->isValid($email, $validation);
213216
}

tests/lib/Mail/MailerTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MailerTest extends TestCase {
3838
private $l10n;
3939
/** @var Mailer */
4040
private $mailer;
41-
/** @var IEventDispatcher */
41+
/** @var IEventDispatcher&MockObject */
4242
private $dispatcher;
4343

4444

@@ -193,6 +193,7 @@ public function testSendInvalidMailException() {
193193
]);
194194
$this->expectException(\Exception::class);
195195

196+
/** @var Message&MockObject */
196197
$message = $this->getMockBuilder('\OC\Mail\Message')
197198
->disableOriginalConstructor()->getMock();
198199
$message->expects($this->once())
@@ -207,20 +208,27 @@ public function testSendInvalidMailException() {
207208
*/
208209
public function mailAddressProvider() {
209210
return [
210-
['lukas@owncloud.com', true],
211-
['lukas@localhost', true],
212-
['lukas@192.168.1.1', true],
213-
['lukas@éxämplè.com', true],
214-
['asdf', false],
215-
['', false],
216-
['lukas@owncloud.org@owncloud.com', false],
211+
['lukas@owncloud.com', true, false],
212+
['lukas@localhost', true, false],
213+
['lukas@192.168.1.1', true, false],
214+
['lukas@éxämplè.com', true, false],
215+
['asdf', false, false],
216+
['', false, false],
217+
['lukas@owncloud.org@owncloud.com', false, false],
218+
['test@localhost', true, false],
219+
['test@localhost', false, true],
217220
];
218221
}
219222

220223
/**
221224
* @dataProvider mailAddressProvider
222225
*/
223-
public function testValidateMailAddress($email, $expected) {
226+
public function testValidateMailAddress($email, $expected, $strict) {
227+
$this->config
228+
->expects($this->atMost(1))
229+
->method('getAppValue')
230+
->with('core', 'enforce_strict_email_check')
231+
->willReturn($strict ? 'yes' : 'no');
224232
$this->assertSame($expected, $this->mailer->validateMailAddress($email));
225233
}
226234

tests/lib/UtilTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,25 @@ public function testFileInfoLoaded() {
9090
$this->assertEquals($expected, \OC_Util::fileInfoLoaded());
9191
}
9292

93+
/**
94+
* Host is "localhost" this is a valid for emails,
95+
* but not for default strict email verification that requires a top level domain.
96+
* So we check that with strict email verification we fallback to the default
97+
*/
98+
public function testGetDefaultEmailAddressStrict() {
99+
$email = \OCP\Util::getDefaultEmailAddress("no-reply");
100+
$this->assertEquals('no-reply@localhost.localdomain', $email);
101+
}
102+
103+
/**
104+
* If no strict email check is enabled "localhost" should validate as a valid email domain
105+
*/
93106
public function testGetDefaultEmailAddress() {
107+
$config = \OC::$server->getConfig();
108+
$config->setAppValue('core', 'enforce_strict_email_check', 'no');
94109
$email = \OCP\Util::getDefaultEmailAddress("no-reply");
95110
$this->assertEquals('no-reply@localhost', $email);
111+
$config->deleteAppValue('core', 'enforce_strict_email_check');
96112
}
97113

98114
public function testGetDefaultEmailAddressFromConfig() {

0 commit comments

Comments
 (0)