user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] view: do not offer links to 0-byte multipart attachments
@ 2020-05-25  8:21  5% Eric Wong
  0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2020-05-25  8:21 UTC (permalink / raw)
  To: meta

Offering links to download 0-byte files is useless.  We could
waste memory by preserving $eml->{bdy} during iteration, but
offering attachments of type "multipart" is not very useful,
as users are usually interested in decoded attachments or
the entire raw message.

Fixes: e60231148eb604a3 ("descend into message/(rfc822|news|global) parts")
---
 lib/PublicInbox/View.pm | 5 +++++
 t/psgi_attach.t         | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 93a5b329155..a05ac4142f2 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -499,6 +499,11 @@ sub submsg_hdr ($$) {
 sub attach_link ($$$$;$) {
 	my ($ctx, $ct, $p, $fn, $err) = @_;
 	my ($part, $depth, $idx) = @$p;
+
+	# Eml iteration clobbers multipart ->{bdy}, so do not offer
+	# downloads for 0-byte multipart attachments
+	return unless $part->{bdy};
+
 	my $nl = $idx eq '1' ? '' : "\n"; # like join("\n", ...)
 	my $size = bytes::length($part->body);
 
diff --git a/t/psgi_attach.t b/t/psgi_attach.t
index c6f8072ff9a..9a734f813fa 100644
--- a/t/psgi_attach.t
+++ b/t/psgi_attach.t
@@ -73,6 +73,8 @@ $im->init_bare;
 		my $mid = '20200418222508.GA13918@dcvr';
 		my $irt = '20200418222020.GA2745@dcvr';
 		$res = $cb->(GET("/test/$mid/"));
+		unlike($res->content, qr! multipart/mixed, Size: 0 bytes!,
+			'0-byte download not offered');
 		like($res->content, qr/\bhref="2-embed2x\.eml"/s,
 			'href to message/rfc822 attachment visible');
 		like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,

^ permalink raw reply related	[relevance 5%]

* [PATCH 2/2] descend into message/(rfc822|news|global) parts
  2020-05-16 10:03  7% [PATCH/RFC 0/2] recurse into message/rfc822 parts Eric Wong
@ 2020-05-16 10:03  3% ` Eric Wong
  0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2020-05-16 10:03 UTC (permalink / raw)
  To: meta

Email::MIME never supported this properly, but there's real
instances of forwarded messages as message/rfc822 attachments.
message/news is legacy thing which we'll see in archives, and
message/global appears to be the new thing.

gmime also supports message/rfc2822, so we'll support it anyways
despite lacking other evidence of its existence.

Existing attachments remain downloadable as a whole message,
but individual attachments of subparts are now downloadable
and can be displayed in HTML, too.

Furthermore, ensure Xapian can now search for common headers
inside those messages as well as the message bodies.
---
 lib/PublicInbox/Eml.pm       | 37 +++++++++++++++++++++++-----
 lib/PublicInbox/MsgIter.pm   |  6 ++++-
 lib/PublicInbox/SearchIdx.pm | 47 ++++++++++++++++++++++--------------
 lib/PublicInbox/View.pm      | 30 ++++++++++++++++++++---
 t/eml.t                      | 28 +++++++++++++++++++++
 t/psgi_attach.t              |  9 +++++++
 t/search.t                   | 25 +++++++++++++++++++
 7 files changed, 154 insertions(+), 28 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index ef401141c13..6f6874cd237 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -60,6 +60,14 @@ my %DECODE_FULL = (
 our %STR_TYPE = (text => 1);
 our %STR_SUBTYPE = (plain => 1, html => 1);
 
+# message/* subtypes we descend into
+our %MESSAGE_DESCEND = (
+	news => 1, # RFC 1849 (obsolete, but archives are forever)
+	rfc822 => 1, # RFC 2046
+	rfc2822 => 1, # gmime handles this (but not rfc5322)
+	global => 1, # RFC 6532
+);
+
 my %re_memo;
 sub re_memo ($) {
 	my ($k) = @_;
@@ -149,13 +157,25 @@ sub ct ($) {
 }
 
 # returns a queue of sub-parts iff it's worth descending into
-# TODO: descend into message/rfc822 parts (Email::MIME didn't)
 sub mp_descend ($$) {
 	my ($self, $nr) = @_; # or $once for top-level
-	my $bnd = ct($self)->{attributes}->{boundary} // return; # single-part
+	my $ct = ct($self);
+	my $type = lc($ct->{type});
+	if ($type eq 'message' && $MESSAGE_DESCEND{lc($ct->{subtype})}) {
+		my $nxt = new(undef, body_raw($self));
+		$self->{-call_cb} = $nxt->{is_submsg} = 1;
+		return [ $nxt ];
+	}
+	return if $type ne 'multipart';
+	my $bnd = $ct->{attributes}->{boundary} // return; # single-part
 	return if $bnd eq '' || length($bnd) >= $mime_boundary_length_limit;
 	$bnd = quotemeta($bnd);
 
+	# this is a multipart message that didn't get descended into in
+	# public-inbox <= 1.5.0, so ensure we call the user callback for
+	# this part to not break PSGI downloads.
+	$self->{-call_cb} = $self->{is_submsg};
+
 	# "multipart" messages can exist w/o a body
 	my $bdy = ($nr ? delete($self->{bdy}) : \(body_raw($self))) or return;
 
@@ -189,14 +209,15 @@ sub mp_descend ($$) {
 		# compatibility with Email::MIME
 		$parts[-1] =~ s/\n\r?\n\z/\n/s if $epilogue_missing;
 
-		@parts = grep /[^ \t\r\n]/s, @parts; # ignore empty parts
+		# ignore empty parts
+		@parts = map { new_sub(undef, \$_) } grep /[^ \t\r\n]/s, @parts;
 
 		# Keep "From: someone..." from preamble in old,
 		# buggy versions of git-send-email, otherwise drop it
 		# There's also a case where quoted text showed up in the
 		# preamble
 		# <20060515162817.65F0F1BBAE@citi.umich.edu>
-		unshift(@parts, $pre) if $pre =~ /:/s;
+		unshift(@parts, new_sub(undef, \$pre)) if $pre =~ /:/s;
 		return \@parts;
 	}
 	# "multipart", but no boundary found, treat as single part
@@ -217,6 +238,9 @@ sub each_part {
 	my ($self, $cb, $arg, $once) = @_;
 	my $p = mp_descend($self, $once // 0) or
 					return $cb->([$self, 0, 0], $arg);
+
+	$cb->([$self, 0, 0], $arg) if $self->{-call_cb}; # rare
+
 	$p = [ $p, 0 ];
 	my @s; # our virtual stack
 	my $nr = 0;
@@ -226,11 +250,12 @@ sub each_part {
 		my (undef, @idx) = @$p;
 		@idx = (join('.', @idx));
 		my $depth = ($idx[0] =~ tr/././) + 1;
-		my $sub = new_sub(undef, \(shift @{$p->[0]}));
+		my $sub = shift @{$p->[0]};
 		if ($depth < $mime_nesting_limit &&
 				(my $nxt = mp_descend($sub, $nr))) {
 			push(@s, $p) if scalar @{$p->[0]};
 			$p = [ $nxt, @idx, 0 ];
+			$cb->([$sub, $depth, @idx], $arg) if $sub->{-call_cb};
 		} else { # a leaf node
 			$cb->([$sub, $depth, @idx], $arg);
 		}
@@ -270,7 +295,7 @@ sub subparts {
 	if ($$bdy =~ /^--\Q$bnd\E--[ \t]*\r?\n(.+)\z/sm) {
 		$self->{epilogue} = $1;
 	}
-	map { new_sub(undef, \$_) } @$parts;
+	@$parts;
 }
 
 sub parts_set {
diff --git a/lib/PublicInbox/MsgIter.pm b/lib/PublicInbox/MsgIter.pm
index 7c28d019abc..5ec2a4d9c7f 100644
--- a/lib/PublicInbox/MsgIter.pm
+++ b/lib/PublicInbox/MsgIter.pm
@@ -64,8 +64,12 @@ sub msg_part_text ($$) {
 	# times when it should not have been:
 	#   <87llgalspt.fsf@free.fr>
 	#   <200308111450.h7BEoOu20077@mail.osdl.org>
+	# But also do not try this with ->{is_submsg} (message/rfc822),
+	# since a broken multipart/mixed inside a message/rfc822 part
+	# has not been seen in the wild, yet...
 	if ($err && ($ct =~ m!\btext/\b!i ||
-			$ct =~ m!\bmultipart/mixed\b!i)) {
+			(!$part->{is_submsg} &&
+				$ct =~ m!\bmultipart/mixed\b!i) ) ) {
 		my $cte = $part->header_raw('Content-Transfer-Encoding');
 		if (defined($cte) && $cte =~ /\b7bit\b/i) {
 			$s = $part->body;
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 4bdd69f540b..5f5ae895e43 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -284,6 +284,13 @@ sub index_xapian { # msg_iter callback
 	if (defined $fn && $fn ne '') {
 		index_text($self, $fn, 1, 'XFN');
 	}
+	if ($part->{is_submsg}) {
+		my $mids = mids_for_index($part);
+		index_ids($self, $doc, $part, $mids);
+		my $smsg = PublicInbox::Smsg->new($part);
+		index_users($self, $smsg);
+		index_text($self, $smsg->subject, 1, 'S') if $smsg->subject;
+	}
 
 	my ($s, undef) = msg_part_text($part, $ct);
 	defined $s or return;
@@ -307,6 +314,27 @@ sub index_xapian { # msg_iter callback
 	}
 }
 
+sub index_ids ($$$$) {
+	my ($self, $doc, $hdr, $mids) = @_;
+	for my $mid (@$mids) {
+		index_text($self, $mid, 1, 'XM');
+
+		# because too many Message-IDs are prefixed with
+		# "Pine.LNX."...
+		if ($mid =~ /\w{12,}/) {
+			my @long = ($mid =~ /(\w{3,}+)/g);
+			index_text($self, join(' ', @long), 1, 'XM');
+		}
+	}
+	$doc->add_boolean_term('Q' . $_) for @$mids;
+	for my $l ($hdr->header_raw('List-Id')) {
+		$l =~ /<([^>]+)>/ or next;
+		my $lid = $1;
+		$doc->add_boolean_term('G' . $lid);
+		index_text($self, $lid, 1, 'XL'); # probabilistic
+	}
+}
+
 sub add_xapian ($$$$) {
 	my ($self, $mime, $smsg, $mids) = @_;
 	$smsg->{mime} = $mime; # XXX dangerous
@@ -321,22 +349,12 @@ sub add_xapian ($$$$) {
 	add_val($doc, PublicInbox::Search::DT(), $dt);
 
 	my $tg = term_generator($self);
-
 	$tg->set_document($doc);
 	index_text($self, $subj, 1, 'S') if $subj;
 	index_users($self, $smsg);
 
 	msg_iter($mime, \&index_xapian, [ $self, $doc ]);
-	foreach my $mid (@$mids) {
-		index_text($self, $mid, 1, 'XM');
-
-		# because too many Message-IDs are prefixed with
-		# "Pine.LNX."...
-		if ($mid =~ /\w{12,}/) {
-			my @long = ($mid =~ /(\w{3,}+)/g);
-			index_text($self, join(' ', @long), 1, 'XM');
-		}
-	}
+	index_ids($self, $doc, $hdr, $mids);
 	$smsg->{to} = $smsg->{cc} = ''; # WWW doesn't need these, only NNTP
 	PublicInbox::OverIdx::parse_references($smsg, $hdr, $mids);
 	my $data = $smsg->to_doc_data;
@@ -351,13 +369,6 @@ sub add_xapian ($$$$) {
 			}
 		}
 	}
-	$doc->add_boolean_term('Q' . $_) foreach @$mids;
-	for my $l ($hdr->header_raw('List-Id')) {
-		$l =~ /<([^>]+)>/ or next;
-		my $lid = $1;
-		$doc->add_boolean_term('G' . $lid);
-		index_text($self, $lid, 1, 'XL'); # probabilistic
-	}
 	$self->{xdb}->replace_document($smsg->{num}, $doc);
 }
 
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index ef5f4b3a25e..a1920212194 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -17,6 +17,7 @@ use PublicInbox::Address;
 use PublicInbox::WwwStream;
 use PublicInbox::Reply;
 use PublicInbox::ViewDiff qw(flush_diff);
+use PublicInbox::Eml;
 use POSIX qw(strftime);
 use Time::Local qw(timegm);
 use PublicInbox::Smsg qw(subject_normalized);
@@ -480,6 +481,21 @@ sub multipart_text_as_html {
 	$_[0]->each_part(\&add_text_body, $_[1], 1);
 }
 
+sub submsg_hdr ($$) {
+	my ($ctx, $eml) = @_;
+	my $obfs_ibx = $ctx->{-obfs_ibx};
+	my $rv = $ctx->{obuf};
+	$$rv .= "\n";
+	for my $h (qw(From To Cc Subject Date Message-ID X-Alt-Message-ID)) {
+		my @v = $eml->header($h);
+		for my $v (@v) {
+			obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
+			$v = ascii_html($v);
+			$$rv .= "$h: $v\n";
+		}
+	}
+}
+
 sub attach_link ($$$$;$) {
 	my ($ctx, $ct, $p, $fn, $err) = @_;
 	my ($part, $depth, $idx) = @$p;
@@ -511,6 +527,9 @@ EOF
 	$desc = ascii_html($desc);
 	$$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
 	$$rv .= "</a>\n";
+
+	submsg_hdr($ctx, $part) if $part->{is_submsg};
+
 	undef;
 }
 
@@ -518,6 +537,7 @@ sub add_text_body { # callback for each_part
 	my ($p, $ctx) = @_;
 	my $upfx = $ctx->{mhref};
 	my $ibx = $ctx->{-inbox};
+	my $l = $ctx->{-linkify} //= PublicInbox::Linkify->new;
 	# $p - from each_part: [ Email::MIME-like, depth, $idx ]
 	my ($part, $depth, $idx) = @$p;
 	my $ct = $part->content_type || 'text/plain';
@@ -525,6 +545,12 @@ sub add_text_body { # callback for each_part
 	my ($s, $err) = msg_part_text($part, $ct);
 	return attach_link($ctx, $ct, $p, $fn) unless defined $s;
 
+	my $rv = $ctx->{obuf};
+	if ($part->{is_submsg}) {
+		submsg_hdr($ctx, $part);
+		$$rv .= "\n";
+	}
+
 	# makes no difference to browsers, and don't screw up filename
 	# link generation in diffs with the extra '%0D'
 	$s =~ s/\r\n/\n/sg;
@@ -571,13 +597,11 @@ sub add_text_body { # callback for each_part
 	# split off quoted and unquoted blocks:
 	my @sections = PublicInbox::MsgIter::split_quotes($s);
 	undef $s; # free memory
-	my $rv = $ctx->{obuf};
-	if (defined($fn) || $depth > 0 || $err) {
+	if (defined($fn) || ($depth > 0 && !$part->{is_submsg}) || $err) {
 		# badly-encoded message with $err? tell the world about it!
 		attach_link($ctx, $ct, $p, $fn, $err);
 		$$rv .= "\n";
 	}
-	my $l = $ctx->{-linkify} //= PublicInbox::Linkify->new;
 	foreach my $cur (@sections) {
 		if ($cur =~ /\A>/) {
 			# we use a <span> here to allow users to specify
diff --git a/t/eml.t b/t/eml.t
index c91deb3ab29..b7f58ac7069 100644
--- a/t/eml.t
+++ b/t/eml.t
@@ -117,6 +117,34 @@ EOF
 		'', 'each_part can clobber body');
 }
 
+if ('descend into message/rfc822') {
+	my $eml = eml_load 't/data/message_embed.eml';
+	my @parts;
+	$eml->each_part(sub {
+		my ($part, $level, @ex) = @{$_[0]};
+		push @parts, [ $part, $level, @ex ];
+	});
+	is(scalar(@parts), 6, 'got all parts');
+	like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
+		'first part found');
+	is_deeply([ @{$parts[0]}[1..2] ], [ 1, '1' ],
+		'got expected depth and level for part #0');
+	is($parts[1]->[0]->filename, 'embed2x.eml',
+		'attachment filename found');
+	is_deeply([ @{$parts[1]}[1..2] ], [ 1, '2' ],
+		'got expected depth and level for part #1');
+	is_deeply([ @{$parts[2]}[1..2] ], [ 2, '2.1' ],
+		'got expected depth and level for part #2');
+	is_deeply([ @{$parts[3]}[1..2] ], [ 3, '2.1.1' ],
+		'got expected depth and level for part #3');
+	is_deeply([ @{$parts[4]}[1..2] ], [ 3, '2.1.2' ],
+		'got expected depth and level for part #4');
+	is($parts[4]->[0]->filename, 'test.eml',
+		'another attachment filename found');
+	is_deeply([ @{$parts[5]}[1..2] ], [ 4, '2.1.2.1' ],
+		'got expected depth and level for part #5');
+}
+
 # body-less, boundary-less
 for my $cls (@classes) {
 	my $call = 0;
diff --git a/t/psgi_attach.t b/t/psgi_attach.t
index 12f9e6eeecd..c6f8072ff9a 100644
--- a/t/psgi_attach.t
+++ b/t/psgi_attach.t
@@ -75,6 +75,9 @@ $im->init_bare;
 		$res = $cb->(GET("/test/$mid/"));
 		like($res->content, qr/\bhref="2-embed2x\.eml"/s,
 			'href to message/rfc822 attachment visible');
+		like($res->content, qr/\bhref="2\.1\.2-test\.eml"/s,
+			'href to nested message/rfc822 attachment visible');
+
 		$res = $cb->(GET("/test/$mid/2-embed2x.eml"));
 		my $eml = PublicInbox::Eml->new(\($res->content));
 		is_deeply([ $eml->header_raw('Message-ID') ], [ "<$irt>" ],
@@ -85,6 +88,12 @@ $im->init_bare;
 			'1st attachment is as expected');
 		is($subs[1]->header('Content-Type'), 'message/rfc822',
 			'2nd attachment is as expected');
+
+		$res = $cb->(GET("/test/$mid/2.1.2-test.eml"));
+		$eml = PublicInbox::Eml->new(\($res->content));
+		is_deeply([ $eml->header_raw('Message-ID') ],
+			[ '<20200418214114.7575-1-e@yhbt.net>' ],
+			'nested eml retrieved');
 	});
 }
 done_testing();
diff --git a/t/search.t b/t/search.t
index 6dd5047454a..9d74f5e0532 100644
--- a/t/search.t
+++ b/t/search.t
@@ -479,6 +479,31 @@ EOF
 	is_deeply($found, [], 'matched on phrase with l:');
 }
 
+$ibx->with_umask(sub {
+	$rw_commit->();
+	my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
+	ok($doc_id > 0, 'messages within messages');
+	$rw->commit_txn_lazy;
+	$ro->reopen;
+	my $n_test_eml = $ro->query('n:test.eml');
+	is(scalar(@$n_test_eml), 1, 'got a result');
+	my $n_embed2x_eml = $ro->query('n:embed2x.eml');
+	is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
+	for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
+			20200418214114.7575-1-e@yhbt.net)) {
+		is($ro->query("m:$m")->[0]->{mid},
+			'20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
+		is($ro->query("mid:$m")->[0]->{mid},
+			'20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
+	}
+	is($ro->query('dfpost:4dc62c50')->[0]->{mid},
+		'20200418222508.GA13918@dcvr',
+		'diff search reaches inside message/rfc822');
+	is($ro->query('s:"mail header experiments"')->[0]->{mid},
+		'20200418222508.GA13918@dcvr',
+		'Subject search reaches inside message/rfc822');
+});
+
 done_testing();
 
 1;

^ permalink raw reply related	[relevance 3%]

* [PATCH/RFC 0/2] recurse into message/rfc822 parts
@ 2020-05-16 10:03  7% Eric Wong
  2020-05-16 10:03  3% ` [PATCH 2/2] descend into message/(rfc822|news|global) parts Eric Wong
  0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2020-05-16 10:03 UTC (permalink / raw)
  To: meta

Multipart parts aren't the only things which nest,
message/rfc822 attachments can contain any sort of full
message.

I noticed gmime supports this while evaluating(*) it to replace
Email::MIME, and it seems needed for IMAP support.

Email::MIME seemed to attempt to support descending into
message/*, but didn't do it properly, so it never got
triggered.

There's definitely some message/rfc822 attachments in various
archives out there, and it looks like message/global is becoming
a thing, and some message/news for legacy stuff...

gmime supports message/rfc2822, too, which doesn't seem
specified anywhere...

Search indexing multiple From/To/Cc/Subject/Message-ID/List-Id
headers is straightforward, Date is not...

Also, note the t/data/message_embed.eml example includes
a circular reference :)   I have no intention of doing threading
for attached messages (AFAIK no mail client does this), but
maybe making the contents of References / In-Reply-To is
a helpful thing in general.

Eric Wong (2):
  t/psgi_attach: assert message/* parts are downloadable
  descend into message/(rfc822|news|global) parts

 MANIFEST                     |   1 +
 lib/PublicInbox/Eml.pm       |  37 ++++++--
 lib/PublicInbox/MsgIter.pm   |   6 +-
 lib/PublicInbox/SearchIdx.pm |  47 ++++++----
 lib/PublicInbox/View.pm      |  30 ++++++-
 t/data/message_embed.eml     | 163 +++++++++++++++++++++++++++++++++++
 t/eml.t                      |  28 ++++++
 t/psgi_attach.t              |  27 ++++++
 t/search.t                   |  25 ++++++
 9 files changed, 336 insertions(+), 28 deletions(-)
 create mode 100644 t/data/message_embed.eml


^ permalink raw reply	[relevance 7%]

Results 1-3 of 3 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2020-05-16 10:03  7% [PATCH/RFC 0/2] recurse into message/rfc822 parts Eric Wong
2020-05-16 10:03  3% ` [PATCH 2/2] descend into message/(rfc822|news|global) parts Eric Wong
2020-05-25  8:21  5% [PATCH] view: do not offer links to 0-byte multipart attachments Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).