8000 [11.x] Fix the RateLimiter issue when using dynamic keys (#53763) · tibbsa/laravel-framework@5ed48d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ed48d3

Browse files
authored
[11.x] Fix the RateLimiter issue when using dynamic keys (laravel#53763)
* Fix the RateLimite issue when generating dynamic keys * Adjust limit condition in Limit::fallbackKey() method
1 parent e4a7357 commit 5ed48d3

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Illuminate/Cache/RateLimiting/Limit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function response(callable $callback)
150150
*/
151151
public function fallbackKey()
152152
{
153-
$prefix = $this->key ? '' : "{$this->key}:";
153+
$prefix = $this->key ? "{$this->key}:" : '';
154154

155155
return "{$prefix}attempts:{$this->maxAttempts}:decay:{$this->decaySeconds}";
156156
}

tests/Cache/RateLim 8000 iterTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Illuminate\Tests\Cache;
44

5+
use Illuminate\Cache\ArrayStore;
56
use Illuminate\Cache\RateLimiter;
67
use Illuminate\Cache\RateLimiting\Limit;
8+
use Illuminate\Cache\Repository;
79
use Illuminate\Contracts\Cache\Repository as Cache;
810
use PHPUnit\Framework\Attributes\DataProvider;
911
use PHPUnit\Framework\TestCase;
@@ -38,6 +40,33 @@ public function testRegisterNamedRateLimiter(mixed $name, string $expected): voi
3840

3941
$this->assertNotNull($limiterClosure);
4042
}
43+
44+
public function testShouldUseOriginKeyAsPrefixWhenMultipleLimiterWithSameKey()
45+
{
46+
$rateLimiter = new RateLimiter(new Repository(new ArrayStore));
47+
48+
$rateLimiter->for('user_limiter', fn (string $userId) => [
49+
Limit::perSecond(3)->by($userId),
50+
Limit::perMinute(5)->by($userId),
51+
]);
52+
53+
$userId1 = '123';
54+
$userId2 = '456';
55+
56+
$limiterForUser1 = $rateLimiter->limiter('user_limiter')($userId1);
57+
$limiterForUser2 = $rateLimiter->limiter('user_limiter')($userId2);
58+
59+
for ($i = 0; $i < 3; $i++) {
60+
$this->assertFalse($rateLimiter->tooManyAttempts($limiterForUser1[0]->key, $limiterForUser1[0]->maxAttempts));
61+
$this->assertFalse($rateLimiter->tooManyAttempts($limiterForUser2[0]->key, $limiterForUser2[0]->maxAttempts));
62+
63+
$rateLimiter->hit($limiterForUser1[0]->key, $limiterForUser1[0]->decaySeconds);
64+
$rateLimiter->hit($limiterForUser2[0]->key, $limiterForUser2[0]->decaySeconds);
65+
}
66+
67+
$this->assertNotSame($limiterForUser1[0]->key, $limiterForUser2[0]->key);
68+
$this->assertNotSame($limiterForUser1[1]->key, $limiterForUser2[1]->key);
69+
}
4170
}
4271

4372
enum BackedEnumNamedRateLimiter: string

0 commit comments

Comments
 (0)
0