fix sign() when a header to add is the first added in raw mail

This commit is contained in:
Stéphane Depierrepont aka Toorop
2015-06-03 15:41:50 +02:00
parent 7cb594e75f
commit 987f4b1d99
2 changed files with 24 additions and 12 deletions

View File

@@ -309,10 +309,8 @@ func canonicalize(email *[]byte, cano string, h []string) (headers, body []byte,
} }
} }
if match != nil { if match != nil {
headersToKeepList.PushBack(match.Value.(string)) headersToKeepList.PushBack(match.Value.(string) + "\r\n")
headersList.Remove(match) headersList.Remove(match)
} else {
headersToKeepList.PushBack(headerToKeep + ":\r\n")
} }
} }
@@ -520,7 +518,7 @@ func getHeadersList(rawHeader *[]byte) (*list.List, error) {
} else { } else {
// New header, save current if exists // New header, save current if exists
if len(currentHeader) != 0 { if len(currentHeader) != 0 {
headersList.PushBack(string(currentHeader)) headersList.PushBack(string(bytes.TrimRight(currentHeader, "\r\n")))
currentHeader = []byte{} currentHeader = []byte{}
} }
currentHeader = append(currentHeader, line...) currentHeader = append(currentHeader, line...)

View File

@@ -75,15 +75,13 @@ var headerSimple = "From: =?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@tmail.i
"Received: from mail483.ha.ovh.net (b6.ovh.net [213.186.33.56])" + CRLF + "Received: from mail483.ha.ovh.net (b6.ovh.net [213.186.33.56])" + CRLF +
" by mo51.mail-out.ovh.net (Postfix) with SMTP id A6E22FF8934" + CRLF + " by mo51.mail-out.ovh.net (Postfix) with SMTP id A6E22FF8934" + CRLF +
" for <toorop@toorop.fr>; Mon, 4 May 2015 14:00:47 +0200 (CEST)" + CRLF + " for <toorop@toorop.fr>; Mon, 4 May 2015 14:00:47 +0200 (CEST)" + CRLF +
"Received: (qmail 21323 invoked from network); 1 May 2015 09:48:39 -0000" + CRLF + "Received: (qmail 21323 invoked from network); 1 May 2015 09:48:39 -0000" + CRLF
"In-Reply-To:" + CRLF
var headerRelaxed = "from:=?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@tmail.io>" + CRLF + var headerRelaxed = "from:=?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@tmail.io>" + CRLF +
"date:Fri, 1 May 2015 11:48:37 +0200" + CRLF + "date:Fri, 1 May 2015 11:48:37 +0200" + CRLF +
"mime-version:1.0" + CRLF + "mime-version:1.0" + CRLF +
"received:from mail483.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo51.mail-out.ovh.net (Postfix) with SMTP id A6E22FF8934 for <toorop@toorop.fr>; Mon, 4 May 2015 14:00:47 +0200 (CEST)" + CRLF + "received:from mail483.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo51.mail-out.ovh.net (Postfix) with SMTP id A6E22FF8934 for <toorop@toorop.fr>; Mon, 4 May 2015 14:00:47 +0200 (CEST)" + CRLF +
"received:(qmail 21323 invoked from network); 1 May 2015 09:48:39 -0000" + CRLF + "received:(qmail 21323 invoked from network); 1 May 2015 09:48:39 -0000" + CRLF
"in-reply-to:" + CRLF
var bodySimple = "Hello world" + CRLF + var bodySimple = "Hello world" + CRLF +
"line with trailing space " + CRLF + "line with trailing space " + CRLF +
@@ -119,11 +117,11 @@ var signedSimpleSimple = "DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=simple
" WcFzOPxn66nnn+CRKaz553tjIn1GeFQ=" + CRLF + emailBase " WcFzOPxn66nnn+CRKaz553tjIn1GeFQ=" + CRLF + emailBase
var signedSimpleSimpleLength = "DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=simple/simple;" + CRLF + var signedSimpleSimpleLength = "DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=simple/simple;" + CRLF +
" s=test; d=tmail.io; l=5; h=from:date:mime-version:received:received;" + CRLF + " s=test; d=tmail.io; l=5; h=from:subject:date:message-id;" + CRLF +
" bh=GF+NsyJx/iX1Yab8k4suJkMG7DBO2lGAB9F2SCY4GWk=;" + CRLF + " bh=GF+NsyJx/iX1Yab8k4suJkMG7DBO2lGAB9F2SCY4GWk=;" + CRLF +
" b=SoEhlu1Emm2ASqo8jMhz6FIf2nNHt3ouY4Av/pFFEkQ048RqUFP437ap7RbtL2wh0N3Kkm" + CRLF + " b=P4cX4WxnSytfsQ3skg3fYIRljleh2iDJidlr/GPfA4S8pTPNZj4SPhB7CJ6OcbSWwJ6Yer" + CRLF +
" AKF2TcTLZ++1nalq+djU+/aP4KYQd4RWWFBjkxDzvCH4bvB1M5AGp4Qz9ldmdMQBWOvvSp" + CRLF + " rHGEmCSEGHJPQm+P12iujJlQ784i34JsBvMC5YAMIQ0DHTNhJRHEyShg1I0B3tqArogdap" + CRLF +
" DIpJW4XNA/uqLSswtjCYbJsSg9Ywv1o=" + CRLF + emailBase " qwWLUSFEhPTXglZVhcHIvYZA9X38iF4=" + CRLF + emailBase
var signedNoFrom = "DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=simple/simple;" + CRLF + var signedNoFrom = "DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=simple/simple;" + CRLF +
" s=test; d=tmail.io; h=from:date:mime-version:received:received;" + CRLF + " s=test; d=tmail.io; h=from:date:mime-version:received:received;" + CRLF +
@@ -194,6 +192,16 @@ var fromGmail = "Return-Path: toorop@gmail.com" + CRLF +
"Toorop" + CRLF + "Toorop" + CRLF +
"http://www.protecmail.com" + CRLF + CRLF + CRLF "http://www.protecmail.com" + CRLF + CRLF + CRLF
var missingHeaderMail = "Received: tmail deliverd remote 439903a23facd153908f3e17fb487962d01f4b44; 02 Jun 2015 10:00:24 +0000" + CRLF +
"X-Env-From: toorop@toorop.fr" + CRLF +
"Received: from 192.168.0.2 (no reverse) by 192.168.0.46 (no reverse) whith" + CRLF +
" SMTP; 02 Jun 2015 10:00:23 +0000; tmail 0.0.8;" + CRLF +
" d3c348615ef29692ca8bdacb40d0e147c977579c" + CRLF +
"Message-ID: <1433239223.d3c348615ef29692ca8bdacb40d0e147c977579c@toorop.fr>" + CRLF +
"Date: Thu, 21 May 2015 19:43:42 +0200" + CRLF +
"Subject: test" + CRLF + CRLF +
"test"
func Test_NewSigOptions(t *testing.T) { func Test_NewSigOptions(t *testing.T) {
options := NewSigOptions() options := NewSigOptions()
assert.Equal(t, "rsa-sha256", options.Algo) assert.Equal(t, "rsa-sha256", options.Algo)
@@ -309,6 +317,11 @@ func Test_Sign(t *testing.T) {
err = Sign(&emailSimple, options) err = Sign(&emailSimple, options)
assert.Equal(t, []byte(signedSimpleSimple), emailSimple) assert.Equal(t, []byte(signedSimpleSimple), emailSimple)
options.Headers = []string{"from", "subject", "date", "message-id"}
memail := []byte(missingHeaderMail)
err = Sign(&memail, options)
assert.NoError(t, err)
options.BodyLength = 5 options.BodyLength = 5
options.Canonicalization = "simple/simple" options.Canonicalization = "simple/simple"
emailSimple = append([]byte(nil), email...) emailSimple = append([]byte(nil), email...)
@@ -360,4 +373,5 @@ func Test_Verify(t *testing.T) {
status, err = Verify(&email) status, err = Verify(&email)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, SUCCESS, status) assert.Equal(t, SUCCESS, status)
} }