/', '', $html); } /** * Test the elimination of some XSS vulnerabilities */ public function test_html_xss() { // #1488850 $html = 'Firefox' . 'Internet Explorer

' . 'Firefox' . 'Internet Explorer' . 'CLICK ME'; // #6896 $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertDoesNotMatchRegularExpression('/data:text/', $washed, 'Remove data:text/html links'); $this->assertDoesNotMatchRegularExpression('/vbscript:/', $washed, 'Remove vbscript: links'); $this->assertDoesNotMatchRegularExpression('/data:application/', $washed, 'Remove data:application links'); } /** * Test fixing of invalid href */ public function test_href() { $html = "

FirefoxFirefox"; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression('|href="http://test\.com"|', $washed, 'Link href with newlines (#1488940)'); $this->assertMatchesRegularExpression('|href="http://domain\.com"|', $washed, 'Link href with no protocol (#7454)'); } /** * Test data:image with newlines (#8613) */ public function test_data_image_with_newline() { $html = "

"; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertSame("

", $this->cleanupResult($washed)); } /** * Test XSS in area's href (#5240) */ public function test_href_area() { $html = '

' . 'Internet Explorer

' . '' . '

' . 'Internet Explorer

' . ''; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertDoesNotMatchRegularExpression('/data:text/', $washed, 'data:text/html in area href'); $this->assertDoesNotMatchRegularExpression('/vbscript:/', $washed, 'vbscript: in area href'); $this->assertDoesNotMatchRegularExpression('/javascript:/', $washed, 'javascript: in area href'); } /** * Test removing of object tag, but keeping innocent children */ public function test_object() { $html = "
\n\n" . "\n" . '

This alternative text should survive

' . "
\n
"; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertDoesNotMatchRegularExpression('/<\/?object/', $washed, 'Remove object tag'); $this->assertDoesNotMatchRegularExpression('/assertMatchesRegularExpression('/

/', $washed, 'Keep embedded tags'); } /** * Test handling HTML comments */ public function test_comments() { $washer = new \rcube_washtml(); $html = '

p2

'; $washed = $this->cleanupResult($washer->wash($html)); $this->assertSame('

p2

', $washed, 'HTML conditional comments (#1489004)'); $html = '

para2

'; $washed = $this->cleanupResult($washer->wash($html)); $this->assertSame('

para1

para2

', $washed, 'HTML comments - simple comment'); $html = '

para1

para2

'; $washed = $this->cleanupResult($washer->wash($html)); $this->assertSame('

para1

para2

', $washed, 'HTML comments - tags inside (#1489904)'); $html = '

para1

para2

'; $washed = $this->cleanupResult($washer->wash($html)); $this->assertSame('

para1

para2

', $washed, 'HTML comments - bracket inside'); $html = "

\n2\n4

"; $washed = $this->cleanupResult($washer->wash($html)); $this->assertSame("

\n2\n4

", $washed, 'HTML comments (#6464)'); } /** * Test fixing of invalid self-closing elements (#1489137) */ public function test_self_closing() { $html = '|', $washed); } /** * Test fixing of invalid closing tags (#1489446) */ public function test_closing_tag_attrs() { $html = 'test'; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression('||', $washed); } /** * Test fixing of invalid lists nesting (#1488768) */ public function test_lists() { $data = [ [ '
  1. First
  2. Second
  3. Third
', '
  1. First
  2. Second
  3. Third
', ], [ '
  1. First
', '
  1. First
', ], [ '
  1. First
    1. First sub
', '
  1. First
    1. First sub
', ], [ '', '', ], [ '', '', ], [ '
    ', '
      ', ], [ '
        ', '
          ', ], ]; foreach ($data as $element) { \rcube_washtml::fix_broken_lists($element[0]); $this->assertSame($element[1], $element[0], 'Broken nested lists (#1488768)'); } } /** * Test color style handling (#1489697) */ public function test_color_style() { $html = '

          a

          '; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression('|color: rgb\(241, 245, 218\)|', $washed, 'Color style (#1489697)'); $this->assertMatchesRegularExpression('|font-size: 10px|', $washed, 'Font-size style'); } /** * Test handling of unicode chars in style (#1489777) */ public function test_style_unicode() { $html = " test"; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression( '|style="font-family: \"新細明體\",\"serif\"; color: red"|', $washed, 'Unicode chars in style attribute - quoted (#1489697)' ); $html = " test"; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression( '|style="font-family: 新細明體; color: red"|', $washed, 'Unicode chars in style attribute (#1489697)' ); } /** * Test deprecated body attributes (#7109) */ public function test_style_body_attrs() { $html = ' '; $washer = new \rcube_washtml(['html_elements' => ['body']]); $washed = $washer->wash($html); $this->assertMatchesRegularExpression('|bgcolor="#fff"|', $washed, 'Body bgcolor attribute'); $this->assertMatchesRegularExpression('|text="#000"|', $washed, 'Body text attribute'); $this->assertMatchesRegularExpression('|background="#test"|', $washed, 'Body background attribute'); $this->assertMatchesRegularExpression('|link="#111"|', $washed, 'Body link attribute'); $this->assertMatchesRegularExpression('|alink="#222"|', $washed, 'Body alink attribute'); $this->assertMatchesRegularExpression('|vlink="#333"|', $washed, 'Body vlink attribute'); } /** * Test style item fixes */ public function test_style_wash() { $html = '

          a

          '; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertMatchesRegularExpression('|line-height: 1;|', $washed, 'Untouched line-height (#1489917)'); $this->assertMatchesRegularExpression('|; height: 10px|', $washed, 'Fixed height units'); $html = "
          "; $expected = '
          '; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertSame($this->cleanupResult($washed), $expected, 'White-space and new-line characters handling'); } /** * Test invalid style cleanup - XSS prevention (#1490227) */ public function test_style_wash_xss() { $html = ""; $exp = ""; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertTrue(strpos($washed, $exp) !== false, 'Style quotes XSS issue (#1490227)'); $html = ""; $exp = ""; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertTrue(strpos($washed, $exp) !== false, 'Style quotes XSS issue (#1490227)'); } /** * Test handling of title tag */ public function test_title() { $washer = new \rcube_washtml(); $html = 'title1

          test

          '; $washed = $washer->wash($html); $this->assertSame('

          test

          ', $this->cleanupResult($washed)); $html = 'title1<img />title2

          test

          '; $washed = $washer->wash($html); $this->assertSame('

          test

          ', $this->cleanupResult($washed)); } /** * Test SVG cleanup */ public function test_wash_svg() { $svg = ' 410 An example text '; $exp = ' 410 An example text '; $washer = new \rcube_washtml(); $washed = $washer->wash($svg); $this->assertSame($washed, $exp, 'SVG content'); } /** * Test SVG cleanup * * @dataProvider provide_wash_svg_tests_cases */ #[DataProvider('provide_wash_svg_tests_cases')] public function test_wash_svg_tests($input, $expected) { $washer = new \rcube_washtml(); $washed = $washer->wash($input); $this->assertSame($expected, $this->cleanupResult($washed), 'SVG content'); } /** * Test cases for SVG tests */ public static function provide_wash_svg_tests_cases(): iterable { $svg1 = ""; return [ [ '', '', ], [ 'Hello victim!', 'Hello victim!', ], [ '

          Hello victim!

          ', '

          Hello victim!

          ', ], [ '

          Hello victim!

          ', '

          Hello victim!

          ', ], [ '', '', ], [ '', '', ], [ 'XSS', 'XSS', ], [ 'XSS', 'XSS', ], [ '' . 'XSS', 'XSS', ], [ '' . 'XSS', 'XSS', ], [ '' . 'XSS', 'XSS', ], [ '' . 'XSS', 'XSS', ], [ '' . 'XSS', 'XSS', ], [ "", '', ], [ '', '', ], [ 'XSS', 'XSS', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], ]; } /** * Test various XSS issues * * @dataProvider provide_wash_xss_tests_cases */ #[DataProvider('provide_wash_xss_tests_cases')] public function test_wash_xss_tests($input, $expected) { $washer = new \rcube_washtml(['allow_remote' => true, 'html_elements' => ['body']]); $washed = $washer->wash($input); $this->assertSame($expected, $this->cleanupResult($washed), 'XSS issues'); } /** * Test cases for various XSS issues */ public static function provide_wash_xss_tests_cases(): iterable { return [ [ 'test', 'test', ], [ 'blah', 'blah', ], [ 'XSS', 'XSS', ], [ 'XSS', 'XSS', ], [ 'XSS', 'XSS', ], [ '', '', ], [ '', ], [ 'clickme', 'clickme', ], [ 'clickme', 'clickme', ], [ 'clickme', 'clickme', ], [ 'clickme', 'clickme', ], ]; } /** * Test position:fixed cleanup - (#5264) */ public function test_style_wash_position_fixed() { $html = ""; $exp = ''; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertTrue(strpos($washed, $exp) !== false, 'Position:fixed (#5264)'); } /** * Test MathML cleanup */ public function test_wash_mathml() { $mathml = ' ID = 12 kn WL ( VGS -Vt )2 I_D = \frac{1}{2} k_n \frac{W}{L} (V_{GS}-V_t)^2 '; $exp = ' ID = 12 kn WL ( VGS -Vt )2 I_D = \frac{1}{2} k_n \frac{W}{L} (V_{GS}-V_t)^2 '; $washer = new \rcube_washtml(); $washed = $washer->wash($mathml); // remove whitespace between tags $washed = preg_replace('/>[\s\r\n\t]+<', $washed); $exp = preg_replace('/>[\s\r\n\t]+<', $exp); $this->assertSame(trim($washed), trim($exp), 'MathML content'); } /** * Test external links in src of input/video elements (#5583) */ public function test_src_wash() { $html = ''; $washer = new \rcube_washtml(); $washed = $washer->wash($html); $this->assertTrue($washer->extlinks); $this->assertStringNotContainsString('TRACKING', $washed, 'Src attribute of tag (#5583)'); $html = '