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)
4 # Used to filter incoming mail for -mda and importers
5 # This only exposes one function: run
6 # Note: the settings here are highly opinionated. Obviously, this is
7 # Free Software (AGPLv3), so you may change it if you host yourself.
8 package PublicInbox::Filter;
12 use Email::MIME::ContentType qw/parse_content_type/;
15 our $VERSION = '0.0.1';
16 use constant NO_HTML => '*** We only accept plain-text email, no HTML ***';
17 use constant TEXT_ONLY => '*** We only accept plain-text email ***';
19 # start with the same defaults as mailman
20 our $BAD_EXT = qr/\.(exe|bat|cmd|com|pif|scr|vbs|cpl|zip)\s*\z/i;
21 our $MIME_HTML = qr!\btext/x?html\b!i;
22 our $MIME_TEXT_ANY = qr!\btext/[a-z0-9\+\._-]+\b!i;
24 # this is highly opinionated delivery
25 # returns 0 only if there is nothing to deliver
27 my ($class, $mime, $filter) = @_;
29 my $content_type = $mime->header('Content-Type') || 'text/plain';
31 # kill potentially bad/confusing headers
32 # Note: ssoma already does this, but since we mangle the message,
33 # we should do this before it gets to ssoma.
34 foreach my $d (qw(status lines content-length)) {
35 $mime->header_set($d);
38 if ($content_type =~ m!\btext/plain\b!i) {
39 return 1; # yay, nothing to do
40 } elsif ($content_type =~ $MIME_HTML) {
41 $filter->reject(NO_HTML) if $filter;
42 # HTML-only, non-multipart
43 my $body = $mime->body;
44 my $ct_parsed = parse_content_type($content_type);
45 dump_html(\$body, $ct_parsed->{attributes}->{charset});
46 replace_body($mime, $body);
48 } elsif ($content_type =~ m!\bmultipart/!i) {
49 return strip_multipart($mime, $content_type, $filter);
51 $filter->reject(TEXT_ONLY) if $filter;
52 replace_body($mime, "$content_type message scrubbed");
58 my ($mime, $part, $type) = ($_[0], $_[1], $_[3]);
59 # don't copy $_[2], that's the body (it may be huge)
61 # Email::MIME insists on setting Date:, so just set it consistently
62 # to avoid conflicts to avoid git merge conflicts in a split brain
64 unless (defined $part->header('Date')) {
65 my $date = $mime->header('Date') ||
66 'Thu, 01 Jan 1970 00:00:00 +0000';
67 $part->header_set('Date', $date);
70 $part->charset_set(undef);
71 $part->name_set(undef);
72 $part->filename_set(undef);
73 $part->format_set(undef);
74 $part->encoding_set('8bit');
75 $part->disposition_set(undef);
76 $part->content_type_set($type);
77 $part->body_set($_[2]);
80 # converts one part of a multipart message to text
81 sub html_part_to_text {
82 my ($mime, $part) = @_;
83 my $body = $part->body;
84 my $ct_parsed = parse_content_type($part->content_type);
85 dump_html(\$body, $ct_parsed->{attributes}->{charset});
86 replace_part($mime, $part, $body, 'text/plain');
89 # modifies $_[0] in place
91 my ($body, $charset) = @_;
92 $charset ||= 'US-ASCII';
93 my @cmd = qw(lynx -stdin -stderr -dump);
97 # be careful about remote command injection!
98 if ($charset =~ /\A([A-Za-z0-9\-]+)\z/) {
99 push @cmd, "-assume_charset=$charset";
101 if (IPC::Run::run(\@cmd, $body, \$out, \$err)) {
102 $out =~ s/\r\n/\n/sg;
105 # give them an ugly version:
106 $$body = "public-inbox HTML conversion failed: $err\n" .
111 # this is to correct old archives during import.
112 sub strip_multipart {
113 my ($mime, $content_type, $filter) = @_;
119 # scan through all parts once
120 $mime->walk_parts(sub {
122 return if $part->subparts; # walk_parts already recurses
124 # some extensions are just bad, reject them outright
125 my $fn = $part->filename;
126 if (defined($fn) && $fn =~ $BAD_EXT) {
127 $filter->reject("Bad file type: $1") if $filter;
132 my $part_type = $part->content_type || '';
133 if ($part_type =~ m!\btext/plain\b!i) {
135 } elsif ($part_type =~ $MIME_HTML) {
136 $filter->reject(NO_HTML) if $filter;
138 } elsif ($part_type =~ $MIME_TEXT_ANY) {
139 # Give other text attachments the benefit of the doubt,
140 # here? Could be source code or script the user wants
144 } elsif ($part_type eq '' ||
145 $part_type =~ m!\bapplication/octet-stream\b!i) {
146 # unfortunately, some mailers don't set correct types,
147 # let messages of unknown type through but do not
148 # change the sender-specified type
149 if (recheck_type_ok($part)) {
152 $filter->reject("Bad attachment: $part_type ".
157 } elsif ($part_type =~ m!\bapplication/pgp-signature\b!i) {
158 # PGP signatures are not huge, we may keep them.
159 # They can only be valid if it's the last element,
160 # so we keep them iff the message is unmodified:
161 if ($rejected == 0 && !@html) {
165 $filter->reject("unacceptable mime-type: $part_type ".
168 # reject everything else, including non-PGP signatures
173 if ($content_type =~ m!\bmultipart/alternative\b!i) {
174 if (scalar @keep == 1) {
175 return collapse($mime, $keep[0]);
177 } else { # convert HTML parts to plain text
178 foreach my $part (@html) {
179 html_part_to_text($mime, $part);
185 @keep = (Email::MIME->create(
187 content_type => 'text/plain',
188 charset => 'US-ASCII',
191 body_str => 'all attachments scrubbed by '. __PACKAGE__
195 if (scalar(@html) || $rejected) {
196 $mime->parts_set(\@keep);
197 $mime->body_set($mime->body_raw);
206 $mime->header_set('X-Content-Filtered-By', __PACKAGE__ ." $VERSION");
210 my ($mime, $part) = @_;
211 $mime->header_set('Content-Type', $part->content_type);
212 $mime->body_set($part->body_raw);
213 my $cte = $part->header('Content-Transfer-Encoding');
214 if (defined($cte) && $cte ne '') {
215 $mime->header_set('Content-Transfer-Encoding', $cte);
223 $mime->body_set($_[1]);
224 $mime->header_set('Content-Type', 'text/plain');
225 if ($mime->header('Content-Transfer-Encoding')) {
226 $mime->header_set('Content-Transfer-Encoding', undef);
231 # Check for display-able text, no messed up binaries
232 # Note: we can not rewrite the message with the detected mime type
233 sub recheck_type_ok {
236 ((length($s) < 0x10000) && ($s =~ /\A([[:print:]\s]+)\z/s));