Skip to content

Commit f357e5d

Browse files
Ikkegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ee4d679 commit f357e5d

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

mailinfo.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,86 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
5454
get_sane_name(&mi->name, &mi->name, &mi->email);
5555
}
5656

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+
57137
static void handle_from(struct mailinfo *mi, const struct strbuf *from)
58138
{
59139
char *at;
@@ -63,6 +143,8 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
63143
strbuf_init(&f, from->len);
64144
strbuf_addbuf(&f, from);
65145

146+
unquote_quoted_pair(&f);
147+
66148
at = strchr(f.buf, '@');
67149
if (!at) {
68150
parse_bogus_from(mi, from);

t/t5100-mailinfo.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,18 @@ test_expect_success 'mailinfo unescapes with --mboxrd' '
144144
test_cmp expect mboxrd/msg
145145
'
146146

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+
147161
test_done

t/t5100/comment.expect

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Author: A U Thor (this is (really) a comment (honestly))
2+
3+
Subject: testing comments
4+
Date: Sun, 25 May 2008 00:38:18 -0700
5+

t/t5100/comment.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
2+
From: "A U Thor" <[email protected]> (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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Author: Author "The Author" Name
2+
3+
Subject: testing quoted-pair
4+
Date: Sun, 25 May 2008 00:38:18 -0700
5+

t/t5100/quoted-string.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
2+
From: "Author \"The Author\" Name" <[email protected]>
3+
Date: Sun, 25 May 2008 00:38:18 -0700
4+
Subject: [PATCH] testing quoted-pair
5+
6+
7+
8+
---
9+
patch

0 commit comments

Comments
 (0)