Skip to content

Commit a59bdb8

Browse files
[6.x] Fix rate limiting unicode issue (#39375)
* Fix rate limiting unicode issue * Apply fixes from StyleCI Co-authored-by: Taylor Otwell <[email protected]>
1 parent bfd1189 commit a59bdb8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Illuminate/Cache/RateLimiter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function __construct(Cache $cache)
3636
*/
3737
public function tooManyAttempts($key, $maxAttempts)
3838
{
39+
$key = $this->cleanRateLimiterKey($key);
40+
3941
if ($this->attempts($key) >= $maxAttempts) {
4042
if ($this->cache->has($key.':timer')) {
4143
return true;
@@ -56,6 +58,8 @@ public function tooManyAttempts($key, $maxAttempts)
5658
*/
5759
public function hit($key, $decaySeconds = 60)
5860
{
61+
$key = $this->cleanRateLimiterKey($key);
62+
5963
$this->cache->add(
6064
$key.':timer', $this->availableAt($decaySeconds), $decaySeconds
6165
);
@@ -79,6 +83,8 @@ public function hit($key, $decaySeconds = 60)
7983
*/
8084
public function attempts($key)
8185
{
86+
$key = $this->cleanRateLimiterKey($key);
87+
8288
return $this->cache->get($key, 0);
8389
}
8490

@@ -90,6 +96,8 @@ public function attempts($key)
9096
*/
9197
public function resetAttempts($key)
9298
{
99+
$key = $this->cleanRateLimiterKey($key);
100+
93101
return $this->cache->forget($key);
94102
}
95103

@@ -102,6 +110,8 @@ public function resetAttempts($key)
102110
*/
103111
public function retriesLeft($key, $maxAttempts)
104112
{
113+
$key = $this->cleanRateLimiterKey($key);
114+
105115
$attempts = $this->attempts($key);
106116

107117
return $maxAttempts - $attempts;
@@ -115,6 +125,8 @@ public function retriesLeft($key, $maxAttempts)
115125
*/
116126
public function clear($key)
117127
{
128+
$key = $this->cleanRateLimiterKey($key);
129+
118130
$this->resetAttempts($key);
119131

120132
$this->cache->forget($key.':timer');
@@ -128,6 +140,19 @@ public function clear($key)
128140
*/
129141
public function availableIn($key)
130142
{
143+
$key = $this->cleanRateLimiterKey($key);
144+
131145
return $this->cache->get($key.':timer') - $this->currentTime();
132146
}
147+
148+
/**
149+
* Clean the rate limiter key from unicode characters.
150+
*
151+
* @param string $key
152+
* @return string
153+
*/
154+
public function cleanRateLimiterKey($key)
155+
{
156+
return preg_replace('/&([a-z])[a-z]+;/i', '$1', htmlentities($key));
157+
}
133158
}

tests/Cache/CacheRateLimiterTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ public function testClearClearsTheCacheKeys()
6666

6767
$rateLimiter->clear('key');
6868
}
69+
70+
public function testKeysAreSanitizedFromUnicodeCharacters()
71+
{
72+
$cache = m::mock(Cache::class);
73+
$cache->shouldReceive('get')->once()->with('john', 0)->andReturn(1);
74+
$cache->shouldReceive('has')->once()->with('john:timer')->andReturn(true);
75+
$cache->shouldReceive('add')->never();
76+
$rateLimiter = new RateLimiter($cache);
77+
78+
$this->assertTrue($rateLimiter->tooManyAttempts('jôhn', 1));
79+
}
6980
}

0 commit comments

Comments
 (0)