webmail: fix loading a "view" (messages in a mailbox) when the "initial" message cannot be parsed

when we send a list of messages from the mox backend to the js frontend, we
include a parsed form of the "initial" message: the one we immediately show,
typically the top-most (unread) message. however, if that message could not be
parsed (due to invalid header syntax), we would fail the entire operation of
loading the view.

with this change, we simply don't return a parsed form of an initial message if
we cannot parse it. that will cause the webmail frontend to not select &
display a message immediately. if you then try to open the message, you'll
still get an error message as before. but at least the view has been loaded,
and you can open the raw message to inspect the contents.

for issue #219 by wneessen
This commit is contained in:
Mechiel Lukkien
2024-10-05 09:39:46 +02:00
parent 5d97bf198a
commit fb65ec0676
3 changed files with 26 additions and 8 deletions

View File

@ -33,6 +33,7 @@ var Pedantic bool
var (
ErrBadContentType = errors.New("bad content-type")
ErrHeader = errors.New("bad message header")
)
var (
@ -394,6 +395,8 @@ func newPart(log mlog.Log, strict bool, r io.ReaderAt, offset int64, parent *Par
}
// Header returns the parsed header of this part.
//
// Returns a ErrHeader for messages with invalid header syntax.
func (p *Part) Header() (textproto.MIMEHeader, error) {
if p.header != nil {
return p.header, nil
@ -431,6 +434,12 @@ func parseHeader(r io.Reader) (textproto.MIMEHeader, error) {
}
msg, err := mail.ReadMessage(bytes.NewReader(buf))
if err != nil {
// Recognize parsing errors from net/mail.ReadMessage.
// todo: replace with own message parsing code that returns proper error types.
errstr := err.Error()
if strings.HasPrefix(errstr, "malformed initial line:") || strings.HasPrefix(errstr, "malformed header line:") {
err = fmt.Errorf("%w: %v", ErrHeader, err)
}
return zero, err
}
return textproto.MIMEHeader(msg.Header), nil