Skip to content

Commit 45b638e

Browse files
Merge branch '2.4-develop' into stability_control
2 parents 1ddfde4 + 528cfb0 commit 45b638e

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
/**
@@ -116,6 +120,34 @@ public function getIdsNotMatchingTags($tags = [])
116120
return parent::getIdsNotMatchingTags($tags);
117121
}
118122

123+
/**
124+
* Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
125+
*
126+
* Throw an exception if a problem is found
127+
*
128+
* @param string $string Cache id or tag
129+
* @throws Zend_Cache_Exception
130+
* @return void
131+
*/
132+
protected function _validateIdOrTag($string)
133+
{
134+
if ($this->_backend instanceof Redis) {
135+
if (!is_string($string)) {
136+
Zend_Cache::throwException('Invalid id or tag : must be a string');
137+
}
138+
if (substr($string, 0, 9) == 'internal-') {
139+
Zend_Cache::throwException('"internal-*" ids or tags are reserved');
140+
}
141+
if (!preg_match('~^[a-zA-Z0-9_{}]+$~D', $string)) {
142+
Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_{}]");
143+
}
144+
145+
return;
146+
}
147+
148+
parent::_validateIdOrTag($string);
149+
}
150+
119151
/**
120152
* Set the backend
121153
*

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)