source: github/tests/mailfunc.php @ 8955ca6

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 8955ca6 was 5570ad6, checked in by thomascube <thomas@…>, 3 years ago

Improved charset detection in vcard import + added unit tests for it

  • Property mode set to 100644
File size: 4.7 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 = 'spell';
19    $IMAP = $RCMAIL->imap;
20   
21    require_once '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/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('/<!-- input ignored -->/', $html, "No input elements allowed");
58    $this->assertPattern('/<!-- link ignored -->/', $html, "No external links allowed");
59    $this->assertPattern('/<a[^>]+ target="_blank">/', $html, "Set target to _blank");
60    $this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
61   
62    // render HTML in safe mode
63    $html2 = rcmail_html4inline(rcmail_print_body($part, array('safe' => true)), 'foo');
64   
65    $this->assertPattern('/<style [^>]+>/', $html2, "Allow styles in safe mode");
66    $this->assertPattern('#src="http://evilsite.net/mailings/ex3.jpg"#', $html2, "Allow external images in HTML (safe mode)");
67    $this->assertPattern("#url\('http://evilsite.net/newsletter/image/bg/bg-64.jpg'\)#", $html2, "Allow external images in CSS (safe mode)");
68   
69    $css = '<link rel="stylesheet" type="text/css" href="./bin/modcss.php?u='.urlencode('http://anysite.net/styles/mail.css').'&amp;c=foo"';
70    $this->assertPattern('#'.preg_quote($css).'#', $html2, "Filter external styleseehts with bin/modcss.php");
71  }
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));
80   
81    $this->assertNoPattern('/src="skins/', $washed, "Remove local references");
82    $this->assertNoPattern('/\son[a-z]+/', $washed, "Remove on* attributes");
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");
87  }
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));
112   
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");
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");
115  }
116
117}
118
119?>
Note: See TracBrowser for help on using the repository browser.