Aleksander Machniak
2013-06-11 174327c25cd68f203e0985df51b94765738c7dce
commit | author | age
e82492 1 <?php
AM 2
3 /**
4  * Test class to test steps/mail/func.inc functions
5  *
6  * @package Tests
7  */
8 class MailFunc extends PHPUnit_Framework_TestCase
9 {
10
afa0b1 11     function setUp()
e82492 12     {
AM 13         // simulate environment to successfully include func.inc
14         $GLOBALS['RCMAIL'] = $RCMAIL = rcmail::get_instance();
15         $GLOBALS['OUTPUT'] = $OUTPUT = $RCMAIL->load_gui();
16         $RCMAIL->action = 'autocomplete';
17         $RCMAIL->storage_init(false);
18
19         require_once INSTALL_PATH . 'program/steps/mail/func.inc';
20
21         $GLOBALS['EMAIL_ADDRESS_PATTERN'] = $EMAIL_ADDRESS_PATTERN;
22     }
23
24     /**
25      * Helper method to create a HTML message part object
26      */
27     function get_html_part($body)
28     {
29         $part = new rcube_message_part;
30         $part->ctype_primary = 'text';
31         $part->ctype_secondary = 'html';
32         $part->body = file_get_contents(TESTS_DIR . $body);
33         $part->replaces = array();
34         return $part;
35     }
36
37
38     /**
39      * Test sanitization of a "normal" html message
40      */
41     function test_html()
42     {
43         $part = $this->get_html_part('src/htmlbody.txt');
44         $part->replaces = array('ex1.jpg' => 'part_1.2.jpg', 'ex2.jpg' => 'part_1.2.jpg');
45
46         // render HTML in normal mode
47         $html = rcmail_html4inline(rcmail_print_body($part, array('safe' => false)), 'foo');
48
49         $this->assertRegExp('/src="'.$part->replaces['ex1.jpg'].'"/', $html, "Replace reference to inline image");
50         $this->assertRegExp('#background="./program/resources/blocked.gif"#', $html, "Replace external background image");
51         $this->assertNotRegExp('/ex3.jpg/', $html, "No references to external images");
52         $this->assertNotRegExp('/<meta [^>]+>/', $html, "No meta tags allowed");
53         //$this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
54         $this->assertNotRegExp('/<form [^>]+>/', $html, "No form tags allowed");
55         $this->assertRegExp('/Subscription form/', $html, "Include <form> contents");
56         $this->assertRegExp('/<!-- link ignored -->/', $html, "No external links allowed");
1e3254 57         $this->assertRegExp('/<a[^>]+ target="_blank"/', $html, "Set target to _blank");
e82492 58         $this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
AM 59
60         // render HTML in safe mode
61         $html2 = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'foo');
62
63         $this->assertRegExp('/<style [^>]+>/', $html2, "Allow styles in safe mode");
64         $this->assertRegExp('#src="http://evilsite.net/mailings/ex3.jpg"#', $html2, "Allow external images in HTML (safe mode)");
65         $this->assertRegExp("#url\('?http://evilsite.net/newsletter/image/bg/bg-64.jpg'?\)#", $html2, "Allow external images in CSS (safe mode)");
66         $css = '<link rel="stylesheet" .+_u=tmp-[a-z0-9]+\.css.+_action=modcss';
67         $this->assertRegExp('#'.$css.'#Ui', $html2, "Filter (anonymized) external styleseehts with utils/modcss.inc");
68     }
69
70     /**
71      * Test the elimination of some trivial XSS vulnerabilities
72      */
73     function test_html_xss()
74     {
75         $part = $this->get_html_part('src/htmlxss.txt');
76         $washed = rcmail_print_body($part, array('safe' => true));
77
78         $this->assertNotRegExp('/src="skins/', $washed, "Remove local references");
79         $this->assertNotRegExp('/\son[a-z]+/', $washed, "Remove on* attributes");
80
81         $html = rcmail_html4inline($washed, 'foo');
82         $this->assertNotRegExp('/onclick="return rcmail.command(\'compose\',\'xss@somehost.net\',this)"/', $html, "Clean mailto links");
83         $this->assertNotRegExp('/alert/', $html, "Remove alerts");
84     }
85
86     /**
87      * Test HTML sanitization to fix the CSS Expression Input Validation Vulnerability
88      * reported at http://www.securityfocus.com/bid/26800/
89      */
90     function test_html_xss2()
91     {
92         $part = $this->get_html_part('src/BID-26800.txt');
93         $washed = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'dabody', '', $attr, true);
94
95         $this->assertNotRegExp('/alert|expression|javascript|xss/', $washed, "Remove evil style blocks");
96         $this->assertNotRegExp('/font-style:italic/', $washed, "Allow valid styles");
97     }
98
99     /**
74cd0a 100      * Test the elimination of some XSS vulnerabilities
AM 101      */
102     function test_html_xss3()
103     {
104         // #1488850
105         $html = '<p><a href="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">Firefox</a>'
106             .'<a href="vbscript:alert(document.cookie)">Internet Explorer</a></p>';
107         $washed = rcmail_wash_html($html, array('safe' => true), array());
108
109         $this->assertNotRegExp('/data:text/', $washed, "Remove data:text/html links");
110         $this->assertNotRegExp('/vbscript:/', $washed, "Remove vbscript: links");
111     }
112
113     /**
e82492 114      * Test washtml class on non-unicode characters (#1487813)
AM 115      */
116     function test_washtml_utf8()
117     {
118         $part = $this->get_html_part('src/invalidchars.html');
119         $washed = rcmail_print_body($part);
120
121         $this->assertRegExp('/<p>символ<\/p>/', $washed, "Remove non-unicode characters from HTML message body");
122     }
123
124     /**
125      * Test links pattern replacements in plaintext messages
126      */
127     function test_plaintext()
128     {
129         $part = new rcube_message_part;
130         $part->ctype_primary = 'text';
131         $part->ctype_secondary = 'plain';
132         $part->body = quoted_printable_decode(file_get_contents(TESTS_DIR . 'src/plainbody.txt'));
133         $html = rcmail_print_body($part, array('safe' => true));
134
135         $this->assertRegExp('/<a href="mailto:nobody@roundcube.net" onclick="return rcmail.command\(\'compose\',\'nobody@roundcube.net\',this\)">nobody@roundcube.net<\/a>/', $html, "Mailto links with onclick");
1e3254 136         $this->assertRegExp('#<a rel="noreferrer" target="_blank" href="http://www.apple.com/legal/privacy">http://www.apple.com/legal/privacy</a>#', $html, "Links with target=_blank");
AM 137         $this->assertRegExp('#\\[<a rel="noreferrer" target="_blank" href="http://example.com/\\?tx\\[a\\]=5">http://example.com/\\?tx\\[a\\]=5</a>\\]#', $html, "Links with square brackets");
e82492 138     }
AM 139
140     /**
141      * Test mailto links in html messages
142      */
143     function test_mailto()
144     {
145         $part = $this->get_html_part('src/mailto.txt');
146
147         // render HTML in normal mode
148         $html = rcmail_html4inline(rcmail_print_body($part, array('safe' => false)), 'foo');
149
150         $mailto = '<a href="mailto:me@me.com?subject=this is the subject&amp;body=this is the body"'
1e3254 151             .' onclick="return rcmail.command(\'compose\',\'me@me.com?subject=this is the subject&amp;body=this is the body\',this)" rel="noreferrer">e-mail</a>';
e82492 152
AM 153         $this->assertRegExp('|'.preg_quote($mailto, '|').'|', $html, "Extended mailto links");
154     }
155
156     /**
157      * Test the elimination of HTML comments
158      */
159     function test_html_comments()
160     {
161         $part = $this->get_html_part('src/htmlcom.txt');
162         $washed = rcmail_print_body($part, array('safe' => true));
163
164         // #1487759
165         $this->assertRegExp('|<p>test1</p>|', $washed, "Buggy HTML comments");
166         // but conditional comments (<!--[if ...) should be removed
167         $this->assertNotRegExp('|<p>test2</p>|', $washed, "Conditional HTML comments");
168     }
169
170     /**
171      * Test URI base resolving in HTML messages
172      */
173     function test_resolve_base()
174     {
175         $html = file_get_contents(TESTS_DIR . 'src/htmlbase.txt');
7ac944 176         $html = rcube_washtml::resolve_base($html);
e82492 177
AM 178         $this->assertRegExp('|src="http://alec\.pl/dir/img1\.gif"|', $html, "URI base resolving [1]");
179         $this->assertRegExp('|src="http://alec\.pl/dir/img2\.gif"|', $html, "URI base resolving [2]");
180         $this->assertRegExp('|src="http://alec\.pl/img3\.gif"|', $html, "URI base resolving [3]");
181
182         // base resolving exceptions
183         $this->assertRegExp('|src="cid:theCID"|', $html, "URI base resolving exception [1]");
184         $this->assertRegExp('|src="http://other\.domain\.tld/img3\.gif"|', $html, "URI base resolving exception [2]");
185     }
186 }