SandboxTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. static protected $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_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  30. );
  31. }
  32. public function testSandboxGloballySet()
  33. {
  34. $twig = $this->getEnvironment(false, array(), self::$templates);
  35. $this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally');
  36. $twig = $this->getEnvironment(true, array(), self::$templates);
  37. try {
  38. $twig->loadTemplate('1_basic1')->render(self::$params);
  39. $this->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
  40. } catch (Twig_Sandbox_SecurityError $e) {
  41. }
  42. $twig = $this->getEnvironment(true, array(), self::$templates);
  43. try {
  44. $twig->loadTemplate('1_basic2')->render(self::$params);
  45. $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
  46. } catch (Twig_Sandbox_SecurityError $e) {
  47. }
  48. $twig = $this->getEnvironment(true, array(), self::$templates);
  49. try {
  50. $twig->loadTemplate('1_basic3')->render(self::$params);
  51. $this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
  52. } catch (Twig_Sandbox_SecurityError $e) {
  53. }
  54. $twig = $this->getEnvironment(true, array(), self::$templates);
  55. try {
  56. $twig->loadTemplate('1_basic4')->render(self::$params);
  57. $this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
  58. } catch (Twig_Sandbox_SecurityError $e) {
  59. }
  60. $twig = $this->getEnvironment(true, array(), self::$templates);
  61. try {
  62. $twig->loadTemplate('1_basic5')->render(self::$params);
  63. $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) is called in the template');
  64. } catch (Twig_Sandbox_SecurityError $e) {
  65. }
  66. $twig = $this->getEnvironment(true, array(), self::$templates);
  67. try {
  68. $twig->loadTemplate('1_basic6')->render(self::$params);
  69. $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) 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_basic7')->render(self::$params);
  75. $this->fail('Sandbox throws a SecurityError exception if an unallowed function is called in the template');
  76. } catch (Twig_Sandbox_SecurityError $e) {
  77. }
  78. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => 'foo'));
  79. FooObject::reset();
  80. $this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods');
  81. $this->assertEquals(1, FooObject::$called['foo'], 'Sandbox only calls method once');
  82. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => '__toString'));
  83. FooObject::reset();
  84. $this->assertEquals('foo', $twig->loadTemplate('1_basic5')->render(self::$params), 'Sandbox allow some methods');
  85. $this->assertEquals(1, FooObject::$called['__toString'], 'Sandbox only calls method once');
  86. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array('upper'));
  87. $this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters');
  88. $twig = $this->getEnvironment(true, array(), self::$templates, array('if'));
  89. $this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags');
  90. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array(), array('FooObject' => 'bar'));
  91. $this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties');
  92. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array(), array(), array('cycle'));
  93. $this->assertEquals('bar', $twig->loadTemplate('1_basic7')->render(self::$params), 'Sandbox allow some functions');
  94. foreach (array('getfoobar', 'getFoobar', 'getFooBar') as $name) {
  95. $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('FooObject' => $name));
  96. FooObject::reset();
  97. $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic8')->render(self::$params), 'Sandbox allow methods in a case-insensitive way');
  98. $this->assertEquals(2, FooObject::$called['getFooBar'], 'Sandbox only calls method once');
  99. }
  100. }
  101. public function testSandboxLocallySetForAnInclude()
  102. {
  103. self::$templates = array(
  104. '2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
  105. '2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  106. );
  107. $twig = $this->getEnvironment(false, array(), self::$templates);
  108. $this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
  109. self::$templates = array(
  110. '3_basic' => '{{ obj.foo }}{% sandbox %}{% include "3_included" %}{% endsandbox %}{{ obj.foo }}',
  111. '3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
  112. );
  113. $twig = $this->getEnvironment(true, array(), self::$templates);
  114. try {
  115. $twig->loadTemplate('3_basic')->render(self::$params);
  116. $this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
  117. } catch (Twig_Sandbox_SecurityError $e) {
  118. }
  119. }
  120. public function testMacrosInASandbox()
  121. {
  122. $twig = $this->getEnvironment(true, array('autoescape' => true), array('index' => <<<EOF
  123. {% macro test(text) %}<p>{{ text }}</p>{% endmacro %}
  124. {{ _self.test('username') }}
  125. EOF
  126. ), array('macro'), array('escape'));
  127. $this->assertEquals('<p>username</p>', $twig->loadTemplate('index')->render(array()));
  128. }
  129. protected function getEnvironment($sandboxed, $options, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array(), $functions = array())
  130. {
  131. $loader = new Twig_Loader_Array($templates);
  132. $twig = new Twig_Environment($loader, array_merge(array('debug' => true, 'cache' => false, 'autoescape' => false), $options));
  133. $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
  134. $twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
  135. return $twig;
  136. }
  137. }
  138. class FooObject
  139. {
  140. static public $called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
  141. public $bar = 'bar';
  142. static public function reset()
  143. {
  144. self::$called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
  145. }
  146. public function __toString()
  147. {
  148. ++self::$called['__toString'];
  149. return 'foo';
  150. }
  151. public function foo()
  152. {
  153. ++self::$called['foo'];
  154. return 'foo';
  155. }
  156. public function getFooBar()
  157. {
  158. ++self::$called['getFooBar'];
  159. return 'foobar';
  160. }
  161. }