source: github/tests/mailfunc.php @ cfc27c9

Last change on this file since cfc27c9 was cfc27c9, checked in by Aleksander Machniak <alec@…>, 10 months ago

Keep some resource files (images) in separate directory

  • Property mode set to 100644
File size: 6.9 KB
Line 
1<?php
2
3/**
4 * Test class to test steps/mail/func.inc functions
5 *
6 * @package Tests
7 */
8class 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();
18    $RCMAIL->action = 'autocomplete';
19    $RCMAIL->storage_init(false);
20   
21    require_once INSTALL_PATH . 'program/steps/mail/func.inc';
22   
23    $GLOBALS['EMAIL_ADDRESS_PATTERN'] = $EMAIL_ADDRESS_PATTERN;
24  }
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
48    $html = rcmail_html4inline(rcmail_print_body($part, array('safe' => false)), 'foo');
49
50    $this->assertPattern('/src="'.$part->replaces['ex1.jpg'].'"/', $html, "Replace reference to inline image");
51    $this->assertPattern('#background="./program/resources/blocked.gif"#', $html, "Replace external background image");
52    $this->assertNoPattern('/ex3.jpg/', $html, "No references to external images");
53    $this->assertNoPattern('/<meta [^>]+>/', $html, "No meta tags allowed");
54    //$this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
55    $this->assertNoPattern('/<form [^>]+>/', $html, "No form tags allowed");
56    $this->assertPattern('/Subscription form/', $html, "Include <form> contents");
57    $this->assertPattern('/<!-- link ignored -->/', $html, "No external links allowed");
58    $this->assertPattern('/<a[^>]+ target="_blank">/', $html, "Set target to _blank");
59    $this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
60   
61    // render HTML in safe mode
62    $html2 = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'foo');
63   
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)");
66    $this->assertPattern("#url\('?http://evilsite.net/newsletter/image/bg/bg-64.jpg'?\)#", $html2, "Allow external images in CSS (safe mode)");
67    $css = '<link rel="stylesheet" .+_u=tmp-[a-z0-9]+\.css.+_action=modcss';
68    $this->assertPattern('#'.$css.'#Ui', $html2, "Filter (anonymized) external styleseehts with utils/modcss.inc");
69  }
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));
78   
79    $this->assertNoPattern('/src="skins/', $washed, "Remove local references");
80    $this->assertNoPattern('/\son[a-z]+/', $washed, "Remove on* attributes");
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");
85  }
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');
94    $washed = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'dabody', '', $attr, true);
95
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  /**
101   * Test washtml class on non-unicode characters (#1487813)
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  /**
112   * Test links pattern replacements in plaintext messages
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));
121
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");
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");
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");
125  }
126
127  /**
128   * Test mailto links in html messages
129   */
130  function test_mailto()
131  {
132    $part = $this->get_html_part('src/mailto.txt');
133
134    // render HTML in normal mode
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
143  /**
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
157  /**
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]");
168
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]");
172  }
173}
Note: See TracBrowser for help on using the repository browser.