diff --git a/internal/web/web.go b/internal/web/web.go new file mode 100644 index 0000000..53d36dc --- /dev/null +++ b/internal/web/web.go @@ -0,0 +1,299 @@ +// Package web serves the public archive pages and the unsubscribe links. +package web + +import ( + "bytes" + "errors" + "html/template" + "log" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "codeberg.org/squidd/parsimony/internal/archive" + "codeberg.org/squidd/parsimony/internal/router" + "codeberg.org/squidd/parsimony/internal/store" +) + +const pageSize = 25 + +type server struct { + store *store.Store + router *router.Router + tmpl *template.Template +} + +// New returns the public web handler. +func New(s *store.Store, rt *router.Router) http.Handler { + srv := &server{store: s, router: rt, tmpl: template.Must(template.New("").Funcs(template.FuncMap{ + "hide": archive.Obfuscate, + "date": func(t time.Time) string { return t.UTC().Format("2006-01-02 15:04") }, + }).Parse(templates))} + + mux := http.NewServeMux() + mux.HandleFunc("GET /{$}", srv.index) + mux.HandleFunc("GET /archive/{address}", srv.archive) + mux.HandleFunc("GET /archive/{address}/{id}", srv.message) + mux.HandleFunc("GET /unsubscribe/{token}", srv.unsubscribePage) + mux.HandleFunc("POST /unsubscribe/{token}", srv.unsubscribe) + // Catch-all for GET so unknown paths get the HTML 404 page and other + // methods on known paths still answer 405. + mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { srv.notFound(w) }) + return secure(mux) +} + +// secure adds the headers every page needs. +func secure(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := w.Header() + h.Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'") + h.Set("X-Content-Type-Options", "nosniff") + h.Set("Referrer-Policy", "no-referrer") + h.Set("Content-Type", "text/html; charset=utf-8") + if strings.HasPrefix(r.URL.Path, "/unsubscribe/") { + h.Set("Cache-Control", "no-store") // the URL carries a secret + } + next.ServeHTTP(w, r) + }) +} + +func (s *server) render(w http.ResponseWriter, status int, name string, data any) { + var buf bytes.Buffer + if err := s.tmpl.ExecuteTemplate(&buf, name, data); err != nil { + log.Printf("web: render %s: %v", name, err) + http.Error(w, "internal error", http.StatusInternalServerError) + return + } + w.WriteHeader(status) + w.Write(buf.Bytes()) +} + +func (s *server) notFound(w http.ResponseWriter) { s.render(w, http.StatusNotFound, "notfound", nil) } + +func (s *server) fail(w http.ResponseWriter, err error) { + log.Printf("web: %v", err) + http.Error(w, "internal error", http.StatusInternalServerError) +} + +// publicList resolves a list address and only returns lists with a public archive. +func (s *server) publicList(address string) (store.List, bool, error) { + l, err := s.store.FindList(address) + if errors.Is(err, store.ErrNotFound) || (err == nil && l.ArchivePolicy != store.ArchivePublic) { + return store.List{}, false, nil + } + return l, err == nil, err +} + +func (s *server) index(w http.ResponseWriter, r *http.Request) { + all, err := s.store.ListLists() + if err != nil { + s.fail(w, err) + return + } + var lists []store.List + for _, l := range all { + if l.ArchivePolicy == store.ArchivePublic { + lists = append(lists, l) + } + } + s.render(w, http.StatusOK, "index", lists) +} + +type archiveView struct { + List store.List + Query string + Messages []store.Message + Total int + Page int + PrevURL string + NextURL string + ResultText string +} + +func (s *server) archive(w http.ResponseWriter, r *http.Request) { + l, ok, err := s.publicList(r.PathValue("address")) + if err != nil { + s.fail(w, err) + return + } + if !ok { + s.notFound(w) + return + } + q := strings.TrimSpace(r.URL.Query().Get("q")) + page, _ := strconv.Atoi(r.URL.Query().Get("page")) + page = max(page, 1) + + fetch := func(offset int) ([]store.Message, int, error) { + if q != "" { + return s.store.SearchMessages(l.ID, q, pageSize, offset) + } + return s.store.ListMessages(l.ID, pageSize, offset) + } + ms, total, err := fetch((page - 1) * pageSize) + if err == nil { + if pages := (total + pageSize - 1) / pageSize; pages > 0 && page > pages { + page = pages + ms, total, err = fetch((page - 1) * pageSize) + } + } + if err != nil { + s.fail(w, err) + return + } + + v := archiveView{List: l, Query: q, Messages: ms, Total: total, Page: page} + link := func(p int) string { + vals := url.Values{} + if q != "" { + vals.Set("q", q) + } + if p > 1 { + vals.Set("page", strconv.Itoa(p)) + } + u := "/archive/" + l.Address() + if len(vals) > 0 { + u += "?" + vals.Encode() + } + return u + } + if page > 1 { + v.PrevURL = link(page - 1) + } + if page*pageSize < total { + v.NextURL = link(page + 1) + } + if q != "" { + v.ResultText = strconv.Itoa(total) + " results" + if total == 1 { + v.ResultText = "1 result" + } + } + s.render(w, http.StatusOK, "archive", v) +} + +type messageView struct { + List store.List + Message store.Message + Thread []store.Message +} + +func (s *server) message(w http.ResponseWriter, r *http.Request) { + l, ok, err := s.publicList(r.PathValue("address")) + if err != nil { + s.fail(w, err) + return + } + id, perr := strconv.ParseInt(r.PathValue("id"), 10, 64) + if !ok || perr != nil || id <= 0 { + s.notFound(w) + return + } + m, err := s.store.GetMessage(id) + if errors.Is(err, store.ErrNotFound) || (err == nil && m.ListID != l.ID) { + s.notFound(w) // also hides messages that belong to other lists + return + } + if err != nil { + s.fail(w, err) + return + } + thread, err := s.store.ThreadMessages(l.ID, m.ThreadID) + if err != nil { + s.fail(w, err) + return + } + s.render(w, http.StatusOK, "message", messageView{List: l, Message: m, Thread: thread}) +} + +func (s *server) unsubscribePage(w http.ResponseWriter, r *http.Request) { + l, err := s.router.CheckUnsubscribe(r.PathValue("token")) + switch { + case errors.Is(err, router.ErrBadToken): + s.notFound(w) + case errors.Is(err, router.ErrGone): + s.render(w, http.StatusOK, "unsubscribed", map[string]any{"Already": true}) + case err != nil: + s.fail(w, err) + default: + s.render(w, http.StatusOK, "unsubscribe", l) + } +} + +func (s *server) unsubscribe(w http.ResponseWriter, r *http.Request) { + err := s.router.Unsubscribe(r.PathValue("token")) + switch { + case errors.Is(err, router.ErrBadToken): + s.notFound(w) + case err != nil: + s.fail(w, err) + default: + s.render(w, http.StatusOK, "unsubscribed", map[string]any{"Already": false}) + } +} + +const templates = ` +{{define "head"}} + +{{.}} +{{end}} +{{define "foot"}}{{end}} + +{{define "index"}}{{template "head" "Mailing list archives"}} +

Mailing list archives

+{{if .}} +{{else}}

No public archives.

{{end}} +{{template "foot"}}{{end}} + +{{define "archive"}}{{template "head" .List.Title}} + +

{{.List.Title}}

+

{{.List.Address}}

+
+{{if .Query}}

{{.ResultText}} for "{{.Query}}"

{{end}} +{{if .Messages}} +{{range .Messages}} +{{end}}
SubjectFromDate
{{hide .Subject}}{{hide .Sender}}{{date .Date}}
+

{{if .PrevURL}}← Newer{{end}} {{if .NextURL}}Older →{{end}}

+{{else}}

{{if .Query}}No messages found.{{else}}No messages yet.{{end}}

{{end}} +{{template "foot"}}{{end}} + +{{define "message"}}{{template "head" (hide .Message.Subject)}} + +

{{hide .Message.Subject}}

+

{{hide .Message.Sender}} · {{date .Message.Date}}

+
{{hide .Message.Body}}
+{{if gt (len .Thread) 1}}

Thread

{{end}} +{{template "foot"}}{{end}} + +{{define "unsubscribe"}}{{template "head" "Unsubscribe"}} +

Unsubscribe

+

Stop receiving messages from {{.Title}} ({{.Address}})?

+
+{{template "foot"}}{{end}} + +{{define "unsubscribed"}}{{template "head" "Unsubscribed"}} +

{{if .Already}}You are already unsubscribed{{else}}You are unsubscribed{{end}}

+

{{if .Already}}This address no longer receives messages from the list.{{else}}You will not receive any more messages from the list.{{end}}

+{{template "foot"}}{{end}} + +{{define "notfound"}}{{template "head" "Not found"}} +

Not found

Back to the archives

+{{template "foot"}}{{end}} +` diff --git a/internal/web/web_test.go b/internal/web/web_test.go new file mode 100644 index 0000000..ba41751 --- /dev/null +++ b/internal/web/web_test.go @@ -0,0 +1,288 @@ +package web + +import ( + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "codeberg.org/squidd/parsimony/internal/router" + "codeberg.org/squidd/parsimony/internal/store" +) + +type fixture struct { + h http.Handler + s *store.Store + rt *router.Router + pub store.List + alice int64 + bobTok string +} + +func newFixture(t *testing.T) *fixture { + t.Helper() + s, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { s.Close() }) + pub, _ := s.CreateList("dev", "example.org", "Dev talk") + s.SetPolicies(pub.ID, store.PostMembers, store.JoinOpen, store.ArchivePublic) + priv, _ := s.CreateList("secret", "example.org", "Secret") + s.SetPolicies(priv.ID, store.PostMembers, store.JoinOpen, store.ArchivePrivate) + off, _ := s.CreateList("off", "example.org", "Off") + s.SetPolicies(off.ID, store.PostMembers, store.JoinOpen, store.ArchiveOff) + + s.AddMember(pub.ID, "alice@example.com", "Alice") + s.AddMember(pub.ID, "bob@example.com", "Bob") + ms, _ := s.Members(pub.ID) + rt := router.New(s) + return &fixture{h: New(s, rt), s: s, rt: rt, pub: pub, alice: ms[0].ID, bobTok: rt.UnsubscribeToken(ms[1].ID)} +} + +func (f *fixture) add(t *testing.T, listID int64, id, subject, sender, body string, at int64, inReplyTo string) int64 { + t.Helper() + n, _, err := f.s.ArchiveMessage(listID, store.Message{ + MessageID: id, InReplyTo: inReplyTo, Subject: subject, Sender: sender, Body: body, Date: time.Unix(at, 0).UTC(), + }) + if err != nil { + t.Fatal(err) + } + return n +} + +func get(t *testing.T, h http.Handler, path string) *httptest.ResponseRecorder { + t.Helper() + return request(t, h, "GET", path) +} + +func request(t *testing.T, h http.Handler, method, path string) *httptest.ResponseRecorder { + t.Helper() + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(method, path, nil)) + return rec +} + +func wantStatus(t *testing.T, rec *httptest.ResponseRecorder, code int) { + t.Helper() + if rec.Code != code { + t.Fatalf("status = %d, want %d; body: %.300s", rec.Code, code, rec.Body.String()) + } +} + +func wantBody(t *testing.T, rec *httptest.ResponseRecorder, parts ...string) { + t.Helper() + for _, p := range parts { + if !strings.Contains(rec.Body.String(), p) { + t.Errorf("body lacks %q:\n%.800s", p, rec.Body.String()) + } + } +} + +func wantNoBody(t *testing.T, rec *httptest.ResponseRecorder, parts ...string) { + t.Helper() + for _, p := range parts { + if strings.Contains(rec.Body.String(), p) { + t.Errorf("body must not contain %q", p) + } + } +} + +func TestIndexListsOnlyPublicLists(t *testing.T) { + f := newFixture(t) + rec := get(t, f.h, "/") + wantStatus(t, rec, 200) + wantBody(t, rec, "dev@example.org", `href="/archive/dev@example.org"`) + wantNoBody(t, rec, "secret@example.org", "off@example.org") +} + +func TestIndexEscapesTitles(t *testing.T) { + f := newFixture(t) + rec := get(t, f.h, "/") + wantBody(t, rec, "Dev <b>talk</b>") + wantNoBody(t, rec, "talk") +} + +func TestIndexWithoutPublicLists(t *testing.T) { + s, _ := store.Open(":memory:") + defer s.Close() + rec := get(t, New(s, router.New(s)), "/") + wantStatus(t, rec, 200) + wantBody(t, rec, "No public archives") +} + +func TestUnknownPathIs404(t *testing.T) { + f := newFixture(t) + wantStatus(t, get(t, f.h, "/nope"), 404) +} + +func TestNonPublicOrUnknownArchivesAre404(t *testing.T) { + f := newFixture(t) + for _, p := range []string{"/archive/secret@example.org", "/archive/off@example.org", "/archive/ghost@example.org", + "/archive/not-an-address", "/archive/secret@example.org?q=x"} { + wantStatus(t, get(t, f.h, p), 404) + } +} + +func TestArchivePageShowsMessagesNewestFirst(t *testing.T) { + f := newFixture(t) + f.add(t, f.pub.ID, "<1@x>", "Old topic", "Alice", "a", 1_700_000_000, "") + f.add(t, f.pub.ID, "<2@x>", "New topic", "Bob ", "b", 1_700_100_000, "") + rec := get(t, f.h, "/archive/dev@example.org") + wantStatus(t, rec, 200) + wantBody(t, rec, "Old topic", "New topic", "Bob <i>", "2023-11-14") + wantNoBody(t, rec, "Bob ") + if strings.Index(rec.Body.String(), "New topic") > strings.Index(rec.Body.String(), "Old topic") { + t.Fatal("newest message must come first") + } +} + +func TestArchivePaging(t *testing.T) { + f := newFixture(t) + for i := 0; i < pageSize+3; i++ { + f.add(t, f.pub.ID, fmt.Sprintf("", i), fmt.Sprintf("Subject number %d", i), "A", "b", 1_700_000_000+int64(i), "") + } + p1 := get(t, f.h, "/archive/dev@example.org") + wantBody(t, p1, fmt.Sprintf("Subject number %d", pageSize+2), "page=2") + wantNoBody(t, p1, "Subject number 0<", "page=0") + p2 := get(t, f.h, "/archive/dev@example.org?page=2") + wantStatus(t, p2, 200) + wantBody(t, p2, "Subject number 0<") + wantNoBody(t, p2, "page=3") + for _, bad := range []string{"?page=abc", "?page=0", "?page=-4", "?page=999"} { + wantStatus(t, get(t, f.h, "/archive/dev@example.org"+bad), 200) + } +} + +func TestArchiveSearch(t *testing.T) { + f := newFixture(t) + f.add(t, f.pub.ID, "<1@x>", "Deploy", "A", "kubernetes rollout", 1_700_000_000, "") + f.add(t, f.pub.ID, "<2@x>", "Lunch", "B", "pizza", 1_700_000_001, "") + rec := get(t, f.h, "/archive/dev@example.org?q=kubernetes") + wantStatus(t, rec, 200) + wantBody(t, rec, "Deploy", "1 result") + wantNoBody(t, rec, "Lunch") + rec = get(t, f.h, "/archive/dev@example.org?q=zzzz") + wantBody(t, rec, "No messages found") + // The query is echoed back escaped and survives paging links. + rec = get(t, f.h, "/archive/dev@example.org?q="+url.QueryEscape(`">`)) + wantStatus(t, rec, 200) + wantNoBody(t, rec, "") +} + +func TestMessagePage(t *testing.T) { + f := newFixture(t) + root := f.add(t, f.pub.ID, "", "Original topic", "Alice", "Write to carol@example.com ", 1_700_000_000, "") + reply := f.add(t, f.pub.ID, "", "Re: Original topic", "Bob", "I agree", 1_700_000_100, "") + + rec := get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", root)) + wantStatus(t, rec, 200) + wantBody(t, rec, "Original topic", "Alice", "carol@…", "<script>alert(1)</script>", + fmt.Sprintf(`href="/archive/dev@example.org/%d"`, reply), "Re: Original topic") + wantNoBody(t, rec, "carol@example.com", "") + + rec = get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", reply)) + wantStatus(t, rec, 200) + wantBody(t, rec, "I agree", fmt.Sprintf(`href="/archive/dev@example.org/%d"`, root)) +} + +func TestMessagePageHidesOtherListsAndPrivateOnes(t *testing.T) { + f := newFixture(t) + priv, _ := f.s.FindList("secret@example.org") + secretID := f.add(t, priv.ID, "", "Top secret", "A", "classified", 1_700_000_000, "") + pubID := f.add(t, f.pub.ID, "", "Public", "A", "hello", 1_700_000_000, "") + + wantStatus(t, get(t, f.h, fmt.Sprintf("/archive/secret@example.org/%d", secretID)), 404) + // A public list URL must not reveal a message that belongs to another list. + wantStatus(t, get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", secretID)), 404) + wantStatus(t, get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", pubID)), 200) + for _, p := range []string{"/archive/dev@example.org/abc", "/archive/dev@example.org/0", "/archive/dev@example.org/999", "/archive/dev@example.org/-1"} { + wantStatus(t, get(t, f.h, p), 404) + } +} + +func TestPrivacyOfOriginalSender(t *testing.T) { + f := newFixture(t) + id := f.add(t, f.pub.ID, "<1@x>", "Contact bob@secret.example", "bob", "ping", 1_700_000_000, "") + rec := get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", id)) + wantNoBody(t, rec, "bob@secret.example") + wantBody(t, rec, "bob@…") + rec = get(t, f.h, "/archive/dev@example.org") + wantNoBody(t, rec, "bob@secret.example") +} + +func TestSecurityHeaders(t *testing.T) { + f := newFixture(t) + for _, p := range []string{"/", "/archive/dev@example.org", "/nope"} { + h := get(t, f.h, p).Header() + if h.Get("X-Content-Type-Options") != "nosniff" || h.Get("Referrer-Policy") != "no-referrer" || + !strings.Contains(h.Get("Content-Security-Policy"), "default-src 'none'") || + !strings.HasPrefix(h.Get("Content-Type"), "text/html") { + t.Errorf("%s: headers %v", p, h) + } + } +} + +func TestOnlyGetOnArchive(t *testing.T) { + f := newFixture(t) + wantStatus(t, request(t, f.h, "POST", "/archive/dev@example.org"), 405) + wantStatus(t, request(t, f.h, "DELETE", "/"), 405) +} + +func TestUnsubscribeConfirmPageDoesNotUnsubscribe(t *testing.T) { + f := newFixture(t) + rec := get(t, f.h, "/unsubscribe/"+f.bobTok) + wantStatus(t, rec, 200) + wantBody(t, rec, `method="post"`, "Dev <b>talk</b>") + if ok, _ := f.s.IsMember(f.pub.ID, "bob@example.com"); !ok { + t.Fatal("a GET (link prefetch) must never unsubscribe") + } + h := rec.Header() + if h.Get("Cache-Control") != "no-store" || h.Get("Referrer-Policy") != "no-referrer" { + t.Errorf("token page headers: %v", h) + } +} + +func TestUnsubscribePost(t *testing.T) { + f := newFixture(t) + rec := request(t, f.h, "POST", "/unsubscribe/"+f.bobTok) + wantStatus(t, rec, 200) + wantBody(t, rec, "unsubscribed") + if ok, _ := f.s.IsMember(f.pub.ID, "bob@example.com"); ok { + t.Fatal("bob must be unsubscribed") + } + if ok, _ := f.s.IsMember(f.pub.ID, "alice@example.com"); !ok { + t.Fatal("alice must stay") + } + // Repeated one-click requests and a later visit are harmless. + wantStatus(t, request(t, f.h, "POST", "/unsubscribe/"+f.bobTok), 200) + rec = get(t, f.h, "/unsubscribe/"+f.bobTok) + wantStatus(t, rec, 200) + wantBody(t, rec, "already unsubscribed") +} + +func TestUnsubscribeRejectsBadTokens(t *testing.T) { + f := newFixture(t) + for _, tok := range []string{"x", "1-000000000000", "abc-def", f.bobTok + "0", "0-" + strings.Repeat("0", 12)} { + wantStatus(t, get(t, f.h, "/unsubscribe/"+tok), 404) + wantStatus(t, request(t, f.h, "POST", "/unsubscribe/"+tok), 404) + } + if n, _ := f.s.Members(f.pub.ID); len(n) != 2 { + t.Fatal("bad tokens must not remove anyone") + } +} + +func TestUnsubscribeOneClickBodyIsAccepted(t *testing.T) { + f := newFixture(t) + req := httptest.NewRequest("POST", "/unsubscribe/"+f.bobTok, strings.NewReader("List-Unsubscribe=One-Click")) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + rec := httptest.NewRecorder() + f.h.ServeHTTP(rec, req) + wantStatus(t, rec, 200) + if ok, _ := f.s.IsMember(f.pub.ID, "bob@example.com"); ok { + t.Fatal("bob must be unsubscribed") + } +}