]> Sergey Matveev's repositories - public-inbox.git/commitdiff
eml: header_str_set: correctly encode UTF-8 headers
authorEric Wong <e@yhbt.net>
Sun, 28 Jun 2020 07:14:40 +0000 (07:14 +0000)
committerEric Wong <e@yhbt.net>
Sun, 28 Jun 2020 18:35:47 +0000 (18:35 +0000)
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
t/filter_subjecttag.t

index ec682b919eab0d796d6d49089f94ed594a00f2f8..1fecd41be9f4b1f2d11d4591918935ac9b1aba16 100644 (file)
@@ -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);
 }
index 75effa2769a5ad9052dc49b9e96d26c67f38514c..e2d91e7416e2a140a7ed8af8bc2a5bd73dfdbdeb 100644 (file)
@@ -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();