Skip to content

Commit 1956824

Browse files
committed
pkg/email: decode rfc2047 subjects
It's not done transparently by the email library. Add a test that verifies the result.
1 parent 82df6b0 commit 1956824

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pkg/email/parser.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func Parse(r io.Reader, ownEmails, goodLists, domains []string) (*Email, error)
137137
return nil, err
138138
}
139139
bodyStr := string(body)
140-
subject := msg.Header.Get("Subject")
140+
subject := decodeSubject(msg.Header.Get("Subject"))
141141
var cmds []*SingleCommand
142142
var patch string
143143
if !fromMe {
@@ -565,3 +565,13 @@ func RemoveFromEmailList(list []string, toRemove string) []string {
565565
}
566566
return result
567567
}
568+
569+
// Decode RFC 2047-encoded subjects.
570+
func decodeSubject(rawSubject string) string {
571+
decoder := new(mime.WordDecoder)
572+
decodedSubject, err := decoder.DecodeHeader(rawSubject)
573+
if err != nil {
574+
return rawSubject
575+
}
576+
return decodedSubject
577+
}

pkg/email/parser_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,4 +1117,25 @@ Content-Transfer-Encoding: quoted-printable
11171117
},
11181118
},
11191119
}},
1120+
{`Sender: foo@foobar.com
1121+
Subject: [PATCH] =?UTF-8?q?Add=20a=20new=20test=20'migrate.cow=5Fafter=5Ff?= =?UTF-8?q?ork'=20that=20verifies=20correct=20RMAP=20handling=20of=20Copy-?= =?UTF-8?q?On-Write=20pages=20after=20fork().=20Before=20a=20write,=20pare?= =?UTF-8?q?nt=20and=20child=20share=20the=20same=20PFN;?=
1122+
To: <bar@foo.com>
1123+
From: <foo@foobar.com>
1124+
Message-ID: <1250334f-7220-2bff-5d87-b87573758d81@bar.com>
1125+
Date: Sun, 7 May 2017 19:54:00 -0700
1126+
MIME-Version: 1.0
1127+
Content-Type: text/plain; charset=UTF-8
1128+
Content-Transfer-Encoding: 8bit
1129+
1130+
Body
1131+
`, Email{
1132+
MessageID: "<1250334f-7220-2bff-5d87-b87573758d81@bar.com>",
1133+
Date: time.Date(2017, time.May, 7, 19, 54, 0, 0, parseTestZone),
1134+
Subject: "[PATCH] Add a new test 'migrate.cow_after_fork' that verifies correct RMAP handling of Copy-On-Write pages after fork(). Before a write, parent and child share the same PFN;",
1135+
Author: "foo@foobar.com",
1136+
Cc: []string{"bar@foo.com", "foo@foobar.com"},
1137+
RawCc: []string{"bar@foo.com", "foo@foobar.com"},
1138+
Body: `Body
1139+
`,
1140+
}},
11201141
}

0 commit comments

Comments
 (0)