send-email: align RFC 2047 decoding more closely with the spec
PATCH v2 1/2 send-email: align RFC 2047 decoding more closely with the spec
More specifically:
* Add "\" to the list of characters not allowed in a token (see RFC 2047 errata).
* Share regexes between unquote_rfc2047 and is_rfc2047_quoted. Besides removing duplication, this also makes unquote_rfc2047 more stringent.
* Allow both "q" and "Q" to identify the encoding.
* Allow lowercase hexadecimal digits in the "Q" encoding.
And, more on the cosmetic side:
* Change the "encoded-text" regex to exclude rather than include characters, for clarity and consistency with "token".
--- git-send-email.perl | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
git-send-email.perl +19 -11
@@ -145,6 +145,11 @@ my $have_mail_address = eval { require Mail::Address; 1 }; my $smtp; my $auth; +# Regexes for RFC 2047 productions. +my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/; +my $re_encoded_text = qr/[^? \000-\037\177-\377]+/; +my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/; + # Variables we fill in automatically, or via prompting: my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh, $initial_reply_to,$initial_subject,@files,
@@ -913,15 +918,20 @@ $time = time - scalar $#files; sub unquote_rfc2047 { local ($_) = @_; - my $encoding; - s{=\?([^?]+)\?q\?(.*?)\?=}{ - $encoding = $1; - my $e = $2; - $e =~ s/_/ /g; - $e =~ s/=([0-9A-F]{2})/chr(hex($1))/eg; - $e; + my $charset; + s{$re_encoded_word}{ + $charset = $1; + my $encoding = $2; + my $text = $3; + if ($encoding eq 'q' || $encoding eq 'Q') { + $text =~ s/_/ /g; + $text =~ s/=([0-9A-F]{2})/chr(hex($1))/egi; + $text; + } else { + $&; # other encodings not supported yet + } }eg; - return wantarray ? ($_, $encoding) : $_; + return wantarray ? ($_, $charset) : $_; } sub quote_rfc2047 {
@@ -934,10 +944,8 @@ sub quote_rfc2047 { sub is_rfc2047_quoted { my $s = shift; - my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/; - my $encoded_text = qr/[!->@-~]+/; length($s) <= 75 && - $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o; + $s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o; } sub subject_needs_rfc2047_quoting {
Signature · 1 line
2.1.1
PATCH v2 2/2 send-email: handle adjacent RFC 2047-encoded words properly
The RFC says that they are to be concatenated after decoding (i.e. the intervening whitespace is ignored).
--- git-send-email.perl | 26 ++++++++++++++++---------- t/t9001-send-email.sh | 7 +++++++ 2 files changed, 23 insertions(+), 10 deletions(-)
git-send-email.perl +16 -10
@@ -919,17 +919,23 @@ $time = time - scalar $#files; sub unquote_rfc2047 { local ($_) = @_; my $charset; - s{$re_encoded_word}{ - $charset = $1; - my $encoding = $2; - my $text = $3; - if ($encoding eq 'q' || $encoding eq 'Q') { - $text =~ s/_/ /g; - $text =~ s/=([0-9A-F]{2})/chr(hex($1))/egi; - $text; - } else { - $&; # other encodings not supported yet + my $sep = qr/[ \t]+/; + s{$re_encoded_word(?:$sep$re_encoded_word)*}{ + my @words = split $sep, $&; + foreach (@words) { + m/$re_encoded_word/; + $charset = $1; + my $encoding = $2; + my $text = $3; + if ($encoding eq 'q' || $encoding eq 'Q') { + $_ = $text; + s/_/ /g; + s/=([0-9A-F]{2})/chr(hex($1))/egi; + } else { + # other encodings not supported yet + } } + join '', @words; }eg; return wantarray ? ($_, $charset) : $_; }
t/t9001-send-email.sh +7 -0
@@ -240,6 +240,13 @@ test_expect_success $PREREQ 'non-ascii self name is suppressed' " 'non_ascii_self_suppressed' " +# This name is long enough to force format-patch to split it into multiple +# encoded-words, assuming it uses UTF-8 with the "Q" encoding. +test_expect_success $PREREQ 'long non-ascii self name is suppressed' " + test_suppress_self_quoted 'Ƒüñníęř €. Nâṁé' '[email protected]' \ + 'long_non_ascii_self_suppressed' +" + test_expect_success $PREREQ 'sanitized self name is suppressed' " test_suppress_self_unquoted '\"A U. Thor\"' '[email protected]' \ 'self_name_sanitized_suppressed'
Signature · 1 line
2.1.1
Thanks; queued both patches.