feat(web): public archive pages and one-click unsubscribe
- Add list index, paginated archive with search, and message pages with thread links - Show only lists whose archive policy is public; hide others and foreign messages with 404 - Escape all content and obfuscate email addresses in subjects, senders and bodies - Add GET confirm page and POST one-click unsubscribe with signed tokens - Send CSP, nosniff, no-referrer and no-store headers
This commit is contained in:
parent
8253dd608e
commit
19c845caab
2 changed files with 587 additions and 0 deletions
299
internal/web/web.go
Normal file
299
internal/web/web.go
Normal file
|
|
@ -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"}}<!doctype html>
|
||||
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{.}}</title>
|
||||
<style>
|
||||
body{font:16px/1.5 system-ui,sans-serif;max-width:50rem;margin:2rem auto;padding:0 1rem;color:#222}
|
||||
a{color:#0b5cad}
|
||||
h1{font-size:1.5rem}
|
||||
table{border-collapse:collapse;width:100%}
|
||||
td,th{text-align:left;padding:.35rem .5rem;border-bottom:1px solid #ddd;vertical-align:top}
|
||||
pre{white-space:pre-wrap;word-wrap:break-word;background:#f6f6f6;padding:1rem;border-radius:4px}
|
||||
.muted{color:#666;font-size:.9rem}
|
||||
.cur{font-weight:bold}
|
||||
input[type=search]{padding:.35rem;width:16rem}
|
||||
button{padding:.5rem 1rem;font-size:1rem}
|
||||
nav a{margin-right:1rem}
|
||||
</style></head><body>{{end}}
|
||||
{{define "foot"}}</body></html>{{end}}
|
||||
|
||||
{{define "index"}}{{template "head" "Mailing list archives"}}
|
||||
<h1>Mailing list archives</h1>
|
||||
{{if .}}<ul>{{range .}}<li><a href="/archive/{{.Address}}">{{.Address}}</a> <span class="muted">{{.Title}}</span></li>{{end}}</ul>
|
||||
{{else}}<p>No public archives.</p>{{end}}
|
||||
{{template "foot"}}{{end}}
|
||||
|
||||
{{define "archive"}}{{template "head" .List.Title}}
|
||||
<nav><a href="/">All lists</a></nav>
|
||||
<h1>{{.List.Title}}</h1>
|
||||
<p class="muted">{{.List.Address}}</p>
|
||||
<form method="get" action="/archive/{{.List.Address}}"><input type="search" name="q" value="{{.Query}}" placeholder="Search"> <button>Search</button></form>
|
||||
{{if .Query}}<p class="muted">{{.ResultText}} for "{{.Query}}"</p>{{end}}
|
||||
{{if .Messages}}<table><tr><th>Subject</th><th>From</th><th>Date</th></tr>
|
||||
{{range .Messages}}<tr><td><a href="/archive/{{$.List.Address}}/{{.ID}}">{{hide .Subject}}</a></td><td>{{hide .Sender}}</td><td class="muted">{{date .Date}}</td></tr>
|
||||
{{end}}</table>
|
||||
<p>{{if .PrevURL}}<a href="{{.PrevURL}}">← Newer</a>{{end}} {{if .NextURL}}<a href="{{.NextURL}}">Older →</a>{{end}}</p>
|
||||
{{else}}<p>{{if .Query}}No messages found.{{else}}No messages yet.{{end}}</p>{{end}}
|
||||
{{template "foot"}}{{end}}
|
||||
|
||||
{{define "message"}}{{template "head" (hide .Message.Subject)}}
|
||||
<nav><a href="/">All lists</a><a href="/archive/{{.List.Address}}">{{.List.Title}}</a></nav>
|
||||
<h1>{{hide .Message.Subject}}</h1>
|
||||
<p class="muted">{{hide .Message.Sender}} · {{date .Message.Date}}</p>
|
||||
<pre>{{hide .Message.Body}}</pre>
|
||||
{{if gt (len .Thread) 1}}<h2>Thread</h2><ul>
|
||||
{{range .Thread}}<li>{{if eq .ID $.Message.ID}}<span class="cur">{{hide .Subject}}</span>{{else}}<a href="/archive/{{$.List.Address}}/{{.ID}}">{{hide .Subject}}</a>{{end}}
|
||||
<span class="muted">{{hide .Sender}}, {{date .Date}}</span></li>{{end}}</ul>{{end}}
|
||||
{{template "foot"}}{{end}}
|
||||
|
||||
{{define "unsubscribe"}}{{template "head" "Unsubscribe"}}
|
||||
<h1>Unsubscribe</h1>
|
||||
<p>Stop receiving messages from <strong>{{.Title}}</strong> ({{.Address}})?</p>
|
||||
<form method="post"><button>Unsubscribe</button></form>
|
||||
{{template "foot"}}{{end}}
|
||||
|
||||
{{define "unsubscribed"}}{{template "head" "Unsubscribed"}}
|
||||
<h1>{{if .Already}}You are already unsubscribed{{else}}You are unsubscribed{{end}}</h1>
|
||||
<p>{{if .Already}}This address no longer receives messages from the list.{{else}}You will not receive any more messages from the list.{{end}}</p>
|
||||
{{template "foot"}}{{end}}
|
||||
|
||||
{{define "notfound"}}{{template "head" "Not found"}}
|
||||
<h1>Not found</h1><p><a href="/">Back to the archives</a></p>
|
||||
{{template "foot"}}{{end}}
|
||||
`
|
||||
288
internal/web/web_test.go
Normal file
288
internal/web/web_test.go
Normal file
|
|
@ -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 <b>talk</b>")
|
||||
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, "<b>talk</b>")
|
||||
}
|
||||
|
||||
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 <i>", "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 <i>")
|
||||
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("<m%d@x>", 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(`"><script>x</script>`))
|
||||
wantStatus(t, rec, 200)
|
||||
wantNoBody(t, rec, "<script>x</script>")
|
||||
}
|
||||
|
||||
func TestMessagePage(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
root := f.add(t, f.pub.ID, "<root@x>", "Original topic", "Alice", "Write to carol@example.com <script>alert(1)</script>", 1_700_000_000, "")
|
||||
reply := f.add(t, f.pub.ID, "<re@x>", "Re: Original topic", "Bob", "I agree", 1_700_000_100, "<root@x>")
|
||||
|
||||
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", "<script>alert(1)</script>")
|
||||
|
||||
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, "<s@x>", "Top secret", "A", "classified", 1_700_000_000, "")
|
||||
pubID := f.add(t, f.pub.ID, "<p@x>", "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")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue