net/mail: escape arbitrary input when including them in errors

This ensures that input with unexpected characters like control
characters will not be interpreted in unintended ways should they ever
be printed or logged.

Change-Id: I9ee18b44d8ca067886dca065e5aae9266a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/780041
Reviewed-by: Roland Shoemaker <roland@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Nicholas Husin <husin@google.com>
This commit is contained in:
Nicholas S. Husin 2026-05-19 15:04:41 -04:00 committed by Nicholas Husin
parent 0db7bea636
commit edf006c9a3

View file

@ -81,7 +81,7 @@ func readHeader(r *textproto.Reader) (map[string][]string, error) {
if err != nil {
return m, err
}
return m, errors.New("malformed initial line: " + line)
return m, fmt.Errorf("malformed initial line: %q", line)
}
for {
@ -93,7 +93,7 @@ func readHeader(r *textproto.Reader) (map[string][]string, error) {
// Key ends at first colon.
k, v, ok := strings.Cut(kv, ":")
if !ok {
return m, errors.New("malformed header line: " + kv)
return m, fmt.Errorf("malformed header line: %q", kv)
}
key := textproto.CanonicalMIMEHeaderKey(k)