mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
dkim: Messages without a blank line should have an empty body
Today, if a message does not have a blank line (which separates headers and body), we also include all lines except the first as the body (even the ones parsed as headers). This is incorrect. This patch fixes the code so that in those messages, all the lines are headers, and the body is empty. Note that since SMTP messages have to end with a new line, that final line will act as the separator if there is no body. So this bug only impacts signing/verification of files on disk via chasquid-util.
This commit is contained in:
@@ -37,7 +37,9 @@ func parseMessage(message string) (headers, string, error) {
|
||||
// Group the lines of each header: the first line, followed by its
|
||||
// continuation lines (if any).
|
||||
groups := [][]string{}
|
||||
eoh := 0
|
||||
|
||||
// End of headers. If there's no blank line, all the lines are headers.
|
||||
eoh := len(lines)
|
||||
for i, line := range lines {
|
||||
if line == "" {
|
||||
eoh = i
|
||||
@@ -67,7 +69,13 @@ func parseMessage(message string) (headers, string, error) {
|
||||
headers = append(headers, h)
|
||||
}
|
||||
|
||||
return headers, strings.Join(lines[eoh+1:], "\r\n"), nil
|
||||
// The body is everything after the blank line (if there is one).
|
||||
body := ""
|
||||
if eoh < len(lines) {
|
||||
body = strings.Join(lines[eoh+1:], "\r\n")
|
||||
}
|
||||
|
||||
return headers, body, nil
|
||||
}
|
||||
|
||||
// Parse a header, given its lines (the first one, followed by the
|
||||
|
||||
@@ -116,8 +116,8 @@ func TestParseMessageWithErrors(t *testing.T) {
|
||||
|
||||
func TestParseMessageWithoutBlankLine(t *testing.T) {
|
||||
// If there's no blank line, all the lines are headers, including the
|
||||
// continuation lines of the last one.
|
||||
hs, _, err := parseMessage("From: a@b\r\nTo: c@d\r\n e@f")
|
||||
// continuation lines of the last one, and the body is empty.
|
||||
hs, body, err := parseMessage("From: a@b\r\nTo: c@d\r\n e@f")
|
||||
if err != nil {
|
||||
t.Fatalf("parseMessage error: %v", err)
|
||||
}
|
||||
@@ -128,6 +128,9 @@ func TestParseMessageWithoutBlankLine(t *testing.T) {
|
||||
if diff := cmp.Diff(want, hs); diff != "" {
|
||||
t.Errorf("parseMessage headers mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
if body != "" {
|
||||
t.Errorf("parseMessage body: wanted empty, got %q", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMessageManyContinuations(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user