PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/Twig/Tests/Extension/SandboxTest.php

https://bitbucket.org/gencer/twig
PHP | 220 lines | 167 code | 41 blank | 12 comment | 0 complexity | 6304b5dc8efcd728a7c293adc5476553 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_Extension_SandboxTest extends PHPUnit_Framework_TestCase
  11. {
  12. protected static $params, $templates;
  13. public function setUp()
  14. {
  15. self::$params = array(
  16. 'name' => 'Fabien',
  17. 'obj' => new FooObject(),
  18. 'arr' => array('obj' => new FooObject()),
  19. );
  20. self::$templates = array(
  21. '1_basic1' => '{{ obj.foo }}',
  22. '1_basic2' => '{{ name|upper }}',
  23. '1_basic3' => '{% if name %}foo{% endif %}',
  24. '1_basic4' => '{{ obj.bar }}',
  25. '1_basic5' => '{{ obj }}',
  26. '1_basic6' => '{{ arr.obj }}',
  27. '1_basic7' => '{{ cycle(["foo","bar"], 1) }}',
  28. '1_basic8' => '{{ obj.getfoobar }}{{ obj.getFooBar }}',
  29. '1_basic9' => '{{ obj.foobar }}{{ obj.fooBar }}',
  30. '1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  31. '1_layout' => '{% block content %}{% endblock %}',
  32. '1_child' => '{% extends "1_layout" %}{% block content %}{{ "a"|json_encode }}{% endblock %}',
  33. );
  34. }
  35. /**
  36. * @expectedException Twig_Sandbox_SecurityError
  37. * @expectedExceptionMessage Filter "json_encode" is not allowed in "1_child".
  38. */
  39. public function testSandboxWithInheritance()
  40. {
  41. $twig = $this->getEnvironment(true, array(), self::$templates, array('block'));
  42. $twig->loadTemplate('1_child')->render(array());
  43. }
  44. public function testSandboxGloballySet()
  45. {
  46. $twig = $this->getEnvironment(false, array(), self::$templates);
  47. $this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally');
  48. $twig = $this->getEnvironment(true, array(), self::$templates);
  49. try {
  50. $twig->loadTemplate('1_basic1')->render(self::$params);
  51. $this->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
  52. } catch (Twig_Sandbox_SecurityError $e) {
  53. }
  54. $twig = $this->getEnvironment(true, array(), self::$templates);
  55. try {
  56. $twig->loadTemplate('1_basic2')->render(self::$params);
  57. $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
  58. } catch (Twig_Sandbox_SecurityError $e) {
  59. }
  60. $twig = $this->getEnvironment(true, array(), self::$templates);
  61. try {
  62. $twig->loadTemplate('1_basic3')->render(self::$params);
  63. $this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
  64. } catch (Twig_Sandbox_SecurityError $e) {
  65. }
  66. $twig = $this->getEnvironment(true, array(), self::$templates);
  67. try {
  68. $twig->loadTemplate('1_basic4')->render(self::$params);
  69. $this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
  70. } catch (Twig_Sandbox_SecurityError $e) {
  71. }
  72. $twig = $this->getEnvironment(true, array(), self::$templates);
  73. try {
  74. $twig->loadTemplate('1_basic5')->render(self::$params);
  75. $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) is called in the template');
  76. } catch (Twig_Sandbox_SecurityError $e) {
  77. }
  78. $twig = $this->getEnvironment(true, array(), self::$templates);
  79. try {
  80. $twig->loadTemplate('1_basic6')->render(self::$params);
  81. $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) is called in the template');
  82. } catch (Twig_Sandbox_SecurityError $e) {
  83. }
  84. $twig = $this->getEnvironment(true, array(), self::$templates);
  85. try {
  86. $twig->loadTemplate('1_basic7')->render(self::$params);
  87. $this->fail('Sandbox throws a SecurityError exception if an unallowed function is called in the template');
  88. } catch (Twig_Sandbox_SecurityError $e) {
  89. }
  90. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => 'foo'));
  91. FooObject::reset();
  92. $this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods');
  93. $this->assertEquals(1, FooObject::$called['foo'], 'Sandbox only calls method once');
  94. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => '__toString'));
  95. FooObject::reset();
  96. $this->assertEquals('foo', $twig->loadTemplate('1_basic5')->render(self::$params), 'Sandbox allow some methods');
  97. $this->assertEquals(1, FooObject::$called['__toString'], 'Sandbox only calls method once');
  98. $twig = $this->getEnvironment(false, array(), self::$templates);
  99. FooObject::reset();
  100. $this->assertEquals('foo', $twig->loadTemplate('1_basic5')->render(self::$params), 'Sandbox allows __toString when sandbox disabled');
  101. $this->assertEquals(1, FooObject::$called['__toString'], 'Sandbox only calls method once');
  102. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array('upper'));
  103. $this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters');
  104. $twig = $this->getEnvironment(true, array(), self::$templates, array('if'));
  105. $this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags');
  106. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array(), array('FooObject' => 'bar'));
  107. $this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties');
  108. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array(), array(), array('cycle'));
  109. $this->assertEquals('bar', $twig->loadTemplate('1_basic7')->render(self::$params), 'Sandbox allow some functions');
  110. foreach (array('getfoobar', 'getFoobar', 'getFooBar') as $name) {
  111. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => $name));
  112. FooObject::reset();
  113. $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic8')->render(self::$params), 'Sandbox allow methods in a case-insensitive way');
  114. $this->assertEquals(2, FooObject::$called['getFooBar'], 'Sandbox only calls method once');
  115. $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic9')->render(self::$params), 'Sandbox allow methods via shortcut names (ie. without get/set)');
  116. }
  117. }
  118. public function testSandboxLocallySetForAnInclude()
  119. {
  120. self::$templates = array(
  121. '2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
  122. '2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  123. );
  124. $twig = $this->getEnvironment(false, array(), self::$templates);
  125. $this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
  126. self::$templates = array(
  127. '3_basic' => '{{ obj.foo }}{% sandbox %}{% include "3_included" %}{% endsandbox %}{{ obj.foo }}',
  128. '3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  129. );
  130. $twig = $this->getEnvironment(true, array(), self::$templates);
  131. try {
  132. $twig->loadTemplate('3_basic')->render(self::$params);
  133. $this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
  134. } catch (Twig_Sandbox_SecurityError $e) {
  135. }
  136. }
  137. public function testMacrosInASandbox()
  138. {
  139. $twig = $this->getEnvironment(true, array('autoescape' => true), array('index' => <<<EOF
  140. {%- import _self as macros %}
  141. {%- macro test(text) %}<p>{{ text }}</p>{% endmacro %}
  142. {{- macros.test('username') }}
  143. EOF
  144. ), array('macro', 'import'), array('escape'));
  145. $this->assertEquals('<p>username</p>', $twig->loadTemplate('index')->render(array()));
  146. }
  147. protected function getEnvironment($sandboxed, $options, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array(), $functions = array())
  148. {
  149. $loader = new Twig_Loader_Array($templates);
  150. $twig = new Twig_Environment($loader, array_merge(array('debug' => true, 'cache' => false, 'autoescape' => false), $options));
  151. $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
  152. $twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
  153. return $twig;
  154. }
  155. }
  156. class FooObject
  157. {
  158. public static $called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
  159. public $bar = 'bar';
  160. public static function reset()
  161. {
  162. self::$called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
  163. }
  164. public function __toString()
  165. {
  166. ++self::$called['__toString'];
  167. return 'foo';
  168. }
  169. public function foo()
  170. {
  171. ++self::$called['foo'];
  172. return 'foo';
  173. }
  174. public function getFooBar()
  175. {
  176. ++self::$called['getFooBar'];
  177. return 'foobar';
  178. }
  179. }