Skip to content

Commit

Permalink
dkim: fix CRLF split between two Write calls
Browse files Browse the repository at this point in the history
Closes: #38
  • Loading branch information
emersion committed Nov 24, 2020
1 parent f413397 commit 816fefe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
41 changes: 27 additions & 14 deletions dkim/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ var canonicalizers = map[Canonicalization]canonicalizer{
CanonicalizationRelaxed: new(relaxedCanonicalizer),
}

// Fix any \n without a matching \r
func fixCRLF(b []byte) []byte {
// crlfFixer fixes any lone LF without a preceding CR.
type crlfFixer struct {
cr bool
}

func (cf *crlfFixer) Fix(b []byte) []byte {
res := make([]byte, 0, len(b))
for i := range b {
if b[i] == '\n' && (i == 0 || b[i-1] != '\r') {
res = append(res, '\r')
for _, ch := range b {
prevCR := cf.cr
cf.cr = false
switch ch {
case '\r':
cf.cr = true
case '\n':
if !prevCR {
res = append(res, '\r')
}
}
res = append(res, b[i])
res = append(res, ch)
}
return res
}
Expand All @@ -45,15 +56,16 @@ func (c *simpleCanonicalizer) CanonicalizeHeader(s string) string {
}

type simpleBodyCanonicalizer struct {
w io.Writer
crlfBuf []byte
w io.Writer
crlfBuf []byte
crlfFixer crlfFixer
}

func (c *simpleBodyCanonicalizer) Write(b []byte) (int, error) {
written := len(b)
b = append(c.crlfBuf, b...)

b = fixCRLF(b)
b = c.crlfFixer.Fix(b)

end := len(b)
// If it ends with \r, maybe the next write will begin with \n
Expand Down Expand Up @@ -116,16 +128,17 @@ func (c *relaxedCanonicalizer) CanonicalizeHeader(s string) string {
}

type relaxedBodyCanonicalizer struct {
w io.Writer
crlfBuf []byte
wspBuf []byte
written bool
w io.Writer
crlfBuf []byte
wspBuf []byte
written bool
crlfFixer crlfFixer
}

func (c *relaxedBodyCanonicalizer) Write(b []byte) (int, error) {
written := len(b)

b = fixCRLF(b)
b = c.crlfFixer.Fix(b)

canonical := make([]byte, 0, len(b))
for _, ch := range b {
Expand Down
21 changes: 21 additions & 0 deletions dkim/canonical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,24 @@ func TestRelaxedCanonicalizer_CanonicalBody(t *testing.T) {
}
}
}

func TestRelaxedCanonicalizer_CanonicalBody_splitCRLF(t *testing.T) {
want := "line 1\r\nline 2\r\n"
writes := [][]byte{
[]byte("line 1\r"),
[]byte("\nline 2"),
}

var b bytes.Buffer
wc := new(relaxedCanonicalizer).CanonicalizeBody(&b)
for _, b := range writes {
if _, err := wc.Write(b); err != nil {
t.Errorf("Expected no error while writing to relaxed body canonicalizer, got: %v", err)
}
}
if err := wc.Close(); err != nil {
t.Errorf("Expected no error while closing relaxed body canonicalizer, got: %v", err)
} else if s := b.String(); s != want {
t.Errorf("Expected canonical body to be %q, but got %q", want, s)
}
}

0 comments on commit 816fefe

Please sign in to comment.