mirror of
https://github.com/golang/go
synced 2024-11-23 21:50:08 -07:00
net/mail: better errors on non-ascii characters
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>
This commit is contained in:
parent
e614d60759
commit
a4f057bcc3
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user