11<?php
22/**
3- * PageCache controller
43 *
54 * Copyright © Magento, Inc. All rights reserved.
65 * See COPYING.txt for license details.
98
109use Magento \Framework \Serialize \Serializer \Base64Json ;
1110use Magento \Framework \Serialize \Serializer \Json ;
11+ use Magento \Framework \Validator \RegexFactory ;
12+ use Magento \Framework \App \ObjectManager ;
1213use Magento \Framework \View \Layout \LayoutCacheKeyInterface ;
1314
1415abstract class Block extends \Magento \Framework \App \Action \Action
@@ -40,28 +41,42 @@ abstract class Block extends \Magento\Framework\App\Action\Action
4041 */
4142 private $ layoutCacheKeyName = 'mage_pagecache ' ;
4243
44+ /**
45+ * @var RegexFactory
46+ */
47+ private RegexFactory $ regexValidatorFactory ;
48+
49+ /**
50+ * Validation pattern for handles array
51+ */
52+ private const VALIDATION_RULE_PATTERN = '/^[a-z0-9]+[a-z0-9_]*$/i ' ;
53+
4354 /**
4455 * @param \Magento\Framework\App\Action\Context $context
4556 * @param \Magento\Framework\Translate\InlineInterface $translateInline
4657 * @param Json $jsonSerializer
4758 * @param Base64Json $base64jsonSerializer
4859 * @param LayoutCacheKeyInterface $layoutCacheKey
60+ * @param RegexFactory|null $regexValidatorFactory
4961 */
5062 public function __construct (
5163 \Magento \Framework \App \Action \Context $ context ,
5264 \Magento \Framework \Translate \InlineInterface $ translateInline ,
5365 Json $ jsonSerializer = null ,
5466 Base64Json $ base64jsonSerializer = null ,
55- LayoutCacheKeyInterface $ layoutCacheKey = null
67+ LayoutCacheKeyInterface $ layoutCacheKey = null ,
68+ ?RegexFactory $ regexValidatorFactory = null
5669 ) {
5770 parent ::__construct ($ context );
5871 $ this ->translateInline = $ translateInline ;
5972 $ this ->jsonSerializer = $ jsonSerializer
60- ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()->get (Json::class);
73+ ?: ObjectManager::getInstance ()->get (Json::class);
6174 $ this ->base64jsonSerializer = $ base64jsonSerializer
62- ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()->get (Base64Json::class);
75+ ?: ObjectManager::getInstance ()->get (Base64Json::class);
6376 $ this ->layoutCacheKey = $ layoutCacheKey
64- ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (LayoutCacheKeyInterface::class);
77+ ?: ObjectManager::getInstance ()->get (LayoutCacheKeyInterface::class);
78+ $ this ->regexValidatorFactory = $ regexValidatorFactory
79+ ?: ObjectManager::getInstance ()->get (RegexFactory::class);
6580 }
6681
6782 /**
@@ -79,6 +94,9 @@ protected function _getBlocks()
7994 }
8095 $ blocks = $ this ->jsonSerializer ->unserialize ($ blocks );
8196 $ handles = $ this ->base64jsonSerializer ->unserialize ($ handles );
97+ if (!$ this ->validateHandleParam ($ handles )) {
98+ return [];
99+ }
82100
83101 $ layout = $ this ->_view ->getLayout ();
84102 $ this ->layoutCacheKey ->addCacheKeys ($ this ->layoutCacheKeyName );
@@ -95,4 +113,22 @@ protected function _getBlocks()
95113
96114 return $ data ;
97115 }
116+
117+ /**
118+ * Validates handles parameter
119+ *
120+ * @param array $handles
121+ * @return bool
122+ */
123+ private function validateHandleParam ($ handles ): bool
124+ {
125+ $ validator = $ this ->regexValidatorFactory ->create (['pattern ' => self ::VALIDATION_RULE_PATTERN ]);
126+ foreach ($ handles as $ handle ) {
127+ if ($ handle && !$ validator ->isValid ($ handle )) {
128+ return false ;
129+ }
130+ }
131+
132+ return true ;
133+ }
98134}
0 commit comments