fix duplicate call of methods if using sandbox

This commit is contained in:
nikic
2011-03-17 18:26:29 +01:00
parent 64f8709de2
commit 7cb09a5fbf
3 changed files with 13 additions and 15 deletions
+9
View File
@@ -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.
*
+3 -11
View File
@@ -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);
}
/**
+1 -4
View File
@@ -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), <<<EOF
if (is_object(\$_tmp = "foo")) {
\$this->env->getExtension('sandbox')->checkMethodAllowed(\$_tmp, '__toString');
}
echo "foo";
echo \$this->env->getExtension('sandbox')->ensureToStringAllowed("foo");
EOF
);