File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
lib/internal/Magento/Framework/Cache Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 55 */
66namespace Magento \Framework \Cache ;
77
8+ use Magento \Framework \Cache \Backend \Redis ;
9+ use Zend_Cache ;
10+ use Zend_Cache_Exception ;
11+
812class 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 *
Original file line number Diff line number Diff line change 1111namespace Magento \Framework \Cache \Test \Unit ;
1212
1313use Magento \Framework \Cache \Backend \Decorator \AbstractDecorator ;
14+ use Magento \Framework \Cache \Backend \Redis ;
1415use 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 ;
1519use PHPUnit \Framework \TestCase ;
20+ use Zend_Cache_Exception ;
1621
1722class 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}
You can’t perform that action at this time.
0 commit comments