Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] fix(Mailer): Allow to enforce strict email format #45660

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace OC\Mail;

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -233,8 +234,10 @@ public function validateMailAddress(string $email): bool {
// Shortcut: empty addresses are never valid
return false;
}

$strictMailCheck = $this->config->getAppValue('core', 'enforce_strict_email_check', 'no') === 'yes';
$validator = new EmailValidator();
$validation = new RFCValidation();
$validation = $strictMailCheck ? new NoRFCWarningsValidation() : new RFCValidation();

return $validator->isValid($email, $validation);
}
Expand Down
26 changes: 17 additions & 9 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MailerTest extends TestCase {
private $l10n;
/** @var Mailer */
private $mailer;
/** @var IEventDispatcher */
/** @var IEventDispatcher&MockObject */
private $dispatcher;


Expand Down Expand Up @@ -197,6 +197,7 @@ public function testSendInvalidMailException() {
]);
$this->expectException(\Exception::class);

/** @var Message&MockObject */
$message = $this->getMockBuilder('\OC\Mail\Message')
->disableOriginalConstructor()->getMock();
$message->expects($this->once())
Expand All @@ -211,20 +212,27 @@ public function testSendInvalidMailException() {
*/
public function mailAddressProvider() {
return [
['lukas@owncloud.com', true],
['lukas@localhost', true],
['lukas@192.168.1.1', true],
['lukas@éxämplè.com', true],
['asdf', false],
['', false],
['lukas@owncloud.org@owncloud.com', false],
['lukas@owncloud.com', true, false],
['lukas@localhost', true, false],
['lukas@192.168.1.1', true, false],
['lukas@éxämplè.com', true, false],
['asdf', false, false],
['', false, false],
['lukas@owncloud.org@owncloud.com', false, false],
['test@localhost', true, false],
['test@localhost', false, true],
];
}

/**
* @dataProvider mailAddressProvider
*/
public function testValidateMailAddress($email, $expected) {
public function testValidateMailAddress($email, $expected, $strict) {
$this->config
->expects($this->atMost(1))
->method('getAppValue')
->with('core', 'enforce_strict_email_check')
->willReturn($strict ? 'yes' : 'no');
$this->assertSame($expected, $this->mailer->validateMailAddress($email));
}

Expand Down
16 changes: 16 additions & 0 deletions tests/lib/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,27 @@ public function testFileInfoLoaded() {
$this->assertEquals($expected, \OC_Util::fileInfoLoaded());
}

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

/**
* If strict email check is enabled "localhost.localdomain" should validate as a valid email domain
*/
public function testGetDefaultEmailAddressStrict() {
$config = \OC::$server->getConfig();
$config->setAppValue('core', 'enforce_strict_email_check', 'yes');
$email = \OCP\Util::getDefaultEmailAddress("no-reply");
$this->assertEquals('no-reply@localhost.localdomain', $email);
$config->deleteAppValue('core', 'enforce_strict_email_check');
}

public function testGetDefaultEmailAddressFromConfig() {
$config = \OC::$server->getConfig();
$config->setSystemValue('mail_domain', 'example.com');
Expand Down
Loading