fix: correct rate limiter

This commit is contained in:
Maël Gangloff 2024-08-05 22:26:18 +02:00
parent 686d39da62
commit 5663b1a3b4
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
2 changed files with 11 additions and 16 deletions

View File

@ -14,6 +14,7 @@ use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface; use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
@ -64,13 +65,10 @@ class DomainRefreshController extends AbstractController
if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) { if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) {
$limiter = $this->rdapRequestsLimiter->create($userId); $limiter = $this->rdapRequestsLimiter->create($userId);
$limit = $limiter->consume(); try {
$limiter->consume()->ensureAccepted();
if (false === $limit->isAccepted()) { } catch (RateLimitExceededException $e) {
$this->logger->warning('User {username} was rate limited by the API.', [ throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage());
'username' => $this->getUser()->getUserIdentifier(),
]);
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
} }
} }

View File

@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory; use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
@ -54,17 +55,13 @@ class RegistrationController extends AbstractController
throw new UnauthorizedHttpException('', 'Registration is disabled on this instance'); throw new UnauthorizedHttpException('', 'Registration is disabled on this instance');
} }
$limiter = $this->userRegisterLimiter->create($request->getClientIp());
if (false === $this->kernel->isDebug()) { if (false === $this->kernel->isDebug()) {
$limit = $limiter->consume(); $limiter = $this->userRegisterLimiter->create($request->getClientIp());
if (false === $limit->isAccepted()) { try {
$this->logger->warning('IP address {ip} was rate limited by the Registration API.', [ $limiter->consume()->ensureAccepted();
'ip' => $request->getClientIp(), } catch (RateLimitExceededException $e) {
]); throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage());
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
} }
} }