Skip to content

Commit

Permalink
[9.x] Prevent double sanitized key in RateLimiter@tooManyAttempts (#…
Browse files Browse the repository at this point in the history
…42462)

* Prevent double sanitized key

* Fix test for PHP8.0
  • Loading branch information
HanakJakub authored May 23, 2022
1 parent c61fd7a commit 127a9ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 6
*/
public function tooManyAttempts($key, $maxAttempts)
{
$key = $this->cleanRateLimiterKey($key);

if ($this->attempts($key) >= $maxAttempts) {
if ($this->cache->has($key.':timer')) {
if ($this->cache->has($this->cleanRateLimiterKey($key).':timer')) {
return true;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Cache/CacheRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,19 @@ public function testKeysAreSanitizedFromUnicodeCharacters()

$this->assertTrue($rateLimiter->tooManyAttempts('jôhn', 1));
}

public function testKeyIsSanitizedOnlyOnce()
{
$cache = m::mock(Cache::class);
$rateLimiter = new RateLimiter($cache);

$key = "john'doe";
$cleanedKey = $rateLimiter->cleanRateLimiterKey($key);

$cache->shouldReceive('get')->once()->with($cleanedKey, 0)->andReturn(1);
$cache->shouldReceive('has')->once()->with("$cleanedKey:timer")->andReturn(true);
$cache->shouldReceive('add')->never();

$this->assertTrue($rateLimiter->tooManyAttempts($key, 1));
}
}

0 comments on commit 127a9ca

Please sign in to comment.