parsimony/internal/web/web_test.go
tgiachi 96a1243c2f fix(web): refuse local addresses on the subscribe form
root@localhost, a@b and a@[127.0.0.1] passed mail.ParseAddress, so the
public form could make the daemon mail itself or its own network.

The domain of a typed address must now contain a dot, not be a domain
literal, be made of non-empty labels of letters, digits and hyphens, and
not be localhost or end in .localhost, .local, .internal or .invalid.
.test and .example stay accepted (the end-to-end tests use them).
2026-11-10 15:11:43 +01:00

877 lines
34 KiB
Go

package web
import (
"context"
"encoding/base64"
"fmt"
"html"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
"codeberg.org/squidd/parsimony/assets"
"codeberg.org/squidd/parsimony/internal/outbound"
"codeberg.org/squidd/parsimony/internal/queue"
"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
out *outbound.Fake
clock time.Time // what the handler thinks the time is; tests move it
}
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)
closed, _ := s.CreateList("closed", "example.org", "Closed")
s.SetPolicies(closed.ID, store.PostMembers, store.JoinClosed, store.ArchivePublic)
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)
f := &fixture{s: s, rt: rt, pub: pub, alice: ms[0].ID, bobTok: rt.UnsubscribeToken(ms[1].ID),
out: &outbound.Fake{}, clock: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)}
f.h = newHandler(s, rt, func() time.Time { return f.clock }, nil)
return f
}
// trust rebuilds the handler so that it believes X-Forwarded-For from the given proxies.
func (f *fixture) trust(t *testing.T, cidrs ...string) {
t.Helper()
var nets []*net.IPNet
for _, c := range cidrs {
_, n, err := net.ParseCIDR(c)
if err != nil {
t.Fatal(err)
}
nets = append(nets, n)
}
f.h = newHandler(f.s, f.rt, func() time.Time { return f.clock }, nets)
}
// flush sends what is queued, as the background worker would, and returns everything sent so far.
func (f *fixture) flush(t *testing.T) []outbound.Sent {
t.Helper()
w := &queue.Worker{Store: f.s, Sender: f.out}
for {
st, err := w.RunOnce(context.Background())
if err != nil {
t.Fatal(err)
}
if st == (queue.Stats{}) {
return f.out.Sent
}
}
}
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 &lt;b&gt;talk&lt;/b&gt;")
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), nil), "/")
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 &lt;i&gt;", "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")
// A page past the end shows the last page instead of an empty one.
far := get(t, f.h, "/archive/dev@example.org?page=999")
wantBody(t, far, "Subject number 0<")
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@…", "&lt;script&gt;alert(1)&lt;/script&gt;",
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 &lt;b&gt;talk&lt;/b&gt;")
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 &#43;
if !strings.Contains(body, `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Parsimony">`) {
t.Errorf("%s: no inline logo", p)
}
if !strings.Contains(body, `<link rel="icon" type="image/svg+xml" href="`+favicon+`">`) {
t.Errorf("%s: no favicon (a data: URI, because the CSP allows nothing else)", p)
}
if !strings.Contains(body, `<a class="brand" href="/" aria-label="All archives">`) {
t.Errorf("%s: the logo must lead back to the index", p)
}
}
}
func TestTheCSPAllowsOnlyDataImages(t *testing.T) {
f := newFixture(t)
csp := get(t, f.h, "/").Header().Get("Content-Security-Policy")
if !strings.Contains(csp, "img-src data:") || !strings.Contains(csp, "default-src 'none'") {
t.Fatalf("csp = %q", csp)
}
for _, loose := range []string{"*", "http:", "https:", "'unsafe-eval'", "script-src"} {
if strings.Contains(csp, loose) {
t.Errorf("the CSP must stay strict, it contains %q: %s", loose, csp)
}
}
}
// Subscribe form.
const neutralText = "If this address can subscribe, a confirmation message is on its way. Open it and reply to confirm."
// subscribe posts a form from the TCP peer ip.
func subscribe(t *testing.T, f *fixture, list, ip string, form url.Values) *httptest.ResponseRecorder {
t.Helper()
return subscribeRaw(f.h, list, ip, form.Encode(), nil)
}
func subscribeRaw(h http.Handler, list, ip, body string, hdr map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest("POST", "/subscribe/"+list, strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for k, v := range hdr {
req.Header.Set(k, v)
}
req.RemoteAddr = net.JoinHostPort(ip, "4321")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func email(addr string) url.Values { return url.Values{"email": {addr}} }
func TestSubscribeFormIsShownForPublicOpenLists(t *testing.T) {
f := newFixture(t)
rec := get(t, f.h, "/subscribe/dev@example.org")
wantStatus(t, rec, 200)
wantBody(t, rec, `method="post"`, `action="/subscribe/dev@example.org"`, `name="email"`, `type="email"`,
`name="website"`, "Dev &lt;b&gt;talk&lt;/b&gt;", "<button>Subscribe</button>")
wantNoBody(t, rec, "<b>talk</b>")
if got := rec.Header().Get("Cache-Control"); got != "no-store" {
t.Errorf("Cache-Control = %q, want no-store", got)
}
if csp := rec.Header().Get("Content-Security-Policy"); csp != "default-src 'none'; img-src data:; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" {
t.Errorf("the CSP must not change: %q", csp)
}
// The honeypot is hidden from people and from assistive technology, and nothing fills it in.
wantBody(t, rec, `class="hp"`, `tabindex="-1"`, `autocomplete="off"`, `aria-hidden="true"`)
}
func TestSubscribeIs404ForEverythingElseAndLooksLikeAMissingList(t *testing.T) {
f := newFixture(t)
missing := get(t, f.h, "/subscribe/ghost@example.org")
wantStatus(t, missing, 404)
for _, p := range []string{
"/subscribe/secret@example.org", // private archive
"/subscribe/off@example.org", // no archive
"/subscribe/closed@example.org", // public archive, nobody may join
"/subscribe/not-an-address",
"/subscribe/..%2Fdev@example.org",
"/subscribe/dev@example.org%2F..%2Fsecret@example.org",
"/subscribe/dev@example.org%00",
"/subscribe/dev@example.org/extra",
"/subscribe/",
"/subscribe",
} {
rec := get(t, f.h, p)
wantStatus(t, rec, 404)
if rec.Body.String() != missing.Body.String() {
t.Errorf("%s: the 404 page must be the same as for a missing list", p)
}
rec = request(t, f.h, "POST", p)
if rec.Code != 404 && rec.Code != 405 {
t.Errorf("POST %s = %d, want 404 or 405", p, rec.Code)
}
}
}
func TestSubscribeQueuesOneConfirmationMail(t *testing.T) {
f := newFixture(t)
rec := subscribe(t, f, "dev@example.org", "192.0.2.1", email("carol@x.com"))
wantStatus(t, rec, 200)
wantBody(t, rec, neutralText)
wantNoBody(t, rec, "carol")
if got := rec.Header().Get("Cache-Control"); got != "no-store" {
t.Errorf("Cache-Control = %q, want no-store", got)
}
sent := f.flush(t)
if len(sent) != 1 || len(sent[0].To) != 1 || sent[0].To[0] != "carol@x.com" {
t.Fatalf("want one confirmation mail to carol, got %+v", sent)
}
if !strings.Contains(string(sent[0].Data), "Reply-To: dev+confirm-") {
t.Fatalf("not a confirmation mail: %s", sent[0].Data)
}
if ok, _ := f.s.IsMember(f.pub.ID, "carol@x.com"); ok {
t.Fatal("not a member before confirming")
}
// Exactly one pending confirmation: the store refuses a second one within the hour.
subscribe(t, f, "dev@example.org", "192.0.2.1", email("carol@x.com"))
if sent := f.flush(t); len(sent) != 1 {
t.Fatalf("a repeat must not queue another mail, sent %d", len(sent))
}
}
func TestSubscribeAnswersTheSameWhateverHappens(t *testing.T) {
f := newFixture(t)
f.s.AddMember(f.pub.ID, "member@x.com", "")
fresh := subscribe(t, f, "dev@example.org", "192.0.2.1", email("new@x.com"))
member := subscribe(t, f, "dev@example.org", "192.0.2.2", email("member@x.com"))
limited := subscribe(t, f, "dev@example.org", "192.0.2.3", email("new@x.com")) // the store rate limits it
if fresh.Code != 200 || member.Code != 200 || limited.Code != 200 {
t.Fatalf("statuses %d %d %d", fresh.Code, member.Code, limited.Code)
}
for name, rec := range map[string]*httptest.ResponseRecorder{"member": member, "rate limited": limited} {
if rec.Body.String() != fresh.Body.String() {
t.Errorf("%s: the page differs from the page for a new address", name)
}
}
wantBody(t, fresh, neutralText)
if sent := f.flush(t); len(sent) != 1 || sent[0].To[0] != "new@x.com" {
t.Fatalf("only the new address gets mail: %+v", sent)
}
}
func TestSubscribeAcceptsTheAddressInAnyCase(t *testing.T) {
f := newFixture(t)
f.s.AddMember(f.pub.ID, "member@x.com", "")
subscribe(t, f, "dev@example.org", "192.0.2.1", email(" MEMBER@X.com "))
if sent := f.flush(t); len(sent) != 0 {
t.Fatalf("a member gets no mail whatever the case: %+v", sent)
}
}
func TestSubscribeHoneypotDoesNothing(t *testing.T) {
f := newFixture(t)
good := subscribe(t, f, "dev@example.org", "192.0.2.1", email("ok@x.com"))
f.flush(t)
f.out.Sent = nil
bot := subscribe(t, f, "dev@example.org", "192.0.2.2", url.Values{"email": {"bot@x.com"}, "website": {"http://spam"}})
wantStatus(t, bot, 200)
if bot.Body.String() != good.Body.String() {
t.Error("a bot must see the page a person sees")
}
// Even a bad address gets the success page when the honeypot is filled.
bot = subscribe(t, f, "dev@example.org", "192.0.2.2", url.Values{"email": {"nonsense"}, "website": {"x"}})
wantStatus(t, bot, 200)
wantBody(t, bot, neutralText)
if sent := f.flush(t); len(sent) != 0 {
t.Fatalf("the honeypot must queue nothing: %+v", sent)
}
// No pending confirmation was created either: a real request for the same address still works.
subscribe(t, f, "dev@example.org", "192.0.2.3", email("bot@x.com"))
if sent := f.flush(t); len(sent) != 1 {
t.Fatalf("a real request after the honeypot must send its mail, sent %d", len(sent))
}
}
func TestSubscribeRejectsBadAddressesAndEscapesWhatWasTyped(t *testing.T) {
f := newFixture(t)
long := strings.Repeat("a", 250) + "@x.com" // 256 characters
for i, c := range []struct{ name, in string }{
{"empty", ""},
{"blank", " "},
{"no at sign", "carol"},
{"display name", "Carol <carol@x.com>"},
{"angle brackets only", "<carol@x.com>"},
{"two addresses", "a@x.com, b@x.com"},
{"two addresses with a space", "a@x.com b@x.com"},
{"markup", `"><script>alert(1)</script>`},
{"markup in an address", `<script>alert(1)</script>@x.com`},
{"quoted local part", `"a b"@x.com`},
{"control character", "a\x01@x.com"},
{"line break", "a@x.com\r\nBcc: v@x.com"},
{"non-ASCII", "cärol@x.com"},
{"too long", long},
{"no dot in the domain", "a@b"},
{"localhost", "root@localhost"},
{"localhost in capitals", "root@LOCALHOST"},
{"under localhost", "a@app.localhost"},
{"mDNS", "a@printer.local"},
{"internal", "a@svc.internal"},
{"invalid", "a@x.invalid"},
{"domain literal", "a@[127.0.0.1]"},
{"IPv6 domain literal", "a@[IPv6:::1]"},
{"trailing dot", "a@x.com."},
{"underscore in the domain", "a@x_y.com"},
{"symbol in the domain", "a@x!y.com"},
{"plus in the domain", "a@x+y.com"},
} {
rec := subscribe(t, f, "dev@example.org", fmt.Sprintf("192.0.2.%d", i+1), email(c.in)) // one IP each: the limit is per IP
if rec.Code != 400 {
t.Errorf("%s: status = %d, want 400", c.name, rec.Code)
}
wantBody(t, rec, `name="email"`, "valid email address")
wantNoBody(t, rec, neutralText, "<script>", "\x01")
}
if sent := f.flush(t); len(sent) != 0 {
t.Fatalf("no mail for a bad address: %+v", sent)
}
// The typed value comes back, escaped.
rec := subscribe(t, f, "dev@example.org", "192.0.2.100", email(`<script>alert(1)</script>`))
wantBody(t, rec, `value="&lt;script&gt;alert(1)&lt;/script&gt;"`)
// A 254-character address is the longest accepted.
ok := strings.Repeat("a", 64) + "@" + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 57) + ".com"
if len(ok) != 254 {
t.Fatalf("setup: %d", len(ok))
}
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.101", email(ok)), neutralText)
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.101", email("a"+ok)), "valid email address")
}
func TestSubscribeAcceptsOrdinaryDomains(t *testing.T) {
f := newFixture(t)
for i, addr := range []string{"a@members.test", "a@example.com", "a@mail.example", "a@my-host.Example.ORG", "a@x1.co", "a@localhost.example.com"} {
rec := subscribe(t, f, "dev@example.org", fmt.Sprintf("192.0.2.%d", i+1), email(addr))
wantStatus(t, rec, 200)
wantBody(t, rec, neutralText)
}
}
func TestSubscribeBodyIsLimitedTo4KiB(t *testing.T) {
f := newFixture(t)
body := func(n int) string {
head := "email=carol%40x.com&pad="
return head + strings.Repeat("x", n-len(head))
}
rec := subscribeRaw(f.h, "dev@example.org", "192.0.2.1", body(4096), nil)
wantStatus(t, rec, 200)
wantBody(t, rec, neutralText)
rec = subscribeRaw(f.h, "dev@example.org", "192.0.2.1", body(4097), nil)
wantStatus(t, rec, http.StatusRequestEntityTooLarge)
wantNoBody(t, rec, neutralText)
rec = subscribeRaw(f.h, "dev@example.org", "192.0.2.1", body(1<<20), nil)
wantStatus(t, rec, http.StatusRequestEntityTooLarge)
if sent := f.flush(t); len(sent) != 1 {
t.Fatalf("only the small request sends mail, sent %d", len(sent))
}
}
func TestSubscribeNeverMailsTheAddressForAClosedOrHiddenList(t *testing.T) {
f := newFixture(t)
for _, l := range []string{"closed@example.org", "secret@example.org", "off@example.org", "ghost@example.org"} {
rec := subscribe(t, f, l, "192.0.2.1", email("victim@x.com"))
wantStatus(t, rec, 404)
wantNoBody(t, rec, neutralText)
}
if sent := f.flush(t); len(sent) != 0 {
t.Fatalf("no mail for these lists: %+v", sent)
}
// Even after the list opens, nothing was left pending.
cl, _ := f.s.FindList("closed@example.org")
f.s.SetPolicies(cl.ID, store.PostMembers, store.JoinOpen, store.ArchivePublic)
subscribe(t, f, "closed@example.org", "192.0.2.1", email("victim@x.com"))
if sent := f.flush(t); len(sent) != 1 {
t.Fatalf("one mail once open, sent %d", len(sent))
}
}
func TestSubscribeIsLimitedPerIPAndTheLimitExpires(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email(fmt.Sprintf("u%d@x.com", i))), 200)
}
f.clock = f.clock.Add(10 * time.Minute)
rec := subscribe(t, f, "dev@example.org", "198.51.100.7", email("u10@x.com"))
wantStatus(t, rec, 429)
if got := rec.Header().Get("Retry-After"); got != strconv.Itoa(50*60) {
t.Errorf("Retry-After = %q, want %d", got, 50*60)
}
wantNoBody(t, rec, neutralText)
// A refused request is not counted again, and queued nothing.
if sent := f.flush(t); len(sent) != 10 {
t.Fatalf("sent %d, want 10", len(sent))
}
// Another address is not affected.
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.8", email("other@x.com")), 200)
// The window is an hour from the first request.
f.clock = f.clock.Add(49*time.Minute + 59*time.Second)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("u11@x.com")), 429)
f.clock = f.clock.Add(2 * time.Second)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("u11@x.com")), 200)
}
func TestSubscribeLimitCountsEveryPostOfTheAddress(t *testing.T) {
f := newFixture(t)
// Honeypot hits, bad input and unknown lists all count, so they cannot be used to probe for free.
forms := []url.Values{{"email": {"x"}}, {"email": {"a@x.com"}, "website": {"x"}}, email("b@x.com")}
for i := 0; i < 10; i++ {
subscribe(t, f, []string{"dev@example.org", "ghost@example.org"}[i%2], "198.51.100.9", forms[i%3])
}
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.9", email("c@x.com")), 429)
}
func TestSubscribeLimitIgnoresForwardedFor(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
hdr := map[string]string{"X-Forwarded-For": fmt.Sprintf("203.0.113.%d", i), "X-Real-IP": fmt.Sprintf("203.0.113.%d", i)}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "198.51.100.7", email("a@x.com").Encode(), hdr), 200)
}
hdr := map[string]string{"X-Forwarded-For": "203.0.113.200", "X-Real-IP": "203.0.113.200", "Forwarded": "for=203.0.113.201"}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "198.51.100.7", email("a@x.com").Encode(), hdr), 429)
// And naming the blocked address in the header does not hurt somebody else.
hdr = map[string]string{"X-Forwarded-For": "198.51.100.7"}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "198.51.100.99", email("a@x.com").Encode(), hdr), 200)
}
func TestSubscribeLimiterForgetsIdleAddresses(t *testing.T) {
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
l := newSubscribeLimiter(func() time.Time { return now })
for i := 0; i < 50; i++ {
l.allow(fmt.Sprintf("192.0.2.%d", i))
}
if l.size() != 50 {
t.Fatalf("size = %d, want 50", l.size())
}
now = now.Add(30 * time.Minute)
l.allow("198.51.100.1")
if l.size() != 51 {
t.Fatalf("addresses inside the window are kept, size = %d", l.size())
}
now = now.Add(31 * time.Minute) // the first 50 are now older than an hour
l.allow("198.51.100.2")
if l.size() != 2 {
t.Fatalf("idle addresses must be forgotten, size = %d, want 2", l.size())
}
}
func TestSubscribeFailsWhenTheDatabaseDoes(t *testing.T) {
f := newFixture(t)
f.s.Close()
wantStatus(t, subscribe(t, f, "dev@example.org", "192.0.2.1", email("a@x.com")), 500)
wantStatus(t, get(t, f.h, "/subscribe/dev@example.org"), 500)
}
func TestSubscribeLinkOnlyOnTheArchivePageOfAQualifyingList(t *testing.T) {
f := newFixture(t)
link := `href="/subscribe/dev@example.org"`
rec := get(t, f.h, "/archive/dev@example.org")
wantBody(t, rec, link, ">Subscribe</a>")
// A public archive whose list is closed offers none.
wantNoBody(t, get(t, f.h, "/archive/closed@example.org"), "/subscribe/")
// Nowhere else.
id := f.add(t, f.pub.ID, "<1@x>", "Topic", "Alice", "text", 1_700_000_000, "")
wantNoBody(t, get(t, f.h, "/"), "/subscribe/")
wantNoBody(t, get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", id)), "/subscribe/")
wantNoBody(t, get(t, f.h, "/unsubscribe/"+f.bobTok), "/subscribe/")
}
func TestSubscribeTrimsTheTypedAddress(t *testing.T) {
f := newFixture(t)
f.s.AddMember(f.pub.ID, "member@x.com", "")
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.1", email(" carol@x.com\t")), neutralText)
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.2", email(" MEMBER@x.com ")), neutralText)
sent := f.flush(t)
if len(sent) != 1 || sent[0].To[0] != "carol@x.com" {
t.Fatalf("one mail, to the trimmed address: %+v", sent)
}
}
func TestSubscribeRetryAfterRoundsUp(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
}
f.clock = f.clock.Add(10*time.Minute + 500*time.Millisecond)
rec := subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
wantStatus(t, rec, 429)
if got := rec.Header().Get("Retry-After"); got != "3000" { // 2999.5 s left
t.Errorf("Retry-After = %q, want 3000", got)
}
}
// The hour is over when an hour has passed, not one tick later.
func TestSubscribeWindowEndsAfterExactlyAnHour(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
}
f.clock = f.clock.Add(time.Hour - time.Nanosecond)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com")), 429)
f.clock = f.clock.Add(time.Nanosecond)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com")), 200)
}
func TestSubscribeLimiterSweepsAnAddressWhoseHourIsOver(t *testing.T) {
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
l := newSubscribeLimiter(func() time.Time { return now })
l.allow("192.0.2.1")
now = now.Add(time.Hour) // exactly an hour since the first request
l.allow("192.0.2.2")
if l.size() != 1 {
t.Fatalf("size = %d, want 1 (the first address is forgotten)", l.size())
}
}
func TestSubscribeBehindATrustedProxyLimitsEachClient(t *testing.T) {
f := newFixture(t)
f.trust(t, "10.0.0.0/8")
post := func(xff string) int {
return subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), map[string]string{"X-Forwarded-For": xff}).Code
}
for i := 0; i < 10; i++ {
if c := post("203.0.113.1"); c != 200 {
t.Fatalf("request %d: %d", i+1, c)
}
}
if c := post("203.0.113.1"); c != 429 {
t.Fatalf("the 11th request of one client: %d, want 429", c)
}
// Every visitor comes through the same proxy, yet another one has a budget of their own.
if c := post("203.0.113.2"); c != 200 {
t.Fatalf("another client behind the proxy: %d, want 200", c)
}
}
func TestSubscribeForwardedForFromAnUntrustedPeerIsIgnored(t *testing.T) {
f := newFixture(t)
f.trust(t, "10.0.0.0/8")
for i := 0; i < 10; i++ {
hdr := map[string]string{"X-Forwarded-For": fmt.Sprintf("203.0.113.%d", i)}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "198.51.100.7", email("a@x.com").Encode(), hdr), 200)
}
hdr := map[string]string{"X-Forwarded-For": "203.0.113.200"}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "198.51.100.7", email("a@x.com").Encode(), hdr), 429)
}
func TestSubscribeTakesTheRightMostUntrustedForwardedAddress(t *testing.T) {
f := newFixture(t)
f.trust(t, "10.0.0.0/8", "192.168.0.0/16")
// The client may write anything on the left; the proxies append on the right.
for i := 0; i < 10; i++ {
hdr := map[string]string{"X-Forwarded-For": fmt.Sprintf("198.51.100.%d, 203.0.113.5, 192.168.1.1", i)}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), hdr), 200)
}
hdr := map[string]string{"X-Forwarded-For": "1.1.1.1,203.0.113.5"}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), hdr), 429)
// The header may arrive as several lines: they are one list, in order.
req := httptest.NewRequest("POST", "/subscribe/dev@example.org", strings.NewReader(email("a@x.com").Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("X-Forwarded-For", "198.51.100.77")
req.Header.Add("X-Forwarded-For", "203.0.113.5")
req.RemoteAddr = "10.0.0.2:4321"
rec := httptest.NewRecorder()
f.h.ServeHTTP(rec, req)
wantStatus(t, rec, 429)
// The proxy's own address does not count against the client.
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), map[string]string{"X-Forwarded-For": "203.0.113.6"}), 200)
}
func TestSubscribeBadForwardedForFallsBackToThePeer(t *testing.T) {
f := newFixture(t)
f.trust(t, "10.0.0.0/8")
for i, xff := range []string{"", "garbage", "203.0.113.1, nonsense", "203.0.113.1:5555", "10.0.0.7", " , ", "", "x", "", ""} {
hdr := map[string]string{}
if xff != "" {
hdr["X-Forwarded-For"] = xff
}
if c := subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), hdr).Code; c != 200 {
t.Fatalf("request %d (%q): %d", i+1, xff, c)
}
}
// All ten were counted against the proxy itself.
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), nil), 429)
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), map[string]string{"X-Forwarded-For": "203.0.113.1"}), 200)
}
func TestSubscribeIPv6ClientsShareTheirSlash64(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
wantStatus(t, subscribe(t, f, "dev@example.org", fmt.Sprintf("2001:db8:1:2::%x", i+1), email("a@x.com")), 200)
}
wantStatus(t, subscribe(t, f, "dev@example.org", "2001:db8:1:2:ffff:ffff:ffff:ffff", email("a@x.com")), 429)
wantStatus(t, subscribe(t, f, "dev@example.org", "2001:db8:1:3::1", email("a@x.com")), 200)
// IPv4 addresses stay separate, one each.
for i := 0; i < 10; i++ {
wantStatus(t, subscribe(t, f, "dev@example.org", "192.0.2.1", email("a@x.com")), 200)
}
wantStatus(t, subscribe(t, f, "dev@example.org", "192.0.2.2", email("a@x.com")), 200)
// The same goes for an IPv6 client named by a trusted proxy.
f.trust(t, "10.0.0.0/8")
for i := 0; i < 10; i++ {
hdr := map[string]string{"X-Forwarded-For": fmt.Sprintf("2001:db8:9:9::%x", i+1)}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), hdr), 200)
}
hdr := map[string]string{"X-Forwarded-For": "2001:db8:9:9:1::1"}
wantStatus(t, subscribeRaw(f.h, "dev@example.org", "10.0.0.2", email("a@x.com").Encode(), hdr), 429)
}
func TestSubscribeLimiterIsCapped(t *testing.T) {
t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
now := t0
l := newSubscribeLimiter(func() time.Time { return now })
if l.max != 100000 {
t.Fatalf("max = %d, want 100000", l.max)
}
l.max = 2
l.allow("192.0.2.1")
now = t0.Add(59*time.Minute + 50*time.Second)
l.allow("192.0.2.2") // the periodic sweep runs here and finds nothing to forget
if wait, ok := l.allow("192.0.2.3"); ok || wait != 10*time.Second {
t.Fatalf("a new address when the map is full: wait %v ok %v, want refused for 10s (until the oldest window ends)", wait, ok)
}
if _, ok := l.allow("192.0.2.2"); !ok {
t.Fatal("an address already known keeps its budget")
}
if l.size() != 2 {
t.Fatalf("size = %d, want 2", l.size())
}
// 20 s later the periodic sweep is not due yet, but the first address has expired: a full map
// sweeps before it refuses.
now = t0.Add(time.Hour + 10*time.Second)
if _, ok := l.allow("192.0.2.3"); !ok {
t.Fatal("room was made by forgetting an expired address")
}
if l.size() != 2 {
t.Fatalf("size = %d, want 2", l.size())
}
}