MDL-58454 badges: Fix some minor errors for Open Badges v2
[moodle.git] / lib / tests / htmlpurifier_test.php
blobe14b3191fdf87d0523071439e4a328f73587e275
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for the HTMLPurifier integration
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * HTMLPurifier test case
32 * @package core
33 * @category phpunit
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_htmlpurifier_testcase extends basic_testcase {
39 /**
40 * Verify _blank target is allowed.
42 public function test_allow_blank_target() {
43 // See MDL-52651 for an explanation as to why the rel="noreferrer" attribute is expected here.
44 // Also note we do not need to test links with an existing rel attribute as the HTML Purifier is configured to remove
45 // the rel attribute.
46 $text = '<a href="http://moodle.org" target="_blank">Some link</a>';
47 $expected = '<a href="http://moodle.org" target="_blank" rel="noreferrer noopener">Some link</a>';
48 $result = format_text($text, FORMAT_HTML);
49 $this->assertSame($expected, $result);
51 $result = format_text('<a href="http://moodle.org" target="some">Some link</a>', FORMAT_HTML);
52 $this->assertSame('<a href="http://moodle.org">Some link</a>', $result);
55 /**
56 * Verify our nolink tag accepted.
58 public function test_nolink() {
59 // We can not use format text because nolink changes result.
60 $text = '<nolink><div>no filters</div></nolink>';
61 $result = purify_html($text, array());
62 $this->assertSame($text, $result);
64 $text = '<nolink>xxx<em>xx</em><div>xxx</div></nolink>';
65 $result = purify_html($text, array());
66 $this->assertSame($text, $result);
69 /**
70 * Verify our tex tag accepted.
72 public function test_tex() {
73 $text = '<tex>a+b=c</tex>';
74 $result = purify_html($text, array());
75 $this->assertSame($text, $result);
78 /**
79 * Verify our algebra tag accepted.
81 public function test_algebra() {
82 $text = '<algebra>a+b=c</algebra>';
83 $result = purify_html($text, array());
84 $this->assertSame($text, $result);
87 /**
88 * Verify our hacky multilang works.
90 public function test_multilang() {
91 $text = '<lang lang="en">hmmm</lang><lang lang="anything">hm</lang>';
92 $result = purify_html($text, array());
93 $this->assertSame($text, $result);
95 $text = '<span lang="en" class="multilang">hmmm</span><span lang="anything" class="multilang">hm</span>';
96 $result = purify_html($text, array());
97 $this->assertSame($text, $result);
99 $text = '<span lang="en">hmmm</span>';
100 $result = purify_html($text, array());
101 $this->assertNotSame($text, $result);
103 // Keep standard lang tags.
105 $text = '<span lang="de_DU" class="multilang">asas</span>';
106 $result = purify_html($text, array());
107 $this->assertSame($text, $result);
109 $text = '<lang lang="de_DU">xxxxxx</lang>';
110 $result = purify_html($text, array());
111 $this->assertSame($text, $result);
115 * Tests the 'allowid' option for format_text.
117 public function test_format_text_allowid() {
118 // Start off by not allowing ids (default).
119 $options = array(
120 'nocache' => true
122 $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
123 $this->assertSame('<div>Frog</div>', $result);
125 // Now allow ids.
126 $options['allowid'] = true;
127 $result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
128 $this->assertSame('<div id="example">Frog</div>', $result);
131 public function test_allowobjectembed() {
132 global $CFG;
134 $this->assertSame('0', $CFG->allowobjectembed);
136 $text = '<object width="425" height="350">
137 <param name="movie" value="http://www.youtube.com/v/AyPzM5WK8ys" />
138 <param name="wmode" value="transparent" />
139 <embed src="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" />
140 </object>hmmm';
141 $result = purify_html($text, array());
142 $this->assertSame('hmmm', trim($result));
144 $CFG->allowobjectembed = '1';
146 $expected = '<object width="425" height="350" data="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash">
147 <param name="allowScriptAccess" value="never" />
148 <param name="allowNetworking" value="internal" />
149 <param name="movie" value="http://www.youtube.com/v/AyPzM5WK8ys" />
150 <param name="wmode" value="transparent" />
151 <embed src="http://www.youtube.com/v/AyPzM5WK8ys" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" allowscriptaccess="never" allownetworking="internal" />
152 </object>hmmm';
153 $result = purify_html($text, array());
154 $this->assertSame(str_replace("\n", '', $expected), str_replace("\n", '', $result));
156 $CFG->allowobjectembed = '0';
158 $result = purify_html($text, array());
159 $this->assertSame('hmmm', trim($result));
163 * Test if linebreaks kept unchanged.
165 public function test_line_breaking() {
166 $text = "\n\raa\rsss\nsss\r";
167 $this->assertSame($text, purify_html($text));
171 * Test fixing of strict problems.
173 public function test_tidy() {
174 $text = "<p>xx";
175 $this->assertSame('<p>xx</p>', purify_html($text));
177 $text = "<P>xx</P>";
178 $this->assertSame('<p>xx</p>', purify_html($text));
180 $text = "xx<br>";
181 $this->assertSame('xx<br />', purify_html($text));
185 * Test nesting - this used to cause problems in earlier versions.
187 public function test_nested_lists() {
188 $text = "<ul><li>One<ul><li>Two</li></ul></li><li>Three</li></ul>";
189 $this->assertSame($text, purify_html($text));
193 * Test that XSS protection works, complete smoke tests are in htmlpurifier itself.
195 public function test_cleaning_nastiness() {
196 $text = "x<SCRIPT>alert('XSS')</SCRIPT>x";
197 $this->assertSame('xx', purify_html($text));
199 $text = '<DIV STYLE="background-image:url(javascript:alert(\'XSS\'))">xx</DIV>';
200 $this->assertSame('<div>xx</div>', purify_html($text));
202 $text = '<DIV STYLE="width:expression(alert(\'XSS\'));">xx</DIV>';
203 $this->assertSame('<div>xx</div>', purify_html($text));
205 $text = 'x<IFRAME SRC="javascript:alert(\'XSS\');"></IFRAME>x';
206 $this->assertSame('xx', purify_html($text));
208 $text = 'x<OBJECT TYPE="text/x-scriptlet" DATA="http://ha.ckers.org/scriptlet.html"></OBJECT>x';
209 $this->assertSame('xx', purify_html($text));
211 $text = 'x<EMBED SRC="http://ha.ckers.org/xss.swf" AllowScriptAccess="always"></EMBED>x';
212 $this->assertSame('xx', purify_html($text));
214 $text = 'x<form></form>x';
215 $this->assertSame('xx', purify_html($text));
219 * Test internal function used for clean_text() speedup.
221 public function test_is_purify_html_necessary() {
222 // First our shortcuts.
223 $text = "";
224 $this->assertFalse(is_purify_html_necessary($text));
225 $this->assertSame($text, purify_html($text));
227 $text = "666";
228 $this->assertFalse(is_purify_html_necessary($text));
229 $this->assertSame($text, purify_html($text));
231 $text = "abc\ndef \" ' ";
232 $this->assertFalse(is_purify_html_necessary($text));
233 $this->assertSame($text, purify_html($text));
235 $text = "abc\n<p>def</p>efg<p>hij</p>";
236 $this->assertFalse(is_purify_html_necessary($text));
237 $this->assertSame($text, purify_html($text));
239 $text = "<br />abc\n<p>def<em>efg</em><strong>hi<br />j</strong></p>";
240 $this->assertFalse(is_purify_html_necessary($text));
241 $this->assertSame($text, purify_html($text));
243 // Now failures.
244 $text = "&nbsp;";
245 $this->assertTrue(is_purify_html_necessary($text));
247 $text = "Gin & Tonic";
248 $this->assertTrue(is_purify_html_necessary($text));
250 $text = "Gin > Tonic";
251 $this->assertTrue(is_purify_html_necessary($text));
253 $text = "Gin < Tonic";
254 $this->assertTrue(is_purify_html_necessary($text));
256 $text = "<div>abc</div>";
257 $this->assertTrue(is_purify_html_necessary($text));
259 $text = "<span>abc</span>";
260 $this->assertTrue(is_purify_html_necessary($text));
262 $text = "<br>abc";
263 $this->assertTrue(is_purify_html_necessary($text));
265 $text = "<p class='xxx'>abc</p>";
266 $this->assertTrue(is_purify_html_necessary($text));
268 $text = "<p>abc<em></p></em>";
269 $this->assertTrue(is_purify_html_necessary($text));
271 $text = "<p>abc";
272 $this->assertTrue(is_purify_html_necessary($text));
275 public function test_allowed_schemes() {
276 // First standard schemas.
277 $text = '<a href="http://www.example.com/course/view.php?id=5">link</a>';
278 $this->assertSame($text, purify_html($text));
280 $text = '<a href="https://www.example.com/course/view.php?id=5">link</a>';
281 $this->assertSame($text, purify_html($text));
283 $text = '<a href="ftp://user@ftp.example.com/some/file.txt">link</a>';
284 $this->assertSame($text, purify_html($text));
286 $text = '<a href="nntp://example.com/group/123">link</a>';
287 $this->assertSame($text, purify_html($text));
289 $text = '<a href="news:groupname">link</a>';
290 $this->assertSame($text, purify_html($text));
292 $text = '<a href="mailto:user@example.com">link</a>';
293 $this->assertSame($text, purify_html($text));
295 // Extra schemes allowed in moodle.
296 $text = '<a href="irc://irc.example.com/3213?pass">link</a>';
297 $this->assertSame($text, purify_html($text));
299 $text = '<a href="rtsp://www.example.com/movie.mov">link</a>';
300 $this->assertSame($text, purify_html($text));
302 $text = '<a href="rtmp://www.example.com/video.f4v">link</a>';
303 $this->assertSame($text, purify_html($text));
305 $text = '<a href="teamspeak://speak.example.com/?par=val?par2=val2">link</a>';
306 $this->assertSame($text, purify_html($text));
308 $text = '<a href="gopher://gopher.example.com/resource">link</a>';
309 $this->assertSame($text, purify_html($text));
311 $text = '<a href="mms://www.example.com/movie.mms">link</a>';
312 $this->assertSame($text, purify_html($text));
314 // Now some borked or dangerous schemes.
315 $text = '<a href="javascript://www.example.com">link</a>';
316 $this->assertSame('<a>link</a>', purify_html($text));
318 $text = '<a href="hmmm://www.example.com">link</a>';
319 $this->assertSame('<a>link</a>', purify_html($text));
323 * Test non-ascii domain names
325 public function test_idn() {
327 // Example of domain that gives the same result in IDNA2003 and IDNA2008 .
328 $text = '<a href="http://правительство.рф">правительство.рф</a>';
329 $expected = '<a href="http://xn--80aealotwbjpid2k.xn--p1ai">правительство.рф</a>';
330 $this->assertSame($expected, purify_html($text));
332 // Examples of deviations from http://www.unicode.org/reports/tr46/#Table_Deviation_Characters .
333 $text = '<a href="http://teßt.de">teßt.de</a>';
334 $expected = '<a href="http://xn--tet-6ka.de">teßt.de</a>';
335 $this->assertSame($expected, purify_html($text));
337 $text = '<a href="http://βόλος.com">http://βόλος.com</a>';
338 $expected = '<a href="http://xn--nxasmm1c.com">http://βόλος.com</a>';
339 $this->assertSame($expected, purify_html($text));
341 $text = '<a href="http://نامه‌ای.com">http://نامه‌ای.com</a>';
342 $expected = '<a href="http://xn--mgba3gch31f060k.com">http://نامه‌ای.com</a>';
343 $this->assertSame($expected, purify_html($text));
347 * Tests media tags.
349 * @dataProvider media_tags_provider
350 * @param string $mediatag HTML media tag
351 * @param string $expected expected result
353 public function test_media_tags($mediatag, $expected) {
354 $actual = format_text($mediatag, FORMAT_MOODLE, ['filter' => false]);
355 $this->assertEquals($expected, $actual);
359 * Test cases for the test_media_tags test.
361 public function media_tags_provider() {
362 // Takes an array of attributes, then generates a test for each of them.
363 $generatetestcases = function($prefix, array $attrs, array $templates) {
364 return array_reduce($attrs, function($carry, $attr) use ($prefix, $templates) {
365 $testcase = [$prefix . '/' . $attr => [
366 sprintf($templates[0], $attr),
367 sprintf($templates[1], $attr)
369 return empty(array_values($carry)[0]) ? $testcase : $carry + $testcase;
370 }, [[]]);
373 $audioattrs = [
374 'preload="auto"', 'autoplay=""', 'loop=""', 'muted=""', 'controls=""',
375 'crossorigin="anonymous"', 'crossorigin="use-credentials"'
377 $videoattrs = [
378 'crossorigin="anonymous"', 'crossorigin="use-credentials"',
379 'poster="https://upload.wikimedia.org/wikipedia/en/1/14/Space_jam.jpg"',
380 'preload="auto"', 'autoplay=""', 'playsinline=""', 'loop=""', 'muted=""',
381 'controls=""', 'width="420"', 'height="69"'
383 return $generatetestcases('Plain audio', $audioattrs + ['src="http://example.com/jam.wav"'], [
384 '<audio %1$s>Looks like you can\'t slam the jams.</audio>',
385 '<div class="text_to_html"><audio %1$s>Looks like you can\'t slam the jams.</audio></div>'
386 ]) + $generatetestcases('Audio with one source', $audioattrs, [
387 '<audio %1$s><source src="http://example.com/getup.wav">No tasty jams for you.</audio>',
388 '<div class="text_to_html">' .
389 '<audio %1$s>' .
390 '<source src="http://example.com/getup.wav" />' .
391 'No tasty jams for you.' .
392 '</audio>' .
393 '</div>'
394 ]) + $generatetestcases('Audio with multiple sources', $audioattrs, [
395 '<audio %1$s>' .
396 '<source src="http://example.com/getup.wav" type="audio/wav">' .
397 '<source src="http://example.com/getup.mp3" type="audio/mpeg">' .
398 '<source src="http://example.com/getup.ogg" type="audio/ogg">' .
399 'No tasty jams for you.' .
400 '</audio>',
401 '<div class="text_to_html">' .
402 '<audio %1$s>' .
403 '<source src="http://example.com/getup.wav" type="audio/wav" />' .
404 '<source src="http://example.com/getup.mp3" type="audio/mpeg" />' .
405 '<source src="http://example.com/getup.ogg" type="audio/ogg" />' .
406 'No tasty jams for you.' .
407 '</audio>' .
408 '</div>'
409 ]) + $generatetestcases('Audio with sources and tracks', $audioattrs, [
410 '<audio %1$s>' .
411 '<source src="http://example.com/getup.wav" type="audio/wav">' .
412 '<track kind="subtitles" src="http://example.com/subtitles_en.vtt" label="English" srclang="en">' .
413 '<track kind="subtitles" src="http://example.com/subtitles_es.vtt" label="Espanol" srclang="es">' .
414 'No tasty jams for you.' .
415 '</audio>',
416 '<div class="text_to_html">' .
417 '<audio %1$s>' .
418 '<source src="http://example.com/getup.wav" type="audio/wav" />' .
419 '<track kind="subtitles" src="http://example.com/subtitles_en.vtt" label="English" srclang="en" />' .
420 '<track kind="subtitles" src="http://example.com/subtitles_es.vtt" label="Espanol" srclang="es" />' .
421 'No tasty jams for you.' .
422 '</audio>' .
423 '</div>'
424 ]) + $generatetestcases('Plain video', $videoattrs + ['src="http://example.com/prettygood.mp4'], [
425 '<video %1$s>Oh, that\'s pretty bad 😦</video>',
426 '<div class="text_to_html"><video %1$s>Oh, that\'s pretty bad 😦</video></div>'
427 ]) + $generatetestcases('Video with illegal subtag', $videoattrs + ['src="http://example.com/prettygood.mp4'], [
428 '<video %1$s><subtag></subtag>Oh, that\'s pretty bad 😦</video>',
429 '<div class="text_to_html"><video %1$s>Oh, that\'s pretty bad 😦</video></div>'
430 ]) + $generatetestcases('Video with legal subtag', $videoattrs + ['src="http://example.com/prettygood.mp4'], [
431 '<video %1$s>Did not work <a href="http://example.com/prettygood.mp4">click here to download</a></video>',
432 '<div class="text_to_html"><video %1$s>Did not work <a href="http://example.com/prettygood.mp4">' .
433 'click here to download</a></video></div>'
434 ]) + $generatetestcases('Source tag without video or audio', $videoattrs, [
435 'some text <source src="http://example.com/getup.wav" type="audio/wav"> the end',
436 '<div class="text_to_html">some text the end</div>'
437 ]) + $generatetestcases('Video with one source', $videoattrs, [
438 '<video %1$s><source src="http://example.com/prettygood.mp4">Oh, that\'s pretty bad 😦</video>',
439 '<div class="text_to_html">' .
440 '<video %1$s>' .
441 '<source src="http://example.com/prettygood.mp4" />' .
442 'Oh, that\'s pretty bad 😦' .
443 '</video>' .
444 '</div>'
445 ]) + $generatetestcases('Video with multiple sources', $videoattrs, [
446 '<video %1$s>' .
447 '<source src="http://example.com/prettygood.mp4" type="video/mp4">' .
448 '<source src="http://example.com/eljefe.mp4" type="video/mp4">' .
449 '<source src="http://example.com/turnitup.mov" type="video/mov">' .
450 'Oh, that\'s pretty bad 😦' .
451 '</video>',
452 '<div class="text_to_html">' .
453 '<video %1$s>' .
454 '<source src="http://example.com/prettygood.mp4" type="video/mp4" />' .
455 '<source src="http://example.com/eljefe.mp4" type="video/mp4" />' .
456 '<source src="http://example.com/turnitup.mov" type="video/mov" />' .
457 'Oh, that\'s pretty bad 😦' .
458 '</video>' .
459 '</div>'
460 ]) + $generatetestcases('Video with sources and tracks', $audioattrs, [
461 '<video %1$s>' .
462 '<source src="http://example.com/getup.wav" type="audio/wav">' .
463 '<track kind="subtitles" src="http://example.com/subtitles_en.vtt" label="English" srclang="en">' .
464 '<track kind="subtitles" src="http://example.com/subtitles_es.vtt" label="Espanol" srclang="es">' .
465 'No tasty jams for you.' .
466 '</video>',
467 '<div class="text_to_html">' .
468 '<video %1$s>' .
469 '<source src="http://example.com/getup.wav" type="audio/wav" />' .
470 '<track kind="subtitles" src="http://example.com/subtitles_en.vtt" label="English" srclang="en" />' .
471 '<track kind="subtitles" src="http://example.com/subtitles_es.vtt" label="Espanol" srclang="es" />' .
472 'No tasty jams for you.' .
473 '</video>' .
474 '</div>'
475 ]) + ['Video with invalid crossorigin' => [
476 '<video src="http://example.com/turnitup.mov" crossorigin="can i pls hab?">' .
477 'Oh, that\'s pretty bad 😦' .
478 '</video>',
479 '<div class="text_to_html">' .
480 '<video src="http://example.com/turnitup.mov">' .
481 'Oh, that\'s pretty bad 😦' .
482 '</video>' .
483 '</div>'
484 ]] + ['Audio with invalid crossorigin' => [
485 '<audio src="http://example.com/getup.wav" crossorigin="give me. the jams.">' .
486 'nyemnyemnyem' .
487 '</audio>',
488 '<div class="text_to_html">' .
489 '<audio src="http://example.com/getup.wav">' .
490 'nyemnyemnyem' .
491 '</audio>' .
492 '</div>'
493 ]] + ['Other attributes' => [
494 '<video src="http://example.com/turnitdown.mov" class="nofilter" data-something="data attribute" someattribute="somevalue" onclick="boom">' .
495 '<source src="http://example.com/getup.wav" type="audio/wav" class="shouldberemoved" data-sourcedata="source data" onmouseover="kill session" />' .
496 '<track src="http://example.com/subtitles_en.vtt" class="shouldberemoved" data-trackdata="track data" onmouseover="removeme" />' .
497 'Do not remove attribute class but remove other attributes' .
498 '</video>',
499 '<div class="text_to_html">' .
500 '<video src="http://example.com/turnitdown.mov" class="nofilter">' .
501 '<source src="http://example.com/getup.wav" type="audio/wav" />' .
502 '<track src="http://example.com/subtitles_en.vtt" />' .
503 'Do not remove attribute class but remove other attributes' .
504 '</video>' .
505 '</div>'