File tree 2 files changed +66
-0
lines changed
lib/internal/Magento/Framework/Cache
2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 5
5
*/
6
6
namespace Magento \Framework \Cache ;
7
7
8
+ use Magento \Framework \Cache \Backend \Redis ;
9
+ use Zend_Cache ;
10
+ use Zend_Cache_Exception ;
11
+
8
12
class Core extends \Zend_Cache_Core
9
13
{
10
14
/**
@@ -116,6 +120,34 @@ public function getIdsNotMatchingTags($tags = [])
116
120
return parent ::getIdsNotMatchingTags ($ tags );
117
121
}
118
122
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
+
119
151
/**
120
152
* Set the backend
121
153
*
Original file line number Diff line number Diff line change 11
11
namespace Magento \Framework \Cache \Test \Unit ;
12
12
13
13
use Magento \Framework \Cache \Backend \Decorator \AbstractDecorator ;
14
+ use Magento \Framework \Cache \Backend \Redis ;
14
15
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 ;
15
19
use PHPUnit \Framework \TestCase ;
20
+ use Zend_Cache_Exception ;
16
21
17
22
class CoreTest extends TestCase
18
23
{
@@ -199,4 +204,33 @@ public function testGetIdsNotMatchingTags()
199
204
$ result = $ frontend ->getIdsNotMatchingTags ($ tags );
200
205
$ this ->assertEquals ($ ids , $ result );
201
206
}
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
+ }
202
236
}
You can’t perform that action at this time.
0 commit comments