threads / discuss / 50743

"Problems" with git format-patch --thread email header ordering

Subject: "Problems" with git format-patch --thread email header ordering

## tl;dr

5 messages between Mar 14, 2019 and Apr 5, 2019.

replies: 4people: 3as markdown or json

Linus Torvalds· Mar 14, 2019, 23:44 UTC · lore

So Thomas Gleixner just figured out that the google gmail support for S/MIME is even more broken than we initially thought, and has been rejecting emails that have a non-canonical order of headers in the email. In particular, gmail s/mime parsing hates emails generated with "git format-patch --thread" (and then encrypted to be s/mime).

With the git format-patch header ordering, S/MIME senders will get incomprehensible (and wrong) bounces that say

   Mail delivery failed: returning message to sender (fwd)
    550-5.7.1 Our system has detected that this
    550-5.7.1 message is not RFC 5322 compliant:
    550-5.7.1 'From' header is missing.
when sending to an gmail S/MIME-enabled recipient.

Now, this is very much a gmail bug, but I've long since given up any hope at all that the gmail people would listen to outsiders (and from my interactions with people _inside_ of google, I think they consider anybody outside the "gmail" team to be outsiders - good luck to any Google people trying to get gmail issues fixed either).

Note that the gmail bounce about 'From' header is missing is completely bogus, and claims about RFC 5322 are equally inane. The SMTP RFC's very much say that the order of header files is not guaranteed, and gmail is wrong.

Also note that this does not affect any *normal* emails to gmail recipients. It only seems to affect the server-side s/mime decryption of the email, so you'll never see it unless the recipient actually has s/mime support enabled (and you encode the git format-patch as s/mime).

HOWEVER.

While it's true that header ordering isn't specified, there's a common "canonical" order that the headers are listed in. To quote rfc822:

     Note:  Due to an artifact of the notational conventions, the syn-
            tax  indicates that, when present, some fields, must be in
            a particular order.  Header fields  are  NOT  required  to
            occur  in  any  particular  order, except that the message
            body must occur AFTER  the  headers.   It  is  recommended
            that,  if  present,  headers be sent in the order "Return-
            Path", "Received", "Date",  "From",  "Subject",  "Sender",
            "To", "cc", etc.

Note how that very basic smtp rfc makes it very clear that headers are very much not required to occur in any particular order, but it does have a recommended ordering.

I suspect some broken code inside gmail uses that "notational convention with syntax that indicates that some fields must be in a particular order". The RFC explicitly states that it's wrong, but hey, somebody cut-and-pasted the syntax from the RFC and didn't read the note.

Also note that rfc 5322 (which is a newer version of 822) doesn't really change that, but does make it clear that trace and resent fields must not be re-ordered during retransmission (so "Received" fields etc should stay ordered). That's not about accepting messages, that's about the transfer of them, though. Gmail is still wrong, and pointing to the newer rtc doesn't make gmail any more correct.

So gmail is simply wrong in having some odd ordering requirement.

But git format-patch does create the email headers out in a different order than the one recommended. When you do "git format-patch --thread" to create the emails, the header ordering looks roughly like this:

  Message-Id: <cover.1552606170.git.torvalds@linux-foundation.org>
  From: Linus Torvalds <torvalds@linux-foundation.org>
  Date: Thu, 14 Mar 2019 16:29:30 -0700
  Subject: [PATCH 0/6] *** SUBJECT HERE ***
  MIME-Version: 1.0
  Content-Type: text/plain; charset=UTF-8
  Content-Transfer-Encoding: 8bit

and gmail is quite unhappy with the result if it is then sent as s/mime. Gmail apparently really wants that Date/From/Subject/To/Message-Id ordering.

Don't ask me why. Gmail is simply wrong. But I have a very strong suspicion that it's easier to fix "git format-patch" than it is to fix whatever odd gmail issue.

Comments? Thomas has munged his s/mime infrastructure to re-order things, but git _could_ do the proper recommended ordering.

And yes, if somebody from Google on this list wants to bring this up with the gmail team, I wish you the best of luck. Let me know how it goes.

                   Linus
Junio C Hamano· Mar 15, 2019, 04:47 UTC · re: Linus Torvalds · lore

Re: "Problems" with git format-patch --thread email header ordering

Linus Torvalds <torvalds@linux-foundation.org> writes:
Show 24 quoted lines
> While it's true that header ordering isn't specified, there's a common
> "canonical" order that the headers are listed in. To quote rfc822:
> ...
>             body must occur AFTER  the  headers.   It  is  recommended
>             that,  if  present,  headers be sent in the order "Return-
>             Path", "Received", "Date",  "From",  "Subject",  "Sender",
>             "To", "cc", etc.
>
> But git format-patch does create the email headers out in a different
> order than the one recommended.  When you do "git format-patch
> --thread" to create the emails, the header ordering looks roughly like
> this:
>
>   Message-Id: <cover.1552606170.git.torvalds@linux-foundation.org>
>   From: Linus Torvalds <torvalds@linux-foundation.org>
>   Date: Thu, 14 Mar 2019 16:29:30 -0700
>   Subject: [PATCH 0/6] *** SUBJECT HERE ***
>   MIME-Version: 1.0
>   Content-Type: text/plain; charset=UTF-8
>   Content-Transfer-Encoding: 8bit
> ...
> And yes, if somebody from Google on this list wants to bring this up
> with the gmail team, I wish you the best of luck. Let me know how it
> goes.

I obviously won't do the last one myself, but if the issue is only to swap from and date, then this may be sufficient, perhaps?

I suspect that there would be fallouts in t/ directory before we can call this a fix.

 pretty.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/pretty.c b/pretty.c
index f496f0f128..40c7236fbc 100644
--- a/pretty.c
+++ b/pretty.c
@@ -452,6 +452,14 @@ void pp_user_info(struct pretty_print_context *pp,
 		map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
 
 	if (cmit_fmt_is_mail(pp->fmt)) {
+
+		/* 
+		 * Date: before From:
+		 * cf. <CAHk-=whP1stFZNAaJiMi5eZ9rj0MRt20Y_yHVczZPH+O01d+sA@mail.gmail.com>
+		 */
+		strbuf_addf(sb, "Date: %s\n",
+			    show_ident_date(&ident, DATE_MODE(RFC2822)));
+
 		if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
 			struct strbuf buf = STRBUF_INIT;
 
@@ -502,8 +510,7 @@ void pp_user_info(struct pretty_print_context *pp,
 		break;
 	case CMIT_FMT_EMAIL:
 	case CMIT_FMT_MBOXRD:
-		strbuf_addf(sb, "Date: %s\n",
-			    show_ident_date(&ident, DATE_MODE(RFC2822)));
+		/* has been done much earlier */
 		break;
 	case CMIT_FMT_FULLER:
 		strbuf_addf(sb, "%sDate: %s\n", what,
Linus Torvalds· Mar 15, 2019, 16:59 UTC · re: Junio C Hamano · lore

Re: "Problems" with git format-patch --thread email header ordering

On Thu, Mar 14, 2019 at 9:47 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
>
> Linus Torvalds <torvalds@linux-foundation.org> writes:
>
> > While it's true that header ordering isn't specified, there's a common
> > "canonical" order that the headers are listed in. To quote rfc822:
> > ...
> >             body must occur AFTER  the  headers.   It  is  recommended
> >             that,  if  present,  headers be sent in the order "Return-
> >             Path", "Received", "Date",  "From",  "Subject",  "Sender",
> >             "To", "cc", etc.
>
> I obviously won't do the last one myself, but if the issue is only
> to swap from and date, then this may be sufficient, perhaps?

I'm not actually sure _what_ the order requirements for gmail are, since gmail itself doesn't seem to honor them. Does the order of the Message-ID header line matter, for example?

I don't think it's the order of the From/Date lines, actually, because google itself doesn't do that.

What Thomas Found out was that the exact same email with
    Message-Id/From/Date/Subject/To
(in that order) does not work, but
    Date/From/Subject/To/Message-Id

does work. Weird and "wonderful". But there might be a lot of other orderings that work or don't.

Having looked through some other emails, I know that
    From/To/Subject/Date/Message-Id
    Subject/To/References/From/Message-ID/Date
also works.  Which makes me suspect that it's the Message-ID line that matters.

But it might be something _really_ odd, and maybe not just ordering at all (ie it might be something where gmail wants to see the "From" line, but only in certain circumstances, and only *then* does it matter if the From line comes before some other line or not.

All we know is
 (a) gmail complains about the normal git format-patch ordering with a
"no From line" bounce
 (b) the same email with just headers re-ordered goes through
I *suspect* it's that Message-ID line, but .... It's very annoying.
               Linus
Julian Andres Klode· Apr 4, 2019, 18:06 UTC · re: Linus Torvalds · lore

Re: "Problems" with git format-patch --thread email header ordering

On Fri, Mar 15, 2019 at 09:59:25AM -0700, Linus Torvalds wrote:
Show 39 quoted lines
> On Thu, Mar 14, 2019 at 9:47 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > Linus Torvalds <torvalds@linux-foundation.org> writes:
> >
> > > While it's true that header ordering isn't specified, there's a common
> > > "canonical" order that the headers are listed in. To quote rfc822:
> > > ...
> > >             body must occur AFTER  the  headers.   It  is  recommended
> > >             that,  if  present,  headers be sent in the order "Return-
> > >             Path", "Received", "Date",  "From",  "Subject",  "Sender",
> > >             "To", "cc", etc.
> >
> > I obviously won't do the last one myself, but if the issue is only
> > to swap from and date, then this may be sufficient, perhaps?
> 
> I'm not actually sure _what_ the order requirements for gmail are,
> since gmail itself doesn't seem to honor them. Does the order of the
> Message-ID header line matter, for example?
> 
> I don't think it's the order of the From/Date lines, actually, because
> google itself doesn't do that.
> 
> What Thomas Found out was that the exact same email with
> 
>     Message-Id/From/Date/Subject/To
> 
> (in that order) does not work, but
> 
>     Date/From/Subject/To/Message-Id
> 
> does work. Weird and "wonderful". But there might be a lot of other
> orderings that work or don't.
> 
> Having looked through some other emails, I know that
> 
>     From/To/Subject/Date/Message-Id
>     Subject/To/References/From/Message-ID/Date
> 
> also works.  Which makes me suspect that it's the Message-ID line that matters.

I also know that gmail rewrites the Message-ID / creates one if it is missing or "odd" (such as ends in a .). It those probably makes sense in that twisted world view to require that to be fairly late...

-- 
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en
Junio C Hamano· Apr 5, 2019, 05:24 UTC · re: Julian Andres Klode · lore

Re: "Problems" with git format-patch --thread email header ordering

Julian Andres Klode <jak@jak-linux.org> writes:
Show 21 quoted lines
>> What Thomas Found out was that the exact same email with
>> 
>>     Message-Id/From/Date/Subject/To
>> 
>> (in that order) does not work, but
>> 
>>     Date/From/Subject/To/Message-Id
>> 
>> does work. Weird and "wonderful". But there might be a lot of other
>> orderings that work or don't.
>> 
>> Having looked through some other emails, I know that
>> 
>>     From/To/Subject/Date/Message-Id
>>     Subject/To/References/From/Message-ID/Date
>> 
>> also works.  Which makes me suspect that it's the Message-ID line that matters.
>
> I also know that gmail rewrites the Message-ID / creates one if it is
> missing or "odd" (such as ends in a .). It those probably makes sense
> in that twisted world view to require that to be fairly late...

Here is a patch that is not polished enough to be commited yet but should be testable (as I suspect that existing tests need to be adjusted).

-- >8 --
Subject: [PATCH] format-patch: move message-id and related headers to the end

It has been reported that GMail MSA has an undocumented limitation that makes it reject or accept depending on some ordering of headers in the incoming message. The current suspicion is that they want to see the Message-Id: header near the end, so let's try doing so and see what happens..

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 log-tree.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index 1e56df62a7..111b947264 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -363,24 +363,27 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 	const char *extra_headers = opt->extra_headers;
 	const char *name = oid_to_hex(opt->zero_commit ?
 				      &null_oid : &commit->object.oid);
+	struct strbuf message_ids = STRBUF_INIT;
 
 	*need_8bit_cte_p = 0; /* unknown */
 
 	fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
 	graph_show_oneline(opt->graph);
-	if (opt->message_id) {
-		fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
-		graph_show_oneline(opt->graph);
-	}
+
+	if (opt->message_id)
+		strbuf_addf(&message_ids, "Message-Id: <%s>\n", opt->message_id);
+
 	if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
 		int i, n;
 		n = opt->ref_message_ids->nr;
-		fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
+		strbuf_addf(&message_ids, "In-Reply-To: <%s>\n",
+			    opt->ref_message_ids->items[n-1].string);
 		for (i = 0; i < n; i++)
-			fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
-			       opt->ref_message_ids->items[i].string);
-		graph_show_oneline(opt->graph);
+			strbuf_addf(&message_ids, "%s<%s>\n",
+				    (i > 0 ? "\t" : "References: "),
+				    opt->ref_message_ids->items[i].string);
 	}
+
 	if (opt->mime_boundary && maybe_multipart) {
 		static struct strbuf subject_buffer = STRBUF_INIT;
 		static struct strbuf buffer = STRBUF_INIT;
@@ -425,6 +428,17 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 		opt->diffopt.stat_sep = buffer.buf;
 		strbuf_release(&filename);
 	}
+
+	if (message_ids.len) {
+		static struct strbuf buf = STRBUF_INIT;
+
+		strbuf_reset(&buf);
+		strbuf_addbuf(&buf, &message_ids);
+		if (extra_headers)
+			strbuf_addstr(&buf, extra_headers);
+		extra_headers = buf.buf;
+	}
+
 	*extra_headers_p = extra_headers;
 }
 
-- 
2.21.0-197-g893dd55891

← back to recent threads