when logging email addresses with IDNA domain and/or special characters or utf8 in localpart, log both native utf8 form and form with escape localpart and ascii-only domain

the idea is to make it clear from the logging if non-ascii characters are used.

this is implemented by making mlog recognize if a field value that will be
logged has a LogString method. if so, that value is logged. dns.Domain,
smtp.Address, smtp.Localpart, smtp.Path now have a LogString method.

some explicit calls to String have been replaced to LogString, and some %q
formatting have been replaced with %s, because the escaped localpart would
already have double quotes, and double doublequotes aren't easy to read.
This commit is contained in:
Mechiel Lukkien
2023-03-09 20:18:34 +01:00
parent eb26e9b921
commit 5742ed1537
12 changed files with 93 additions and 18 deletions

View File

@ -3,6 +3,7 @@ package smtp
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/mjl-/mox/dns"
@ -53,6 +54,17 @@ func (lp Localpart) String() string {
return r
}
// LogString returns the localpart as string for use in smtp, and an escaped
// representation if it has non-ascii characters.
func (lp Localpart) LogString() string {
s := lp.String()
qs := strconv.QuoteToASCII(s)
if qs != `"`+s+`"` {
s = "/" + qs
}
return s
}
// DSNString returns the localpart as string for use in a DSN.
// utf8 indicates if the remote MTA supports utf8 messaging. If not, the 7bit DSN
// encoding for "utf-8-addr-xtext" from RFC 6533 is used.
@ -116,6 +128,26 @@ func (a Address) String() string {
return a.Localpart.String() + "@" + a.Domain.Name()
}
// LogString returns the address with with utf-8 in localpart and/or domain. In
// case of an IDNA domain and/or quotable characters in the localpart, an address
// with quoted/escaped localpart and ASCII domain is also returned.
func (a Address) LogString() string {
if a.IsZero() {
return ""
}
s := a.Pack(true)
lp := a.Localpart.String()
qlp := strconv.QuoteToASCII(lp)
escaped := qlp != `"`+lp+`"`
if a.Domain.Unicode != "" || escaped {
if escaped {
lp = qlp
}
s += "/" + lp + "@" + a.Domain.ASCII
}
return s
}
// ParseAddress parses an email address. UTF-8 is allowed.
// Returns ErrBadAddress for invalid addresses.
func ParseAddress(s string) (address Address, err error) {