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