Skip to content

Commit a1016d9

Browse files
brunnre8emersion
authored andcommitted
mail/header: strip CFWS from date headers.
1 parent bc2a7af commit a1016d9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

mail/header.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package mail
22

33
import (
44
"net/mail"
5+
"regexp"
56
"time"
67

78
"github.com/emersion/go-message"
89
)
910

1011
const dateLayout = "Mon, 02 Jan 2006 15:04:05 -0700"
1112

13+
// TODO: this is a blunt way to strip any trailing CFWS (comment). A sharper
14+
// one would strip multiple CFWS, and only if really valid according to
15+
// RFC5322.
16+
var commentRE = regexp.MustCompile(`[ \t]+\(.*\)$`)
17+
1218
// A Header is a mail header.
1319
type Header struct {
1420
message.Header
@@ -31,7 +37,10 @@ func (h *Header) SetAddressList(key string, addrs []*Address) {
3137

3238
// Date parses the Date header field.
3339
func (h *Header) Date() (time.Time, error) {
34-
return mail.ParseDate(h.Get("Date"))
40+
//TODO: remove this once https://go-review.googlesource.com/c/go/+/117596/
41+
// is merged
42+
date := commentRE.ReplaceAllString(h.Get("Date"), "")
43+
return mail.ParseDate(date)
3544
}
3645

3746
// SetDate formats the Date header field.

mail/header_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,20 @@ func TestHeader(t *testing.T) {
4242
t.Errorf("Expected header subject to be %v, but got %v", subject, got)
4343
}
4444
}
45+
46+
func TestCFWSDates(t *testing.T) {
47+
tc := []string{
48+
"Mon, 22 Jul 2019 13:57:29 -0500 (GMT-05:00)",
49+
"Mon, 22 Jul 2019 13:57:29 -0500",
50+
"Mon, 2 Jan 06 15:04:05 MST (Some random stuff)",
51+
"Mon, 2 Jan 06 15:04:05 MST",
52+
}
53+
var h mail.Header
54+
for _, tt := range tc {
55+
h.Set("Date", tt)
56+
_, err := h.Date()
57+
if err != nil {
58+
t.Errorf("Failed to parse time %q: %v", tt, err)
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)