From 264ae1d002f9e04c5ac53cccaceb865da8153841 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Tue, 26 Sep 2023 12:36:31 +1000 Subject: [PATCH 1/2] Convert exception rate limit to seconds --- .../Foundation/Exceptions/Handler.php | 2 +- .../FoundationExceptionsHandlerTest.php | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 1832b3206ecd..301918317dd7 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -360,7 +360,7 @@ protected function shouldntReport(Throwable $e) with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key), $throttle->maxAttempts, fn () => true, - $throttle->decayMinutes + 60 * $throttle->decayMinutes ); }), rescue: false, report: false); } diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 4b4a746283c1..a032bd6dcdae 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -705,6 +705,52 @@ public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 6 $this->assertCount(14, $reported); $this->assertSame('Something in the app went wrong.', $reported[0]->getMessage()); } + + public function testRateLimitExpires() + { + $handler = new class($this->container) extends Handler + { + protected function throttle($e) + { + return Limit::perMinute(1); + } + }; + $reported = []; + $handler->reportable(function (\Throwable $e) use (&$reported) { + $reported[] = $e; + + return false; + }); + $this->container->instance(RateLimiter::class, $limiter = new class(new Repository(new ArrayStore)) extends RateLimiter + { + public $attempted = 0; + + public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 60) + { + $this->attempted++; + + return parent::attempt(...func_get_args()); + } + }); + + Carbon::setTestNow('2000-01-01 00:00:00.000'); + $handler->report(new Exception('Something in the app went wrong 1.')); + Carbon::setTestNow('2000-01-01 00:00:59.999'); + $handler->report(new Exception('Something in the app went wrong 1.')); + + $this->assertSame(2, $limiter->attempted); + $this->assertCount(1, $reported); + $this->assertSame('Something in the app went wrong 1.', $reported[0]->getMessage()); + + Carbon::setTestNow('2000-01-01 00:01:00.000'); + $handler->report(new Exception('Something in the app went wrong 2.')); + Carbon::setTestNow('2000-01-01 00:01:59.999'); + $handler->report(new Exception('Something in the app went wrong 2.')); + + $this->assertSame(4, $limiter->attempted); + $this->assertCount(2, $reported); + $this->assertSame('Something in the app went wrong 2.', $reported[1]->getMessage()); + } } class CustomException extends Exception From 190b4c990d0ca26fa53f127730fa0bbd295643fb Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Tue, 26 Sep 2023 12:38:04 +1000 Subject: [PATCH 2/2] wip --- tests/Foundation/FoundationExceptionsHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index a032bd6dcdae..54bc417624a2 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -706,7 +706,7 @@ public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 6 $this->assertSame('Something in the app went wrong.', $reported[0]->getMessage()); } - public function testRateLimitExpires() + public function testRateLimitExpiresOnBoundary() { $handler = new class($this->container) extends Handler {