Skip to content

Commit b10968c

Browse files
martin-honek-tgmax-ipinfo
authored andcommitted
Sanitize cache keys to prevent an exception on IPv6 addresses
1 parent 09b35d8 commit b10968c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/cache/DefaultCache.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ipinfo\ipinfo\cache;
44

55
use Symfony\Component\Cache\Adapter\ArrayAdapter;
6+
use Symfony\Component\Cache\CacheItem;
67
use Symfony\Contracts\Cache\ItemInterface;
78

89
/**
@@ -32,6 +33,8 @@ public function __construct(int $maxsize, int $ttl)
3233
*/
3334
public function has(string $name): bool
3435
{
36+
$name = $this->sanitizeName($name);
37+
3538
return $this->cache->hasItem($name);
3639
}
3740

@@ -42,11 +45,12 @@ public function has(string $name): bool
4245
*/
4346
public function set(string $name, $value)
4447
{
48+
$name = $this->sanitizeName($name);
4549
if (!$this->cache->hasItem($name)) {
4650
$this->element_queue[] = $name;
4751
}
4852

49-
$this->cache->get($name, function (ItemInterface $item) use ($value) {
53+
$this->cache->get($name, function (ItemInterface $item) use ($value) {
5054
$item->set($value)->expiresAfter($this->ttl);
5155
return $item->get();
5256
});
@@ -61,6 +65,8 @@ public function set(string $name, $value)
6165
*/
6266
public function get(string $name)
6367
{
68+
$name = $this->sanitizeName($name);
69+
6470
return $this->cache->getItem($name)->get();
6571
}
6672

@@ -79,4 +85,14 @@ private function manageSize()
7985
$this->element_queue = array_slice($this->element_queue, $overflow);
8086
}
8187
}
88+
89+
/**
90+
* Remove forbidden characters from cache keys
91+
*/
92+
private function sanitizeName(string $name): string
93+
{
94+
$forbiddenCharacters = str_split(CacheItem::RESERVED_CHARACTERS);
95+
96+
return str_replace($forbiddenCharacters, "", $name);
97+
}
8298
}

0 commit comments

Comments
 (0)