Skip to content

Commit c222823

Browse files
Merge MAGETWO-97043 into 2.2.8-bugfixes-300119
2 parents aca86c0 + 8f78c50 commit c222823

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

lib/internal/Magento/Framework/Filter/Template.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Template implements \Zend_Filter_Interface
5252
*/
5353
protected $string;
5454

55+
/**
56+
* @var string[]
57+
*/
58+
private $restrictedMethods = ['addafterfiltercallback'];
59+
5560
/**
5661
* @param \Magento\Framework\Stdlib\StringUtils $string
5762
* @param array $variables
@@ -297,6 +302,25 @@ protected function getParameters($value)
297302
return $params;
298303
}
299304

305+
/**
306+
* Validate method call initiated in a template.
307+
*
308+
* Deny calls for methods that may disrupt template processing.
309+
*
310+
* @param object $object
311+
* @param string $method
312+
* @return void
313+
* @throws \InvalidArgumentException
314+
*/
315+
private function validateVariableMethodCall($object, string $method)
316+
{
317+
if ($object === $this) {
318+
if (in_array(mb_strtolower($method), $this->restrictedMethods)) {
319+
throw new \InvalidArgumentException("Method $method cannot be called from template.");
320+
}
321+
}
322+
}
323+
300324
/**
301325
* Return variable value for var construction
302326
*
@@ -345,12 +369,12 @@ protected function getVariable($value, $default = '{no_value_defined}')
345369
$last = $i;
346370
} elseif (isset($stackVars[$i - 1]['variable']) && $stackVars[$i]['type'] == 'method') {
347371
// Calling object methods
348-
if (method_exists($stackVars[$i - 1]['variable'], $stackVars[$i]['name'])) {
349-
$stackVars[$i]['args'] = $this->getStackArgs($stackVars[$i]['args']);
350-
$stackVars[$i]['variable'] = call_user_func_array(
351-
[$stackVars[$i - 1]['variable'], $stackVars[$i]['name']],
352-
$stackVars[$i]['args']
353-
);
372+
$object = $stackVars[$i - 1]['variable'];
373+
$method = $stackVars[$i]['name'];
374+
if (method_exists($object, $method)) {
375+
$args = $this->getStackArgs($stackVars[$i]['args']);
376+
$this->validateVariableMethodCall($object, $method);
377+
$stackVars[$i]['variable'] = call_user_func_array([$object, $method], $args);
354378
}
355379
$last = $i;
356380
}

lib/internal/Magento/Framework/Filter/Test/Unit/TemplateTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,15 @@ public function varDirectiveDataProvider()
205205
],
206206
];
207207
}
208+
209+
/**
210+
* Test adding callbacks when already filtering.
211+
*
212+
* @expectedException \InvalidArgumentException
213+
*/
214+
public function testInappropriateCallbacks()
215+
{
216+
$this->templateFilter->setVariables(['filter' => $this->templateFilter]);
217+
$this->templateFilter->filter('Test {{var filter.addAfterFilterCallback(\'mb_strtolower\')}}');
218+
}
208219
}

0 commit comments

Comments
 (0)