From 1c290a9d05cdda6fe2b6caab33ae620822f077a8 Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Sun, 28 Apr 2024 14:21:24 -0500 Subject: [PATCH] Implement common rate limit on public downloads of media. --- config/routes/api_public.php | 3 ++- config/routes/api_station.php | 2 +- src/Middleware/RateLimit.php | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/config/routes/api_public.php b/config/routes/api_public.php index 5b1739240..42a8183c8 100644 --- a/config/routes/api_public.php +++ b/config/routes/api_public.php @@ -120,7 +120,8 @@ return static function (RouteCollectorProxy $group) { $group->get( '/download[.{extension}]', Controller\Api\Stations\Podcasts\Episodes\Media\GetMediaAction::class - )->setName('api:stations:public:podcast:episode:download'); + )->setName('api:stations:public:podcast:episode:download') + ->add(Middleware\RateLimit::forDownloads()); } ); } diff --git a/config/routes/api_station.php b/config/routes/api_station.php index 06d9f4648..6af48c9a4 100644 --- a/config/routes/api_station.php +++ b/config/routes/api_station.php @@ -46,7 +46,7 @@ return static function (RouteCollectorProxy $group) { $group->get('/ondemand/download/{media_id}', Controller\Api\Stations\OnDemand\DownloadAction::class) ->setName('api:stations:ondemand:download') ->add(new Middleware\StationSupportsFeature(StationFeatures::OnDemand)) - ->add(new Middleware\RateLimit('ondemand', 1, 2)); + ->add(Middleware\RateLimit::forDownloads()); // NOTE: See ./api_public.php for podcast public pages. diff --git a/src/Middleware/RateLimit.php b/src/Middleware/RateLimit.php index f781aba8c..40bf7927e 100644 --- a/src/Middleware/RateLimit.php +++ b/src/Middleware/RateLimit.php @@ -27,4 +27,9 @@ final class RateLimit extends AbstractMiddleware return $handler->handle($request); } + + public static function forDownloads(): self + { + return new self('downloads', 30, 10); + } }