Disallow non-ASCII addresses

This commit is contained in:
Roland Shoemaker 2016-01-06 17:26:59 -08:00
parent aa94f07081
commit 4e569184d5
2 changed files with 20 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
)
@ -52,6 +53,15 @@ type MailerImpl struct {
csprgSource idGenerator
}
func isASCII(str string) bool {
for _, r := range str {
if r > unicode.MaxASCII {
return false
}
}
return true
}
// New constructs a Mailer to represent an account on a particular mail
// transfer agent.
func New(server, port, username, password string) MailerImpl {
@ -71,6 +81,9 @@ func (m *MailerImpl) generateMessage(to []string, subject, body string) ([]byte,
now := m.clk.Now().UTC()
addrs := []string{}
for _, a := range to {
if !isASCII(a) {
return nil, fmt.Errorf("Non-ASCII email address")
}
addrs = append(addrs, strconv.Quote(a))
}
headers := []string{

View File

@ -42,3 +42,10 @@ func TestGenerateMessage(t *testing.T) {
test.AssertEquals(t, fields[8], "")
test.AssertEquals(t, fields[9], "this is the body")
}
func TestFailNonASCIIAddress(t *testing.T) {
fc := clock.NewFake()
m := MailerImpl{From: "send@email.com", clk: fc, csprgSource: fakeSource{}}
_, err := m.generateMessage([]string{"遗憾@email.com"}, "test subject", "this is the body\n")
test.AssertError(t, err, "Allowed a non-ASCII to address incorrectly")
}