Fix sandbox being left enabled if an exception is thrown while rendering with include function

This commit is contained in:
Carson Full
2016-12-05 16:08:06 -06:00
parent 1394bd01d5
commit 171a1d47a6
2 changed files with 30 additions and 0 deletions
+12
View File
@@ -1406,6 +1406,18 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a
throw $e;
}
} catch (Throwable $e) {
if ($isSandboxed && !$alreadySandboxed) {
$sandbox->disableSandbox();
}
throw $e;
} catch (Exception $e) {
if ($isSandboxed && !$alreadySandboxed) {
$sandbox->disableSandbox();
}
throw $e;
}
if ($isSandboxed && !$alreadySandboxed) {
+18
View File
@@ -34,6 +34,7 @@ class Twig_Tests_Extension_SandboxTest extends PHPUnit_Framework_TestCase
'1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
'1_layout' => '{% block content %}{% endblock %}',
'1_child' => "{% extends \"1_layout\" %}\n{% block content %}\n{{ \"a\"|json_encode }}\n{% endblock %}",
'1_include' => '{{ include("1_basic1", sandboxed=true) }}',
);
}
@@ -240,6 +241,23 @@ EOF
$this->assertEquals('<p>username</p>', $twig->loadTemplate('index')->render(array()));
}
public function testSandboxDisabledAfterIncludeFunctionError()
{
$twig = $this->getEnvironment(false, array(), self::$templates);
$e = null;
try {
$twig->loadTemplate('1_include')->render(self::$params);
} catch (Throwable $e) {
} catch (Exception $e) {
}
if ($e === null) {
$this->fail('An exception should be thrown for this test to be valid.');
}
$this->assertFalse($twig->getExtension('Twig_Extension_Sandbox')->isSandboxed(), 'Sandboxed include() function call should not leave Sandbox enabled when an error occurs.');
}
protected function getEnvironment($sandboxed, $options, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array(), $functions = array())
{
$loader = new Twig_Loader_Array($templates);