diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 90b723758..6472a5b18 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1432,7 +1432,7 @@ function twig_array_batch($items, $size, $fill = null) * * @internal */ -function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) +function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false, $sandboxed = false) { // array if (Twig_Template::METHOD_CALL !== $type) { @@ -1514,7 +1514,7 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, return true; } - if ($env->hasExtension('Twig_Extension_Sandbox')) { + if ($sandboxed) { $env->getExtension('Twig_Extension_Sandbox')->checkPropertyAllowed($object, $item); } @@ -1591,7 +1591,7 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, return true; } - if ($env->hasExtension('Twig_Extension_Sandbox')) { + if ($sandboxed) { $env->getExtension('Twig_Extension_Sandbox')->checkMethodAllowed($object, $method); } diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index 05a3fc283..b4b1b6862 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -23,9 +23,11 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { + $env = $compiler->getEnvironment(); + // optimize array calls if ( - (!$compiler->getEnvironment()->isStrictVariables() || $this->getAttribute('ignore_strict_check')) + (!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check')) && !$this->getAttribute('is_defined_test') && Twig_Template::ARRAY_CALL === $this->getAttribute('type') ) { @@ -58,7 +60,8 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression $compiler->raw(', ')->subcompile($this->getNode('attribute')); // only generate optional arguments when needed (to make generated code more readable) - $needFourth = $this->getAttribute('ignore_strict_check'); + $needFifth = $env->hasExtension('Twig_Extension_Sandbox'); + $needFourth = $needFifth || $this->getAttribute('ignore_strict_check'); $needThird = $needFourth || $this->getAttribute('is_defined_test'); $needSecond = $needThird || Twig_Template::ANY_CALL !== $this->getAttribute('type'); $needFirst = $needSecond || $this->hasNode('arguments'); @@ -83,6 +86,10 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression $compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check')); } + if ($needFifth) { + $compiler->raw(', ')->repr($env->hasExtension('Twig_Extension_Sandbox')); + } + $compiler->raw(')'); } } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index ca87b8036..747d50c61 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -80,7 +80,7 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase $template = new Twig_TemplateTest($twig); try { - twig_get_attribute($twig, $template->getSourceContext(), $object, $item, array(), 'any'); + twig_get_attribute($twig, $template->getSourceContext(), $object, $item, array(), 'any', false, false, true); if (!$allowed) { $this->fail();