|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\StudioBackendBundle\EventSubscriber; |
| 15 | + |
| 16 | +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\RateLimitException; |
| 17 | +use Pimcore\Bundle\StudioBackendBundle\Util\Trait\StudioBackendPathTrait; |
| 18 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 19 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 20 | +use Symfony\Component\HttpKernel\Event\ResponseEvent; |
| 21 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 22 | +use Symfony\Component\RateLimiter\RateLimit; |
| 23 | +use Symfony\Component\RateLimiter\RateLimiterFactory; |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +final class RateLimitSubscriber implements EventSubscriberInterface |
| 29 | +{ |
| 30 | + use StudioBackendPathTrait; |
| 31 | + |
| 32 | + private const string RATE_LIMIT_ATTRIBUTE = '_studio_rate_limit'; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly string $urlPrefix, |
| 36 | + private readonly RateLimiterFactory $studioApiGeneralLimiter, |
| 37 | + private readonly bool $enabled = true, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public static function getSubscribedEvents(): array |
| 42 | + { |
| 43 | + return [ |
| 44 | + KernelEvents::REQUEST => ['onKernelRequest', 200], |
| 45 | + KernelEvents::RESPONSE => ['onKernelResponse', -10], |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @throws RateLimitException |
| 51 | + */ |
| 52 | + public function onKernelRequest(RequestEvent $event): void |
| 53 | + { |
| 54 | + if (!$this->enabled || !$event->isMainRequest()) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $request = $event->getRequest(); |
| 59 | + |
| 60 | + if ( |
| 61 | + $request->getMethod() === 'OPTIONS' || |
| 62 | + !$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix) |
| 63 | + ) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $key = $request->getClientIp() ?? 'unknown'; |
| 68 | + $limiter = $this->studioApiGeneralLimiter->create($key); |
| 69 | + $rateLimit = $limiter->consume(); |
| 70 | + |
| 71 | + $request->attributes->set(self::RATE_LIMIT_ATTRIBUTE, $rateLimit); |
| 72 | + |
| 73 | + if (!$rateLimit->isAccepted()) { |
| 74 | + throw new RateLimitException(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function onKernelResponse(ResponseEvent $event): void |
| 79 | + { |
| 80 | + if (!$this->enabled || !$event->isMainRequest()) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $request = $event->getRequest(); |
| 85 | + $rateLimit = $request->attributes->get(self::RATE_LIMIT_ATTRIBUTE); |
| 86 | + |
| 87 | + if (!$rateLimit instanceof RateLimit) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $response = $event->getResponse(); |
| 92 | + $response->headers->set('X-RateLimit-Limit', (string) $rateLimit->getLimit()); |
| 93 | + $response->headers->set( |
| 94 | + 'X-RateLimit-Remaining', |
| 95 | + (string) $rateLimit->getRemainingTokens() |
| 96 | + ); |
| 97 | + $response->headers->set( |
| 98 | + 'X-RateLimit-Reset', |
| 99 | + (string) $rateLimit->getRetryAfter()->getTimestamp() |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments