thomascube
2011-02-06 07b95dc49b31d131b1fecdabf2059a447935c196
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';
684057 19     $RCMAIL->imap_init(false);
63d4b1 20     $IMAP = $RCMAIL->imap;
T 21     
22     require_once 'steps/mail/func.inc';
115263 23     
T 24     $GLOBALS['EMAIL_ADDRESS_PATTERN'] = $EMAIL_ADDRESS_PATTERN;
63d4b1 25   }
T 26
27   /**
28    * Helper method to create a HTML message part object
29    */
30   function get_html_part($body)
31   {
32     $part = new rcube_message_part;
33     $part->ctype_primary = 'text';
34     $part->ctype_secondary = 'html';
35     $part->body = file_get_contents(TESTS_DIR . $body);
36     $part->replaces = array();
37     return $part;
38   }
39
40   /**
41    * Test sanitization of a "normal" html message
42    */
43   function test_html()
44   {
45     $part = $this->get_html_part('src/htmlbody.txt');
46     $part->replaces = array('ex1.jpg' => 'part_1.2.jpg', 'ex2.jpg' => 'part_1.2.jpg');
47     
48     // render HTML in normal mode
115263 49     $html = rcmail_html4inline(rcmail_print_body($part, array('safe' => false)), 'foo');
63d4b1 50
T 51     $this->assertPattern('/src="'.$part->replaces['ex1.jpg'].'"/', $html, "Replace reference to inline image");
52     $this->assertPattern('#background="./program/blocked.gif"#', $html, "Replace external background image");
53     $this->assertNoPattern('/ex3.jpg/', $html, "No references to external images");
54     $this->assertNoPattern('/<meta [^>]+>/', $html, "No meta tags allowed");
5570ad 55     //$this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
63d4b1 56     $this->assertNoPattern('/<form [^>]+>/', $html, "No form tags allowed");
T 57     $this->assertPattern('/Subscription form/', $html, "Include <form> contents");
5570ad 58     $this->assertPattern('/<!-- input ignored -->/', $html, "No input elements allowed");
T 59     $this->assertPattern('/<!-- link ignored -->/', $html, "No external links allowed");
63d4b1 60     $this->assertPattern('/<a[^>]+ target="_blank">/', $html, "Set target to _blank");
T 61     $this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
62     
63     // render HTML in safe mode
961afa 64     $html2 = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'foo');
63d4b1 65     
T 66     $this->assertPattern('/<style [^>]+>/', $html2, "Allow styles in safe mode");
67     $this->assertPattern('#src="http://evilsite.net/mailings/ex3.jpg"#', $html2, "Allow external images in HTML (safe mode)");
4859fe 68     $this->assertPattern("#url\('?http://evilsite.net/newsletter/image/bg/bg-64.jpg'?\)#", $html2, "Allow external images in CSS (safe mode)");
6ec465 69     $css = '<link rel="stylesheet" .+_u=tmp-[a-z0-9]+\.css.+_action=modcss';
T 70     $this->assertPattern('#'.$css.'#Ui', $html2, "Filter (anonymized) external styleseehts with utils/modcss.inc");
63d4b1 71   }
T 72
73   /**
74    * Test the elimination of some trivial XSS vulnerabilities
75    */
76   function test_html_xss()
77   {
78     $part = $this->get_html_part('src/htmlxss.txt');
79     $washed = rcmail_print_body($part, array('safe' => true));
115263 80     
63d4b1 81     $this->assertNoPattern('/src="skins/', $washed, "Remove local references");
115263 82     $this->assertNoPattern('/\son[a-z]+/', $washed, "Remove on* attributes");
T 83     
84     $html = rcmail_html4inline($washed, 'foo');
85     $this->assertNoPattern('/onclick="return rcmail.command(\'compose\',\'xss@somehost.net\',this)"/', $html, "Clean mailto links");
86     $this->assertNoPattern('/alert/', $html, "Remove alerts");
63d4b1 87   }
T 88
89   /**
90    * Test HTML sanitization to fix the CSS Expression Input Validation Vulnerability
91    * reported at http://www.securityfocus.com/bid/26800/
92    */
93   function test_html_xss2()
94   {
95     $part = $this->get_html_part('src/BID-26800.txt');
96     $washed = rcmail_print_body($part, array('safe' => true));
97
98     $this->assertNoPattern('/alert|expression|javascript|xss/', $washed, "Remove evil style blocks");
99     $this->assertNoPattern('/font-style:italic/', $washed, "Allow valid styles");
100   }
101
102   /**
103    * Test links pattern replacements in plaintext messages
104    */
105   function test_plaintext()
106   {
107     $part = new rcube_message_part;
108     $part->ctype_primary = 'text';
109     $part->ctype_secondary = 'plain';
110     $part->body = quoted_printable_decode(file_get_contents(TESTS_DIR . 'src/plainbody.txt'));
111     $html = rcmail_print_body($part, array('safe' => true));
0ff554 112
63d4b1 113     $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 114     $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 115     $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 116   }
T 117
ad18d6 118   /**
A 119    * Test mailto links in html messages
120    */
121   function test_mailto()
122   {
123     $part = $this->get_html_part('src/mailto.txt');
124     
125     // render HTML in normal mode
126     $html = rcmail_html4inline(rcmail_print_body($part, array('safe' => false)), 'foo');
127
128     $mailto = '<a href="mailto:me@me.com?subject=this is the subject&amp;body=this is the body"'
129       .' onclick="return rcmail.command(\'compose\',\'me@me.com?subject=this is the subject&amp;body=this is the body\',this)">e-mail</a>';
130
131     $this->assertPattern('|'.preg_quote($mailto, '|').'|', $html, "Extended mailto links");
132   }
133
63d4b1 134 }