Skip to content

Commit 95cd3d5

Browse files
committed
fix(Mailer): Allow to enforce strict email format
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 9db32d1 commit 95cd3d5

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
@@ -36,6 +36,7 @@
3636
namespace OC\Mail;
3737

3838
use Egulias\EmailValidator\EmailValidator;
39+
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
3940
use Egulias\EmailValidator\Validation\RFCValidation;
4041
use OCP\Defaults;
4142
use OCP\EventDispatcher\IEventDispatcher;
@@ -233,8 +234,10 @@ public function validateMailAddress(string $email): bool {
233234
// Shortcut: empty addresses are never valid
234235
return false;
235236
}
237+
238+
$strictMailCheck = $this->config->getAppValue('core', 'enforce_strict_email_check', 'no') === 'yes';
236239
$validator = new EmailValidator();
237-
$validation = new RFCValidation();
240+
$validation = $strictMailCheck ? new NoRFCWarningsValidation() : new RFCValidation();
238241

239242
return $validator->isValid($email, $validation);
240243
}

tests/lib/Mail/MailerTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MailerTest extends TestCase {
4242
private $l10n;
4343
/** @var Mailer */
4444
private $mailer;
45-
/** @var IEventDispatcher */
45+
/** @var IEventDispatcher&MockObject */
4646
private $dispatcher;
4747

4848

@@ -197,6 +197,7 @@ public function testSendInvalidMailException() {
197197
]);
198198
$this->expectException(\Exception::class);
199199

200+
/** @var Message&MockObject */
200201
$message = $this->getMockBuilder('\OC\Mail\Message')
201202
->disableOriginalConstructor()->getMock();
202203
$message->expects($this->once())
@@ -211,20 +212,27 @@ public function testSendInvalidMailException() {
211212
*/
212213
public function mailAddressProvider() {
213214
return [
214-
['lukas@owncloud.com', true],
215-
['lukas@localhost', true],
216-
['lukas@192.168.1.1', true],
217-
['lukas@éxämplè.com', true],
218-
['asdf', false],
219-
['', false],
220-
['lukas@owncloud.org@owncloud.com', false],
215+
['lukas@owncloud.com', true, false],
216+
['lukas@localhost', true, false],
217+
['lukas@192.168.1.1', true, false],
218+
['lukas@éxämplè.com', true, false],
219+
['asdf', false, false],
220+
['', false, false],
221+
['lukas@owncloud.org@owncloud.com', false, false],
222+
['test@localhost', true, false],
223+
['test@localhost', false, true],
221224
];
222225
}
223226

224227
/**
225228
* @dataProvider mailAddressProvider
226229
*/
227-
public function testValidateMailAddress($email, $expected) {
230+
public function testValidateMailAddress($email, $expected, $strict) {
231+
$this->config
232+
->expects($this->atMost(1))
233+
->method('getAppValue')
234+
->with('core', 'enforce_strict_email_check')
235+
->willReturn($strict ? 'yes' : 'no');
228236
$this->assertSame($expected, $this->mailer->validateMailAddress($email));
229237
}
230238

tests/lib/UtilTest.php

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

94+
/**
95+
* Host is "localhost" this is a valid for emails,
96+
* but not for default strict email verification that requires a top level domain.
97+
* So we check that with strict email verification we fallback to the default
98+
*/
9499
public function testGetDefaultEmailAddress() {
95100
$email = \OCP\Util::getDefaultEmailAddress("no-reply");
96101
$this->assertEquals('no-reply@localhost', $email);
97102
}
98103

104+
/**
105+
* If strict email check is enabled "localhost.localdomain" should validate as a valid email domain
106+
*/
107+
public function testGetDefaultEmailAddressStrict() {
108+
$config = \OC::$server->getConfig();
109+
$config->setAppValue('core', 'enforce_strict_email_check', 'yes');
110+
$email = \OCP\Util::getDefaultEmailAddress("no-reply");
111+
$this->assertEquals('no-reply@localhost.localdomain', $email);
112+
$config->deleteAppValue('core', 'enforce_strict_email_check');
113+
}
114+
99115
public function testGetDefaultEmailAddressFromConfig() {
100116
$config = \OC::$server->getConfig();
101117
$config->setSystemValue('mail_domain', 'example.com');

0 commit comments

Comments
 (0)