1// Package webmail implements a webmail client, serving html/js and providing an API for message actions and SSE endpoint for receiving real-time updates.
2package webmail
3
4// todo: should we be serving the messages/parts on a separate (sub)domain for user-content? to limit damage if the csp rules aren't enough.
5
6import (
7 "archive/zip"
8 "bytes"
9 "context"
10 "encoding/base64"
11 "encoding/json"
12 "errors"
13 "fmt"
14 "io"
15 "log/slog"
16 "mime"
17 "net/http"
18 "os"
19 "path/filepath"
20 "regexp"
21 "runtime/debug"
22 "strconv"
23 "strings"
24
25 _ "embed"
26
27 "golang.org/x/net/html"
28
29 "github.com/prometheus/client_golang/prometheus"
30 "github.com/prometheus/client_golang/prometheus/promauto"
31
32 "github.com/mjl-/bstore"
33 "github.com/mjl-/sherpa"
34
35 "github.com/mjl-/mox/message"
36 "github.com/mjl-/mox/metrics"
37 "github.com/mjl-/mox/mlog"
38 "github.com/mjl-/mox/mox-"
39 "github.com/mjl-/mox/moxio"
40 "github.com/mjl-/mox/store"
41 "github.com/mjl-/mox/webauth"
42 "github.com/mjl-/mox/webops"
43)
44
45var pkglog = mlog.New("webmail", nil)
46
47type ctxKey string
48
49// We pass the request to the sherpa handler so the TLS info can be used for
50// the Received header in submitted messages. Most API calls need just the
51// account name.
52var requestInfoCtxKey ctxKey = "requestInfo"
53
54type requestInfo struct {
55 Log mlog.Log
56 LoginAddress string
57 Account *store.Account // Nil only for methods Login and LoginPrep.
58 SessionToken store.SessionToken
59 Response http.ResponseWriter
60 Request *http.Request // For Proto and TLS connection state during message submit.
61}
62
63//go:embed webmail.html
64var webmailHTML []byte
65
66//go:embed webmail.js
67var webmailJS []byte
68
69//go:embed msg.html
70var webmailmsgHTML []byte
71
72//go:embed msg.js
73var webmailmsgJS []byte
74
75//go:embed text.html
76var webmailtextHTML []byte
77
78//go:embed text.js
79var webmailtextJS []byte
80
81var (
82 // Similar between ../webmail/webmail.go:/metricSubmission and ../smtpserver/server.go:/metricSubmission and ../webapisrv/server.go:/metricSubmission
83 metricSubmission = promauto.NewCounterVec(
84 prometheus.CounterOpts{
85 Name: "mox_webmail_submission_total",
86 Help: "Webmail message submission results, known values (those ending with error are server errors): ok, badfrom, messagelimiterror, recipientlimiterror, queueerror, storesenterror.",
87 },
88 []string{
89 "result",
90 },
91 )
92 metricServerErrors = promauto.NewCounterVec(
93 prometheus.CounterOpts{
94 Name: "mox_webmail_errors_total",
95 Help: "Webmail server errors, known values: dkimsign, submit.",
96 },
97 []string{
98 "error",
99 },
100 )
101 metricSSEConnections = promauto.NewGauge(
102 prometheus.GaugeOpts{
103 Name: "mox_webmail_sse_connections",
104 Help: "Number of active webmail SSE connections.",
105 },
106 )
107)
108
109func xcheckf(ctx context.Context, err error, format string, args ...any) {
110 if err == nil {
111 return
112 }
113 msg := fmt.Sprintf(format, args...)
114 errmsg := fmt.Sprintf("%s: %s", msg, err)
115 pkglog.WithContext(ctx).Errorx(msg, err)
116 code := "server:error"
117 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
118 code = "user:error"
119 }
120 panic(&sherpa.Error{Code: code, Message: errmsg})
121}
122
123func xcheckuserf(ctx context.Context, err error, format string, args ...any) {
124 if err == nil {
125 return
126 }
127 msg := fmt.Sprintf(format, args...)
128 errmsg := fmt.Sprintf("%s: %s", msg, err)
129 pkglog.WithContext(ctx).Errorx(msg, err)
130 panic(&sherpa.Error{Code: "user:error", Message: errmsg})
131}
132
133func xdbwrite(ctx context.Context, acc *store.Account, fn func(tx *bstore.Tx)) {
134 err := acc.DB.Write(ctx, func(tx *bstore.Tx) error {
135 fn(tx)
136 return nil
137 })
138 xcheckf(ctx, err, "transaction")
139}
140
141func xdbread(ctx context.Context, acc *store.Account, fn func(tx *bstore.Tx)) {
142 err := acc.DB.Read(ctx, func(tx *bstore.Tx) error {
143 fn(tx)
144 return nil
145 })
146 xcheckf(ctx, err, "transaction")
147}
148
149var webmailFile = &mox.WebappFile{
150 HTML: webmailHTML,
151 JS: webmailJS,
152 HTMLPath: filepath.FromSlash("webmail/webmail.html"),
153 JSPath: filepath.FromSlash("webmail/webmail.js"),
154}
155
156// Serve content, either from a file, or return the fallback data. Caller
157// should already have set the content-type. We use this to return a file from
158// the local file system (during development), or embedded in the binary (when
159// deployed).
160func serveContentFallback(log mlog.Log, w http.ResponseWriter, r *http.Request, path string, fallback []byte) {
161 f, err := os.Open(path)
162 if err == nil {
163 defer f.Close()
164 st, err := f.Stat()
165 if err == nil {
166 http.ServeContent(w, r, "", st.ModTime(), f)
167 return
168 }
169 }
170 http.ServeContent(w, r, "", mox.FallbackMtime(log), bytes.NewReader(fallback))
171}
172
173func init() {
174 mox.NewWebmailHandler = func(maxMsgSize int64, basePath string, isForwarded bool, accountPath string) http.Handler {
175 return http.HandlerFunc(Handler(maxMsgSize, basePath, isForwarded, accountPath))
176 }
177}
178
179// Handler returns a handler for the webmail endpoints, customized for the max
180// message size coming from the listener and cookiePath.
181func Handler(maxMessageSize int64, cookiePath string, isForwarded bool, accountPath string) func(w http.ResponseWriter, r *http.Request) {
182 sh, err := makeSherpaHandler(maxMessageSize, cookiePath, isForwarded)
183 return func(w http.ResponseWriter, r *http.Request) {
184 if err != nil {
185 http.Error(w, "500 - internal server error - cannot handle requests", http.StatusInternalServerError)
186 return
187 }
188 handle(sh, isForwarded, accountPath, w, r)
189 }
190}
191
192func handle(apiHandler http.Handler, isForwarded bool, accountPath string, w http.ResponseWriter, r *http.Request) {
193 ctx := r.Context()
194 log := pkglog.WithContext(ctx).With(slog.String("userauth", ""))
195
196 // Server-sent event connection, for all initial data (list of mailboxes), list of
197 // messages, and all events afterwards. Authenticated through a single use token in
198 // the query string, which it got from a Token API call.
199 if r.URL.Path == "/events" {
200 serveEvents(ctx, log, accountPath, w, r)
201 return
202 }
203
204 defer func() {
205 x := recover()
206 if x == nil {
207 return
208 }
209 err, ok := x.(*sherpa.Error)
210 if !ok {
211 log.WithContext(ctx).Error("handle panic", slog.Any("err", x))
212 debug.PrintStack()
213 metrics.PanicInc(metrics.Webmailhandle)
214 panic(x)
215 }
216 if strings.HasPrefix(err.Code, "user:") {
217 log.Debugx("webmail user error", err)
218 http.Error(w, "400 - bad request - "+err.Message, http.StatusBadRequest)
219 } else {
220 log.Errorx("webmail server error", err)
221 http.Error(w, "500 - internal server error - "+err.Message, http.StatusInternalServerError)
222 }
223 }()
224
225 switch r.URL.Path {
226 case "/":
227 switch r.Method {
228 case "GET", "HEAD":
229 h := w.Header()
230 h.Set("X-Frame-Options", "deny")
231 h.Set("Referrer-Policy", "same-origin")
232 webmailFile.Serve(ctx, log, w, r)
233 default:
234 http.Error(w, "405 - method not allowed - use get", http.StatusMethodNotAllowed)
235 }
236 return
237
238 case "/msg.js", "/text.js":
239 switch r.Method {
240 default:
241 http.Error(w, "405 - method not allowed - use get", http.StatusMethodNotAllowed)
242 return
243 case "GET", "HEAD":
244 }
245
246 path := filepath.Join("webmail", r.URL.Path[1:])
247 var fallback = webmailmsgJS
248 if r.URL.Path == "/text.js" {
249 fallback = webmailtextJS
250 }
251
252 w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
253 serveContentFallback(log, w, r, path, fallback)
254 return
255 }
256
257 isAPI := strings.HasPrefix(r.URL.Path, "/api/")
258 // Only allow POST for calls, they will not work cross-domain without CORS.
259 if isAPI && r.URL.Path != "/api/" && r.Method != "POST" {
260 http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
261 return
262 }
263
264 var loginAddress, accName string
265 var sessionToken store.SessionToken
266 // All other URLs, except the login endpoint require some authentication.
267 if r.URL.Path != "/api/LoginPrep" && r.URL.Path != "/api/Login" {
268 var ok bool
269 isExport := r.URL.Path == "/export"
270 requireCSRF := isAPI || isExport
271 accName, sessionToken, loginAddress, ok = webauth.Check(ctx, log, webauth.Accounts, "webmail", isForwarded, w, r, isAPI, requireCSRF, isExport)
272 if !ok {
273 // Response has been written already.
274 return
275 }
276 }
277
278 if isAPI {
279 var acc *store.Account
280 if accName != "" {
281 log = log.With(slog.String("account", accName))
282 var err error
283 acc, err = store.OpenAccount(log, accName)
284 if err != nil {
285 log.Errorx("open account", err)
286 http.Error(w, "500 - internal server error - error opening account", http.StatusInternalServerError)
287 return
288 }
289 defer func() {
290 err := acc.Close()
291 log.Check(err, "closing account")
292 }()
293 }
294 reqInfo := requestInfo{log, loginAddress, acc, sessionToken, w, r}
295 ctx = context.WithValue(ctx, requestInfoCtxKey, reqInfo)
296 apiHandler.ServeHTTP(w, r.WithContext(ctx))
297 return
298 }
299
300 // We are now expecting the following URLs:
301 // .../export
302 // .../msg/<msgid>/{attachments.zip,parsedmessage.js,raw}
303 // .../msg/<msgid>/{,msg}{text,html,htmlexternal}
304 // .../msg/<msgid>/{view,viewtext,download}/<partid>
305
306 if r.URL.Path == "/export" {
307 webops.Export(log, accName, w, r)
308 return
309 }
310
311 if !strings.HasPrefix(r.URL.Path, "/msg/") {
312 http.NotFound(w, r)
313 return
314 }
315
316 t := strings.Split(r.URL.Path[len("/msg/"):], "/")
317 if len(t) < 2 {
318 http.NotFound(w, r)
319 return
320 }
321
322 id, err := strconv.ParseInt(t[0], 10, 64)
323 if err != nil || id == 0 {
324 http.NotFound(w, r)
325 return
326 }
327
328 // Many of the requests need either a message or a parsed part. Make it easy to
329 // fetch/prepare and cleanup. We only do all the work when the request seems legit
330 // (valid HTTP route and method).
331 xprepare := func() (acc *store.Account, m store.Message, msgr *store.MsgReader, p message.Part, cleanup func(), ok bool) {
332 if r.Method != "GET" {
333 http.Error(w, "405 - method not allowed - post required", http.StatusMethodNotAllowed)
334 return
335 }
336
337 defer func() {
338 if ok {
339 return
340 }
341 if msgr != nil {
342 err := msgr.Close()
343 log.Check(err, "closing message reader")
344 msgr = nil
345 }
346 if acc != nil {
347 err := acc.Close()
348 log.Check(err, "closing account")
349 acc = nil
350 }
351 }()
352
353 var err error
354
355 acc, err = store.OpenAccount(log, accName)
356 xcheckf(ctx, err, "open account")
357
358 m = store.Message{ID: id}
359 err = acc.DB.Get(ctx, &m)
360 if err == bstore.ErrAbsent || err == nil && m.Expunged {
361 http.NotFound(w, r)
362 return
363 }
364 xcheckf(ctx, err, "get message")
365
366 msgr = acc.MessageReader(m)
367
368 p, err = m.LoadPart(msgr)
369 xcheckf(ctx, err, "load parsed message")
370
371 cleanup = func() {
372 err := msgr.Close()
373 log.Check(err, "closing message reader")
374 err = acc.Close()
375 log.Check(err, "closing account")
376 }
377 ok = true
378 return
379 }
380
381 h := w.Header()
382
383 // We set a Content-Security-Policy header that is as strict as possible, depending
384 // on the type of message/part/html/js. We have to be careful because we are
385 // returning data that is coming in from external places. E.g. HTML could contain
386 // javascripts that we don't want to execute, especially not on our domain. We load
387 // resources in an iframe. The CSP policy starts out with default-src 'none' to
388 // disallow loading anything, then start allowing what is safe, such as inlined
389 // datauri images and inline styles. Data can only be loaded when the request is
390 // coming from the same origin (so other sites cannot include resources
391 // (messages/parts)).
392 //
393 // We want to load resources in sandbox-mode, causing the page to be loaded as from
394 // a different origin. If sameOrigin is set, we have a looser CSP policy:
395 // allow-same-origin is set so resources are loaded as coming from this same
396 // origin. This is needed for the msg* endpoints that render a message, where we
397 // load the message body in a separate iframe again (with stricter CSP again),
398 // which we need to access for its inner height. If allowSelfScript is also set
399 // (for "msgtext"), the CSP leaves out the sandbox entirely.
400 //
401 // If allowExternal is set, we allow loading image, media (audio/video), styles and
402 // fronts from external URLs as well as inline URI's. By default we don't allow any
403 // loading of content, except inlined images (we do that ourselves for images
404 // embedded in the email), and we allow inline styles (which are safely constrained
405 // to an iframe).
406 //
407 // If allowSelfScript is set, inline scripts and scripts from our origin are
408 // allowed. Used to display a message including header. The header is rendered with
409 // javascript, the content is rendered in a separate iframe with a CSP that doesn't
410 // have allowSelfScript.
411 headers := func(sameOrigin, allowExternal, allowSelfScript, allowSelfImg bool) {
412 // allow-popups is needed to make opening links in new tabs work.
413 sb := "sandbox allow-popups allow-popups-to-escape-sandbox; "
414 if sameOrigin && allowSelfScript {
415 // Sandbox with both allow-same-origin and allow-script would not provide security,
416 // and would give warning in console about that.
417 sb = ""
418 } else if sameOrigin {
419 sb = "sandbox allow-popups allow-popups-to-escape-sandbox allow-same-origin; "
420 }
421 script := ""
422 if allowSelfScript {
423 script = "; script-src 'unsafe-inline' 'self'; frame-src 'self'; connect-src 'self'"
424 }
425 var csp string
426 if allowExternal {
427 csp = sb + "frame-ancestors 'self'; default-src 'none'; img-src data: http: https: 'unsafe-inline'; style-src 'unsafe-inline' data: http: https:; font-src data: http: https: 'unsafe-inline'; media-src 'unsafe-inline' data: http: https:" + script
428 } else if allowSelfImg {
429 csp = sb + "frame-ancestors 'self'; default-src 'none'; img-src data: 'self'; style-src 'unsafe-inline'" + script
430 } else {
431 csp = sb + "frame-ancestors 'self'; default-src 'none'; img-src data:; style-src 'unsafe-inline'" + script
432 }
433 h.Set("Content-Security-Policy", csp)
434 h.Set("X-Frame-Options", "sameorigin") // Duplicate with CSP, but better too much than too little.
435 h.Set("X-Content-Type-Options", "nosniff")
436 h.Set("Referrer-Policy", "no-referrer")
437 }
438
439 switch {
440 case len(t) == 2 && t[1] == "attachments.zip":
441 acc, m, msgr, p, cleanup, ok := xprepare()
442 if !ok {
443 return
444 }
445 defer cleanup()
446 state := msgState{acc: acc, m: m, msgr: msgr, part: &p}
447 // note: state is cleared by cleanup
448
449 mi, err := messageItem(log, m, &state)
450 xcheckf(ctx, err, "parsing message")
451
452 headers(false, false, false, false)
453 h.Set("Content-Type", "application/zip")
454 h.Set("Cache-Control", "no-store, max-age=0")
455 var subjectSlug string
456 if p.Envelope != nil {
457 s := p.Envelope.Subject
458 s = strings.ToLower(s)
459 s = regexp.MustCompile("[^a-z0-9_.-]").ReplaceAllString(s, "-")
460 s = regexp.MustCompile("--*").ReplaceAllString(s, "-")
461 s = strings.TrimLeft(s, "-")
462 s = strings.TrimRight(s, "-")
463 if s != "" {
464 s = "-" + s
465 }
466 subjectSlug = s
467 }
468 filename := fmt.Sprintf("email-%d-attachments-%s%s.zip", m.ID, m.Received.Format("20060102-150405"), subjectSlug)
469 cd := mime.FormatMediaType("attachment", map[string]string{"filename": filename})
470 h.Set("Content-Disposition", cd)
471
472 zw := zip.NewWriter(w)
473 names := map[string]bool{}
474 for _, a := range mi.Attachments {
475 ap := a.Part
476 name := tryDecodeParam(log, ap.ContentTypeParams["name"])
477 if name == "" {
478 // We don't check errors, this is all best-effort.
479 h, _ := ap.Header()
480 disposition := h.Get("Content-Disposition")
481 _, params, _ := mime.ParseMediaType(disposition)
482 name = tryDecodeParam(log, params["filename"])
483 }
484 if name != "" {
485 name = filepath.Base(name)
486 }
487 mt := strings.ToLower(ap.MediaType + "/" + ap.MediaSubType)
488 if name == "" || names[name] {
489 ext := filepath.Ext(name)
490 if ext == "" {
491 // Handle just a few basic types.
492 extensions := map[string]string{
493 "text/plain": ".txt",
494 "text/html": ".html",
495 "image/jpeg": ".jpg",
496 "image/png": ".png",
497 "image/gif": ".gif",
498 "application/zip": ".zip",
499 }
500 ext = extensions[mt]
501 if ext == "" {
502 ext = ".bin"
503 }
504 }
505 var stem string
506 if name != "" && strings.HasSuffix(name, ext) {
507 stem = strings.TrimSuffix(name, ext)
508 } else {
509 stem = "attachment"
510 for _, index := range a.Path {
511 stem += fmt.Sprintf("-%d", index)
512 }
513 }
514 name = stem + ext
515 seq := 0
516 for names[name] {
517 seq++
518 name = stem + fmt.Sprintf("-%d", seq) + ext
519 }
520 }
521 names[name] = true
522
523 fh := zip.FileHeader{
524 Name: name,
525 Modified: m.Received,
526 }
527 nodeflate := map[string]bool{
528 "application/x-bzip2": true,
529 "application/zip": true,
530 "application/x-zip-compressed": true,
531 "application/gzip": true,
532 "application/x-gzip": true,
533 "application/vnd.rar": true,
534 "application/x-rar-compressed": true,
535 "application/x-7z-compressed": true,
536 }
537 // Sniff content-type as well for compressed data.
538 buf := make([]byte, 512)
539 n, _ := io.ReadFull(ap.Reader(), buf)
540 var sniffmt string
541 if n > 0 {
542 sniffmt = strings.ToLower(http.DetectContentType(buf[:n]))
543 }
544 deflate := ap.MediaType != "VIDEO" && ap.MediaType != "AUDIO" && (ap.MediaType != "IMAGE" || ap.MediaSubType == "BMP") && !nodeflate[mt] && !nodeflate[sniffmt]
545 if deflate {
546 fh.Method = zip.Deflate
547 }
548 // We cannot return errors anymore: we have already sent an application/zip header.
549 if zf, err := zw.CreateHeader(&fh); err != nil {
550 log.Check(err, "adding to zip file")
551 return
552 } else if _, err := io.Copy(zf, ap.Reader()); err != nil {
553 log.Check(err, "writing to zip file")
554 return
555 }
556 }
557 err = zw.Close()
558 log.Check(err, "final write to zip file")
559
560 // Raw display of a message, as text/plain.
561 case len(t) == 2 && t[1] == "raw":
562 _, _, msgr, p, cleanup, ok := xprepare()
563 if !ok {
564 return
565 }
566 defer cleanup()
567
568 // We intentially use text/plain. We certainly don't want to return a format that
569 // browsers or users would think of executing. We do set the charset if available
570 // on the outer part. If present, we assume it may be relevant for other parts. If
571 // not, there is not much we could do better...
572 headers(false, false, false, false)
573 ct := "text/plain"
574 params := map[string]string{}
575 if charset := p.ContentTypeParams["charset"]; charset != "" {
576 params["charset"] = charset
577 }
578 h.Set("Content-Type", mime.FormatMediaType(ct, params))
579 h.Set("Cache-Control", "no-store, max-age=0")
580
581 _, err := io.Copy(w, &moxio.AtReader{R: msgr})
582 log.Check(err, "writing raw")
583
584 case len(t) == 2 && (t[1] == "msgtext" || t[1] == "msghtml" || t[1] == "msghtmlexternal"):
585 // msg.html has a javascript tag with message data, and javascript to render the
586 // message header like the regular webmail.html and to load the message body in a
587 // separate iframe with a separate request with stronger CSP.
588 acc, m, msgr, p, cleanup, ok := xprepare()
589 if !ok {
590 return
591 }
592 defer cleanup()
593
594 state := msgState{acc: acc, m: m, msgr: msgr, part: &p}
595 // note: state is cleared by cleanup
596
597 pm, err := parsedMessage(log, m, &state, true, true)
598 xcheckf(ctx, err, "getting parsed message")
599 if t[1] == "msgtext" && len(pm.Texts) == 0 || t[1] != "msgtext" && !pm.HasHTML {
600 http.Error(w, "400 - bad request - no such part", http.StatusBadRequest)
601 return
602 }
603
604 sameorigin := true
605 loadExternal := t[1] == "msghtmlexternal"
606 allowSelfScript := true
607 headers(sameorigin, loadExternal, allowSelfScript, false)
608 h.Set("Content-Type", "text/html; charset=utf-8")
609 h.Set("Cache-Control", "no-store, max-age=0")
610
611 path := filepath.FromSlash("webmail/msg.html")
612 fallback := webmailmsgHTML
613 serveContentFallback(log, w, r, path, fallback)
614
615 case len(t) == 2 && t[1] == "parsedmessage.js":
616 // Used by msg.html, for the msg* endpoints, for the data needed to show all data
617 // except the message body.
618 // This is js with data inside instead so we can load it synchronously, which we do
619 // to get a "loaded" event after the page was actually loaded.
620
621 acc, m, msgr, p, cleanup, ok := xprepare()
622 if !ok {
623 return
624 }
625 defer cleanup()
626 state := msgState{acc: acc, m: m, msgr: msgr, part: &p}
627 // note: state is cleared by cleanup
628
629 pm, err := parsedMessage(log, m, &state, true, true)
630 xcheckf(ctx, err, "parsing parsedmessage")
631 pmjson, err := json.Marshal(pm)
632 xcheckf(ctx, err, "marshal parsedmessage")
633
634 m.MsgPrefix = nil
635 m.ParsedBuf = nil
636 mi := MessageItem{m, pm.envelope, pm.attachments, pm.isSigned, pm.isEncrypted, pm.firstLine, false}
637 mijson, err := json.Marshal(mi)
638 xcheckf(ctx, err, "marshal messageitem")
639
640 headers(false, false, false, false)
641 h.Set("Content-Type", "application/javascript; charset=utf-8")
642 h.Set("Cache-Control", "no-store, max-age=0")
643
644 _, err = fmt.Fprintf(w, "window.messageItem = %s;\nwindow.parsedMessage = %s;\n", mijson, pmjson)
645 log.Check(err, "writing parsedmessage.js")
646
647 case len(t) == 2 && t[1] == "text":
648 // Returns text.html whichs loads the message data with a javascript tag and
649 // renders just the text content with the same code as webmail.html. Used by the
650 // iframe in the msgtext endpoint. Not used by the regular webmail viewer, it
651 // renders the text itself, with the same shared js code.
652 acc, m, msgr, p, cleanup, ok := xprepare()
653 if !ok {
654 return
655 }
656 defer cleanup()
657
658 state := msgState{acc: acc, m: m, msgr: msgr, part: &p}
659 // note: state is cleared by cleanup
660
661 pm, err := parsedMessage(log, m, &state, true, true)
662 xcheckf(ctx, err, "parsing parsedmessage")
663
664 if len(pm.Texts) == 0 {
665 http.Error(w, "400 - bad request - no text part in message", http.StatusBadRequest)
666 return
667 }
668
669 // Needed for inner document height for outer iframe height in separate message view.
670 sameorigin := true
671 allowSelfScript := true
672 allowSelfImg := true
673 headers(sameorigin, false, allowSelfScript, allowSelfImg)
674 h.Set("Content-Type", "text/html; charset=utf-8")
675 h.Set("Cache-Control", "no-store, max-age=0")
676
677 // We typically return the embedded file, but during development it's handy to load
678 // from disk.
679 path := filepath.FromSlash("webmail/text.html")
680 fallback := webmailtextHTML
681 serveContentFallback(log, w, r, path, fallback)
682
683 case len(t) == 2 && (t[1] == "html" || t[1] == "htmlexternal"):
684 // Returns the first HTML part, with "cid:" URIs replaced with an inlined datauri
685 // if the referenced Content-ID attachment can be found.
686 _, _, _, p, cleanup, ok := xprepare()
687 if !ok {
688 return
689 }
690 defer cleanup()
691
692 setHeaders := func() {
693 // Needed for inner document height for outer iframe height in separate message
694 // view. We only need that when displaying as a separate message on the msghtml*
695 // endpoints. When displaying in the regular webmail, we don't need to know the
696 // inner height so we load it as different origin, which should be safer.
697 sameorigin := r.URL.Query().Get("sameorigin") == "true"
698 allowExternal := strings.HasSuffix(t[1], "external")
699 headers(sameorigin, allowExternal, false, false)
700
701 h.Set("Content-Type", "text/html; charset=utf-8")
702 h.Set("Cache-Control", "no-store, max-age=0")
703 }
704
705 // todo: skip certain html parts? e.g. with content-disposition: attachment?
706 var done bool
707 var usePart func(p *message.Part, parents []*message.Part)
708 usePart = func(p *message.Part, parents []*message.Part) {
709 if done {
710 return
711 }
712 mt := p.MediaType + "/" + p.MediaSubType
713 switch mt {
714 case "TEXT/HTML":
715 done = true
716 err := inlineSanitizeHTML(log, setHeaders, w, p, parents)
717 if err != nil {
718 http.Error(w, "400 - bad request - "+err.Error(), http.StatusBadRequest)
719 }
720 return
721 }
722 parents = append(parents, p)
723 for _, sp := range p.Parts {
724 usePart(&sp, parents)
725 }
726 }
727 usePart(&p, nil)
728
729 if !done {
730 http.Error(w, "400 - bad request - no html part in message", http.StatusBadRequest)
731 }
732
733 case len(t) == 3 && (t[1] == "view" || t[1] == "viewtext" || t[1] == "download"):
734 // View any part, as referenced in the last element path. "0" is the whole message,
735 // 0.0 is the first subpart, etc. "view" returns it with the content-type from the
736 // message (could be dangerous, but we set strict CSP headers), "viewtext" returns
737 // data with a text/plain content-type so the browser will attempt to display it,
738 // and "download" adds a content-disposition header causing the browser the
739 // download the file.
740 _, _, _, p, cleanup, ok := xprepare()
741 if !ok {
742 return
743 }
744 defer cleanup()
745
746 paths := strings.Split(t[2], ".")
747 if len(paths) == 0 || paths[0] != "0" {
748 http.NotFound(w, r)
749 return
750 }
751 ap := p
752 for _, e := range paths[1:] {
753 index, err := strconv.ParseInt(e, 10, 32)
754 if err != nil || index < 0 || int(index) >= len(ap.Parts) {
755 http.NotFound(w, r)
756 return
757 }
758 ap = ap.Parts[int(index)]
759 }
760
761 headers(false, false, false, false)
762 var ct string
763 if t[1] == "viewtext" {
764 ct = "text/plain"
765 } else {
766 ct = strings.ToLower(ap.MediaType + "/" + ap.MediaSubType)
767 }
768 h.Set("Content-Type", ct)
769 h.Set("Cache-Control", "no-store, max-age=0")
770 if t[1] == "download" {
771 name := tryDecodeParam(log, ap.ContentTypeParams["name"])
772 if name == "" {
773 // We don't check errors, this is all best-effort.
774 h, _ := ap.Header()
775 disposition := h.Get("Content-Disposition")
776 _, params, _ := mime.ParseMediaType(disposition)
777 name = tryDecodeParam(log, params["filename"])
778 }
779 if name == "" {
780 name = "attachment.bin"
781 }
782 cd := mime.FormatMediaType("attachment", map[string]string{"filename": name})
783 h.Set("Content-Disposition", cd)
784 }
785
786 _, err := io.Copy(w, ap.Reader())
787 if err != nil && !moxio.IsClosed(err) {
788 log.Errorx("copying attachment", err)
789 }
790 default:
791 http.NotFound(w, r)
792 }
793}
794
795// inlineSanitizeHTML writes the part as HTML, with "cid:" URIs for html "src"
796// attributes inlined and with potentially dangerous tags removed (javascript). The
797// sanitizing is just a first layer of defense, CSP headers block execution of
798// scripts. If the HTML becomes too large, an error is returned. Before writing
799// HTML, setHeaders is called to write the required headers for content-type and
800// CSP. On error, setHeader is not called, no output is written and the caller
801// should write an error response.
802func inlineSanitizeHTML(log mlog.Log, setHeaders func(), w io.Writer, p *message.Part, parents []*message.Part) error {
803 // Prepare cids if there is a chance we will use them.
804 cids := map[string]*message.Part{}
805 for _, parent := range parents {
806 if parent.MediaType+"/"+parent.MediaSubType == "MULTIPART/RELATED" && p.DecodedSize < 2*1024*1024 {
807 for i, rp := range parent.Parts {
808 if rp.ContentID != "" {
809 cids[strings.ToLower(rp.ContentID)] = &parent.Parts[i]
810 }
811 }
812 }
813 }
814
815 node, err := html.Parse(p.ReaderUTF8OrBinary())
816 if err != nil {
817 return fmt.Errorf("parsing html: %v", err)
818 }
819
820 // We track size, if it becomes too much, we abort and still copy as regular html.
821 var totalSize int64
822 if err := inlineNode(node, cids, &totalSize); err != nil {
823 return fmt.Errorf("inline cid uris in html nodes: %w", err)
824 }
825 sanitizeNode(node)
826 setHeaders()
827 err = html.Render(w, node)
828 log.Check(err, "writing html")
829 return nil
830}
831
832// We inline cid: URIs into data: URIs. If a cid is missing in the
833// multipart/related, we ignore the error and continue with other HTML nodes. It
834// will probably just result in a "broken image". We limit the max size we
835// generate. We only replace "src" attributes that start with "cid:". A cid URI
836// could theoretically occur in many more places, like link href, and css url().
837// That's probably not common though. Let's wait for someone to need it.
838func inlineNode(node *html.Node, cids map[string]*message.Part, totalSize *int64) error {
839 for i, a := range node.Attr {
840 if a.Key != "src" || !caselessPrefix(a.Val, "cid:") || a.Namespace != "" {
841 continue
842 }
843 cid := a.Val[4:]
844 ap := cids["<"+strings.ToLower(cid)+">"]
845 if ap == nil {
846 // Missing cid, can happen with email, no need to stop returning data.
847 continue
848 }
849 *totalSize += ap.DecodedSize
850 if *totalSize >= 10*1024*1024 {
851 return fmt.Errorf("html too large")
852 }
853 var sb strings.Builder
854 if _, err := fmt.Fprintf(&sb, "data:%s;base64,", strings.ToLower(ap.MediaType+"/"+ap.MediaSubType)); err != nil {
855 return fmt.Errorf("writing datauri: %v", err)
856 }
857 w := base64.NewEncoder(base64.StdEncoding, &sb)
858 if _, err := io.Copy(w, ap.Reader()); err != nil {
859 return fmt.Errorf("writing base64 datauri: %v", err)
860 }
861 node.Attr[i].Val = sb.String()
862 }
863 for node = node.FirstChild; node != nil; node = node.NextSibling {
864 if err := inlineNode(node, cids, totalSize); err != nil {
865 return err
866 }
867 }
868 return nil
869}
870
871func caselessPrefix(k, pre string) bool {
872 return len(k) >= len(pre) && strings.EqualFold(k[:len(pre)], pre)
873}
874
875var targetable = map[string]bool{
876 "a": true,
877 "area": true,
878 "form": true,
879 "base": true,
880}
881
882// sanitizeNode removes script elements, on* attributes, javascript: href
883// attributes, adds target="_blank" to all links and to a base tag.
884func sanitizeNode(node *html.Node) {
885 i := 0
886 var haveTarget, haveRel bool
887 for i < len(node.Attr) {
888 a := node.Attr[i]
889 // Remove dangerous attributes.
890 if strings.HasPrefix(a.Key, "on") || a.Key == "href" && caselessPrefix(a.Val, "javascript:") || a.Key == "src" && caselessPrefix(a.Val, "data:text/html") {
891 copy(node.Attr[i:], node.Attr[i+1:])
892 node.Attr = node.Attr[:len(node.Attr)-1]
893 continue
894 }
895 if a.Key == "target" {
896 node.Attr[i].Val = "_blank"
897 haveTarget = true
898 }
899 if a.Key == "rel" && targetable[node.Data] {
900 node.Attr[i].Val = "noopener noreferrer"
901 haveRel = true
902 }
903 i++
904 }
905 // Ensure target attribute is set for elements that can have it.
906 if !haveTarget && node.Type == html.ElementNode && targetable[node.Data] {
907 node.Attr = append(node.Attr, html.Attribute{Key: "target", Val: "_blank"})
908 haveTarget = true
909 }
910 if haveTarget && !haveRel {
911 node.Attr = append(node.Attr, html.Attribute{Key: "rel", Val: "noopener noreferrer"})
912 }
913
914 parent := node
915 node = node.FirstChild
916 var haveBase bool
917 for node != nil {
918 // Set next now, we may remove cur, which clears its NextSibling.
919 cur := node
920 node = node.NextSibling
921
922 // Remove script elements.
923 if cur.Type == html.ElementNode && cur.Data == "script" {
924 parent.RemoveChild(cur)
925 continue
926 }
927 sanitizeNode(cur)
928 }
929 if parent.Type == html.ElementNode && parent.Data == "head" && !haveBase {
930 n := html.Node{Type: html.ElementNode, Data: "base", Attr: []html.Attribute{{Key: "target", Val: "_blank"}, {Key: "rel", Val: "noopener noreferrer"}}}
931 parent.AppendChild(&n)
932 }
933}
934