user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH] eml: header_str_set: correctly encode UTF-8 headers
Date: Sun, 28 Jun 2020 07:14:40 +0000
Message-ID: <20200628071440.17244-1-e@yhbt.net> (raw)

Apparently, using $1 from an octet string still results in a
multi-byte string.  Thus we need to perform utf8::encode after
the regexp character match to ensure wide characters don't get
passed to encode_base64.

This fixes a bug in which caused -watch to crash when using
PublicInbox::Filter::SubjectTag to remove "[list prefix]"
tags from Subject: lines.

I only found this bug because the proposed -watch updates for
NNTP/IMAP support introduced a possible bug which triggered a
full rescan of old archives:

  https://public-inbox.org/meta/20200627100400.9871-1-e@yhbt.net/
---
 lib/PublicInbox/Eml.pm |  7 +++++--
 t/filter_subjecttag.t  | 10 ++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index ec682b919ea..1fecd41be9f 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -368,9 +368,12 @@ sub header_str_set {
 	my ($self, $name, @vals) = @_;
 	for (@vals) {
 		next unless /[^\x20-\x7e]/;
-		utf8::encode($_); # to octets
 		# 39: int((75 - length("Subject: =?UTF-8?B?".'?=') ) / 4) * 3;
-		s/(.{1,39})/'=?UTF-8?B?'.encode_base64($1, '').'?='/ges;
+		s/(.{1,39})/
+			my $x = $1;
+			utf8::encode($x); # to octets
+			'=?UTF-8?B?'.encode_base64($x, '').'?='
+		/xges;
 	}
 	header_set($self, $name, @vals);
 }
diff --git a/t/filter_subjecttag.t b/t/filter_subjecttag.t
index 75effa2769a..e2d91e7416e 100644
--- a/t/filter_subjecttag.t
+++ b/t/filter_subjecttag.t
@@ -24,4 +24,14 @@ $mime->header_str_set('Subject', '[FOO] bar');
 $mime = $f->delivery($mime);
 is($mime->header('Subject'), 'bar', 'filtered non-reply');
 
+$f = PublicInbox::Filter::SubjectTag->new(-tag => '[sox-devel]');
+my $eml = PublicInbox::Eml->new(<<EOF);
+Subject: Re: [SoX-devel] =?utf-8?b?xaE?=
+
+EOF
+$eml = $f->delivery($eml);
+my $s = $eml->header('Subject');
+utf8::encode($s); # to octets
+is($s, "Re: \xc5\xa1", 'subject filtered correctly');
+
 done_testing();

                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200628071440.17244-1-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git