mailinfo: unescape quoted-pair in header fields

rfc2822 has provisions for quoted strings in structured header fields, but also allows for escaping these with so-called quoted-pairs. The only thing git currently does is removing exterior quotes, but quotes within are left alone. Remove exterior quotes and remove escape characters so that they don't show up in the author field. Signed-off-by: Kevin Daudt <me@ikke.info> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Daudt committed Sep 28, 2016 at 21:52 UTC f357e5de31595c28a303aaf5443c26f492441a6f
6 files changed +124
mailinfo.c
+82
@@ -54,6 +54,86 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
54 get_sane_name(&mi->name, &mi->name, &mi->email);
55 }
56
57 +static const char *unquote_comment(struct strbuf *outbuf, const char *in)
58 +{
59 + int c;
60 + int take_next_litterally = 0;
61 +
62 + strbuf_addch(outbuf, '(');
63 +
64 + while ((c = *in++) != 0) {
65 + if (take_next_litterally == 1) {
66 + take_next_litterally = 0;
67 + } else {
68 + switch (c) {
69 + case '\\':
70 + take_next_litterally = 1;
71 + continue;
72 + case '(':
73 + in = unquote_comment(outbuf, in);
74 + continue;
75 + case ')':
76 + strbuf_addch(outbuf, ')');
77 + return in;
78 + }
79 + }
80 +
81 + strbuf_addch(outbuf, c);
82 + }
83 +
84 + return in;
85 +}
86 +
87 +static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
88 +{
89 + int c;
90 + int take_next_litterally = 0;
91 +
92 + while ((c = *in++) != 0) {
93 + if (take_next_litterally == 1) {
94 + take_next_litterally = 0;
95 + } else {
96 + switch (c) {
97 + case '\\':
98 + take_next_litterally = 1;
99 + continue;
100 + case '"':
101 + return in;
102 + }
103 + }
104 +
105 + strbuf_addch(outbuf, c);
106 + }
107 +
108 + return in;
109 +}
110 +
111 +static void unquote_quoted_pair(struct strbuf *line)
112 +{
113 + struct strbuf outbuf;
114 + const char *in = line->buf;
115 + int c;
116 +
117 + strbuf_init(&outbuf, line->len);
118 +
119 + while ((c = *in++) != 0) {
120 + switch (c) {
121 + case '"':
122 + in = unquote_quoted_string(&outbuf, in);
123 + continue;
124 + case '(':
125 + in = unquote_comment(&outbuf, in);
126 + continue;
127 + }
128 +
129 + strbuf_addch(&outbuf, c);
130 + }
131 +
132 + strbuf_swap(&outbuf, line);
133 + strbuf_release(&outbuf);
134 +
135 +}
136 +
137 static void handle_from(struct mailinfo *mi, const struct strbuf *from)
138 {
139 char *at;
@@ -63,6 +143,8 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
143 strbuf_init(&f, from->len);
144 strbuf_addbuf(&f, from);
145
146 + unquote_quoted_pair(&f);
147 +
148 at = strchr(f.buf, '@');
149 if (!at) {
150 parse_bogus_from(mi, from);
t/t5100-mailinfo.sh
+14
@@ -144,4 +144,18 @@ test_expect_success 'mailinfo unescapes with --mboxrd' '
144 test_cmp expect mboxrd/msg
145 '
146
147 +test_expect_success 'mailinfo handles rfc2822 quoted-string' '
148 + mkdir quoted-string &&
149 + git mailinfo /dev/null /dev/null <"$DATA/quoted-string.in" \
150 + >quoted-string/info &&
151 + test_cmp "$DATA/quoted-string.expect" quoted-string/info
152 +'
153 +
154 +test_expect_success 'mailinfo handles rfc2822 comment' '
155 + mkdir comment &&
156 + git mailinfo /dev/null /dev/null <"$DATA/comment.in" \
157 + >comment/info &&
158 + test_cmp "$DATA/comment.expect" comment/info
159 +'
160 +
161 test_done
t/t5100/comment.expect new
+5
@@ -0,0 +1,5 @@
1 +Author: A U Thor (this is (really) a comment (honestly))
2 +Email: somebody@example.com
3 +Subject: testing comments
4 +Date: Sun, 25 May 2008 00:38:18 -0700
5 +
t/t5100/comment.in new
+9
@@ -0,0 +1,9 @@
1 +From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
2 +From: "A U Thor" <somebody@example.com> (this is \(really\) a comment (honestly))
3 +Date: Sun, 25 May 2008 00:38:18 -0700
4 +Subject: [PATCH] testing comments
5 +
6 +
7 +
8 +---
9 +patch
t/t5100/quoted-string.expect new
+5
@@ -0,0 +1,5 @@
1 +Author: Author "The Author" Name
2 +Email: somebody@example.com
3 +Subject: testing quoted-pair
4 +Date: Sun, 25 May 2008 00:38:18 -0700
5 +
t/t5100/quoted-string.in new
+9
@@ -0,0 +1,9 @@
1 +From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
2 +From: "Author \"The Author\" Name" <somebody@example.com>
3 +Date: Sun, 25 May 2008 00:38:18 -0700
4 +Subject: [PATCH] testing quoted-pair
5 +
6 +
7 +
8 +---
9 +patch