src/EventSubscriber/RateLimitSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\SimpleRateLimiter;
  4. use Psr\Log\LoggerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class RateLimitSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly SimpleRateLimiter $limiter,
  13.         private readonly LoggerInterface $logger,
  14.     ) {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::REQUEST => ['onKernelRequest'50],
  20.         ];
  21.     }
  22.     public function onKernelRequest(RequestEvent $event): void
  23.     {
  24.         if (!$event->isMainRequest()) {
  25.             return;
  26.         }
  27.         $request $event->getRequest();
  28.         $path $request->getPathInfo();
  29.         $method strtoupper($request->getMethod());
  30.         $rules = [
  31.             ['pattern' => '#^/api/identity/requests$#''methods' => ['POST'], 'limit' => 5'window' => 3600],
  32.             ['pattern' => '#^/api/identity/requests/\d+/proofs$#''methods' => ['POST'], 'limit' => 10'window' => 3600],
  33.             ['pattern' => '#^/api/hyper-contact/consent$#''methods' => ['POST'], 'limit' => 30'window' => 3600],
  34.             ['pattern' => '#^/api/hyper-contact/nearby$#''methods' => ['GET'], 'limit' => 120'window' => 3600],
  35.             ['pattern' => '#^/api/public-places/nearby$#''methods' => ['GET'], 'limit' => 180'window' => 3600],
  36.             ['pattern' => '#^/w1/scoring/contest$#''methods' => ['POST'], 'limit' => 20'window' => 3600],
  37.         ];
  38.         foreach ($rules as $rule) {
  39.             if (!preg_match($rule['pattern'], $path)) {
  40.                 continue;
  41.             }
  42.             if (!in_array($method$rule['methods'], true)) {
  43.                 continue;
  44.             }
  45.             $ip $request->getClientIp() ?? '0.0.0.0';
  46.             $key sprintf('%s:%s:%s'$ip$method$path);
  47.             $result $this->limiter->hit($key, (int) $rule['limit'], (int) $rule['window']);
  48.             if (!$result['allowed']) {
  49.                 $retryAfter max(1$result['reset_at'] - time());
  50.                 $response = new JsonResponse([
  51.                     'error' => 'rate_limited',
  52.                     'retry_after' => $retryAfter,
  53.                 ], 429);
  54.                 $response->headers->set('Retry-After', (string) $retryAfter);
  55.                 $this->logger->warning('rate_limited', [
  56.                     'path' => $path,
  57.                     'method' => $method,
  58.                     'ip' => $ip,
  59.                 ]);
  60.                 $event->setResponse($response);
  61.                 return;
  62.             }
  63.         }
  64.     }
  65. }