]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter.pm
filter: fix handling of bad attachments
[public-inbox.git] / lib / PublicInbox / Filter.pm
1 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # This only exposes one function: run
5 # Note: the settings here are highly opinionated.  Obviously, this is
6 # Free Software (AGPLv3), so you may change it if you host yourself.
7 package PublicInbox::Filter;
8 use strict;
9 use warnings;
10 use Email::MIME;
11 use Email::MIME::ContentType qw/parse_content_type/;
12 use Email::Filter;
13 use IPC::Run;
14 our $VERSION = '0.0.1';
15 use constant NO_HTML => '*** We only accept plain-text email, no HTML ***';
16 use constant TEXT_ONLY => '*** We only accept plain-text email ***';
17
18 # start with the same defaults as mailman
19 our $BAD_EXT = qr/\.(exe|bat|cmd|com|pif|scr|vbs|cpl|zip)\s*\z/i;
20 our $MIME_HTML = qr!\btext/x?html\b!i;
21 our $MIME_TEXT_ANY = qr!\btext/[a-z0-9\+\._-]+\b!i;
22
23 # this is highly opinionated delivery
24 # returns 0 only if there is nothing to deliver
25 sub run {
26         my ($class, $mime, $filter) = @_;
27
28         my $content_type = $mime->header('Content-Type') || 'text/plain';
29
30         # kill potentially bad/confusing headers
31         # Note: ssoma already does this, but since we mangle the message,
32         # we should do this before it gets to ssoma.
33         # We also kill Mail-{Followup,Reply}-To headers due to
34         # the nature of public-inbox having no real subscribers.
35         foreach my $d (qw(status lines content-length
36                         mail-followup-to mail-reply-to)) {
37                 $mime->header_set($d);
38         }
39
40         if ($content_type =~ m!\btext/plain\b!i) {
41                 return 1; # yay, nothing to do
42         } elsif ($content_type =~ $MIME_HTML) {
43                 $filter->reject(NO_HTML) if $filter;
44                 # HTML-only, non-multipart
45                 my $body = $mime->body;
46                 my $ct_parsed = parse_content_type($content_type);
47                 dump_html(\$body, $ct_parsed->{attributes}->{charset});
48                 replace_body($mime, $body);
49                 return 1;
50         } elsif ($content_type =~ m!\bmultipart/!i) {
51                 return strip_multipart($mime, $content_type, $filter);
52         } else {
53                 $filter->reject(TEXT_ONLY) if $filter;
54                 replace_body($mime, "$content_type message scrubbed");
55                 return 0;
56         }
57 }
58
59 sub replace_part {
60         my ($mime, $part, $type) = ($_[0], $_[1], $_[3]);
61         # don't copy $_[2], that's the body (it may be huge)
62
63         # Email::MIME insists on setting Date:, so just set it consistently
64         # to avoid conflicts to avoid git merge conflicts in a split brain
65         # situation.
66         unless (defined $part->header('Date')) {
67                 my $date = $mime->header('Date') ||
68                            'Thu, 01 Jan 1970 00:00:00 +0000';
69                 $part->header_set('Date', $date);
70         }
71
72         $part->charset_set(undef);
73         $part->name_set(undef);
74         $part->filename_set(undef);
75         $part->format_set(undef);
76         $part->encoding_set('8bit');
77         $part->disposition_set(undef);
78         $part->content_type_set($type);
79         $part->body_set($_[2]);
80 }
81
82 # converts one part of a multipart message to text
83 sub html_part_to_text {
84         my ($mime, $part) = @_;
85         my $body = $part->body;
86         my $ct_parsed = parse_content_type($part->content_type);
87         dump_html(\$body, $ct_parsed->{attributes}->{charset});
88         replace_part($mime, $part, $body, 'text/plain');
89 }
90
91 # modifies $_[0] in place
92 sub dump_html {
93         my ($body, $charset) = @_;
94         $charset ||= 'US-ASCII';
95         my @cmd = qw(lynx -stdin -stderr -dump);
96         my $out = "";
97         my $err = "";
98
99         # be careful about remote command injection!
100         if ($charset =~ /\A([A-Za-z0-9\-]+)\z/) {
101                 push @cmd, "-assume_charset=$charset";
102         }
103         if (IPC::Run::run(\@cmd, $body, \$out, \$err)) {
104                 $out =~ s/\r\n/\n/sg;
105                 $$body = $out;
106         } else {
107                 # give them an ugly version:
108                 $$body = "public-inbox HTML conversion failed: $err\n" .
109                          $$body . "\n";
110         }
111 }
112
113 # this is to correct old archives during import.
114 sub strip_multipart {
115         my ($mime, $content_type, $filter) = @_;
116
117         my (@html, @keep);
118         my $rejected = 0;
119         my $ok = 1;
120
121         # scan through all parts once
122         $mime->walk_parts(sub {
123                 my ($part) = @_;
124                 return if $part->subparts; # walk_parts already recurses
125
126                 # some extensions are just bad, reject them outright
127                 my $fn = $part->filename;
128                 if (defined($fn) && $fn =~ $BAD_EXT) {
129                         $filter->reject("Bad file type: $1") if $filter;
130                         $rejected++;
131                         return;
132                 }
133
134                 my $part_type = $part->content_type || '';
135                 if ($part_type =~ m!\btext/plain\b!i) {
136                         push @keep, $part;
137                 } elsif ($part_type =~ $MIME_HTML) {
138                         $filter->reject(NO_HTML) if $filter;
139                         push @html, $part;
140                 } elsif ($part_type =~ $MIME_TEXT_ANY) {
141                         # Give other text attachments the benefit of the doubt,
142                         # here?  Could be source code or script the user wants
143                         # help with.
144
145                         push @keep, $part;
146                 } elsif ($part_type eq '' ||
147                          $part_type =~ m!\bapplication/octet-stream\b!i) {
148                         # unfortunately, some mailers don't set correct types,
149                         # let messages of unknown type through but do not
150                         # change the sender-specified type
151                         if (recheck_type_ok($part)) {
152                                 push @keep, $part;
153                         } elsif ($filter) {
154                                 $filter->reject("Bad attachment: $part_type ".
155                                                 TEXT_ONLY);
156                         } else {
157                                 $rejected++;
158                         }
159                 } elsif ($part_type =~ m!\bapplication/pgp-signature\b!i) {
160                         # PGP signatures are not huge, we may keep them.
161                         # They can only be valid if it's the last element,
162                         # so we keep them iff the message is unmodified:
163                         if ($rejected == 0 && !@html) {
164                                 push @keep, $part;
165                         }
166                 } elsif ($filter) {
167                         $filter->reject("unacceptable mime-type: $part_type ".
168                                         TEXT_ONLY);
169                 } else {
170                         # reject everything else, including non-PGP signatures
171                         $rejected++;
172                 }
173         });
174
175         if ($content_type =~ m!\bmultipart/alternative\b!i) {
176                 if (scalar @keep == 1) {
177                         return collapse($mime, $keep[0]);
178                 }
179         } else { # convert HTML parts to plain text
180                 foreach my $part (@html) {
181                         html_part_to_text($mime, $part);
182                         push @keep, $part;
183                 }
184         }
185
186         if (@keep == 0) {
187                 @keep = (Email::MIME->create(
188                         attributes => {
189                                 content_type => 'text/plain',
190                                 charset => 'US-ASCII',
191                                 encoding => '8bit',
192                         },
193                         body_str => 'all attachments scrubbed by '. __PACKAGE__
194                 ));
195                 $ok = 0;
196         }
197         if (scalar(@html) || $rejected) {
198                 $mime->parts_set(\@keep);
199                 $mime->body_set($mime->body_raw);
200                 mark_changed($mime);
201         } # else: no changes
202
203         return $ok;
204 }
205
206 sub mark_changed {
207         my ($mime) = @_;
208         $mime->header_set('X-Content-Filtered-By', __PACKAGE__ ." $VERSION");
209 }
210
211 sub collapse {
212         my ($mime, $part) = @_;
213         $mime->header_set('Content-Type', $part->content_type);
214         $mime->body_set($part->body_raw);
215         my $cte = $part->header('Content-Transfer-Encoding');
216         if (defined($cte) && $cte ne '') {
217                 $mime->header_set('Content-Transfer-Encoding', $cte);
218         }
219         mark_changed($mime);
220         return 1;
221 }
222
223 sub replace_body {
224         my $mime = $_[0];
225         $mime->body_set($_[1]);
226         $mime->header_set('Content-Type', 'text/plain');
227         if ($mime->header('Content-Transfer-Encoding')) {
228                 $mime->header_set('Content-Transfer-Encoding', undef);
229         }
230         mark_changed($mime);
231 }
232
233 # Check for display-able text, no messed up binaries
234 # Note: we can not rewrite the message with the detected mime type
235 sub recheck_type_ok {
236         my ($part) = @_;
237         my $s = $part->body;
238         ((length($s) < 0x10000) && ($s =~ /\A([[:print:]\s]+)\z/s));
239 }
240
241 1;