]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter.pm
drop Mail-Followup-To, Mail-Reply-To, and Reply-To
[public-inbox.git] / lib / PublicInbox / Filter.pm
1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
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::Open2;
14 our $VERSION = '0.0.1';
15
16 # start with the same defaults as mailman
17 our $BAD_EXT = qr/\.(?:exe|bat|cmd|com|pif|scr|vbs|cpl)\z/i;
18
19 # this is highly opinionated delivery
20 # returns 0 only if there is nothing to deliver
21 sub run {
22         my ($class, $simple) = @_;
23
24         my $content_type = $simple->header("Content-Type") || "text/plain";
25
26         # kill potentially bad/confusing headers
27         # Note: ssoma already does this, but since we mangle the message,
28         # we should do this before it gets to ssoma.
29         # We also kill Mail-{Followup,Reply}-To and Reply-To headers due to
30         # the nature of public-inbox having no real subscribers.
31         foreach my $d (qw(status lines content-length
32                         mail-followup-to mail-reply-to reply-to)) {
33                 $simple->header_set($d);
34         }
35
36         if ($content_type =~ m!\btext/plain\b!i) {
37                 return 1; # yay, nothing to do
38         } elsif ($content_type =~ m!\btext/html\b!i) {
39                 # HTML-only, non-multipart
40                 my $body = $simple->body;
41                 my $ct_parsed = parse_content_type($content_type);
42                 dump_html($body, $ct_parsed->{attributes}->{charset});
43                 replace_body($simple, $body);
44                 return 1;
45         } elsif ($content_type =~ m!\bmultipart/!i) {
46                 return strip_multipart($simple, $content_type);
47         } else {
48                 replace_body($simple, "$content_type message scrubbed");
49                 return 0;
50         }
51 }
52
53 sub replace_part {
54         my ($simple, $part, $type) = ($_[0], $_[1], $_[3]);
55         # don't copy $_[2], that's the body (it may be huge)
56
57         # Email::MIME insists on setting Date:, so just set it consistently
58         # to avoid conflicts to avoid git merge conflicts in a split brain
59         # situation.
60         unless (defined $part->header('Date')) {
61                 my $date = $simple->header('Date') ||
62                            'Thu, 01 Jan 1970 00:00:00 +0000';
63                 $part->header_set('Date', $date);
64         }
65
66         $part->charset_set(undef);
67         $part->name_set(undef);
68         $part->filename_set(undef);
69         $part->format_set(undef);
70         $part->encoding_set('8bit');
71         $part->disposition_set(undef);
72         $part->content_type_set($type);
73         $part->body_set($_[2]);
74 }
75
76 # converts one part of a multipart message to text
77 sub html_part_to_text {
78         my ($simple, $part) = @_;
79         my $body = $part->body;
80         my $ct_parsed = parse_content_type($part->content_type);
81         dump_html($body, $ct_parsed->{attributes}->{charset});
82         replace_part($simple, $part, $body, 'text/plain');
83 }
84
85 # modifies $_[0] in place
86 sub dump_html {
87         my $charset = $_[1] || 'US-ASCII';
88         my $cmd = "lynx -stdin -dump";
89
90         # be careful about remote command injection!
91         if ($charset =~ /\A[A-Za-z0-9\-]+\z/) {
92                 $cmd .= " -assume_charset=$charset";
93         }
94
95         my $pid = open2(my $out, my $in, $cmd);
96         print $in $_[0];
97         close $in;
98         {
99                 local $/;
100                 $_[0] = <$out>;
101         }
102         waitpid($pid, 0);
103 }
104
105 # this is to correct user errors and not expected to cover all corner cases
106 # if users don't want to hit this, they should be sending text/plain messages
107 # unfortunately, too many people send HTML mail and we'll attempt to convert
108 # it to something safer, smaller and harder-to-track.
109 sub strip_multipart {
110         my ($simple, $content_type) = @_;
111         my $mime = Email::MIME->new($simple->as_string);
112
113         my (@html, @keep);
114         my $rejected = 0;
115         my $ok = 1;
116
117         # scan through all parts once
118         $mime->walk_parts(sub {
119                 my ($part) = @_;
120                 return if $part->subparts; # walk_parts already recurses
121
122                 # some extensions are just bad, reject them outright
123                 my $fn = $part->filename;
124                 if (defined($fn) && $fn =~ $BAD_EXT) {
125                         $rejected++;
126                         return;
127                 }
128
129                 my $part_type = $part->content_type;
130                 if ($part_type =~ m!\btext/plain\b!i) {
131                         push @keep, $part;
132                 } elsif ($part_type =~ m!\btext/html\b!i) {
133                         push @html, $part;
134                 } elsif ($part_type =~ m!\btext/[a-z0-9\+\._-]+\b!i) {
135                         # Give other text attachments the benefit of the doubt,
136                         # here?  Could be source code or script the user wants
137                         # help with.
138
139                         push @keep, $part;
140                 } else {
141                         # reject everything else
142                         #
143                         # Yes, we drop GPG/PGP signatures because:
144                         # * hardly anybody bothers to verify signatures
145                         # * we strip/convert HTML parts, which could invalidate
146                         #   the signature
147                         # * they increase the size of messages greatly
148                         #   (especially short ones)
149                         # * they do not compress well
150                         #
151                         # Instead, rely on soft verification measures:
152                         # * content of the message is most important
153                         # * we encourage Cc: all replies, so replies go to
154                         #   the original sender
155                         # * Received, User-Agent, and similar headers
156                         #   (this is also to encourage using self-hosted mail
157                         #   servers (using 100% Free Software, of course :)
158                         #
159                         # Furthermore, identity theft is uncommon in Free/Open
160                         # Source, even in communities where signatures are rare.
161                         $rejected++;
162                 }
163         });
164
165         if ($content_type =~ m!\bmultipart/alternative\b!i) {
166                 if (scalar @keep == 1) {
167                         return collapse($simple, $keep[0]);
168                 }
169         } else { # convert HTML parts to plain text
170                 foreach my $part (@html) {
171                         html_part_to_text($simple, $part);
172                         push @keep, $part;
173                 }
174         }
175
176         if (@keep == 0) {
177                 @keep = (Email::MIME->create(
178                         attributes => {
179                                 content_type => 'text/plain',
180                                 charset => 'US-ASCII',
181                                 encoding => '8bit',
182                         },
183                         body_str => 'all attachments scrubbed by '. __PACKAGE__
184                 ));
185                 $ok = 0;
186         }
187         if (scalar(@html) || $rejected) {
188                 $mime->parts_set(\@keep);
189                 $simple->body_set($mime->body_raw);
190                 mark_changed($simple);
191         } # else: no changes
192
193         return $ok;
194 }
195
196 sub mark_changed {
197         my ($simple) = @_;
198         $simple->header_set("X-Content-Filtered-By", __PACKAGE__ ." $VERSION");
199 }
200
201 sub collapse {
202         my ($simple, $part) = @_;
203         $simple->header_set("Content-Type", $part->content_type);
204         $simple->body_set($part->body_raw);
205         mark_changed($simple);
206         return 1;
207 }
208
209 sub replace_body {
210         my $simple = $_[0];
211         $simple->body_set($_[1]);
212         $simple->header_set("Content-Type", "text/plain");
213         if ($simple->header("Content-Transfer-Encoding")) {
214                 $simple->header_set("Content-Transfer-Encoding", undef);
215         }
216         mark_changed($simple);
217 }
218
219 1;