2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'include/HTMLPurifier/HTMLPurifier.standalone.php';
39 require_once 'include/HTMLPurifier/HTMLPurifier.autoload.php';
42 * cid: scheme implementation
44 class HTMLPurifier_URIScheme_cid extends HTMLPurifier_URIScheme
46 public $browsable = true;
47 public $may_omit_host = true;
49 public function doValidate(&$uri, $config, $context) {
50 $uri->userinfo = null;
54 $uri->fragment = null;
60 class HTMLPurifier_Filter_Xmp extends HTMLPurifier_Filter
65 public function preFilter($html, $config, $context)
67 return preg_replace("#<(/)?xmp>#i", "<\\1pre>", $html);
77 static public $instance;
80 * HTMLPurifier instance
85 function __construct()
88 $config = HTMLPurifier_Config::createDefault();
90 if(!is_dir(sugar_cached("htmlclean"))) {
91 create_cache_directory("htmlclean/");
93 $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
94 $config->set('Core.Encoding', 'UTF-8');
95 $hidden_tags = array('script' => true, 'style' => true, 'title' => true, 'head' => true);
96 $config->set('Core.HiddenElements', $hidden_tags);
97 $config->set('Cache.SerializerPath', sugar_cached("htmlclean"));
98 $config->set('URI.Base', $sugar_config['site_url']);
99 $config->set('CSS.Proprietary', true);
100 $config->set('HTML.TidyLevel', 'light');
101 $config->set('HTML.ForbiddenElements', array('body' => true, 'html' => true));
102 $config->set('AutoFormat.RemoveEmpty', false);
103 $config->set('Cache.SerializerPermissions', 0775);
105 //$config->set('Filter.ExtractStyleBlocks', true);
106 $config->set('Filter.ExtractStyleBlocks.TidyImpl', false); // can't use csstidy, GPL
108 $config->set('HTML.SafeObject', true);
110 $config->set('HTML.SafeEmbed', true);
111 $config->set('Output.FlashCompat', true);
112 // for iframe and xmp
113 $config->set('Filter.Custom', array(new HTMLPurifier_Filter_Xmp()));
115 $config->set('HTML.DefinitionID', 'Sugar HTML Def');
116 $config->set('HTML.DefinitionRev', 2);
117 $config->set('Cache.SerializerPath', sugar_cached('htmlclean/'));
118 // IDs are namespaced
119 $config->set('Attr.EnableID', true);
120 $config->set('Attr.IDPrefix', 'sugar_text_');
122 if ($def = $config->maybeGetRawHTMLDefinition()) {
123 $form = $def->addElement(
125 'Flow', // content set
126 'Empty', // allowed children
127 'Core', // attribute collection
130 'rel' => 'Enum#stylesheet', // only stylesheets supported here
131 'type' => 'Enum#text/css' // only CSS supported here
134 $iframe = $def->addElement(
136 'Flow', // content set
137 'Optional: #PCDATA | Flow | Block', // allowed children
138 'Core', // attribute collection
141 'frameborder' => 'Enum#0,1',
142 'marginwidth' => 'Pixels',
143 'marginheight' => 'Pixels',
144 'scrolling' => 'Enum#|yes,no,auto',
145 'align' => 'Enum#top,middle,bottom,left,right,center',
146 'height' => 'Length',
150 $iframe->excludes=array('iframe');
152 $uri = $config->getDefinition('URI');
153 $uri->addFilter(new SugarURIFilter(), $config);
154 HTMLPurifier_URISchemeRegistry::instance()->register('cid', new HTMLPurifier_URIScheme_cid());
156 $this->purifier = new HTMLPurifier($config);
160 * Get cleaner instance
161 * @return SugarCleaner
163 public static function getInstance()
165 if(is_null(self::$instance)) {
166 self::$instance = new self;
168 return self::$instance;
172 * Clean string from potential XSS problems
173 * @param string $html
174 * @param bool $encoded Was it entity-encoded?
177 static public function cleanHtml($html, $encoded = false)
179 if(empty($html)) return $html;
182 $html = from_html($html);
184 if(!preg_match('<[^-A-Za-z0-9 `~!@#$%^&*()_=+{}\[\];:\'",./\\?\r\n|\x80-\xFF]>', $html)) {
185 /* if it only has "safe" chars, don't bother */
188 $purifier = self::getInstance()->purifier;
189 $cleanhtml = $purifier->purify($html);
190 // $styles = $purifier->context->get('StyleBlocks');
191 // if(count($styles) > 0) {
192 // $cleanhtml = "<style>".join("</style><style>", $styles)."</style>".$cleanhtml;
196 $cleanhtml = to_html($cleanhtml);
201 static public function stripTags($string, $encoded = true)
204 $string = from_html($string);
206 $string = filter_var($string, FILTER_SANITIZE_STRIPPED, FILTER_FLAG_NO_ENCODE_QUOTES);
207 return $encoded?to_html($string):$string;
212 * URI filter for HTMLPurifier
213 * Approves only resource URIs that are in the list of trusted domains
214 * Until we have comprehensive CSRF protection, we need to sanitize URLs in emails, etc.
215 * to avoid CSRF attacks.
217 class SugarURIFilter extends HTMLPurifier_URIFilter
219 public $name = 'SugarURIFilter';
220 // public $post = true;
221 protected $allowed = array();
223 public function prepare($config)
225 global $sugar_config;
226 if(!empty($sugar_config['security_trusted_domains']) && is_array($sugar_config['security_trusted_domains']))
228 $this->allowed = $sugar_config['security_trusted_domains'];
231 $def = $config->getDefinition('URI');
232 if(!empty($def->base) && !empty($this->base->host)) {
233 $this->allowed[] = $def->base->host;
238 public function filter(&$uri, $config, $context)
240 // skip non-resource URIs
241 if (!$context->get('EmbeddedURI', true)) return true;
243 //if(empty($this->allowed)) return false;
245 if(!empty($uri->scheme) && strtolower($uri->scheme) != 'http' && strtolower($uri->scheme) != 'https') {
246 // do not touch non-HTTP URLs
250 // relative URLs permitted since email templates use it
251 // if(empty($uri->host)) return false;
252 // allow URLs with no query
253 if(empty($uri->query)) return true;
255 // allow URLs for known good hosts
256 foreach($this->allowed as $allow) {
257 // must be equal to our domain or subdomain of our domain
258 if($uri->host == $allow || substr($uri->host, -(strlen($allow)+1)) == ".$allow") {
263 // Here we try to block URLs that may be used for nasty XSRF stuff by
264 // referring back to Sugar URLs
265 // allow URLs that don't start with /? or /index.php?
266 if(!empty($uri->path) && $uri->path != '/') {
267 $lpath = strtolower($uri->path);
268 if(substr($lpath, -10) != '/index.php' && $lpath != 'index.php') {
273 $query_items = array();
274 parse_str(from_html($uri->query), $query_items);
275 // weird query, probably harmless
276 if(empty($query_items)) return true;
277 // suspiciously like SugarCRM query, reject
278 if(!empty($query_items['module']) && !empty($query_items['action'])) return false;
279 // looks like non-download entry point - allow only specific entry points
280 if(!empty($query_items['entryPoint']) && !in_array($query_items['entryPoint'], array('download', 'image', 'getImage'))) {