Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add rate limiting for test mails
  • Loading branch information
Guite committed Jan 4, 2022
1 parent b5e077a commit 06dee14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG-4.0.md
Expand Up @@ -9,4 +9,4 @@
- none yet

- Features:
- none yet
- [Mailer] Utilize rate limiter component for test email functionality.
4 changes: 4 additions & 0 deletions config/packages/rate_limiter.yaml
Expand Up @@ -4,3 +4,7 @@ framework:
policy: 'fixed_window'
limit: 20
interval: '60 minutes'
test_mails:
policy: 'fixed_window'
limit: 5
interval: '30 minutes'
8 changes: 8 additions & 0 deletions src/system/MailerModule/Controller/ConfigController.php
Expand Up @@ -16,10 +16,12 @@
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Annotation\Route;
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
use Zikula\Bundle\CoreBundle\Site\SiteDefinitionInterface;
Expand Down Expand Up @@ -98,13 +100,19 @@ public function test(
Request $request,
VariableApiInterface $variableApi,
MailerInterface $mailer,
RateLimiterFactory $testMailsLimiter,
LoggerInterface $mailLogger, // $mailLogger var name auto-injects the mail channel handler
SiteDefinitionInterface $site
): array {
$form = $this->createForm(TestType::class, $this->getDataValues($variableApi, $site));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->get('test')->isClicked()) {
$limiter = $testMailsLimiter->create($request->getClientIp());
if (false === $limiter->consume(1)->isAccepted()) {
throw new TooManyRequestsHttpException();
}

$formData = $form->getData();
$html = in_array($formData['messageType'], ['html', 'multipart']) ? true : false;
try {
Expand Down

0 comments on commit 06dee14

Please sign in to comment.