mhtxthtml.pl untested

filter: Statementstmt 0.0% 0 / 261 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 426 CC 66 SCAR 77.4
CC
66
Cov
0%
filter
3080.0
resolve_cid
90.0
strip_comments
12.0
CRAP 2300.9
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: mhtxthtml.pl,v 2.42 2011/01/09 16:12:14 ehood Exp $
4 ## Author:
5 ## Earl Hood mhonarc@mhonarc.org
6 ## Description:
7 ## Library defines routine to filter text/html body parts
8 ## for MHonArc.
9 ## Filter routine can be registered with the following:
10 ## <MIMEFILTERS>
11 ## text/html:m2h_text_html'filter:mhtxthtml.pl
12 ## </MIMEFILTERS>
13 ##---------------------------------------------------------------------------##
14 ## MHonArc -- Internet mail-to-HTML converter
15 ## Copyright (C) 1995-2010 Earl Hood, mhonarc@mhonarc.org
16 ##
17 ## This program is free software; you can redistribute it and/or modify
18 ## it under the terms of the GNU General Public License as published by
19 ## the Free Software Foundation; either version 2 of the License, or
20 ## (at your option) any later version.
21 ##
22 ## This program is distributed in the hope that it will be useful,
23 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ## GNU General Public License for more details.
26 ##
27 ## You should have received a copy of the GNU General Public License
28 ## along with this program; if not, write to the Free Software
29 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 ##---------------------------------------------------------------------------##
31  
32 package m2h_text_html;
33  
34 # Script related attributes: Basically any attribute that starts with "on"
35 my $SAttr = q/\bon\w+\b/;
36  
37 # Script/questionable related elements
38 my $SElem = q/\b(?:applet|embed|form|ilayer|input|layer|link|meta|/
39 . q/object|option|param|select|textarea)\b/;
40  
41 # Elements with auto-loaded URL attributes
42 my $AElem = q/\b(?:img|body|iframe|frame|object|script|input)\b/;
43 # URL attributes
44 my $UAttr =
45 q/\b(?:action|background|cite|classid|codebase|data|datasrc|/
46 . q/dynsrc|for|href|longdesc|lowsrc|profile|src|url|usemap|/
47 . q/vrml)\b/;
48  
49 # Used to reverse the effects of CHARSETCONVERTERS
50 my %special_to_char = (
51 'lt' => '<',
52 'gt' => '>',
53 'amp' => '&',
54 'quot' => '"',
55 );
56  
57 ##---------------------------------------------------------------------------
58 ## The filter must modify HTML content parts for merging into the
59 ## final filtered HTML messages. Modification is needed so the
60 ## resulting filtered message is valid HTML.
61 ##
62 ## CAUTION: Some of these options can open up a site to attacks.
63 ## The MIMEFILTERS reference page provide additional
64 ## information on the risks associated with enabling
65 ## a given option.
66 ##
67 ## Arguments:
68 ##
69 ## allowcomments Preserve any comment declarations. Normally
70 ## Comment declarations are munged to prevent
71 ## SSI attacks or comments that can conflict
72 ## with MHonArc processing. Use this option
73 ## with care.
74 ##
75 ## allownoncidurls Preserve URL-based attributes that are not
76 ## cid: URLs. Normally, any URL-based attribute
77 ## -- href, src, background, classid, data,
78 ## longdesc -- will be stripped if it is not a
79 ## cid: URL. This is to prevent malicious URLs
80 ## that verify mail addresses for spam purposes,
81 ## secretly set cookies, or gather some
82 ## statistical data automatically with the use of
83 ## elements that cause browsers to automatically
84 ## fetch data: IMG, BODY, IFRAME, FRAME, OBJECT,
85 ## SCRIPT, INPUT.
86 ##
87 ## allowscript Preserve any markup associated with scripting.
88 ## This includes elements and attributes related
89 ## to scripting. The default is to delete any
90 ## scripting markup for security reasons.
91 ##
92 ## attachcheck Honor attachment disposition. By default,
93 ## all text/html data is displayed inline on
94 ## the message page. If attachcheck is specified
95 ## and Content-Disposition specifies the data as
96 ## an attachment, the data is saved to a file
97 ## with a link to it from the message page.
98 ## NOTE: This option can expose your site to
99 ## XSS attacks.
100 ##
101 ## disablerelated Disable MHTML processing.
102 ##
103 ## nofont Remove <FONT> tags.
104 ##
105 ## notitle Do not print title.
106 ##
107 ## subdir Place derived files in a subdirectory
108 ##
109  
110 # CAUTION:
111 # The script stripping code is probably not complete. Since a
112 # whitelist model is not being used -- because full HTML parsing
113 # would be required (and possible reliance on non-standard modules) --
114 # Future scripting extensions added to HTML could get by the filtering.
115 # The FAQ mentions the problems with HTML messages and recommends
116 # disabling HTML in archives.
117  
118 sub filter {
119 my ($fields, $data, $isdecode, $args) = @_;
120 $args = '' unless defined $args;
121  
122 # Bug-32013 (CVE-2010-4524): Invalid tags cause immediate rejection.
123 # Bug-32014 (CVE-2010-1677): Prevents DoS if massively nested.
124 my $allowcom = $args =~ /\ballowcomments\b/i;
125 strip_comments($fields, $data) unless $allowcom;
126 if ($$data =~ /<[^>]*</) {
127 # XXX: This will reject HTML that includes a '<' char in a
128 # comment declaration if allowcomments is enabled.
129 return bad_html_reject($fields, "Nested start tags");
130 }
131  
132 ## Check if content-disposition should be checked
133 if ($args =~ /\battachcheck\b/i) {
134 my ($disp, $nameparm, $raw) =
135 readmail::MAILhead_get_disposition($fields);
136 if ($disp =~ /\battachment\b/i) {
137 require 'mhexternal.pl';
138 return (
139 m2h_external::filter(
140 $fields, $data, $isdecode,
141 readmail::get_filter_args('m2h_external::filter')
142 )
143 );
144 }
145 }
146  
147 local (@files) = (); # XXX: Used by resolve_cid!!!
148 my $base = '';
149 my $title = '';
150 my $noscript = 1;
151 $noscript = 0 if $args =~ /\ballowscript\b/i;
152 my $nofont = $args =~ /\bnofont\b/i;
153 my $notitle = $args =~ /\bnotitle\b/i;
154 my $onlycid = $args !~ /\ballownoncidurls\b/i;
155 my $subdir = $args =~ /\bsubdir\b/i;
156 my $norelate = $args =~ /\bdisablerelated\b/i;
157 my $atdir = $subdir ? $mhonarc::MsgPrefix . $mhonarc::MHAmsgnum : "";
158 my $tmp, $i;
159  
160 my $charset = $fields->{'x-mha-charset'};
161 my ($charcnv, $real_charset_name) =
162 readmail::MAILload_charset_converter($charset);
163 if (defined($charcnv) && defined(&$charcnv)) {
164 $$data = &$charcnv($$data, $real_charset_name);
165 # translate HTML specials back
166 $$data =~ s/&([lg]t|amp|quot);/$special_to_char{$1}/g;
167 } elsif ($charcnv ne '-decode-') {
168 do_warn($fields, "Unrecognized character set: $charset");
169 }
170  
171 ## Unescape ascii letters to simplify strip code
172 dehtmlize_ascii($data);
173  
174 ## Strip out scripting markup: Do this early on so scripting
175 ## data does not infect subsequent filtering operations
176 if ($noscript) {
177 # remove scripting elements and attributes
178 $$data =~ s|<script[^>]*>.*?</script\s*>||gios;
179  
180 $$data =~ s|<style[^>]*>.*?</style\s*>||gios;
181 for ($i = 0; $$data =~ s|</?style\b[^>]*>||gio; ++$i) {
182 return bad_html_reject("Nested <style> tags") if $i > 0;
183 }
184  
185 # Just neutralize scripting attributes. Since we do not
186 # do true tag-based parsing, this ensures that valid content
187 # is not removed (but it will still get modified)
188 $$data =~ s/($SAttr)(\s*=)/_${1}_${2}/gi;
189  
190 for ($i = 0; $$data =~ s|</?$SElem[^>]*>||gio; ++$i) {
191 return bad_html_reject("Nested scriptable/form tags") if $i > 0;
192 }
193 for ($i = 0; $$data =~ s|</?script\b||gio; ++$i) {
194 return bad_html_reject("Nested <script> tags") if $i > 0;
195 }
196  
197 # for netscape 4.x browsers
198 $$data =~ s/(=\s*["']?\s*)(?:\&\{)+/$1/g;
199  
200 # Neutralize javascript:... URLs: Unfortunately, browsers
201 # are stupid enough to recognize a javascript URL with whitespace
202 # in it (like tabs and newlines).
203 $$data =~ s/\bj\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t/_javascript_/gi;
204 $$data =~ s/\bv\s*b\s*s\s*c\s*r\s*i\s*p\s*t/_vbscript_/gi;
205 $$data =~ s/\be\s*c\s*m\s*a\s*c\s*r\s*i\s*p\s*t/_ecmascript_/gi;
206  
207 # IE has a very unsecure expression() operator extension to
208 # CSS, so we have to nuke it also.
209 $$data =~ s/\bexpression\b/_expression_/gi;
210 }
211  
212 ## Get/remove title
213 if (!$notitle) {
214 if ($$data =~ s|<title\s*>([^<]*)</title\s*>||io) {
215 $title = "<address>Title: <strong>$1</strong></address>\n"
216 unless $1 eq "";
217 }
218 } else {
219 $$data =~ s|<title\s*>[^<]*</title\s*>||io;
220 }
221  
222 ## Get/remove BASE url: The base URL may be defined in the HTML
223 ## data or defined in the entity header.
224 BASEURL: {
225 if ($$data =~ s|(<base\s[^>]*>)||i) {
226 $tmp = $1;
227 if ($tmp =~ m|href\s*=\s*['"]([^'"]+)['"]|i) {
228 $base = $1;
229 } elsif ($tmp =~ m|href\s*=\s*([^\s>]+)|i) {
230 $base = $1;
231 }
232 last BASEURL if ($base =~ /\S/);
233 }
234 if (( defined($tmp = $fields->{'content-base'}[0])
235 || defined($tmp = $fields->{'content-location'}[0])
236 )
237 && ($tmp =~ m%/%)
238 ) {
239 ($base = $tmp) =~ s/['"\s]//g;
240 }
241 }
242 for ($i = 0; $$data =~ s|</?base[^>]*>||gio; ++$i) {
243 return bad_html_reject("Nested base tags") if $i > 0;
244 }
245 if ($base =~ /['"<>]/) {
246 do_warn($fields,
247 "Ignoring BASE href due to questionable characters: $base");
248 $base = '';
249 } else {
250 $base =~ s|(.*/).*|$1|;
251 }
252  
253 ## Strip out certain elements/tags to support proper inclusion:
254 ## some browsers are forgiving about dublicating header tags, but
255 ## we try to do things right. It also help minimize XSS exploits.
256 $$data =~ s|<head\s*>[\s\S]*</head\s*>||io;
257 for ($i = 0; $$data =~ s|<!doctype\s[^>]*>||gio; ++$i) {
258 return bad_html_reject("Nested doctypes") if $i > 0;
259 }
260 for ($i = 0; $$data =~ s|</?html\b[^>]*>||gio; ++$i) {
261 return bad_html_reject("Nested <html> tags") if $i > 0;
262 }
263 for ($i = 0; $$data =~ s|</?x-html\b[^>]*>||gio; ++$i) {
264 return bad_html_reject("Nested <x-html> tags") if $i > 0;
265 }
266 for ($i = 0; $$data =~ s|</?meta\b[^>]*>||gio; ++$i) {
267 return bad_html_reject("Nested <meta> tags") if $i > 0;
268 }
269 for ($i = 0; $$data =~ s|</?link\b[^>]*>||gio; ++$i) {
270 return bad_html_reject("Nested <link> tags") if $i > 0;
271 }
272  
273 ## Strip out style information if requested.
274 if ($nofont) {
275 if (!$noscript) {
276 # Only do this if we did not do it above.
277 $$data =~ s|<style[^>]*>.*?</style\s*>||gios;
278 }
279  
280 for ($i = 0; $$data =~ s|</?font\b[^>]*>||gio; ++$i) {
281 return bad_html_reject("Nested <font> tags") if $i > 0;
282 }
283 for ($i = 0; $$data =~ s/\b(?:style|class)\s*=\s*"[^"]*"//gio; ++$i) {
284 return bad_html_reject("Nested style|class attributes")
285 if $i > 0;
286 }
287 for ($i = 0; $$data =~ s/\b(?:style|class)\s*=\s*'[^']*'//gio; ++$i) {
288 return bad_html_reject("Nested style|class attributes")
289 if $i > 0;
290 }
291 for ($i = 0; $$data =~ s/\b(?:style|class)\s*=\s*[^\s>]+//gio; ++$i) {
292 return bad_html_reject("Nested style|class attributes")
293 if $i > 0;
294 }
295 for ($i = 0; $$data =~ s|</?style\b[^>]*>||gio; ++$i) {
296 return bad_html_reject("Nested <style> tags") if $i > 0;
297 }
298 }
299  
300 ## Modify relative urls to absolute using BASE
301 if ($base =~ /\S/) {
302 $$data =~ s/($UAttr\s*=\s*['"])([^'"]+)(['"])/
303 join("", $1, readmail::apply_base_url($base,$2), $3)/geoix;
304 }
305  
306 ## Check for frames: Do not support, so just show source
307 if ($$data =~ m/<frameset\b/i) {
308 $$data = join('', '<pre>', mhonarc::htmlize($$data), '</pre>');
309 return ($title . $$data, @files);
310 }
311  
312 ## Check for body attributes
313 if ($$data =~ s|<body\b([^>]*)>||i) {
314 require 'mhutil.pl';
315 my $a = $1;
316 my %attr = mhonarc::parse_vardef_str($a, 1);
317 if (%attr) {
318 ## Use a table with a single cell to encapsulate data to
319 ## set visual properties. We use a mixture of old attributes
320 ## and CSS to set properties since browsers may not support
321 ## all of the CSS settings via the STYLE attribute.
322 my $tpre = '<table width="100%"><tr><td ';
323 my $tsuf = "";
324 $tpre .= qq|background="$attr{'background'}" |
325 if $attr{'background'};
326 $tpre .= qq|bgcolor="$attr{'bgcolor'}" |
327 if $attr{'bgcolor'};
328 $tpre .= qq|style="|;
329 $tpre .= qq|background-color: $attr{'bgcolor'}; |
330 if $attr{'bgcolor'};
331 if ($attr{'background'}) {
332  
333 if ($attr{'background'} =
334 &resolve_cid($onlycid, $attr{'background'}, $atdir)) {
335 $tpre .= qq|background-image: url($attr{'background'}) |;
336 }
337 }
338 $tpre .= qq|color: $attr{'text'}; |
339 if $attr{'text'};
340 $tpre .= qq|a:link { color: $attr{'link'} } |
341 if $attr{'link'};
342 $tpre .= qq|a:active { color: $attr{'alink'} } |
343 if $attr{'alink'};
344 $tpre .= qq|a:visited { color: $attr{'vlink'} } |
345 if $attr{'vlink'};
346 $tpre .= '">';
347 if ($attr{'text'}) {
348 $tpre .= qq|<font color="$attr{'text'}">|;
349 $tsuf .= '</font>';
350 }
351 $tsuf .= '</td></tr></table>';
352 $$data = $tpre . $$data . $tsuf;
353 }
354 }
355 for ($i = 0; $$data =~ s|</?body\b[^>]*>||igo; ++$i) {
356 return bad_html_reject("Nested <body> tags") if $i > 0;
357 }
358  
359 my $ahref_tmp;
360 if ($onlycid) {
361 # If only cid URLs allowed, we still try to preserve <a href> or
362 # any hyperlinks in a document would be stripped out.
363 # Algorithm: Replace HREF attribute string in <A>'s with a
364 # random string. We then restore HREF after CID
365 # resolution. We do not worry about javascript since
366 # we neutralized it earlier.
367 $ahref_tmp = mhonarc::rand_string('alnkXXXXXXXXXX');
368  
369 # Make sure "href" not in rand string
370 $ahref_tmp =~ s/href/XXXX/gi;
371  
372 # Remove occurances of random string from input first. This
373 # should cause nothing to be deleted, but is done to avoid
374 # a potential exploit attempt.
375 $$data =~ s/\b$ahref_tmp\b//g;
376  
377 # Replace all <a href> with <a RAND_STR>. We make sure to
378 # leave cid: attributes alone since they are processed later.
379 $$data =~ s/(<a\b[^>]*)href\s*=\s*("(?!\s*cid:)[^"]+")
380 /$1$ahref_tmp=$2/gix; # double-quoted delim attribute
381 $$data =~ s/(<a\b[^>]*)href\s*=\s*('(?!\s*cid:)[^']+')
382 /$1$ahref_tmp=$2/gix; # single-quoted delim attribute
383 $$data =~ s/(<a\b[^>]*)href\s*=\s*((?!['"]?\s*cid:)[^\s>]+)
384 /$1$ahref_tmp=$2/gix; # non-quoted attribute
385 }
386  
387 ## Check for CID URLs (multipart/related HTML). Multiple expressions
388 ## exist to handle variations in how attribute values are delimited.
389 if ($norelate) {
390 if ($onlycid) {
391 $$data =~ s/($UAttr\s*=\s*["])[^"]+(["])/$1$2/goi;
392 $$data =~ s/($UAttr\s*=\s*['])[^']+(['])/$1$2/goi;
393 $$data =~ s/($UAttr\s*=\s*[^\s'">][^\s>]+)/ /goi;
394 }
395 } else {
396 $$data =~ s/($UAttr\s*=\s*["])([^"]+)(["])
397 /join("",$1,&resolve_cid($onlycid, $2, $atdir),$3)/geoix;
398 $$data =~ s/($UAttr\s*=\s*['])([^']+)(['])
399 /join("",$1,&resolve_cid($onlycid, $2, $atdir),$3)/geoix;
400 $$data =~ s/($UAttr\s*=\s*)([^\s'">][^\s>]+)
401 /join("",$1,'"',&resolve_cid($onlycid, $2, $atdir),'"')
402 /geoix;
403 }
404  
405 if ($onlycid) {
406 # Restore HREF attributes of <A>'s.
407 $$data =~ s/\b$ahref_tmp\b/href/g;
408 }
409  
410 ## NOTE: Comment strip moved to top.
411 ## Check comment declarations: may screw-up mhonarc processing
412 ## and avoids someone sneaking in SSIs.
413 # if (!$allowcom) {
414 # #$$data =~ s/<!(?:--(?:[^-]|-[^-])*--\s*)+>//go; # can crash perl
415 # $$data =~ s/<!--[^-]+[#X%\$\[]*/<!--/g; # Just mung them (faster)
416 # }
417  
418 ## Prevent comment spam
419 ## <http://www.google.com/googleblog/2005/01/preventing-comment-spam.html>
420 $$data =~ s/(<a\b)/$1 rel="nofollow"/gi;
421  
422 ($title . $$data, @files);
423 }
424  
425 ##---------------------------------------------------------------------------
426  
427 sub resolve_cid {
428 my $onlycid = shift;
429 my $cid_in = shift;
430 my $attachdir = shift;
431 my $cid = $cid_in;
432  
433 $cid =~ s/&#(?:x0*40|64);/@/g;
434 my $href = $readmail::Cid{$cid};
435 if (!defined($href)) {
436 my $basename = $cid;
437 $basename =~ s/.*\///;
438 if (!defined($href = $readmail::Cid{$basename})) {
439 return "" if $onlycid;
440 return ($cid =~ /^cid:/i) ? "" : $cid_in;
441 }
442 $cid = $basename;
443 }
444  
445 if ($href->{'uri'}) {
446 # Part already converted; multiple references to part
447 return $href->{'uri'};
448 }
449  
450 # Get content-type of data and return if type is excluded
451 my $ctype = $href->{'fields'}{'x-mha-content-type'};
452 if (!defined($ctype)) {
453 $ctype = $href->{'fields'}{'content-type'}[0];
454 ($ctype) = $ctype =~ m{^\s*([\w\-\./]+)};
455 }
456 return "" if readmail::MAILis_excluded($ctype);
457  
458 require 'mhmimetypes.pl';
459 my $filename;
460 my $decodefunc =
461 readmail::load_decoder(
462 $href->{'fields'}{'content-transfer-encoding'}[0]);
463 if (defined($decodefunc) && defined(&$decodefunc)) {
464 my $data = &$decodefunc(${$href->{'body'}});
465 $filename = mhonarc::write_attachment($ctype, \$data,
466 {'-dirpath' => $attachdir});
467 } else {
468 $filename = mhonarc::write_attachment($ctype, $href->{'body'},
469 {'-dirpath' => $attachdir});
470 }
471 $href->{'filtered'} = 1; # mark part filtered for readmail.pl
472 $href->{'uri'} = $filename;
473  
474 push(@files, $filename); # @files defined in filter!!
475 $filename;
476 }
477  
478 ##---------------------------------------------------------------------------
479  
480 sub dehtmlize_ascii {
481 my $str = shift;
482 my $str_r = ref($str) ? $str : \$str;
483  
484 $$str_r =~ s{\&\#(\d+);?}{
485 my $n = int($1);
486 if (($n >= 7 && $n <= 13) ||
487 ($n == 32) || ($n == 61) ||
488 ($n >= 48 && $n <= 58) ||
489 ($n >= 64 && $n <= 90) ||
490 ($n >= 97 && $n <= 122)) {
491 pack('C', $n);
492 } else {
493 '&#'.$1.';'
494 }
495 }gex;
496 $$str_r =~ s{\&\#[xX]([0-9abcdefABCDEF]+);?}{
497 my $n = hex($1);
498 if (($n >= 7 && $n <= 13) ||
499 ($n == 32) || ($n == 61) ||
500 ($n >= 48 && $n <= 58) ||
501 ($n >= 64 && $n <= 90) ||
502 ($n >= 97 && $n <= 122)) {
503 pack('C', $n);
504 } else {
505 '&#x'.$1.';'
506 }
507 }gex;
508  
509 $$str_r;
510 }
511  
512 ##---------------------------------------------------------------------------
513  
514 sub strip_comments {
515 my $fields = shift; # for diagnostics
516 my $data = shift; # ref to text to strip
517  
518 # We avoid using regex since it can lead to performance problems.
519 # We also do not do full SGML-style comment declarations since it
520 # increases parsing complexity. Here, we just remove any
521 # "<!-- ... -->" strings. Although whitespace is allowed between
522 # final "--" and ">", we do not support it.
523  
524 my $n = index($$data, '<!--', 0);
525 if ($n < 0) {
526 # Nothing to do. Good.
527 return $data;
528 }
529  
530 my $ret = '';
531 while ($n >= 0) {
532 $ret .= substr($$data, 0, $n);
533 substr($$data, 0, $n) = '';
534 $n = index($$data, '-->', 0);
535 if ($n < 0) {
536 # No end to comment declaration: Warn and strip rest of data.
537 do_warn($fields, 'HTML comment declaration not terminated.');
538 $$data = '';
539 last;
540 }
541 substr($$data, 0, $n + 3) = '';
542 $n = index($$data, '<!--', 0);
543 }
544 $ret .= $$data;
545 $$data = $ret;
546 $data;
547 }
548  
549 ##---------------------------------------------------------------------------
550 #
551 sub do_warn {
552 my $fields = shift;
553 my $mesg = shift;
554 warn qq/\n/,
555 qq/Warning: $mesg\n/,
556 qq/ Message-Id: <$mhonarc::MHAmsgid>\n/,
557 qq/ Message Subject: /, $fields->{'x-mha-subject'}, qq/\n/,
558 qq/ Message Number: $mhonarc::MHAmsgnum\n/;
559 }
560  
561 sub bad_html_reject {
562 my $fields = shift;
563 my $detail = shift;
564 do_warn($fields, "Rejecting Invalid HTML: $detail");
565 undef;
566 }
567  
568 ##---------------------------------------------------------------------------
569  
570 1;