HTMLDefinitionTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. class HTMLPurifier_HTMLDefinitionTest extends HTMLPurifier_Harness
  3. {
  4. function expectError($error = false, $message = '%s') {
  5. // Because we're testing a definition, it's vital that the cache
  6. // is turned off for tests that expect errors.
  7. $this->config->set('Cache.DefinitionImpl', null);
  8. parent::expectError($error);
  9. }
  10. function test_parseTinyMCEAllowedList() {
  11. $def = new HTMLPurifier_HTMLDefinition();
  12. // note: this is case-sensitive, but its config schema
  13. // counterpart is not. This is generally a good thing for users,
  14. // but it's a slight internal inconsistency
  15. $this->assertEqual(
  16. $def->parseTinyMCEAllowedList(''),
  17. array(array(), array())
  18. );
  19. $this->assertEqual(
  20. $def->parseTinyMCEAllowedList('a,b,c'),
  21. array(array('a' => true, 'b' => true, 'c' => true), array())
  22. );
  23. $this->assertEqual(
  24. $def->parseTinyMCEAllowedList('a[x|y|z]'),
  25. array(array('a' => true), array('a.x' => true, 'a.y' => true, 'a.z' => true))
  26. );
  27. $this->assertEqual(
  28. $def->parseTinyMCEAllowedList('*[id]'),
  29. array(array(), array('*.id' => true))
  30. );
  31. $this->assertEqual(
  32. $def->parseTinyMCEAllowedList('a[*]'),
  33. array(array('a' => true), array('a.*' => true))
  34. );
  35. $this->assertEqual(
  36. $def->parseTinyMCEAllowedList('span[style],strong,a[href|title]'),
  37. array(array('span' => true, 'strong' => true, 'a' => true),
  38. array('span.style' => true, 'a.href' => true, 'a.title' => true))
  39. );
  40. $this->assertEqual(
  41. // alternate form:
  42. $def->parseTinyMCEAllowedList(
  43. 'span[style]
  44. strong
  45. a[href|title]
  46. '),
  47. $val = array(array('span' => true, 'strong' => true, 'a' => true),
  48. array('span.style' => true, 'a.href' => true, 'a.title' => true))
  49. );
  50. $this->assertEqual(
  51. $def->parseTinyMCEAllowedList(' span [ style ], strong'."\n\t".'a[href | title]'),
  52. $val
  53. );
  54. }
  55. function test_Allowed() {
  56. $config1 = HTMLPurifier_Config::create(array(
  57. 'HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
  58. 'HTML.AllowedAttributes' => array('a@href', '*@id')
  59. ));
  60. $config2 = HTMLPurifier_Config::create(array(
  61. 'HTML.Allowed' => 'b,i,p,a[href],*[id]'
  62. ));
  63. $this->assertEqual($config1->getHTMLDefinition(), $config2->getHTMLDefinition());
  64. }
  65. function assertPurification_AllowedElements_p() {
  66. $this->assertPurification('<p><b>Jelly</b></p>', '<p>Jelly</p>');
  67. }
  68. function test_AllowedElements() {
  69. $this->config->set('HTML.AllowedElements', 'p');
  70. $this->assertPurification_AllowedElements_p();
  71. }
  72. function test_AllowedElements_multiple() {
  73. $this->config->set('HTML.AllowedElements', 'p,div');
  74. $this->assertPurification('<div><p><b>Jelly</b></p></div>', '<div><p>Jelly</p></div>');
  75. }
  76. function test_AllowedElements_invalidElement() {
  77. $this->config->set('HTML.AllowedElements', 'obviously_invalid,p');
  78. $this->expectError(new PatternExpectation("/Element 'obviously_invalid' is not supported/"));
  79. $this->assertPurification_AllowedElements_p();
  80. }
  81. function test_AllowedElements_invalidElement_xssAttempt() {
  82. $this->config->set('HTML.AllowedElements', '<script>,p');
  83. $this->expectError(new PatternExpectation("/Element '&lt;script&gt;' is not supported/"));
  84. $this->assertPurification_AllowedElements_p();
  85. }
  86. function test_AllowedElements_multipleInvalidElements() {
  87. $this->config->set('HTML.AllowedElements', 'dr-wiggles,dr-pepper,p');
  88. $this->expectError(new PatternExpectation("/Element 'dr-wiggles' is not supported/"));
  89. $this->expectError(new PatternExpectation("/Element 'dr-pepper' is not supported/"));
  90. $this->assertPurification_AllowedElements_p();
  91. }
  92. function assertPurification_AllowedAttributes_global_style() {
  93. $this->assertPurification(
  94. '<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
  95. '<p style="font-weight:bold;">Jelly</p><br style="clear:both;" />');
  96. }
  97. function test_AllowedAttributes_global_preferredSyntax() {
  98. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  99. $this->config->set('HTML.AllowedAttributes', 'style');
  100. $this->assertPurification_AllowedAttributes_global_style();
  101. }
  102. function test_AllowedAttributes_global_verboseSyntax() {
  103. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  104. $this->config->set('HTML.AllowedAttributes', '*@style');
  105. $this->assertPurification_AllowedAttributes_global_style();
  106. }
  107. function test_AllowedAttributes_global_discouragedSyntax() {
  108. // Emit errors eventually
  109. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  110. $this->config->set('HTML.AllowedAttributes', '*.style');
  111. $this->assertPurification_AllowedAttributes_global_style();
  112. }
  113. function assertPurification_AllowedAttributes_local_p_style() {
  114. $this->assertPurification(
  115. '<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
  116. '<p style="font-weight:bold;">Jelly</p><br />');
  117. }
  118. function test_AllowedAttributes_local_preferredSyntax() {
  119. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  120. $this->config->set('HTML.AllowedAttributes', 'p@style');
  121. $this->assertPurification_AllowedAttributes_local_p_style();
  122. }
  123. function test_AllowedAttributes_local_discouragedSyntax() {
  124. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  125. $this->config->set('HTML.AllowedAttributes', 'p.style');
  126. $this->assertPurification_AllowedAttributes_local_p_style();
  127. }
  128. function test_AllowedAttributes_multiple() {
  129. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  130. $this->config->set('HTML.AllowedAttributes', 'p@style,br@class,title');
  131. $this->assertPurification(
  132. '<p style="font-weight:bold;" class="foo" title="foo">Jelly</p><br style="clear:both;" class="foo" title="foo" />',
  133. '<p style="font-weight:bold;" title="foo">Jelly</p><br class="foo" title="foo" />'
  134. );
  135. }
  136. function test_AllowedAttributes_local_invalidAttribute() {
  137. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  138. $this->config->set('HTML.AllowedAttributes', array('p@style', 'p@<foo>'));
  139. $this->expectError(new PatternExpectation("/Attribute '&lt;foo&gt;' in element 'p' not supported/"));
  140. $this->assertPurification_AllowedAttributes_local_p_style();
  141. }
  142. function test_AllowedAttributes_global_invalidAttribute() {
  143. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  144. $this->config->set('HTML.AllowedAttributes', array('style', '<foo>'));
  145. $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
  146. $this->assertPurification_AllowedAttributes_global_style();
  147. }
  148. function test_AllowedAttributes_local_invalidAttributeDueToMissingElement() {
  149. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  150. $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style');
  151. $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
  152. $this->assertPurification_AllowedAttributes_local_p_style();
  153. }
  154. function test_AllowedAttributes_duplicate() {
  155. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  156. $this->config->set('HTML.AllowedAttributes', 'p.style,p@style');
  157. $this->assertPurification_AllowedAttributes_local_p_style();
  158. }
  159. function test_AllowedAttributes_multipleErrors() {
  160. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  161. $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style,<foo>');
  162. $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
  163. $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
  164. $this->assertPurification_AllowedAttributes_local_p_style();
  165. }
  166. function test_ForbiddenElements() {
  167. $this->config->set('HTML.ForbiddenElements', 'b');
  168. $this->assertPurification('<b>b</b><i>i</i>', 'b<i>i</i>');
  169. }
  170. function test_ForbiddenElements_invalidElement() {
  171. $this->config->set('HTML.ForbiddenElements', 'obviously_incorrect');
  172. // no error!
  173. $this->assertPurification('<i>i</i>');
  174. }
  175. function assertPurification_ForbiddenAttributes_b_style() {
  176. $this->assertPurification(
  177. '<b style="float:left;">b</b><i style="float:left;">i</i>',
  178. '<b>b</b><i style="float:left;">i</i>');
  179. }
  180. function test_ForbiddenAttributes() {
  181. $this->config->set('HTML.ForbiddenAttributes', 'b@style');
  182. $this->assertPurification_ForbiddenAttributes_b_style();
  183. }
  184. function test_ForbiddenAttributes_incorrectSyntax() {
  185. $this->config->set('HTML.ForbiddenAttributes', 'b.style');
  186. $this->expectError("Error with b.style: tag.attr syntax not supported for HTML.ForbiddenAttributes; use tag@attr instead");
  187. $this->assertPurification('<b style="float:left;">Test</b>');
  188. }
  189. function test_ForbiddenAttributes_incorrectGlobalSyntax() {
  190. $this->config->set('HTML.ForbiddenAttributes', '*.style');
  191. $this->expectError("Error with *.style: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead");
  192. $this->assertPurification('<b style="float:left;">Test</b>');
  193. }
  194. function assertPurification_ForbiddenAttributes_style() {
  195. $this->assertPurification(
  196. '<b class="foo" style="float:left;">b</b><i style="float:left;">i</i>',
  197. '<b class="foo">b</b><i>i</i>');
  198. }
  199. function test_ForbiddenAttributes_global() {
  200. $this->config->set('HTML.ForbiddenAttributes', 'style');
  201. $this->assertPurification_ForbiddenAttributes_style();
  202. }
  203. function test_ForbiddenAttributes_globalVerboseFormat() {
  204. $this->config->set('HTML.ForbiddenAttributes', '*@style');
  205. $this->assertPurification_ForbiddenAttributes_style();
  206. }
  207. function test_addAttribute() {
  208. $config = HTMLPurifier_Config::createDefault();
  209. $def = $config->getHTMLDefinition(true);
  210. $def->addAttribute('span', 'custom', 'Enum#attribute');
  211. $purifier = new HTMLPurifier($config);
  212. $input = '<span custom="attribute">Custom!</span>';
  213. $output = $purifier->purify($input);
  214. $this->assertIdentical($input, $output);
  215. }
  216. function test_addAttribute_multiple() {
  217. $config = HTMLPurifier_Config::createDefault();
  218. $def = $config->getHTMLDefinition(true);
  219. $def->addAttribute('span', 'custom', 'Enum#attribute');
  220. $def->addAttribute('span', 'foo', 'Text');
  221. $purifier = new HTMLPurifier($config);
  222. $input = '<span custom="attribute" foo="asdf">Custom!</span>';
  223. $output = $purifier->purify($input);
  224. $this->assertIdentical($input, $output);
  225. }
  226. function test_addElement() {
  227. $config = HTMLPurifier_Config::createDefault();
  228. $def = $config->getHTMLDefinition(true);
  229. $def->addElement('marquee', 'Inline', 'Inline', 'Common', array('width' => 'Length'));
  230. $purifier = new HTMLPurifier($config);
  231. $input = '<span><marquee width="50">Foobar</marquee></span>';
  232. $output = $purifier->purify($input);
  233. $this->assertIdentical($input, $output);
  234. }
  235. function test_injector() {
  236. generate_mock_once('HTMLPurifier_Injector');
  237. $injector = new HTMLPurifier_InjectorMock();
  238. $injector->name = 'MyInjector';
  239. $injector->setReturnValue('checkNeeded', false);
  240. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  241. $module->info_injector[] = $injector;
  242. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
  243. array(
  244. 'MyInjector' => $injector,
  245. )
  246. );
  247. }
  248. function test_injectorMissingNeeded() {
  249. generate_mock_once('HTMLPurifier_Injector');
  250. $injector = new HTMLPurifier_InjectorMock();
  251. $injector->name = 'MyInjector';
  252. $injector->setReturnValue('checkNeeded', 'a');
  253. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  254. $module->info_injector[] = $injector;
  255. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
  256. array()
  257. );
  258. }
  259. function test_injectorIntegration() {
  260. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  261. $module->info_injector[] = 'Linkify';
  262. $this->assertIdentical(
  263. $this->config->getHTMLDefinition()->info_injector,
  264. array('Linkify' => new HTMLPurifier_Injector_Linkify())
  265. );
  266. }
  267. function test_injectorIntegrationFail() {
  268. $this->config->set('HTML.Allowed', 'p');
  269. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  270. $module->info_injector[] = 'Linkify';
  271. $this->assertIdentical(
  272. $this->config->getHTMLDefinition()->info_injector,
  273. array()
  274. );
  275. }
  276. function test_notAllowedRequiredAttributeError() {
  277. $this->expectError("Required attribute 'src' in element 'img' was not allowed, which means 'img' will not be allowed either");
  278. $this->config->set('HTML.Allowed', 'img[alt]');
  279. $this->config->getHTMLDefinition();
  280. }
  281. }
  282. // vim: et sw=4 sts=4