Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix rate limiting unicode issue #39375

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function __construct(Cache $cache)
*/
public function tooManyAttempts($key, $maxAttempts)
{
$key = $this->cleanRateLimiterKey($key);

if ($this->attempts($key) >= $maxAttempts) {
if ($this->cache->has($key.':timer')) {
return true;
Expand All @@ -56,6 +58,8 @@ public function tooManyAttempts($key, $maxAttempts)
*/
public function hit($key, $decaySeconds = 60)
{
$key = $this->cleanRateLimiterKey($key);

$this->cache->add(
$key.':timer', $this->availableAt($decaySeconds), $decaySeconds
);
Expand All @@ -79,6 +83,8 @@ public function hit($key, $decaySeconds = 60)
*/
public function attempts($key)
{
$key = $this->cleanRateLimiterKey($key);

return $this->cache->get($key, 0);
}

Expand All @@ -90,6 +96,8 @@ public function attempts($key)
*/
public function resetAttempts($key)
{
$key = $this->cleanRateLimiterKey($key);

return $this->cache->forget($key);
}

Expand All @@ -102,6 +110,8 @@ public function resetAttempts($key)
*/
public function retriesLeft($key, $maxAttempts)
{
$key = $this->cleanRateLimiterKey($key);

$attempts = $this->attempts($key);

return $maxAttempts - $attempts;
Expand All @@ -115,6 +125,8 @@ public function retriesLeft($key, $maxAttempts)
*/
public function clear($key)
{
$key = $this->cleanRateLimiterKey($key);

$this->resetAttempts($key);

$this->cache->forget($key.':timer');
Expand All @@ -128,6 +140,19 @@ public function clear($key)
*/
public function availableIn($key)
{
$key = $this->cleanRateLimiterKey($key);

return $this->cache->get($key.':timer') - $this->currentTime();
}

/**
* Clean the rate limiter key from unicode characters.
*
* @param string $key
* @return string
*/
public function cleanRateLimiterKey($key)
{
return preg_replace('/&([a-z])[a-z]+;/i', '$1', htmlentities($key));
}
}
11 changes: 11 additions & 0 deletions tests/Cache/CacheRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ public function testClearClearsTheCacheKeys()

$rateLimiter->clear('key');
}

public function testKeysAreSanitizedFromUnicodeCharacters()
{
$cache = m::mock(Cache::class);
$cache->shouldReceive('get')->once()->with('john', 0)->andReturn(1);
$cache->shouldReceive('has')->once()->with('john:timer')->andReturn(true);
$cache->shouldReceive('add')->never();
$rateLimiter = new RateLimiter($cache);

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