format-patch: make rfc2047 encoding more strict

RFC 2047 requires more characters to be encoded than it is currently done. Especially, RFC 2047 distinguishes between allowed remaining characters in encoded words in addresses (From, To, etc.) and other headers, such as Subject. Make add_rfc2047() and is_rfc2047_special() location dependent and include all non-allowed characters to hopefully be RFC 2047 conformant. This especially fixes a problem, where RFC 822 specials (e. g. ".") were left unencoded in addresses, which was solved with a non-standard-conforming workaround in the past (which is going to be removed in a follow-up patch). Signed-off-by: Jan H. Schönherr <schnhrr@cs.tu-berlin.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jan H. Schönherr committed Oct 18, 2012 at 16:43 UTC 0fcec2ce5467740eb613da6a977deca3e7e866ba
3 files changed +72 -12
git-compat-util.h
+2
@@ -466,6 +466,7 @@ extern const char tolower_trans_tbl[256];
466 #undef isdigit
467 #undef isalpha
468 #undef isalnum
469 +#undef isprint
470 #undef islower
471 #undef isupper
472 #undef tolower
@@ -483,6 +484,7 @@ extern unsigned char sane_ctype[256];
484 #define isdigit(x) sane_istest(x,GIT_DIGIT)
485 #define isalpha(x) sane_istest(x,GIT_ALPHA)
486 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
487 +#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
488 #define islower(x) sane_iscase(x, 1)
489 #define isupper(x) sane_iscase(x, 0)
490 #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
pretty.c
+59 -8
@@ -272,16 +272,65 @@ static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
272 strbuf_addch(out, '"');
273 }
274
275 -static int is_rfc2047_special(char ch)
275 +enum rfc2047_type {
276 + RFC2047_SUBJECT,
277 + RFC2047_ADDRESS,
278 +};
279 +
280 +static int is_rfc2047_special(char ch, enum rfc2047_type type)
281 {
277 - if (ch == ' ' || ch == '\n')
282 + /*
283 + * rfc2047, section 4.2:
284 + *
285 + * 8-bit values which correspond to printable ASCII characters other
286 + * than "=", "?", and "_" (underscore), MAY be represented as those
287 + * characters. (But see section 5 for restrictions.) In
288 + * particular, SPACE and TAB MUST NOT be represented as themselves
289 + * within encoded words.
290 + */
291 +
292 + /*
293 + * rule out non-ASCII characters and non-printable characters (the
294 + * non-ASCII check should be redundant as isprint() is not localized
295 + * and only knows about ASCII, but be defensive about that)
296 + */
297 + if (non_ascii(ch) || !isprint(ch))
298 + return 1;
299 +
300 + /*
301 + * rule out special printable characters (' ' should be the only
302 + * whitespace character considered printable, but be defensive and use
303 + * isspace())
304 + */
305 + if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
306 return 1;
307
280 - return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
308 + /*
309 + * rfc2047, section 5.3:
310 + *
311 + * As a replacement for a 'word' entity within a 'phrase', for example,
312 + * one that precedes an address in a From, To, or Cc header. The ABNF
313 + * definition for 'phrase' from RFC 822 thus becomes:
314 + *
315 + * phrase = 1*( encoded-word / word )
316 + *
317 + * In this case the set of characters that may be used in a "Q"-encoded
318 + * 'encoded-word' is restricted to: <upper and lower case ASCII
319 + * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
320 + * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
321 + * 'phrase' MUST be separated from any adjacent 'word', 'text' or
322 + * 'special' by 'linear-white-space'.
323 + */
324 +
325 + if (type != RFC2047_ADDRESS)
326 + return 0;
327 +
328 + /* '=' and '_' are special cases and have been checked above */
329 + return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
330 }
331
332 static void add_rfc2047(struct strbuf *sb, const char *line, int len,
284 - const char *encoding)
333 + const char *encoding, enum rfc2047_type type)
334 {
335 static const int max_length = 78; /* per rfc2822 */
336 static const int max_encoded_length = 76; /* per rfc2047 */
@@ -304,7 +353,7 @@ needquote:
353 line_len += strlen(encoding) + 5; /* 5 for =??q? */
354 for (i = 0; i < len; i++) {
355 unsigned ch = line[i] & 0xFF;
307 - int is_special = is_rfc2047_special(ch);
356 + int is_special = is_rfc2047_special(ch, type);
357
358 /*
359 * According to RFC 2047, we could encode the special character
@@ -358,11 +407,13 @@ void pp_user_info(const struct pretty_print_context *pp,
407 display_name_length = name_tail - line;
408 strbuf_addstr(sb, "From: ");
409 if (!has_rfc822_specials(line, display_name_length)) {
361 - add_rfc2047(sb, line, display_name_length, encoding);
410 + add_rfc2047(sb, line, display_name_length,
411 + encoding, RFC2047_ADDRESS);
412 } else {
413 struct strbuf quoted = STRBUF_INIT;
414 add_rfc822_quoted(&quoted, line, display_name_length);
365 - add_rfc2047(sb, quoted.buf, quoted.len, encoding);
415 + add_rfc2047(sb, quoted.buf, quoted.len,
416 + encoding, RFC2047_ADDRESS);
417 strbuf_release(&quoted);
418 }
419 if (namelen - display_name_length + last_line_length(sb) > 78) {
@@ -1294,7 +1345,7 @@ void pp_title_line(const struct pretty_print_context *pp,
1345 strbuf_grow(sb, title.len + 1024);
1346 if (pp->subject) {
1347 strbuf_addstr(sb, pp->subject);
1297 - add_rfc2047(sb, title.buf, title.len, encoding);
1348 + add_rfc2047(sb, title.buf, title.len, encoding, RFC2047_SUBJECT);
1349 } else {
1350 strbuf_addbuf(sb, &title);
1351 }
t/t4014-format-patch.sh
+11 -4
@@ -818,21 +818,28 @@ check_author() {
818 cat >expect <<'EOF'
819 From: "Foo B. Bar" <author@example.com>
820 EOF
821 -test_expect_success 'format-patch quotes dot in headers' '
821 +test_expect_success 'format-patch quotes dot in from-headers' '
822 check_author "Foo B. Bar"
823 '
824
825 cat >expect <<'EOF'
826 From: "Foo \"The Baz\" Bar" <author@example.com>
827 EOF
828 -test_expect_success 'format-patch quotes double-quote in headers' '
828 +test_expect_success 'format-patch quotes double-quote in from-headers' '
829 check_author "Foo \"The Baz\" Bar"
830 '
831
832 cat >expect <<'EOF'
833 -From: =?UTF-8?q?"F=C3=B6o=20B.=20Bar"?= <author@example.com>
833 +From: =?UTF-8?q?F=C3=B6o=20Bar?= <author@example.com>
834 EOF
835 -test_expect_success 'rfc2047-encoded headers also double-quote 822 specials' '
835 +test_expect_success 'format-patch uses rfc2047-encoded from-headers when necessary' '
836 + check_author "Föo Bar"
837 +'
838 +
839 +cat >expect <<'EOF'
840 +From: =?UTF-8?q?F=C3=B6o=20B=2E=20Bar?= <author@example.com>
841 +EOF
842 +test_expect_failure 'rfc2047-encoded from-headers leave no rfc822 specials' '
843 check_author "Föo B. Bar"
844 '
845