Aleksander Machniak
2016-05-20 e48f8945b32ab5b67f1cdeb53a37d3d196e31e4d
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
22     /**
23      * Helper method to create a HTML message part object
24      */
25     function get_html_part($body)
26     {
27         $part = new rcube_message_part;
28         $part->ctype_primary = 'text';
29         $part->ctype_secondary = 'html';
30         $part->body = file_get_contents(TESTS_DIR . $body);
31         $part->replaces = array();
32         return $part;
33     }
34
35
36     /**
37      * Test sanitization of a "normal" html message
38      */
39     function test_html()
40     {
41         $part = $this->get_html_part('src/htmlbody.txt');
42         $part->replaces = array('ex1.jpg' => 'part_1.2.jpg', 'ex2.jpg' => 'part_1.2.jpg');
43
44         // render HTML in normal mode
48ba44 45         $html = rcmail_html4inline(rcmail_print_body($part->body, $part, array('safe' => false)), 'foo');
e82492 46
AM 47         $this->assertRegExp('/src="'.$part->replaces['ex1.jpg'].'"/', $html, "Replace reference to inline image");
d20481 48         $this->assertRegExp('#background="program/resources/blocked.gif"#', $html, "Replace external background image");
e82492 49         $this->assertNotRegExp('/ex3.jpg/', $html, "No references to external images");
AM 50         $this->assertNotRegExp('/<meta [^>]+>/', $html, "No meta tags allowed");
51         //$this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
52         $this->assertNotRegExp('/<form [^>]+>/', $html, "No form tags allowed");
53         $this->assertRegExp('/Subscription form/', $html, "Include <form> contents");
54         $this->assertRegExp('/<!-- link ignored -->/', $html, "No external links allowed");
1e3254 55         $this->assertRegExp('/<a[^>]+ target="_blank"/', $html, "Set target to _blank");
e82492 56         $this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
AM 57
58         // render HTML in safe mode
48ba44 59         $html2 = rcmail_html4inline(rcmail_print_body($part->body, $part, array('safe' => true)), 'foo');
e82492 60
AM 61         $this->assertRegExp('/<style [^>]+>/', $html2, "Allow styles in safe mode");
62         $this->assertRegExp('#src="http://evilsite.net/mailings/ex3.jpg"#', $html2, "Allow external images in HTML (safe mode)");
63         $this->assertRegExp("#url\('?http://evilsite.net/newsletter/image/bg/bg-64.jpg'?\)#", $html2, "Allow external images in CSS (safe mode)");
e7cd99 64         $css = '<link rel="stylesheet" .+_action=modcss.+_u=tmp-[a-z0-9]+\.css';
e82492 65         $this->assertRegExp('#'.$css.'#Ui', $html2, "Filter (anonymized) external styleseehts with utils/modcss.inc");
AM 66     }
67
68     /**
69      * Test the elimination of some trivial XSS vulnerabilities
70      */
71     function test_html_xss()
72     {
73         $part = $this->get_html_part('src/htmlxss.txt');
48ba44 74         $washed = rcmail_print_body($part->body, $part, array('safe' => true));
e82492 75
AM 76         $this->assertNotRegExp('/src="skins/', $washed, "Remove local references");
77         $this->assertNotRegExp('/\son[a-z]+/', $washed, "Remove on* attributes");
78
79         $html = rcmail_html4inline($washed, 'foo');
80         $this->assertNotRegExp('/onclick="return rcmail.command(\'compose\',\'xss@somehost.net\',this)"/', $html, "Clean mailto links");
81         $this->assertNotRegExp('/alert/', $html, "Remove alerts");
82     }
83
84     /**
85      * Test HTML sanitization to fix the CSS Expression Input Validation Vulnerability
86      * reported at http://www.securityfocus.com/bid/26800/
87      */
88     function test_html_xss2()
89     {
90         $part = $this->get_html_part('src/BID-26800.txt');
48ba44 91         $washed = rcmail_html4inline(rcmail_print_body($part->body, $part, array('safe' => true)), 'dabody', '', $attr, true);
e82492 92
AM 93         $this->assertNotRegExp('/alert|expression|javascript|xss/', $washed, "Remove evil style blocks");
94         $this->assertNotRegExp('/font-style:italic/', $washed, "Allow valid styles");
95     }
96
97     /**
74cd0a 98      * Test the elimination of some XSS vulnerabilities
AM 99      */
100     function test_html_xss3()
101     {
102         // #1488850
103         $html = '<p><a href="data:text/html,&lt;script&gt;alert(document.cookie)&lt;/script&gt;">Firefox</a>'
104             .'<a href="vbscript:alert(document.cookie)">Internet Explorer</a></p>';
105         $washed = rcmail_wash_html($html, array('safe' => true), array());
106
107         $this->assertNotRegExp('/data:text/', $washed, "Remove data:text/html links");
108         $this->assertNotRegExp('/vbscript:/', $washed, "Remove vbscript: links");
109     }
110
111     /**
e82492 112      * Test washtml class on non-unicode characters (#1487813)
AM 113      */
114     function test_washtml_utf8()
115     {
116         $part = $this->get_html_part('src/invalidchars.html');
48ba44 117         $washed = rcmail_print_body($part->body, $part);
e82492 118
c00875 119         $this->assertRegExp('/<p>(символ|симол)<\/p>/', $washed, "Remove non-unicode characters from HTML message body");
e82492 120     }
AM 121
122     /**
123      * Test links pattern replacements in plaintext messages
124      */
125     function test_plaintext()
126     {
127         $part = new rcube_message_part;
128         $part->ctype_primary = 'text';
129         $part->ctype_secondary = 'plain';
130         $part->body = quoted_printable_decode(file_get_contents(TESTS_DIR . 'src/plainbody.txt'));
48ba44 131         $html = rcmail_print_body($part->body, $part, array('safe' => true));
e82492 132
AM 133         $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 134         $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 135         $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 136     }
AM 137
138     /**
139      * Test mailto links in html messages
140      */
141     function test_mailto()
142     {
143         $part = $this->get_html_part('src/mailto.txt');
144
145         // render HTML in normal mode
48ba44 146         $html = rcmail_html4inline(rcmail_print_body($part->body, $part, array('safe' => false)), 'foo');
e82492 147
fed081 148         $mailto = '<a href="mailto:me@me.com"'
1e3254 149             .' 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 150
AM 151         $this->assertRegExp('|'.preg_quote($mailto, '|').'|', $html, "Extended mailto links");
152     }
153
154     /**
155      * Test the elimination of HTML comments
156      */
157     function test_html_comments()
158     {
159         $part = $this->get_html_part('src/htmlcom.txt');
48ba44 160         $washed = rcmail_print_body($part->body, $part, array('safe' => true));
e82492 161
AM 162         // #1487759
163         $this->assertRegExp('|<p>test1</p>|', $washed, "Buggy HTML comments");
164         // but conditional comments (<!--[if ...) should be removed
165         $this->assertNotRegExp('|<p>test2</p>|', $washed, "Conditional HTML comments");
166     }
167
168     /**
169      * Test URI base resolving in HTML messages
170      */
171     function test_resolve_base()
172     {
173         $html = file_get_contents(TESTS_DIR . 'src/htmlbase.txt');
7ac944 174         $html = rcube_washtml::resolve_base($html);
e82492 175
AM 176         $this->assertRegExp('|src="http://alec\.pl/dir/img1\.gif"|', $html, "URI base resolving [1]");
177         $this->assertRegExp('|src="http://alec\.pl/dir/img2\.gif"|', $html, "URI base resolving [2]");
178         $this->assertRegExp('|src="http://alec\.pl/img3\.gif"|', $html, "URI base resolving [3]");
179
180         // base resolving exceptions
181         $this->assertRegExp('|src="cid:theCID"|', $html, "URI base resolving exception [1]");
182         $this->assertRegExp('|src="http://other\.domain\.tld/img3\.gif"|', $html, "URI base resolving exception [2]");
183     }
c20fa4 184
AM 185     /**
186      * Test identities selection using Return-Path header
187      */
188     function test_rcmail_identity_select()
189     {
190         $identities = array(
191             array(
192                 'name' => 'Test',
193                 'email_ascii' => 'addr@domain.tld',
194                 'ident' => 'Test <addr@domain.tld>',
195             ),
196             array(
197                 'name' => 'Test',
198                 'email_ascii' => 'thing@domain.tld',
199                 'ident' => 'Test <thing@domain.tld>',
200             ),
201             array(
202                 'name' => 'Test',
203                 'email_ascii' => 'other@domain.tld',
204                 'ident' => 'Test <other@domain.tld>',
205             ),
206         );
207
208         $message = new stdClass;
209         $message->headers = new rcube_message_header;
210         $message->headers->set('Return-Path', '<some_thing@domain.tld>');
211         $res = rcmail_identity_select($message, $identities);
212
213         $this->assertSame($identities[0], $res);
214
215         $message->headers->set('Return-Path', '<thing@domain.tld>');
216         $res = rcmail_identity_select($message, $identities);
217
218         $this->assertSame($identities[1], $res);
219     }
a8b004 220
AM 221     /**
222      * Test identities selection (#1489378)
223      */
224     function test_rcmail_identity_select2()
225     {
226         $identities = array(
227             array(
228                 'name' => 'Test 1',
229                 'email_ascii' => 'addr1@domain.tld',
230                 'ident' => 'Test 1 <addr1@domain.tld>',
231             ),
232             array(
233                 'name' => 'Test 2',
234                 'email_ascii' => 'addr2@domain.tld',
235                 'ident' => 'Test 2 <addr2@domain.tld>',
236             ),
237             array(
238                 'name' => 'Test 3',
239                 'email_ascii' => 'addr3@domain.tld',
240                 'ident' => 'Test 3 <addr3@domain.tld>',
241             ),
242             array(
243                 'name' => 'Test 4',
244                 'email_ascii' => 'addr2@domain.tld',
245                 'ident' => 'Test 4 <addr2@domain.tld>',
246             ),
247         );
248
249         $message = new stdClass;
250         $message->headers = new rcube_message_header;
251
252         $message->headers->set('From', '<addr2@domain.tld>');
253         $res = rcmail_identity_select($message, $identities);
254         $this->assertSame($identities[1], $res);
255
256         $message->headers->set('From', 'Test 2 <addr2@domain.tld>');
257         $res = rcmail_identity_select($message, $identities);
258         $this->assertSame($identities[1], $res);
259
260         $message->headers->set('From', 'Other <addr2@domain.tld>');
261         $res = rcmail_identity_select($message, $identities);
262         $this->assertSame($identities[1], $res);
263
264         $message->headers->set('From', 'Test 4 <addr2@domain.tld>');
265         $res = rcmail_identity_select($message, $identities);
266         $this->assertSame($identities[3], $res);
267     }
e82492 268 }