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

@ -1,6 +1,7 @@
package smtp
import (
"strconv"
"strings"
"github.com/mjl-/mox/dns"
@ -22,6 +23,24 @@ func (p Path) String() string {
return p.XString(false)
}
// LogString returns both the ASCII-only and optional UTF-8 representation.
func (p Path) LogString() string {
if p.Localpart == "" && p.IPDomain.IsZero() {
return ""
}
s := p.XString(true)
lp := p.Localpart.String()
qlp := strconv.QuoteToASCII(lp)
escaped := qlp != `"`+lp+`"`
if p.IPDomain.Domain.Unicode != "" || escaped {
if escaped {
lp = qlp
}
s += "/" + lp + "@" + p.IPDomain.XString(false)
}
return s
}
// XString is like String, but returns unicode UTF-8 domain names if utf8 is
// true.
func (p Path) XString(utf8 bool) string {