Skip to content

Commit eea1828

Browse files
committed
Fix cache validation regexp for Redis cache interface
1 parent a047c76 commit eea1828

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lib/internal/Magento/Framework/Cache/Core.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Framework\Cache;
77

8+
use Magento\Framework\Cache\Backend\Redis;
9+
use Zend_Cache;
10+
use Zend_Cache_Exception;
11+
812
class Core extends \Zend_Cache_Core
913
{
1014
/**
@@ -126,6 +130,34 @@ public function getIdsNotMatchingTags($tags = [])
126130
return parent::getIdsNotMatchingTags($tags);
127131
}
128132

133+
/**
134+
* Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
135+
*
136+
* Throw an exception if a problem is found
137+
*
138+
* @param string $string Cache id or tag
139+
* @throws Zend_Cache_Exception
140+
* @return void
141+
*/
142+
protected function _validateIdOrTag($string)
143+
{
144+
if ($this->_backend instanceof Redis) {
145+
if (!is_string($string)) {
146+
Zend_Cache::throwException('Invalid id or tag : must be a string');
147+
}
148+
if (substr($string, 0, 9) == 'internal-') {
149+
Zend_Cache::throwException('"internal-*" ids or tags are reserved');
150+
}
151+
if (!preg_match('~^[a-zA-Z0-9_{}]+$~D', $string)) {
152+
Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_{}]");
153+
}
154+
155+
return;
156+
}
157+
158+
parent::_validateIdOrTag($string);
159+
}
160+
129161
/**
130162
* Set the backend
131163
*

lib/internal/Magento/Framework/Cache/Test/Unit/CoreTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
namespace Magento\Framework\Cache\Test\Unit;
1212

1313
use Magento\Framework\Cache\Backend\Decorator\AbstractDecorator;
14+
use Magento\Framework\Cache\Backend\Redis;
1415
use Magento\Framework\Cache\Core;
16+
use Magento\Framework\Cache\Frontend\Adapter\Zend;
17+
use Magento\Framework\Cache\Frontend\Decorator\Bare;
18+
use Magento\Framework\Cache\FrontendInterface;
1519
use PHPUnit\Framework\TestCase;
20+
use Zend_Cache_Exception;
1621

1722
class CoreTest extends TestCase
1823
{
@@ -199,4 +204,33 @@ public function testGetIdsNotMatchingTags()
199204
$result = $frontend->getIdsNotMatchingTags($tags);
200205
$this->assertEquals($ids, $result);
201206
}
207+
208+
public function testLoadAllowsToUseCurlyBracketsInPrefixOnRedisBackend()
209+
{
210+
$id = 'abc';
211+
212+
$mockBackend = $this->createMock(Redis::class);
213+
$core = new Core([
214+
'cache_id_prefix' => '{prefix}_'
215+
]);
216+
$core->setBackend($mockBackend);
217+
218+
$core->load($id);
219+
$this->assertNull(null);
220+
}
221+
222+
public function testLoadNotAllowsToUseCurlyBracketsInPrefixOnNonRedisBackend()
223+
{
224+
$id = 'abc';
225+
226+
$core = new Core([
227+
'cache_id_prefix' => '{prefix}_'
228+
]);
229+
$core->setBackend($this->_mockBackend);
230+
231+
$this->expectException(Zend_Cache_Exception::class);
232+
$this->expectExceptionMessage("Invalid id or tag '{prefix}_abc' : must use only [a-zA-Z0-9_]");
233+
234+
$core->load($id);
235+
}
202236
}

0 commit comments

Comments
 (0)