")
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")
}
}
func TestEveryPageCarriesTheLogoAndTheFavicon(t *testing.T) {
f := newFixture(t)
id := f.add(t, f.pub.ID, "<1@x>", "Hello", "A", "body", 1_700_000_000, "")
favicon := "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(assets.Mark))
for _, p := range []string{"/", "/archive/dev@example.org", fmt.Sprintf("/archive/dev@example.org/%d", id), "/unsubscribe/" + f.bobTok, "/nope"} {
rec := get(t, f.h, p)
body := html.UnescapeString(rec.Body.String()) // html/template writes "+" in an attribute as +
if !strings.Contains(body, `