Aleksander Machniak
2016-05-08 ca9ad75d96f9af0b7ced8b51644bfca4361ea39c
commit | author | age
7ac944 1 <?php
AM 2
3 /**
4  * Test class to test rcube_washtml class
5  *
6  * @package Tests
7  */
8 class Framework_Washtml extends PHPUnit_Framework_TestCase
9 {
10
11     /**
12      * Test the elimination of some XSS vulnerabilities
13      */
14     function test_html_xss3()
15     {
16         // #1488850
17         $html = '<p><a href="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">Firefox</a>'
ca9ad7 18             .'<a href="vbscript:alert(document.cookie)">Internet Explorer</a></p>'
AM 19             .'<p><A href="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">Firefox</a>'
20             .'<A HREF="vbscript:alert(document.cookie)">Internet Explorer</a></p>';
7ac944 21
AM 22         $washer = new rcube_washtml;
23         $washed = $washer->wash($html);
24
25         $this->assertNotRegExp('/data:text/', $washed, "Remove data:text/html links");
26         $this->assertNotRegExp('/vbscript:/', $washed, "Remove vbscript: links");
27     }
28
1f910c 29     /**
AM 30      * Test fixing of invalid href (#1488940)
31      */
32     function test_href()
33     {
34         $html = "<p><a href=\"\nhttp://test.com\n\">Firefox</a>";
35
36         $washer = new rcube_washtml;
37         $washed = $washer->wash($html);
38
39         $this->assertRegExp('|href="http://test.com">|', $washed, "Link href with newlines (#1488940)");
40     }
41
1e2468 42     /**
665236 43      * Test XSS in area's href (#5240)
AM 44      */
45     function test_href_area()
46     {
47         $html = '<p><area href="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">'
48             . '<area href="vbscript:alert(document.cookie)">Internet Explorer</p>'
ca9ad7 49             . '<area href="javascript:alert(document.domain)" shape=default>'
AM 50             . '<p><AREA HREF="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">'
51             . '<Area href="vbscript:alert(document.cookie)">Internet Explorer</p>'
52             . '<area HREF="javascript:alert(document.domain)" shape=default>';
665236 53
AM 54         $washer = new rcube_washtml;
55         $washed = $washer->wash($html);
56
57         $this->assertNotRegExp('/data:text/', $washed, "data:text/html in area href");
58         $this->assertNotRegExp('/vbscript:/', $washed, "vbscript: in area href");
59         $this->assertNotRegExp('/javascript:/', $washed, "javascript: in area href");
60     }
61
62     /**
1e2468 63      * Test handling HTML comments
AM 64      */
65     function test_comments()
66     {
67         $washer = new rcube_washtml;
68
69         $html   = "<!--[if gte mso 10]><p>p1</p><!--><p>p2</p>";
70         $washed = $washer->wash($html);
71
923490 72         $this->assertEquals('<!-- html ignored --><!-- body ignored --><p>p2</p>', $washed, "HTML conditional comments (#1489004)");
1e2468 73
AM 74         $html   = "<!--TestCommentInvalid><p>test</p>";
75         $washed = $washer->wash($html);
76
77         $this->assertEquals('<!-- html ignored --><!-- body ignored --><p>test</p>', $washed, "HTML invalid comments (#1487759)");
82ed25 78
AM 79         $html   = "<p>para1</p><!-- comment --><p>para2</p>";
80         $washed = $washer->wash($html);
81
923490 82         $this->assertEquals('<!-- html ignored --><!-- body ignored --><p>para1</p><p>para2</p>', $washed, "HTML comments - simple comment");
82ed25 83
AM 84         $html   = "<p>para1</p><!-- <hr> comment --><p>para2</p>";
85         $washed = $washer->wash($html);
86
923490 87         $this->assertEquals('<!-- html ignored --><!-- body ignored --><p>para1</p><p>para2</p>', $washed, "HTML comments - tags inside (#1489904)");
1e2468 88     }
AM 89
cb3e2f 90     /**
AM 91      * Test fixing of invalid self-closing elements (#1489137)
92      */
93     function test_self_closing()
94     {
95         $html = "<textarea>test";
96
97         $washer = new rcube_washtml;
98         $washed = $washer->wash($html);
99
100         $this->assertRegExp('|<textarea>test</textarea>|', $washed, "Self-closing textarea (#1489137)");
101     }
102
ffec85 103     /**
AM 104      * Test fixing of invalid closing tags (#1489446)
105      */
106     function test_closing_tag_attrs()
107     {
108         $html = "<a href=\"http://test.com\">test</a href>";
109
110         $washer = new rcube_washtml;
111         $washed = $washer->wash($html);
112
113         $this->assertRegExp('|</a>|', $washed, "Invalid closing tag (#1489446)");
114     }
115
c72507 116     /**
AM 117      * Test fixing of invalid lists nesting (#1488768)
118      */
119     function test_lists()
120     {
121         $data = array(
122             array(
123                 "<ol><li>First</li><li>Second</li><ul><li>First sub</li></ul><li>Third</li></ol>",
124                 "<ol><li>First</li><li>Second<ul><li>First sub</li></ul></li><li>Third</li></ol>"
125             ),
126             array(
127                 "<ol><li>First<ul><li>First sub</li></ul></li></ol>",
128                 "<ol><li>First<ul><li>First sub</li></ul></li></ol>",
129             ),
130             array(
131                 "<ol><li>First<ol><li>First sub</li></ol></li></ol>",
132                 "<ol><li>First<ol><li>First sub</li></ol></li></ol>",
133             ),
134             array(
135                 "<ul><li>First</li><ul><li>First sub</li><ul><li>sub sub</li></ul></ul><li></li></ul>",
136                 "<ul><li>First<ul><li>First sub<ul><li>sub sub</li></ul></li></ul></li><li></li></ul>",
137             ),
138             array(
139                 "<ul><li>First</li><li>second</li><ul><ul><li>sub sub</li></ul></ul></ul>",
140                 "<ul><li>First</li><li>second<ul><ul><li>sub sub</li></ul></ul></li></ul>",
141             ),
142             array(
143                 "<ol><ol><ol></ol></ol></ol>",
144                 "<ol><ol><ol></ol></ol></ol>",
145             ),
146             array(
147                 "<div><ol><ol><ol></ol></ol></ol></div>",
148                 "<div><ol><ol><ol></ol></ol></ol></div>",
149             ),
150         );
151
152         foreach ($data as $element) {
153             rcube_washtml::fix_broken_lists($element[0]);
154
155             $this->assertSame($element[1], $element[0], "Broken nested lists (#1488768)");
156         }
157     }
158
68cf8f 159     /**
AM 160      * Test color style handling (#1489697)
161      */
162     function test_color_style()
163     {
164         $html = "<p style=\"font-size: 10px; color: rgb(241, 245, 218)\">a</p>";
165
166         $washer = new rcube_washtml;
167         $washed = $washer->wash($html);
168
169         $this->assertRegExp('|color: rgb\(241, 245, 218\)|', $washed, "Color style (#1489697)");
170         $this->assertRegExp('|font-size: 10px|', $washed, "Font-size style");
171     }
172
5e3ee8 173     /**
AM 174      * Test handling of unicode chars in style (#1489777)
175      */
176     function test_style_unicode()
177     {
178         $html = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
f96fec 179             <body><span style='font-family:\"新細明體\",\"serif\";color:red'>test</span></body></html>";
5e3ee8 180
AM 181         $washer = new rcube_washtml;
182         $washed = $washer->wash($html);
183
786aa0 184         $this->assertRegExp('|style="font-family: \&quot;新細明體\&quot;,\&quot;serif\&quot;; color: red"|', $washed, "Unicode chars in style attribute - quoted (#1489697)");
f96fec 185
AM 186         $html = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
187             <body><span style='font-family:新細明體;color:red'>test</span></body></html>";
188
189         $washer = new rcube_washtml;
190         $washed = $washer->wash($html);
191
192         $this->assertRegExp('|style="font-family: 新細明體; color: red"|', $washed, "Unicode chars in style attribute (#1489697)");
5e3ee8 193     }
5bf83d 194
AM 195     /**
196      * Test style item fixes
197      */
198     function test_style_wash()
199     {
200         $html = "<p style=\"line-height: 1; height: 10\">a</p>";
201
202         $washer = new rcube_washtml;
203         $washed = $washer->wash($html);
204
205         $this->assertRegExp('|line-height: 1;|', $washed, "Untouched line-height (#1489917)");
206         $this->assertRegExp('|; height: 10px|', $washed, "Fixed height units");
f4c512 207
AM 208         $html     = "<div style=\"padding: 0px\n   20px;border:1px solid #000;\"></div>";
209         $expected = "<div style=\"padding: 0px 20px; border: 1px solid #000\"></div>";
210
211         $washer = new rcube_washtml;
212         $washed = $washer->wash($html);
213
214         $this->assertTrue(strpos($washed, $expected) !== false, "White-space and new-line characters handling");
5bf83d 215     }
786aa0 216
AM 217     /**
218      * Test invalid style cleanup - XSS prevention (#1490227)
219      */
220     function test_style_wash_xss()
221     {
222         $html = "<img style=aaa:'\"/onerror=alert(1)//'>";
223         $exp  = "<img style=\"aaa: '&quot;/onerror=alert(1)//'\" />";
224
225         $washer = new rcube_washtml;
226         $washed = $washer->wash($html);
227
228         $this->assertTrue(strpos($washed, $exp) !== false, "Style quotes XSS issue (#1490227)");
229
230         $html = "<img style=aaa:'&quot;/onerror=alert(1)//'>";
231         $exp  = "<img style=\"aaa: '&quot;/onerror=alert(1)//'\" />";
232
233         $washer = new rcube_washtml;
234         $washed = $washer->wash($html);
235
236         $this->assertTrue(strpos($washed, $exp) !== false, "Style quotes XSS issue (#1490227)");
237     }
ed1d21 238
AM 239     /**
240      * Test SVG cleanup
241      */
242     function test_style_wash_svg()
243     {
244         $svg = '<?xml version="1.0" standalone="no"?>
245 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
246 <svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" viewBox="0 0 100 100">
247   <polygon id="triangle" points="0,0 0,50 50,0" fill="#009900" stroke="#004400" onmouseover="alert(1)" />
248   <text x="50" y="68" font-size="48" fill="#FFF" text-anchor="middle"><![CDATA[410]]></text>
249   <script type="text/javascript">
250     alert(document.cookie);
251   </script>
252   <text x="10" y="25" >An example text</text>
253   <a xlink:href="http://www.w.pl"><rect width="100%" height="100%" /></a>
254   <foreignObject xlink:href="data:text/xml,%3Cscript xmlns=\'http://www.w3.org/1999/xhtml\'%3Ealert(1)%3C/script%3E"/>
255   <set attributeName="onmouseover" to="alert(1)"/>
256   <animate attributeName="onunload" to="alert(1)"/>
257   <animate attributeName="xlink:href" begin="0" from="javascript:alert(1)" />
258 </svg>';
259
260         $exp = '<svg xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" viewBox="0 0 100 100">
261   <polygon id="triangle" points="0,0 0,50 50,0" fill="#009900" stroke="#004400" x-washed="onmouseover" />
262   <text x="50" y="68" font-size="48" fill="#FFF" text-anchor="middle">410</text>
263   <!-- script not allowed -->
264   <text x="10" y="25">An example text</text>
265   <a xlink:href="http://www.w.pl"><rect width="100%" height="100%" /></a>
266   <!-- foreignObject ignored -->
267   <set attributeName="onmouseover" x-washed="to" />
268   <animate attributeName="onunload" x-washed="to" />
269   <animate attributeName="xlink:href" begin="0" x-washed="from" />
270 </svg>';
271
272         $washer = new rcube_washtml;
273         $washed = $washer->wash($svg);
274
275         $this->assertSame($washed, $exp, "SVG content");
276     }
7ac944 277 }