Skip to content
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
25 changes: 25 additions & 0 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@
</UndefinedMethod>
</file>
<file src="apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php">
<DeprecatedMethod>
<code><![CDATA[validateMailAddress]]></code>
<code><![CDATA[validateMailAddress]]></code>
<code><![CDATA[validateMailAddress]]></code>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
<LessSpecificReturnStatement>
<code><![CDATA[$emailAddresses]]></code>
</LessSpecificReturnStatement>
Expand Down Expand Up @@ -452,6 +458,9 @@
</DeprecatedMethod>
</file>
<file src="apps/dav/lib/CalDAV/Schedule/IMipPlugin.php">
<DeprecatedMethod>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
<RedundantCast>
<code><![CDATA[(string)$iTipMessage->recipientName]]></code>
</RedundantCast>
Expand Down Expand Up @@ -864,6 +873,11 @@
<code><![CDATA[getUserFolder]]></code>
</DeprecatedMethod>
</file>
<file src="apps/dav/lib/Listener/CalendarContactInteractionListener.php">
<DeprecatedMethod>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
</file>
<file src="apps/dav/lib/Migration/BuildCalendarSearchIndex.php">
<DeprecatedMethod>
<code><![CDATA[getAppValue]]></code>
Expand Down Expand Up @@ -1572,6 +1586,7 @@
</DeprecatedClass>
<DeprecatedMethod>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
<RedundantCast>
<code><![CDATA[(int)$code]]></code>
Expand Down Expand Up @@ -2079,6 +2094,7 @@
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[setAppValue]]></code>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
</file>
<file src="apps/settings/lib/Controller/WebAuthnController.php">
Expand Down Expand Up @@ -2201,6 +2217,10 @@
</DeprecatedMethod>
</file>
<file src="apps/sharebymail/lib/ShareByMailProvider.php">
<DeprecatedMethod>
<code><![CDATA[validateMailAddress]]></code>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
<InvalidArgument>
<code><![CDATA[$share->getId()]]></code>
<code><![CDATA[(int)$data['id']]]></code>
Expand Down Expand Up @@ -3053,6 +3073,11 @@
<code><![CDATA[listen]]></code>
</DeprecatedMethod>
</file>
<file src="core/Command/User/Add.php">
<DeprecatedMethod>
<code><![CDATA[validateMailAddress]]></code>
</DeprecatedMethod>
</file>
<file src="core/Command/User/AuthTokens/Add.php">
<DeprecatedClass>
<code><![CDATA[IToken::DO_NOT_REMEMBER]]></code>
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@
'OCP\\Mail\\Headers\\AutoSubmitted' => $baseDir . '/lib/public/Mail/Headers/AutoSubmitted.php',
'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IEmailValidator' => $baseDir . '/lib/public/Mail/IEmailValidator.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => $baseDir . '/lib/public/Mail/IMessage.php',
'OCP\\Mail\\Provider\\Address' => $baseDir . '/lib/public/Mail/Provider/Address.php',
Expand Down Expand Up @@ -1820,6 +1821,7 @@
'OC\\Log\\Systemdlog' => $baseDir . '/lib/private/Log/Systemdlog.php',
'OC\\Mail\\Attachment' => $baseDir . '/lib/private/Mail/Attachment.php',
'OC\\Mail\\EMailTemplate' => $baseDir . '/lib/private/Mail/EMailTemplate.php',
'OC\\Mail\\EmailValidator' => $baseDir . '/lib/private/Mail/EmailValidator.php',
'OC\\Mail\\Mailer' => $baseDir . '/lib/private/Mail/Mailer.php',
'OC\\Mail\\Message' => $baseDir . '/lib/private/Mail/Message.php',
'OC\\Mail\\Provider\\Manager' => $baseDir . '/lib/private/Mail/Provider/Manager.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Mail\\Headers\\AutoSubmitted' => __DIR__ . '/../../..' . '/lib/public/Mail/Headers/AutoSubmitted.php',
'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IEmailValidator' => __DIR__ . '/../../..' . '/lib/public/Mail/IEmailValidator.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => __DIR__ . '/../../..' . '/lib/public/Mail/IMessage.php',
'OCP\\Mail\\Provider\\Address' => __DIR__ . '/../../..' . '/lib/public/Mail/Provider/Address.php',
Expand Down Expand Up @@ -1861,6 +1862,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Log\\Systemdlog' => __DIR__ . '/../../..' . '/lib/private/Log/Systemdlog.php',
'OC\\Mail\\Attachment' => __DIR__ . '/../../..' . '/lib/private/Mail/Attachment.php',
'OC\\Mail\\EMailTemplate' => __DIR__ . '/../../..' . '/lib/private/Mail/EMailTemplate.php',
'OC\\Mail\\EmailValidator' => __DIR__ . '/../../..' . '/lib/private/Mail/EmailValidator.php',
'OC\\Mail\\Mailer' => __DIR__ . '/../../..' . '/lib/private/Mail/Mailer.php',
'OC\\Mail\\Message' => __DIR__ . '/../../..' . '/lib/private/Mail/Message.php',
'OC\\Mail\\Provider\\Manager' => __DIR__ . '/../../..' . '/lib/private/Mail/Provider/Manager.php',
Expand Down
37 changes: 37 additions & 0 deletions lib/private/Mail/EmailValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Mail;

use Egulias\EmailValidator\EmailValidator as EquliasEmailValidator;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;
use OCP\IAppConfig;
use OCP\Mail\IEmailValidator;

class EmailValidator implements IEmailValidator {
public function __construct(
private IAppConfig $appConfig,
) {
}

public function isValid(string $email): bool {
if ($email === '') {
// Shortcut: empty addresses are never valid
return false;
}

$strictMailCheck = $this->appConfig->getValueString('core', 'enforce_strict_email_check', 'yes') === 'yes';

$validator = new EquliasEmailValidator();
$validation = $strictMailCheck ? new NoRFCWarningsValidation() : new RFCValidation();

return $validator->isValid($email, $validation);
}
}
17 changes: 4 additions & 13 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
*/
namespace OC\Mail;

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IBinaryFinder;
Expand All @@ -21,6 +18,7 @@
use OCP\Mail\Events\BeforeMessageSent;
use OCP\Mail\IAttachment;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IEmailValidator;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -69,6 +67,7 @@ public function __construct(
private IL10N $l10n,
private IEventDispatcher $dispatcher,
private IFactory $l10nFactory,
private IEmailValidator $emailValidator,
) {
}

Expand Down Expand Up @@ -234,18 +233,10 @@ public function send(IMessage $message): array {
/**
* @param string $email Email address to be validated
* @return bool True if the mail address is valid, false otherwise
* @deprecated 26.0.0 use IEmailValidator.isValid instead
*/
public function validateMailAddress(string $email): bool {
if ($email === '') {
// Shortcut: empty addresses are never valid
return false;
}

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

return $validator->isValid($email, $validation);
return $this->emailValidator->isValid($email);
}

protected function getInstance(): MailerInterface {
Expand Down
8 changes: 7 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
use OC\Lockdown\LockdownManager;
use OC\Log\LogFactory;
use OC\Log\PsrLoggerAdapter;
use OC\Mail\EmailValidator;
use OC\Mail\Mailer;
use OC\Memcache\ArrayCache;
use OC\Memcache\Factory;
Expand Down Expand Up @@ -195,6 +196,7 @@
use OCP\Lock\ILockingProvider;
use OCP\Lockdown\ILockdownManager;
use OCP\Log\ILogFactory;
use OCP\Mail\IEmailValidator;
use OCP\Mail\IMailer;
use OCP\OCM\ICapabilityAwareOCMProvider;
use OCP\OCM\IOCMDiscoveryService;
Expand Down Expand Up @@ -916,6 +918,9 @@ public function __construct($webRoot, \OC\Config $config) {
);
});

/** @since 32.0.0 */
$this->registerAlias(IEmailValidator::class, EmailValidator::class);

$this->registerService(IMailer::class, function (Server $c) {
return new Mailer(
$c->get(\OCP\IConfig::class),
Expand All @@ -924,7 +929,8 @@ public function __construct($webRoot, \OC\Config $config) {
$c->get(IURLGenerator::class),
$c->getL10N('lib'),
$c->get(IEventDispatcher::class),
$c->get(IFactory::class)
$c->get(IFactory::class),
$c->get(IEmailValidator::class),
);
});

Expand Down
24 changes: 24 additions & 0 deletions lib/public/Mail/IEmailValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Mail;

/**
* Validator for email addresses
*
* @since 32.0.0
*/
interface IEmailValidator {
/**
* @param string $email Email address to be validated
* @return bool True if the mail address is valid, false otherwise
* @since 32.0.0
*/
public function isValid(string $email): bool;
}
1 change: 1 addition & 0 deletions lib/public/Mail/IMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function send(IMessage $message): array;
* @param string $email Email address to be validated
* @return bool True if the mail address is valid, false otherwise
* @since 8.1.0
* @deprecated 26.0.0 use IEmailValidator.isValid instead
*/
public function validateMailAddress(string $email): bool;
}
53 changes: 53 additions & 0 deletions tests/lib/Mail/EmailValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\Mail;

use OC\Mail\EmailValidator;
use OCP\IAppConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class EmailValidatorTest extends TestCase {
private IAppConfig&MockObject $appConfig;
private EmailValidator $emailValidator;


protected function setUp(): void {
parent::setUp();

$this->appConfig = $this->createMock(IAppConfig::class);
$this->emailValidator = new EmailValidator($this->appConfig);
}

public static function mailAddressProvider(): array {
return [
['lukas@nextcloud.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@nextcloud.org@nextcloud.com', false, false],
['test@localhost', true, false],
['test@localhost', false, true],
];
}

#[DataProvider('mailAddressProvider')]
public function testIsValid($email, $expected, $strict): void {
$this->appConfig
->expects($this->atMost(1))
->method('getValueString')
->with('core', 'enforce_strict_email_check', 'yes')
->willReturn($strict ? 'yes' : 'no');
$this->assertSame($expected, $this->emailValidator->isValid($email));
}
}
34 changes: 5 additions & 29 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Mail\Events\BeforeMessageSent;
use OCP\Mail\IEmailValidator;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -61,7 +62,8 @@ protected function setUp(): void {
$this->urlGenerator,
$this->l10n,
$this->dispatcher,
$this->createMock(IFactory::class)
$this->createMock(IFactory::class),
$this->createMock(IEmailValidator::class),
);
}

Expand Down Expand Up @@ -181,7 +183,8 @@ public function testEvents(): void {
$this->urlGenerator,
$this->l10n,
$this->dispatcher,
$this->createMock(IFactory::class)
$this->createMock(IFactory::class),
$this->createMock(IEmailValidator::class),
]
)
->getMock();
Expand Down Expand Up @@ -226,33 +229,6 @@ public function testSendInvalidMailException(): void {
$this->mailer->send($message);
}

/**
* @return array
*/
public static function mailAddressProvider(): array {
return [
['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],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('mailAddressProvider')]
public function testValidateMailAddress($email, $expected, $strict): void {
$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));
}

public function testCreateEMailTemplate(): void {
$this->config->method('getSystemValueString')
->with('mail_template_class', '')
Expand Down
Loading
Loading