-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bring back too many recipients check
Signed-off-by: Anna Larch <anna@nextcloud.com>
- Loading branch information
1 parent
a487baf
commit 6dc7866
Showing
9 changed files
with
201 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2024 FIXME Your name <your@email.com> | ||
* | ||
* FIXME @author Your name <your@email.com> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version3600Date20240319181700 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$localMessagesTable = $schema->getTable('mail_local_messages'); | ||
if (!$localMessagesTable->hasColumn('force')) { | ||
$localMessagesTable->addColumn('force', Types::BOOLEAN, [ | ||
'notnull' => false, | ||
'default' => false, | ||
]); | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/* | ||
* @copyright 2024 Anna Larch <anna.larch@gmx.net> | ||
* | ||
* @author Anna Larch <anna.larch@gmx.net> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Mail\Service; | ||
|
||
use OCA\Mail\Account; | ||
use OCA\Mail\AppInfo\Application; | ||
use OCA\Mail\Db\LocalMessage; | ||
use OCA\Mail\Db\Recipient; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class RecipientsService { | ||
|
||
public function __construct( | ||
private LoggerInterface $logger, | ||
private IConfig $config, | ||
private GroupsIntegration $groupsIntegration, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Account $account | ||
* @param LocalMessage $message | ||
* @return LocalMessage | ||
* @throws DoesNotExistException | ||
*/ | ||
public function checkNumberOfRecipients(Account $account, LocalMessage $message): void { | ||
if($message->getForce() === true) { | ||
return; | ||
} | ||
|
||
$numberOfRecipientsThreshold = (int)$this->config->getAppValue( | ||
Application::APP_ID, | ||
'abuse_number_of_recipients_per_message_threshold', | ||
'0', | ||
); | ||
if ($numberOfRecipientsThreshold <= 1) { | ||
return; | ||
} | ||
|
||
$recipients = $this->groupsIntegration->expand($message->getRecipients()); | ||
$to = count(array_filter($recipients, function ($recipient) { | ||
return $recipient->getType() === Recipient::TYPE_TO; | ||
})); | ||
if ($to >= $numberOfRecipientsThreshold) { | ||
$message->setStatus(LocalMessage::STATUS_TOO_MANY_RECIPIENTS); | ||
$this->logger->alert('User {user} sends to a suspicious number of "TO" recipients. {expected} are allowed. {actual} are used', [ | ||
'user' => $account->getUserId(), | ||
'expected' => $numberOfRecipientsThreshold, | ||
'actual' => $to, | ||
]); | ||
return; | ||
} | ||
|
||
$cc = count(array_filter($recipients, function ($recipient) { | ||
return $recipient->getType() === Recipient::TYPE_CC; | ||
})); | ||
if ($cc >= $numberOfRecipientsThreshold) { | ||
$message->setStatus(LocalMessage::STATUS_TOO_MANY_RECIPIENTS); | ||
$this->logger->alert('User {user} sends to a suspicious number of "CC" recipients. {expected} are allowed. {actual} are used', [ | ||
'user' => $account->getUserId(), | ||
'expected' => $numberOfRecipientsThreshold, | ||
'actual' => $cc, | ||
]); | ||
return; | ||
} | ||
|
||
$bcc = count(array_filter($recipients, function ($recipient) { | ||
return $recipient->getType() === Recipient::TYPE_BCC; | ||
})); | ||
if ($bcc >= $numberOfRecipientsThreshold) { | ||
$message->setStatus(LocalMessage::STATUS_TOO_MANY_RECIPIENTS); | ||
$this->logger->alert('User {user} sends to a suspicious number of "BCC" recipients. {expected} are allowed. {actual} are used', [ | ||
'user' => $account->getUserId(), | ||
'expected' => $numberOfRecipientsThreshold, | ||
'actual' => $bcc, | ||
]); | ||
return; | ||
} | ||
} | ||
} |
Oops, something went wrong.