MustacheInjectionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @group mustache_injection
  12. * @group functional
  13. */
  14. class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine;
  20. }
  21. // interpolation
  22. public function testInterpolationInjection()
  23. {
  24. $tpl = $this->mustache->loadTemplate('{{ a }}');
  25. $data = array(
  26. 'a' => '{{ b }}',
  27. 'b' => 'FAIL'
  28. );
  29. $this->assertEquals('{{ b }}', $tpl->render($data));
  30. }
  31. public function testUnescapedInterpolationInjection()
  32. {
  33. $tpl = $this->mustache->loadTemplate('{{{ a }}}');
  34. $data = array(
  35. 'a' => '{{ b }}',
  36. 'b' => 'FAIL'
  37. );
  38. $this->assertEquals('{{ b }}', $tpl->render($data));
  39. }
  40. // sections
  41. public function testSectionInjection()
  42. {
  43. $tpl = $this->mustache->loadTemplate('{{# a }}{{ b }}{{/ a }}');
  44. $data = array(
  45. 'a' => true,
  46. 'b' => '{{ c }}',
  47. 'c' => 'FAIL'
  48. );
  49. $this->assertEquals('{{ c }}', $tpl->render($data));
  50. }
  51. public function testUnescapedSectionInjection()
  52. {
  53. $tpl = $this->mustache->loadTemplate('{{# a }}{{{ b }}}{{/ a }}');
  54. $data = array(
  55. 'a' => true,
  56. 'b' => '{{ c }}',
  57. 'c' => 'FAIL'
  58. );
  59. $this->assertEquals('{{ c }}', $tpl->render($data));
  60. }
  61. // partials
  62. public function testPartialInjection()
  63. {
  64. $tpl = $this->mustache->loadTemplate('{{> partial }}');
  65. $this->mustache->setPartials(array(
  66. 'partial' => '{{ a }}',
  67. ));
  68. $data = array(
  69. 'a' => '{{ b }}',
  70. 'b' => 'FAIL'
  71. );
  72. $this->assertEquals('{{ b }}', $tpl->render($data));
  73. }
  74. public function testPartialUnescapedInjection()
  75. {
  76. $tpl = $this->mustache->loadTemplate('{{> partial }}');
  77. $this->mustache->setPartials(array(
  78. 'partial' => '{{{ a }}}',
  79. ));
  80. $data = array(
  81. 'a' => '{{ b }}',
  82. 'b' => 'FAIL'
  83. );
  84. $this->assertEquals('{{ b }}', $tpl->render($data));
  85. }
  86. // lambdas
  87. public function testLambdaInterpolationInjection()
  88. {
  89. $tpl = $this->mustache->loadTemplate('{{ a }}');
  90. $data = array(
  91. 'a' => array($this, 'lambdaInterpolationCallback'),
  92. 'b' => '{{ c }}',
  93. 'c' => 'FAIL'
  94. );
  95. $this->assertEquals('{{ c }}', $tpl->render($data));
  96. }
  97. public static function lambdaInterpolationCallback()
  98. {
  99. return '{{ b }}';
  100. }
  101. public function testLambdaSectionInjection()
  102. {
  103. $tpl = $this->mustache->loadTemplate('{{# a }}b{{/ a }}');
  104. $data = array(
  105. 'a' => array($this, 'lambdaSectionCallback'),
  106. 'b' => '{{ c }}',
  107. 'c' => 'FAIL'
  108. );
  109. $this->assertEquals('{{ c }}', $tpl->render($data));
  110. }
  111. public static function lambdaSectionCallback($text)
  112. {
  113. return '{{ ' . $text . ' }}';
  114. }
  115. }