Skip to content

Commit cc0c732

Browse files
committed
Merge branch 'dd/mailinfo-with-nul'
Tighten "git mailinfo" to notice and error out when decoded result contains NUL in it. * dd/mailinfo-with-nul: mailinfo: disallow NUL character in mail's header mailinfo.c: avoid strlen on strings that can contains NUL t4254: merge 2 steps of a single test
2 parents 81bfe54 + 3919997 commit cc0c732

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

mailinfo.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,21 @@ static int convert_to_utf8(struct mailinfo *mi,
447447
struct strbuf *line, const char *charset)
448448
{
449449
char *out;
450+
size_t out_len;
450451

451452
if (!mi->metainfo_charset || !charset || !*charset)
452453
return 0;
453454

454455
if (same_encoding(mi->metainfo_charset, charset))
455456
return 0;
456-
out = reencode_string(line->buf, mi->metainfo_charset, charset);
457+
out = reencode_string_len(line->buf, line->len,
458+
mi->metainfo_charset, charset, &out_len);
457459
if (!out) {
458460
mi->input_error = -1;
459461
return error("cannot convert from %s to %s",
460462
charset, mi->metainfo_charset);
461463
}
462-
strbuf_attach(line, out, strlen(out), strlen(out));
464+
strbuf_attach(line, out, out_len, out_len);
463465
return 0;
464466
}
465467

@@ -1136,6 +1138,11 @@ static void handle_info(struct mailinfo *mi)
11361138
else
11371139
continue;
11381140

1141+
if (memchr(hdr->buf, '\0', hdr->len)) {
1142+
error("a NUL byte in '%s' is not allowed.", header[i]);
1143+
mi->input_error = -1;
1144+
}
1145+
11391146
if (!strcmp(header[i], "Subject")) {
11401147
if (!mi->keep_subject) {
11411148
cleanup_subject(mi, hdr);

t/t4254-am-corrupt.sh

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
test_description='git am with corrupt input'
44
. ./test-lib.sh
55

6+
make_mbox_with_nul () {
7+
space=' '
8+
q_nul_in_subject=
9+
q_nul_in_body=
10+
while test $# -ne 0
11+
do
12+
case "$1" in
13+
subject) q_nul_in_subject='=00' ;;
14+
body) q_nul_in_body='=00' ;;
15+
esac &&
16+
shift
17+
done &&
18+
cat <<-EOF
19+
From ec7364544f690c560304f5a5de9428ea3b978b26 Mon Sep 17 00:00:00 2001
20+
From: A U Thor <[email protected]>
21+
Date: Sun, 19 Apr 2020 13:42:07 +0700
22+
Subject: [PATCH] =?ISO-8859-1?q?=C4=CB${q_nul_in_subject}=D1=CF=D6?=
23+
MIME-Version: 1.0
24+
Content-Type: text/plain; charset=ISO-8859-1
25+
Content-Transfer-Encoding: quoted-printable
26+
27+
abc${q_nul_in_body}def
28+
---
29+
diff --git a/afile b/afile
30+
new file mode 100644
31+
index 0000000000..e69de29bb2
32+
--$space
33+
2.26.1
34+
EOF
35+
}
36+
637
test_expect_success setup '
738
# Note the missing "+++" line:
839
cat >bad-patch.diff <<-\EOF &&
@@ -25,13 +56,27 @@ test_expect_success setup '
2556
# fatal: unable to write file '(null)' mode 100644: Bad address
2657
# Also, it had the unwanted side-effect of deleting f.
2758
test_expect_success 'try to apply corrupted patch' '
28-
test_must_fail git -c advice.amWorkDir=false am bad-patch.diff 2>actual
29-
'
30-
31-
test_expect_success 'compare diagnostic; ensure file is still here' '
59+
test_when_finished "git am --abort" &&
60+
test_must_fail git -c advice.amWorkDir=false am bad-patch.diff 2>actual &&
3261
echo "error: git diff header lacks filename information (line 4)" >expected &&
3362
test_path_is_file f &&
3463
test_i18ncmp expected actual
3564
'
3665

66+
test_expect_success "NUL in commit message's body" '
67+
test_when_finished "git am --abort" &&
68+
make_mbox_with_nul body >body.patch &&
69+
test_must_fail git am body.patch 2>err &&
70+
grep "a NUL byte in commit log message not allowed" err
71+
'
72+
73+
test_expect_success "NUL in commit message's header" "
74+
test_when_finished 'git am --abort' &&
75+
make_mbox_with_nul subject >subject.patch &&
76+
test_must_fail git mailinfo msg patch <subject.patch 2>err &&
77+
grep \"a NUL byte in 'Subject' is not allowed\" err &&
78+
test_must_fail git am subject.patch 2>err &&
79+
grep \"a NUL byte in 'Subject' is not allowed\" err
80+
"
81+
3782
test_done

0 commit comments

Comments
 (0)