Commit a4f057bc authored by Ingo Oeser's avatar Ingo Oeser Committed by Brad Fitzpatrick

net/mail: better errors on non-ascii characters

Fixes #12492

Change-Id: I8bb512027639301e2f2c41aab84e6d06ae88b137
Reviewed-on: https://go-review.googlesource.com/14312Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e614d607
......@@ -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, ".") {
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment