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 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
/**
@@ -126,6 +130,34 @@ public function getIdsNotMatchingTags($tags = [])
126
130
return parent ::getIdsNotMatchingTags ($ tags );
127
131
}
128
132
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
+
129
161
/**
130
162
* Set the backend
131
163
*
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