Skip to content

Commit

Permalink
Merge pull request #1572 from ikedas/issue-999
Browse files Browse the repository at this point in the history
Display name in `From:` header field should be quoted / unquoted appropriately
  • Loading branch information
racke authored Dec 13, 2023
2 parents 26b2381 + cfee65c commit 77627d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/lib/Sympa/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,16 @@ sub _get_sender_email {
$sender = Sympa::Tools::Text::canonic_email(
$sender_hdr[0]->address);
my $phrase = $sender_hdr[0]->phrase;
if (defined $phrase and length $phrase) {
if (length($phrase // '')) {
# Unquote quoted strings in the phrase.
# cf. RFC 5322, 3.2.4 and 3.2.5.
$phrase =~ s{(?<!\S) " ((?:\\. | [^\\"])*) " (?!\S)}{
my $qcontent = $1;
$qcontent =~ s/\\(.)/$1/grs;
}egsx;

# Decode B- or Q-encoded words. Note that some (mistaken)
# implementations may quote encoded words.
$gecos = MIME::EncWords::decode_mimewords($phrase,
Charset => 'UTF-8');
# Eliminate hostile characters.
Expand Down
10 changes: 9 additions & 1 deletion src/lib/Sympa/Tools/Text.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ sub addrencode {

return undef unless $addr =~ /\S/;

# Eliminate hostile characters.
$phrase =~ s/(\r\n|\r|\n)(?=[ \t])//g;
$phrase =~ s/[\0\r\n]+//g;

if ($phrase =~ /[^\s\x21-\x7E]/) {
# String containing Non-ASCII should be encoded.
$phrase = MIME::EncWords::encode_mimewords(
Encode::decode('utf8', $phrase),
'Encoding' => 'A',
Expand All @@ -67,10 +72,13 @@ sub addrencode {
'Field' => 'Resent-Sender', # almost longest
'Minimal' => 'DISPNAME', # needs MIME::EncWords >= 1.012.
);
} elsif ($phrase =~ /\S/) {
} elsif ($phrase =~ /[()<>\[\]:;\@\\,\"]/) {
# Otherwise, the string has to be quoted when it is not a
# dot-atom-text (RFC 5322 3.2.3).
$phrase =~ s/([\\\"])/\\$1/g;
$phrase = '"' . $phrase . '"';
}

if ($comment =~ /[^\s\x21-\x27\x2A-\x5B\x5D-\x7E]/) {
$comment = MIME::EncWords::encode_mimewords(
Encode::decode('utf8', $comment),
Expand Down

0 comments on commit 77627d2

Please sign in to comment.