Skip to content

Commit 79fc769

Browse files
shreyp135gitster
authored andcommitted
send-email: validate charset name in 8bit encoding prompt
When a non-ASCII character is detected in the body or subject of the email the user is prompted with, Which 8bit encoding should I declare [UTF-8]? foo After this the input string is validated by the regex, based on the fact that the charset string will be minimum 4 characters [1]. If the string is more than 4 letters the email is sent, if not then a second prompt to confirm is asked to the user, Are you sure you want to use <foo> [y/N]? y This relies on a length based regex heuristic check to validate the user input, and can allow clearly invalid charset names to pass if the input is greater than 4 characters. Add a semantic validation of the charset name using the Encode::find_encoding() module of perl. If the encoding is not recognized, warn the user and ask for confirmation before proceeding. After this validation the lenght based validation becomes redundant and also breaks flow, so change the regex of valid input to any non blank string. Introduce a dedicated helper for confirmation handling that can be reused both by ask() and the custom 8bit prompt flow. Make the encoding warning logic specific to the 8bit prompt, this reduces the load on ask(), and improves maintainability. Additionally, the wording of the first prompt can confuse the user if not read properly or under any default assumptions for a yes/no prompt. Change the wording to make it explicitly clear to the user that the prompt needs a string input, UTF-8 being the default. The intended flow is, Declare which 8bit encoding to use [default: UTF-8]? foobar warning: 'foobar' does not appear to be a valid charset name. Are you sure you want to use <foobar> [y/N]? [1]- git@852a15d Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c02d39 commit 79fc769

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

git-send-email.perl

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Git::LoadCPAN::Error qw(:try);
2424
use Git;
2525
use Git::I18N;
26+
use Encode qw(find_encoding);
2627

2728
Getopt::Long::Configure qw/ pass_through /;
2829

@@ -984,6 +985,18 @@ sub get_patch_subject {
984985
}
985986
}
986987
988+
sub confirm_ask {
989+
my ($resp) = @_;
990+
my $term = term();
991+
return 0
992+
unless defined $term->IN and defined fileno($term->IN) and
993+
defined $term->OUT and defined fileno($term->OUT);
994+
my $yesno = $term->readline(
995+
# TRANSLATORS: please keep [y/N] as is.
996+
sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
997+
return defined $yesno && $yesno =~ /y/i;
998+
}
999+
9871000
sub ask {
9881001
my ($prompt, %arg) = @_;
9891002
my $valid_re = $arg{valid_re};
@@ -1008,10 +1021,7 @@ sub ask {
10081021
return $resp;
10091022
}
10101023
if ($confirm_only) {
1011-
my $yesno = $term->readline(
1012-
# TRANSLATORS: please keep [y/N] as is.
1013-
sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
1014-
if (defined $yesno && $yesno =~ /y/i) {
1024+
if (confirm_ask($resp)) {
10151025
return $resp;
10161026
}
10171027
}
@@ -1044,9 +1054,21 @@ sub file_declares_8bit_cte {
10441054
foreach my $f (sort keys %broken_encoding) {
10451055
print " $f\n";
10461056
}
1047-
$auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
1048-
valid_re => qr/.{4}/, confirm_only => 1,
1049-
default => "UTF-8");
1057+
while(1) {
1058+
my $encoding = ask(__("Declare which 8bit encoding to use [default: UTF-8]? "),
1059+
valid_re => qr/^\S+$/,
1060+
default => "UTF-8");
1061+
next unless defined $encoding;
1062+
if (find_encoding($encoding)) {
1063+
$auto_8bit_encoding = $encoding;
1064+
last;
1065+
}
1066+
printf STDERR __("warning: '%s' does not appear to be a valid charset name.\n"), $encoding;
1067+
if (confirm_ask($encoding)) {
1068+
$auto_8bit_encoding = $encoding;
1069+
last;
1070+
}
1071+
}
10501072
}
10511073
10521074
if (!$force) {

t/t9001-send-email.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
16911691
email-using-8bit >stdout &&
16921692
grep "do not declare a Content-Transfer-Encoding" stdout &&
16931693
grep email-using-8bit stdout &&
1694-
grep "Which 8bit encoding" stdout &&
1694+
grep "Declare which 8bit encoding to use" stdout &&
16951695
grep -E "Content|MIME" msgtxt1 >actual &&
16961696
test_cmp content-type-decl actual
16971697
'

0 commit comments

Comments
 (0)