]> Sergey Matveev's repositories - public-inbox.git/commitdiff
msg_part_text: discover text in application/octet-stream
authorEric Wong <e@80x24.org>
Thu, 11 Mar 2021 01:45:39 +0000 (19:45 -0600)
committerEric Wong <e@80x24.org>
Fri, 12 Mar 2021 02:18:15 +0000 (02:18 +0000)
Some poorly-configured MUAs will send application/octet-stream
even for text-only attachments.  We can't make expect all MUAs
are configured with proper MIME types, and there is plenty of
historical mail that falls into this unfortunate criteria.

v2: simplify the check and ensures returned text is Perl "utf8"

MANIFEST
lib/PublicInbox/MsgIter.pm
t/msg_iter.t
xt/eml_octet-stream.t [new file with mode: 0644]

index 8662d2c0bd9874d2a71ecc43c840cbff4cc28d5a..941a1f90a7189abaa2494815c0f61ba9b802ee42 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -472,6 +472,7 @@ xt/cmp-msgstr.t
 xt/cmp-msgview.t
 xt/create-many-inboxes.t
 xt/eml_check_limits.t
+xt/eml_octet-stream.t
 xt/git-http-backend.t
 xt/git_async_cmp.t
 xt/httpd-async-stream.t
index c503eb98b8c502e3ecda265007772fada888424f..9c6581cc9dd63415a32a0a8f59bafae1cdb2b86d 100644 (file)
@@ -84,6 +84,14 @@ sub msg_part_text ($$) {
                # If forcing charset=UTF-8 failed,
                # caller will warn further down...
                $s = $part->body if $@;
+       } elsif ($err && $ct =~ m!\bapplication/octet-stream\b!i) {
+               # Some unconfigured/poorly-configured MUAs will set
+               # application/octet-stream even for all text attachments.
+               # Try to see if it's printable text that we can index
+               # and display:
+               $s = $part->body;
+               utf8::decode($s);
+               undef($s =~ /[^\p{XPosixPrint}\s]/s ? $s : $err);
        }
        ($s, $err);
 }
index e46d515c8ee4de99aafc8653ee542338f89648f4..ae3594da08324f67b7747ce9b0d8a6432cad02ac 100644 (file)
@@ -1,10 +1,8 @@
 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
-use strict;
-use warnings;
-use Test::More;
-use PublicInbox::TestCommon;
+use strict; use v5.10.1; use PublicInbox::TestCommon;
 use PublicInbox::Hval qw(ascii_html);
+use MIME::QuotedPrint 3.05 qw(encode_qp);
 use_ok('PublicInbox::MsgIter');
 
 {
@@ -88,5 +86,62 @@ use_ok('PublicInbox::MsgIter');
        is($check[1], $nq, 'long quoted section matches');
 }
 
+{
+       open my $fh, '<', 't/utf8.eml' or BAIL_OUT $!;
+       my $expect = do { local $/; <$fh>  };
+       my $qp_patch = encode_qp($expect, "\r\n");
+       my $common = <<EOM;
+Content-Type: multipart/mixed; boundary="DEADBEEF"
+MIME-Version: 1.0
+
+--DEADBEEF
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+       charset=utf-8
+
+blah
+
+--DEADBEEF
+Content-Disposition: attachment;
+       filename=foo.patch
+Content-Type: application/octet-stream;
+       x-unix-mode=0644;
+       name="foo.patch"
+Content-Transfer-Encoding: quoted-printable
+EOM
+       my $eml = PublicInbox::Eml->new(<<EOM);
+$common
+$qp_patch
+--DEADBEEF--
+EOM
+       my @parts;
+       $eml->each_part(sub {
+               my ($part, $level, @ex) = @{$_[0]};
+               my ($s, $err) = msg_part_text($part, $part->content_type);
+               push @parts, $s;
+       });
+       $expect =~ s/\n/\r\n/sg;
+       utf8::decode($expect); # aka "bytes2str"
+       is_deeply(\@parts, [ "blah\r\n", $expect ],
+               'fallback to application/octet-stream as UTF-8 text');
+
+       my $qp_binary = encode_qp("Binary\0crap", "\r\n");
+       $eml = PublicInbox::Eml->new(<<EOM);
+$common
+$qp_binary
+--DEADBEEF--
+EOM
+       @parts = ();
+       my @err;
+       $eml->each_part(sub {
+               my ($part, $level, @ex) = @{$_[0]};
+               my ($s, $err) = msg_part_text($part, $part->content_type);
+               push @parts, $s;
+               push @err, $err;
+       });
+       is_deeply(\@parts, [ "blah\r\n", undef ],
+               'non-text ignored in octet-stream');
+       ok($err[1], 'got error for second element');
+}
+
 done_testing();
-1;
diff --git a/xt/eml_octet-stream.t b/xt/eml_octet-stream.t
new file mode 100644 (file)
index 0000000..8173aec
--- /dev/null
@@ -0,0 +1,77 @@
+#!perl -w
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict; use v5.10.1; use PublicInbox::TestCommon;
+use PublicInbox::Git;
+use PublicInbox::Eml;
+use PublicInbox::MsgIter qw(msg_part_text);
+use PublicInbox::LeiToMail;
+my $eml2mboxcl2 = PublicInbox::LeiToMail->can('eml2mboxcl2');
+my $git_dir = $ENV{GIANT_GIT_DIR};
+plan 'skip_all' => "GIANT_GIT_DIR not defined for $0" unless defined($git_dir);
+use Data::Dumper;
+$Data::Dumper::Useqq = 1;
+my $mboxfh;
+if (my $out = $ENV{DEBUG_MBOXCL2}) {
+       BAIL_OUT("$out exists") if -s $out;
+       open $mboxfh, '>', $out or BAIL_OUT "open $out: $!";
+} else {
+       diag "DEBUG_MBOXCL2 unset, not saving debug output";
+}
+
+my $git = PublicInbox::Git->new($git_dir);
+my @cat = qw(cat-file --buffer --batch-check --batch-all-objects);
+if (require_git(2.19, 1)) {
+       push @cat, '--unordered';
+} else {
+       warn "git <2.19, cat-file lacks --unordered, locality suffers\n";
+}
+my ($errs, $ok, $tot);
+$errs = $ok = $tot = 0;
+my $ep = sub { # eml->each_part callback
+       my ($part, $level, @ex) = @{$_[0]};
+       ++$tot;
+       my $ct = $part->content_type // return;
+       $ct =~ m!\bapplication/octet-stream\b!i or return;
+       my ($s, $err) = msg_part_text($part, $ct);
+       if (defined $s) {
+               ++$ok;
+       } else {
+               warn "binary $err\n";
+               ++$errs;
+               my $x = eval { $part->body };
+               if ($@) {
+                       warn "decode totally failed: $@";
+               } else {
+                       my ($bad) = ($x =~ m/([\p{XPosixPrint}\s]{0,10}
+                                               [^\p{XPosixPrint}\s]+
+                                               [\p{XPosixPrint}\s]{0,10})/sx);
+                       warn Dumper([$bad]);
+               }
+
+               push @{$_[1]}, $err; # $fail
+       }
+};
+
+my $cb = sub {
+       my ($bref, $oid) = @_;
+       my $eml = PublicInbox::Eml->new($bref);
+       local $SIG{__WARN__} = sub { diag("$oid ", @_) };
+       $eml->each_part($ep, my $fail = []);
+       if (@$fail && $mboxfh) {
+               diag "@$fail";
+               print $mboxfh ${$eml2mboxcl2->($eml, { blob => $oid })} or
+                       BAIL_OUT "print: $!";
+       }
+};
+my $cat = $git->popen(@cat);
+while (<$cat>) {
+       my ($oid, $type, $size) = split(/ /);
+       $git->cat_async($oid, $cb) if $size && $type eq 'blob';
+}
+$git->cat_async_wait;
+note "$errs errors";
+note "$ok/$tot messages had text as application/octet-stream";
+ok 1;
+
+done_testing;