]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Bugreport.pm
ignore supplied filenames in MIME to avoid taint issues
[debbugs.git] / Debbugs / CGI / Bugreport.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 #
5 # [Other people have contributed to this file; their copyrights should
6 # be listed here too.]
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
8
9
10 package Debbugs::CGI::Bugreport;
11
12 =head1 NAME
13
14 Debbugs::CGI::Bugreport -- specific routines for the bugreport cgi script
15
16 =head1 SYNOPSIS
17
18
19 =head1 DESCRIPTION
20
21
22 =head1 BUGS
23
24 None known.
25
26 =cut
27
28 use warnings;
29 use strict;
30 use utf8;
31 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
32 use Exporter qw(import);
33
34 use IO::Scalar;
35 use Params::Validate qw(validate_with :types);
36 use Digest::MD5 qw(md5_hex);
37 use Debbugs::Mail qw(get_addresses :reply);
38 use Debbugs::MIME qw(decode_rfc1522 create_mime_message);
39 use Debbugs::CGI qw(:url :html :util);
40 use Debbugs::Common qw(globify_scalar english_join);
41 use Debbugs::UTF8;
42 use Debbugs::Config qw(:config);
43 use Debbugs::Log qw(:read);
44 use POSIX qw(strftime);
45 use Encode qw(decode_utf8 encode_utf8);
46 use URI::Escape qw(uri_escape_utf8);
47 use Scalar::Util qw(blessed);
48 use List::AllUtils qw(sum);
49 use File::Temp;
50
51 BEGIN{
52      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
53      $DEBUG = 0 unless defined $DEBUG;
54
55      @EXPORT = ();
56      %EXPORT_TAGS = ();
57      @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
58      Exporter::export_ok_tags(keys %EXPORT_TAGS);
59      $EXPORT_TAGS{all} = [@EXPORT_OK];
60 }
61
62
63
64 =head2 display_entity
65
66      display_entity(entity      => $entity,
67                     bug_num     => $ref,
68                     outer       => 1,
69                     msg_num     => $msg_num,
70                     attachments => \@attachments,
71                     output      => \$output);
72
73
74 =over
75
76 =item entity -- MIME::Parser entity
77
78 =item bug_num -- Bug number
79
80 =item outer -- Whether this is the outer entity; defaults to 1
81
82 =item msg_num -- message number in the log
83
84 =item attachments -- arrayref of attachments
85
86 =item output -- scalar reference for output
87
88 =back
89
90 =cut
91
92 sub display_entity {
93     my %param = validate_with(params => \@_,
94                               spec   => {entity      => {type => OBJECT,
95                                                         },
96                                          bug_num     => {type => SCALAR,
97                                                          regex => qr/^\d+$/,
98                                                         },
99                                          outer       => {type => BOOLEAN,
100                                                          default => 1,
101                                                         },
102                                          msg_num     => {type => SCALAR,
103                                                         },
104                                          attachments => {type => ARRAYREF,
105                                                          default => [],
106                                                         },
107                                          output      => {type => SCALARREF|HANDLE,
108                                                          default => \*STDOUT,
109                                                         },
110                                          terse       => {type => BOOLEAN,
111                                                          default => 0,
112                                                         },
113                                          msg         => {type => SCALAR,
114                                                          optional => 1,
115                                                         },
116                                          att         => {type => SCALAR,
117                                                          optional => 1,
118                                                         },
119                                          trim_headers => {type => BOOLEAN,
120                                                           default => 1,
121                                                          },
122                                          avatars => {type => BOOLEAN,
123                                                      default => 1,
124                                                     },
125                                         }
126                              );
127
128     my $output = globify_scalar($param{output});
129     my $entity = $param{entity};
130     my $ref = $param{bug_num};
131     my $xmessage = $param{msg_num};
132     my $attachments = $param{attachments};
133
134     my $head = $entity->head;
135     my $disposition = $head->mime_attr('content-disposition');
136     $disposition = 'inline' if not defined $disposition or $disposition eq '';
137     my $type = $entity->effective_type;
138     my $filename = $entity->head->recommended_filename;
139     $filename = '' unless defined $filename;
140     $filename = decode_rfc1522($filename);
141
142     if ($param{outer} and
143         not $param{terse} and
144         not exists $param{att}) {
145          print {$output} "<div class=\"headers\">\n";
146          if ($param{trim_headers}) {
147               my @headers;
148               foreach (qw(From To Cc Subject Date)) {
149                    my $head_field = $head->get($_);
150                    next unless defined $head_field and $head_field ne '';
151                    chomp $head_field;
152                    if ($_ eq 'From' and $param{avatars}) {
153                        my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
154                        if (defined $libravatar_url and length $libravatar_url) {
155                            push @headers,q(<img src=").html_escape($libravatar_url).qq(" alt="">\n);
156                        }
157                    }
158                    push @headers, qq(<div class="header"><span class="headerfield">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</div>\n";
159               }
160               print {$output} join(qq(), @headers);
161          } else {
162               print {$output} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
163          }
164          print {$output} "</div>\n";
165     }
166
167     if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
168              or $type =~ m{^multipart/}
169             )) {
170         push @$attachments, $param{entity};
171         # output this attachment
172         if (exists $param{att} and
173             $param{att} == $#$attachments) {
174             my $head = $entity->head;
175             chomp(my $type = $entity->effective_type);
176             my $body = $entity->stringify_body;
177             # this attachment has its own content type, so we must not
178             # try to convert it to UTF-8 or do anything funky.
179             binmode($output,':raw');
180             print {$output} "Content-Type: $type";
181             my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
182             print {$output} qq(; charset="$charset") if defined $charset;
183             print {$output} "\n";
184             if ($filename ne '') {
185                 my $qf = $filename;
186                 $qf =~ s/"/\\"/g;
187                 $qf =~ s[.*/][];
188                 print {$output} qq{Content-Disposition: inline; filename="$qf"\n};
189             }
190             print {$output} "\n";
191             my $decoder = MIME::Decoder->new($head->mime_encoding);
192             $decoder->decode(IO::Scalar->new(\$body), $output);
193             # we don't reset the layers here, because it makes no
194             # sense to add anything to the output handle after this
195             # point.
196             return(1);
197         }
198         elsif (not exists $param{att}) {
199              my @dlargs = (msg=>$xmessage, att=>$#$attachments);
200              push @dlargs, (filename=>$filename) if $filename ne '';
201              my $printname = $filename;
202              $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
203              print {$output} '<pre class="mime">[<a href="' .
204                   html_escape(bug_links(bug => $ref,
205                                         links_only => 1,
206                                         options => {@dlargs})
207                              ) . qq{">$printname</a> } .
208                                   "($type, $disposition)]</pre>\n";
209         }
210     }
211
212     return 0 if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
213     return 0 unless (($type =~ m[^text/?] and
214                       $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
215                      $type =~ m[^application/pgp(?:;|$)] or
216                      $entity->parts);
217
218     if ($entity->is_multipart) {
219         my @parts = $entity->parts;
220         foreach my $part (@parts) {
221             my $raw_output =
222                 display_entity(entity => $part,
223                                bug_num => $ref,
224                                outer => 0,
225                                msg_num => $xmessage,
226                                output => $output,
227                                attachments => $attachments,
228                                terse => $param{terse},
229                                exists $param{msg}?(msg=>$param{msg}):(),
230                                exists $param{att}?(att=>$param{att}):(),
231                                exists $param{avatars}?(avatars=>$param{avatars}):(),
232                               );
233             if ($raw_output) {
234                 return $raw_output;
235             }
236             # print {$output} "\n";
237         }
238     } elsif ($entity->parts) {
239         # We must be dealing with a nested message.
240          if (not exists $param{att}) {
241               print {$output} "<blockquote>\n";
242          }
243         my @parts = $entity->parts;
244         foreach my $part (@parts) {
245             display_entity(entity => $part,
246                            bug_num => $ref,
247                            outer => 1,
248                            msg_num => $xmessage,
249                            output => $output,
250                            attachments => $attachments,
251                            terse => $param{terse},
252                            exists $param{msg}?(msg=>$param{msg}):(),
253                            exists $param{att}?(att=>$param{att}):(),
254                            exists $param{avatars}?(avatars=>$param{avatars}):(),
255                           );
256             # print {$output} "\n";
257         }
258          if (not exists $param{att}) {
259               print {$output} "</blockquote>\n";
260          }
261     } elsif (not $param{terse}) {
262          my $content_type = $entity->head->get('Content-Type:') || "text/html";
263          my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
264          my $body = $entity->bodyhandle->as_string;
265          $body = convert_to_utf8($body,$charset//'utf8');
266          $body = html_escape($body);
267          my $css_class = "message";
268          # Attempt to deal with format=flowed
269          if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
270               $body =~ s{^\ }{}mgo;
271               # we ignore the other things that you can do with
272               # flowed e-mails cause they don't really matter.
273               $css_class .= " flowed";
274          }
275
276          # if the message is composed entirely of lines which are separated by
277          # newlines, wrap it. [Allow the signature to have special formatting.]
278          if ($body =~ /^([^\n]+\n\n)*[^\n]*\n?(-- \n.+)*$/s or
279              # if the first 20 lines in the message which have any non-space
280              # characters are larger than 100 characters more often than they
281              # are not, then use CSS to try to impose sensible wrapping
282              sum(0,map {length ($_) > 100?1:-1} grep {/\S/} split /\n/,$body,20) > 0
283             ) {
284              $css_class .= " wrapping";
285          }
286          # Add links to URLs
287          # We don't html escape here because we escape above;
288          # wierd terminators are because of that
289          $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
290                     ((?:\&gt\;)?[)]?(?:'|\&\#39\;|\&quot\;)?[:.\,]?(?:\s|$)) # terminators
291               }{<a href=\"$1\">$1</a>$2}gox;
292          # Add links to bug closures
293          $body =~ s[((?:closes|see):\s* # start of closed/referenced bugs
294                         (?:bug)?\#?\s?\d+\s? # first bug
295                         (?:,?\s*(?:bug)?\#?\s?\d+)* # additional bugs
296                     (?:\s|\n|\)|\]|\}|\.|\,|$)) # ends with a space, newline, end of string, or ); fixes #747267
297                   ]
298                    [my $temp = $1;
299                     $temp =~ s{(\d+)}
300                               {bug_links(bug=>$1)}ge;
301                     $temp;]gxie;
302          if (defined $config{cve_tracker} and
303              length $config{cve_tracker}
304             ) {
305              # Add links to CVE vulnerabilities (closes #568464)
306              $body =~ s{(^|\s|[\(\[])(CVE-\d{4}-\d{4,})(\s|[,.-\[\]\)]|$)}
307                        {$1<a href="$config{cve_tracker}$2">$2</a>$3}gxm;
308          }
309          if (not exists $param{att}) {
310               print {$output} qq(<pre class="$css_class">$body</pre>\n);
311          }
312     }
313     return 0;
314 }
315
316
317 =head2 handle_email_message
318
319      handle_email_message($record->{text},
320                           ref        => $bug_number,
321                           msg_num => $msg_number,
322                          );
323
324 Returns a decoded e-mail message and displays entities/attachments as
325 appropriate.
326
327
328 =cut
329
330 sub handle_email_message{
331      my ($record,%param) = @_;
332
333      my $output;
334      my $output_fh = globify_scalar(\$output);
335      my $entity;
336      my $tempdir;
337      if (not blessed $record) {
338          my $parser = MIME::Parser->new();
339          # this will be cleaned up once it goes out of scope
340          $tempdir = File::Temp->newdir();
341          $parser->output_under($tempdir->dirname());
342          if ($record->{inner_file}) {
343              $entity = $parser->parse($record->{fh}) or
344                  die "Unable to parse entity";
345          } else {
346              $entity = $parser->parse_data($record->{text}) or
347                  die "Unable to parse entity";
348          }
349      } else {
350          $entity = $record;
351      }
352      my @attachments = ();
353      my $raw_output =
354          display_entity(entity  => $entity,
355                         bug_num => $param{ref},
356                         outer   => 1,
357                         msg_num => $param{msg_num},
358                         output => $output_fh,
359                         attachments => \@attachments,
360                         terse       => $param{terse},
361                         exists $param{msg}?(msg=>$param{msg}):(),
362                         exists $param{att}?(att=>$param{att}):(),
363                         exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
364                         exists $param{avatars}?(avatars=>$param{avatars}):(),
365                        );
366      return $raw_output?$output:decode_utf8($output);
367 }
368
369 =head2 handle_record
370
371      push @log, handle_record($record,$ref,$msg_num);
372
373 Deals with a record in a bug log as returned by
374 L<Debbugs::Log::read_log_records>; returns the log information that
375 should be output to the browser.
376
377 =cut
378
379 sub handle_record{
380      my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
381
382      # output needs to have the is_utf8 flag on to avoid double
383      # encoding
384      my $output = decode_utf8('');
385      local $_ = $record->{type};
386      if (/html/) {
387          # $record->{text} is not in perl's internal encoding; convert it
388          my $text = decode_rfc1522(decode_utf8(record_text($record)));
389           my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
390           my $class = $text =~ /^<strong>(?:Acknowledgement|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
391           $output .= $text;
392           # Link to forwarded http:// urls in the midst of the report
393           # (even though these links already exist at the top)
394           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;|\&quot\;)?
395                            (?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,gxo;
396           # Add links to the cloned bugs
397           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
398           # Add links to merged bugs
399           $output =~ s{(?<=Merged )([\d\s]+)(?=[\.<])}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
400           # Add links to blocked bugs
401           $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
402                       {(defined $2?$1.bug_links(bug=>$2):'').$3.
403                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
404           $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
405                        (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
406                       {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
407                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
408           $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks ?)([\d\s\,]+)}
409                       {$1.$2.(bug_links(bug=>$3)).$4.
410                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
411           # Add links to reassigned packages
412           $output =~ s{($config{bug}\sreassigned\sfrom\spackage\s(?:[\`']|\&\#39;))([^']+?)((?:'|\&\#39;|\&quot\;)
413                                \sto\s(?:[\`']|\&\#39;|\&quot\;))([^']+?)((?:'|\&\#39;|\&quot\;))}
414           {$1.package_links(package=>$2).$3.
415                package_links(package=>$4).$5}exo;
416           if (defined $time) {
417                $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
418           }
419           $output .= qq{(<a href="} .
420                html_escape(bug_links(bug => $bug_number,
421                                      options => {msg => ($msg_number+1)},
422                                      links_only => 1,
423                                     )
424                           ) . '">full text</a>, <a href="' .
425                                html_escape(bug_links(bug => $bug_number,
426                                                      options => {msg => ($msg_number+1),
427                                                                  mbox => 'yes'},
428                                                      links_only => 1)
429                                           ) . '">mbox</a>, '.
430                                               qq{<a href="#$msg_number">link</a>).</p>};
431
432           $output = qq(<div class="$class"><hr><p>\n<a name="$msg_number"></a>\n) . $output . "</p></div>\n";
433      }
434      elsif (/recips/) {
435          my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
436          if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
437                return ();
438           }
439           elsif (defined $msg_id) {
440                $$seen_msg_ids{$msg_id} = 1;
441           }
442           return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
443           $output .= qq(<hr><p class="msgreceived"><a name="$msg_number" href="#$msg_number">🔗</a>\n);
444           $output .= 'View this message in <a href="' . html_escape(bug_links(bug=>$bug_number, links_only => 1, options=>{msg=>$msg_number, mbox=>'yes'})) . '">rfc822 format</a></p>';
445           $output .= handle_email_message($record,
446                                           ref     => $bug_number,
447                                           msg_num => $msg_number,
448                                           %param,
449                                          );
450      }
451      elsif (/autocheck/) {
452           # Do nothing
453      }
454      elsif (/incoming-recv/) {
455          my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
456           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
457                return ();
458           }
459           elsif (defined $msg_id) {
460                $$seen_msg_ids{$msg_id} = 1;
461           }
462           return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
463           # Incomming Mail Message
464           my ($received,$hostname) = record_regex($record,qr/Received: \(at (\S+)\) by (\S+)\;/o);
465           $output .= qq|<hr><p class="msgreceived"><a name="$msg_number"></a><a name="msg$msg_number"></a><a href="#$msg_number">Message #$msg_number</a> received at |.
466                html_escape("$received\@$hostname") .
467                     q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
468                          q|, <a href="| . html_escape(bug_links(bug => $bug_number,
469                                                                 links_only => 1,
470                                                                 options => {msg=>$msg_number,
471                                                                             mbox=>'yes'}
472                                                                )
473                                                      ) .'">mbox</a>, ';
474           my $parser = MIME::Parser->new();
475
476           # this will be cleaned up once it goes out of scope
477           my $tempdir = File::Temp->newdir();
478           $parser->output_under($tempdir->dirname());
479          $parser->filer->ignore_filename(1);
480          my $entity;
481          if ($record->{inner_file}) {
482              $entity = $parser->parse($record->{fh});
483          } else {
484              $entity = $parser->parse_data($record->{text});
485          }
486           my $r_l = reply_headers($entity);
487           $output .= q(<a href=").
488               html_escape('mailto:'.$bug_number.'@'.$config{email_domain}.'?'.
489                           join('&',map {defined $r_l->{$_}?$_.'='.uri_escape_utf8($r_l->{$_}):()} keys %{$r_l})).
490                               qq(">reply</a>);
491
492           $output .= ')'.":</p>\n";
493           $output .= handle_email_message($entity,
494                                           ref     => $bug_number,
495                                           msg_num => $msg_number,
496                                           %param,
497                                          );
498      }
499      else {
500           die "Unknown record type $_";
501      }
502      return $output;
503 }
504
505
506 sub __libravatar_url {
507     my ($email) = @_;
508     if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
509         return undef;
510     }
511     ($email) = grep {/\@/} get_addresses($email);
512     return $config{libravatar_uri}.uri_escape_utf8($email.($config{libravatar_uri_options}//''));
513 }
514
515
516 1;
517
518
519 __END__
520
521
522
523
524
525