Skip to content

Commit

Permalink
net/mail: better errors on non-ascii characters
Browse files Browse the repository at this point in the history
Fixes #12492

Change-Id: I8bb512027639301e2f2c41aab84e6d06ae88b137
Reviewed-on: https://go-review.googlesource.com/14312
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
nightlyone authored and bradfitz committed Dec 2, 2015
1 parent e614d60 commit a4f057b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/net/mail/message.go
Expand Up @@ -442,17 +442,25 @@ Loop:
return string(qsb), nil
}

var errNonASCII = errors.New("mail: unencoded non-ASCII text in address")

// consumeAtom parses an RFC 5322 atom at the start of p.
// If dot is true, consumeAtom parses an RFC 5322 dot-atom instead.
// If permissive is true, consumeAtom will not fail on
// leading/trailing/double dots in the atom (see golang.org/issue/4938).
func (p *addrParser) consumeAtom(dot bool, permissive bool) (atom string, err error) {
if !isAtext(p.peek(), false) {
if c := p.peek(); !isAtext(c, false) {
if c > 127 {
return "", errNonASCII
}
return "", errors.New("mail: invalid string")
}
i := 1
for ; i < p.len() && isAtext(p.s[i], dot); i++ {
}
if i < p.len() && p.s[i] > 127 {
return "", errNonASCII
}
atom, p.s = string(p.s[:i]), p.s[i:]
if !permissive {
if strings.HasPrefix(atom, ".") {
Expand Down
8 changes: 8 additions & 0 deletions src/net/mail/message_test.go
Expand Up @@ -127,6 +127,14 @@ func TestAddressParsingError(t *testing.T) {
}
}

func TestAddressParsingErrorUnquotedNonASCII(t *testing.T) {
const txt = "µ <micro@example.net>"
_, err := ParseAddress(txt)
if err == nil || !strings.Contains(err.Error(), "unencoded non-ASCII text in address") {
t.Errorf(`mail.ParseAddress(%q) err: %q, want ".*unencoded non-ASCII text in address.*"`, txt, err)
}
}

func TestAddressParsing(t *testing.T) {
tests := []struct {
addrsStr string
Expand Down

0 comments on commit a4f057b

Please sign in to comment.