send-email: detect empty blank lines in command output

The email format does not allow blank lines in headers; detect such input and report it as malformed and add a test for it. Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Maxim Cournoyer committed May 1, 2023 at 10:38 UTC 3a7a18a045cd94a13ed04ef715dcd1b91547a0af
2 files changed +27 -2
git-send-email.perl
+10 -2
@@ -2026,14 +2026,22 @@ foreach my $t (@files) {
2026 }
2027 }
2028
2029 -# Execute a command and return its output lines as an array.
2029 +# Execute a command and return its output lines as an array. Blank
2030 +# lines which do not appear at the end of the output are reported as
2031 +# errors.
2032 sub execute_cmd {
2033 my ($prefix, $cmd, $file) = @_;
2034 my @lines = ();
2035 + my $seen_blank_line = 0;
2036 open my $fh, "-|", "$cmd \Q$file\E"
2037 or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
2038 while (my $line = <$fh>) {
2036 - last if $line =~ /^$/;
2039 + die sprintf(__("(%s) Malformed output from '%s'"), $prefix, $cmd)
2040 + if $seen_blank_line;
2041 + if ($line =~ /^$/) {
2042 + $seen_blank_line = $line =~ /^$/;
2043 + next;
2044 + }
2045 push @lines, $line;
2046 }
2047 close $fh
t/t9001-send-email.sh
+17
@@ -460,6 +460,23 @@ FoldedField: This is a tale
460 grep "^FoldedField: This is a tale best told using multiple lines.$" msgtxt1
461 '
462
463 +# Blank lines in the middle of the output of a command are invalid.
464 +test_expect_success $PREREQ 'malform output reported on blank lines in command output' '
465 + clean_fake_sendmail &&
466 + cp $patches headercmd.patch &&
467 + write_script headercmd-malformed-output <<-\EOF &&
468 + echo "X-Debbugs-CC: someone@example.com
469 +
470 +SomeOtherField: someone-else@example.com"
471 + EOF
472 + ! git send-email \
473 + --from="Example <nobody@example.com>" \
474 + --to=nobody@example.com \
475 + --header-cmd=./headercmd-malformed-output \
476 + --smtp-server="$(pwd)/fake.sendmail" \
477 + headercmd.patch
478 +'
479 +
480 test_expect_success $PREREQ 'reject long lines' '
481 z8=zzzzzzzz &&
482 z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&