]> git.donarmstrong.com Git - debbugs.git/commitdiff
Handle RFC1522 escaped commas in structured headers (#1041638) master
authorDon Armstrong <don@donarmstrong.com>
Mon, 24 Jul 2023 23:17:17 +0000 (16:17 -0700)
committerDon Armstrong <don@donarmstrong.com>
Mon, 24 Jul 2023 23:20:37 +0000 (16:20 -0700)
debian/changelog
lib/Debbugs/MIME.pm
scripts/process
t/01_mime.t

index 60dfa6c721688d6b8c7226eb9a7c506008f773b9..5cdd98c17832a1846accb5809fb54f182948b818 100644 (file)
@@ -6,6 +6,7 @@ debbugs (3.0.0~alpha.1) unstable; urgency=medium
   * Fix unescaped From (closes: #983847)
   * Actually return message/rfc822 when there is a single message instead
     of mbox (closes: #1009181)
+  * Fix missing escaping of comma in address fields (closes: #1041638)
 
  -- Don Armstrong <don@debian.org>  Fri, 09 Mar 2018 11:17:10 -0800
 
index fec3b6e2dc4e1dd43d8d31c020c27343407d0e30..579f3329ffd73ee27598b6d806e41bc8a8bdad1e 100644 (file)
@@ -44,7 +44,7 @@ BEGIN {
     %EXPORT_TAGS = (mime => [qw(parse create_mime_message getmailbody),
                             qw(parse_to_mime_entity),
                            ],
-                   rfc1522 => [qw(decode_rfc1522 encode_rfc1522)],
+                   rfc1522 => [qw(decode_rfc1522 encode_rfc1522 handle_escaped_commas)],
                   );
     @EXPORT_OK=();
     Exporter::export_ok_tags(keys %EXPORT_TAGS);
@@ -54,6 +54,7 @@ BEGIN {
 use File::Path qw(remove_tree);
 use File::Temp qw(tempdir);
 use MIME::Parser;
+use Mail::Message::Field;
 
 use POSIX qw(strftime);
 use List::AllUtils qw(apply);
@@ -396,4 +397,36 @@ sub encode_rfc1522 {
      return $string;
 }
 
+=head2
+
+    $header = handle_escaped_commas('','From: ')
+
+Handle commas in addresses which have been RFC1522 escaped and now need to be
+quoted to avoid parsing as a record separator.
+
+=cut
+
+sub handle_escaped_commas {
+    my ($modified_hdr, $orig_hdr) = @_;
+
+    my $field = Mail::Message::Field->new($orig_hdr);
+    # if the header isn't structured, it can't contain an address
+    if (not $field->isStructured()) {
+       return $modified_hdr
+    }
+    if ($field->name() !~ m/^(?:to|from|reply-to)$/) {
+       return $modified_hdr
+    }
+    my @addresses = $field->addresses();
+    if (not @addresses) {
+       return $modified_hdr
+    }
+    my @return_addresses;
+    for my $address (@addresses) {
+       $address->phrase(decode_rfc1522($address->phrase()));
+       push @return_addresses, $address->format();
+    }
+    return join(', ',@return_addresses)
+}
+
 1;
index 4e4d97afbe816f002b0adb941aa2282733221ccd..831099867c0dce57e5f60932c4c5b86c70500564 100755 (executable)
@@ -184,7 +184,7 @@ for my $hdr (@headerlines) {
                         mail-followup-to|
                         references):
                 |From\s|X-Debbugs-)/xi;
-    $fwd .= encode_utf8($hdr)."\n" if $ins;
+    $fwd .= $orig_hdr."\n" if $ins;
     # print {$debugfh} ">$_<\n";
     if (s/^(\S+):\s*//) {
        my $v = lc $1;
@@ -192,7 +192,13 @@ for my $hdr (@headerlines) {
            push @common_headers, 'X-Loop',$_;
        }
        print {$debugfh} ">$v=$_<\n";
-       $header{$v} = $_;
+       # Handle a comma which is escaped being passed through un-escaped. See
+       # https://bugs.debian.org/1041638
+       if ($_ =~ m/,/ and not $orig_hdr =~ m/,/) {
+           $header{$v} = handle_escaped_commas($_,$orig_hdr);
+       } else {
+           $header{$v} = $_;
+       }
     } else {
        print {$debugfh} "!>$_<\n";
     }
@@ -718,7 +724,7 @@ if ($ref<0) { # new bug report
                                             );
             }
         }
-        if ($name eq 'usertags'){
+        if ($name eq 'usertags' and defined $current_user){
             my %user_tags;
             read_usertags(\%user_tags, $current_user);
             $value =~ s/(?:^\s+|\s+$)//g;
index dcd3b7608f869c41d6f1eed8d666d8ba55832393..ecad37ba6bb08b21d5845ea18534e5b713783d0d 100644 (file)
@@ -1,7 +1,7 @@
 # -*- mode: cperl;-*-
 # $Id: 01_mime.t,v 1.1 2005/08/17 21:46:17 don Exp $
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 
 use warnings;
 use strict;
@@ -36,7 +36,8 @@ ok(Debbugs::MIME::decode_rfc1522(Debbugs::MIME::encode_rfc1522(encode_utf8($test
   "encode_rfc1522 encodes strings that decode_rfc1522 can decode");
 ok(Debbugs::MIME::decode_rfc1522(Debbugs::MIME::encode_rfc1522(encode_utf8($test_str3))) eq $test_str3,
   "encode_rfc1522 properly handles parenthesis and \"");
-
+ok(Debbugs::MIME::handle_escaped_commas(q(),q(From: =?UTF-8?Q?Armstrong=2C?= Don <don@donarmstrong.com>)) eq q("Armstrong, Don" <don@donarmstrong.com>),
+  "handle_escaped_commas properly handles commas in RFC1522 encoded strings");
 
 # Make sure that create_mime_message has encoded headers and doesn't enclude any 8-bit characters