From: nikic <+@ni-po.com> Date: Thu, 17 Mar 2011 17:26:29 +0000 (+0100) Subject: fix duplicate call of methods if using sandbox X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7cb09a5fbfd67217b401727ceba3f21f0fa161fd;p=konrad%2Ftwig.git fix duplicate call of methods if using sandbox --- diff --git a/lib/Twig/Extension/Sandbox.php b/lib/Twig/Extension/Sandbox.php index c5cbbbf..bf76c11 100644 --- a/lib/Twig/Extension/Sandbox.php +++ b/lib/Twig/Extension/Sandbox.php @@ -91,6 +91,15 @@ class Twig_Extension_Sandbox extends Twig_Extension } } + public function ensureToStringAllowed($obj) + { + if (is_object($obj)) { + $this->policy->checkMethodAllowed($obj, '__toString'); + } + + return $obj; + } + /** * Returns the name of the extension. * diff --git a/lib/Twig/Node/SandboxedPrint.php b/lib/Twig/Node/SandboxedPrint.php index 619ff99..fe374a7 100644 --- a/lib/Twig/Node/SandboxedPrint.php +++ b/lib/Twig/Node/SandboxedPrint.php @@ -36,18 +36,10 @@ class Twig_Node_SandboxedPrint extends Twig_Node_Print { $compiler ->addDebugInfo($this) - ->write('if (is_object(') - ->raw('$_tmp = ') - ->subcompile($this->removeNodeFilter($this->getNode('expr'))) - ->raw(')) {'."\n") - ->indent() - ->write('$this->env->getExtension(\'sandbox\')->checkMethodAllowed(') - ->raw('$_tmp, \'__toString\');'."\n") - ->outdent() - ->write('}'."\n") + ->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(') + ->subcompile($this->getNode('expr')) + ->raw(");\n") ; - - parent::compile($compiler); } /** diff --git a/test/Twig/Tests/Node/SandboxedPrintTest.php b/test/Twig/Tests/Node/SandboxedPrintTest.php index 6e30241..85adddf 100644 --- a/test/Twig/Tests/Node/SandboxedPrintTest.php +++ b/test/Twig/Tests/Node/SandboxedPrintTest.php @@ -37,10 +37,7 @@ class Twig_Tests_Node_SandboxedPrintTest extends Twig_Tests_Node_TestCase $tests = array(); $tests[] = array(new Twig_Node_SandboxedPrint(new Twig_Node_Expression_Constant('foo', 0), 0), <<env->getExtension('sandbox')->checkMethodAllowed(\$_tmp, '__toString'); -} -echo "foo"; +echo \$this->env->getExtension('sandbox')->ensureToStringAllowed("foo"); EOF );