diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 6eb2c3601..4a9fe9aa8 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -154,6 +154,11 @@ abstract class Twig_Template implements Twig_TemplateInterface } if (null !== $template) { + // avoid RCEs when sandbox is enabled + if (!$template instanceof Twig_Template) { + throw new \LogicException('A block must be a method on a Twig_Template instance.'); + } + try { $template->$block($context, $blocks); } catch (Twig_Error $e) { diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 717248595..172eabffa 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -10,6 +10,15 @@ */ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase { + /** + * @expectedException LogicException + */ + public function testDisplayBlocksAcceptTemplateOnlyAsBlocks() + { + $template = $this->getMockForAbstractClass('Twig_Template', array(), '', false); + $template->displayBlock('foo', array(), array('foo' => array(new stdClass(), 'foo'))); + } + /** * @dataProvider getAttributeExceptions */