From 6f3c780e42abfc3f2c78cb6e22de99c339f06126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Mon, 5 Aug 2024 22:41:08 +0200 Subject: [PATCH] fix: correct rate limiter --- src/Controller/DomainRefreshController.php | 9 ++++----- src/Controller/RegistrationController.php | 8 +++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Controller/DomainRefreshController.php b/src/Controller/DomainRefreshController.php index 38d0271..b24344a 100644 --- a/src/Controller/DomainRefreshController.php +++ b/src/Controller/DomainRefreshController.php @@ -14,7 +14,6 @@ use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Messenger\Exception\ExceptionInterface; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\RateLimiter\Exception\RateLimitExceededException; use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; @@ -65,10 +64,10 @@ public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) { $limiter = $this->rdapRequestsLimiter->create($userId); - try { - $limiter->consume()->ensureAccepted(); - } catch (RateLimitExceededException $e) { - throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage()); + $limit = $limiter->consume(); + + if (!$limit->isAccepted()) { + throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time()); } } diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index dd2cfb1..8b16124 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -18,7 +18,6 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mime\Address; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; -use Symfony\Component\RateLimiter\Exception\RateLimitExceededException; use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Serializer\SerializerInterface; @@ -57,11 +56,10 @@ public function register(Request $request, UserPasswordHasherInterface $userPass if (false === $this->kernel->isDebug()) { $limiter = $this->userRegisterLimiter->create($request->getClientIp()); + $limit = $limiter->consume(); - try { - $limiter->consume()->ensureAccepted(); - } catch (RateLimitExceededException $e) { - throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage()); + if (!$limit->isAccepted()) { + throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time()); } }