Skip to content

Commit 1985f7a

Browse files
committed
Merge pull request #3 from Nyholm/patch-1
fix #1
2 parents 67256bf + 49e1b50 commit 1985f7a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/FilesystemCachePool.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\Adapter\Filesystem;
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\Exception\InvalidArgumentException;
1516
use League\Flysystem\FileNotFoundException;
1617
use League\Flysystem\Filesystem;
1718
use Psr\Cache\CacheItemInterface;
@@ -74,12 +75,17 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
7475
}
7576

7677
/**
77-
* @param $key
78+
* @param string $key
7879
*
79-
* @return mixed
80+
* @return string
81+
* @throws InvalidArgumentException
8082
*/
8183
private function getFilePath($key)
8284
{
83-
return sprintf('%s/%s', self::CACHE_PATH, urlencode(base64_encode($key)));
85+
if (!preg_match('|^[a-zA-Z0-9_\.: ]+$|', $key)) {
86+
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.:].', $key));
87+
}
88+
89+
return sprintf('%s/%s', self::CACHE_PATH, $key);
8490
}
8591
}

tests/FilesystemCachePoolTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Cache\Adapter\Filesystem\tests;
4+
5+
use Cache\Adapter\Filesystem\FilesystemCachePool;
6+
use League\Flysystem\Filesystem;
7+
use League\Flysystem\Adapter\Local;
8+
9+
/**
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class FilesystemCachePoolTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @expectedException \Psr\Cache\InvalidArgumentException
16+
*/
17+
public function testInvalidKey()
18+
{
19+
$pool = new FilesystemCachePool(new Filesystem(new Local(__DIR__.'/')));
20+
21+
$pool->getItem('test%string');
22+
}
23+
}

0 commit comments

Comments
 (0)