From d43c47ea08af858ffa741b66d6cc35aa6ba71eb2 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:48:51 +0100 Subject: [PATCH 01/30] fix(store): put the pragmas in the DSN so every connection has them - foreign_keys, busy_timeout(5000) and WAL now come from the DSN, not from an Exec on one connection - test: every connection of a 2-connection pool, in memory and on disk --- internal/store/store.go | 12 ++++++----- internal/store/store_test.go | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/internal/store/store.go b/internal/store/store.go index 5abaedf..069b34e 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -215,16 +215,18 @@ CREATE TABLE IF NOT EXISTS settings ( // Open opens (and migrates) the database at path. Use ":memory:" for tests. func Open(path string) (*Store, error) { - db, err := sql.Open("sqlite", path) + // The pragmas are in the DSN so that every connection of the pool has them. + sep := "?" + if strings.Contains(path, "?") { + sep = "&" + } + dsn := path + sep + "_pragma=foreign_keys(1)&_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)" + db, err := sql.Open("sqlite", dsn) if err != nil { return nil, err } // An in-memory database is per-connection: keep a single one. db.SetMaxOpenConns(1) - if _, err := db.Exec("PRAGMA foreign_keys=ON; PRAGMA journal_mode=WAL;"); err != nil { - db.Close() - return nil, err - } if err := migrate(db); err != nil { db.Close() return nil, fmt.Errorf("migrate: %w", err) diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 3aa8483..50aab4e 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -1,7 +1,10 @@ package store import ( + "context" + "database/sql" "errors" + "path/filepath" "testing" ) @@ -124,3 +127,42 @@ func TestRemoveMember(t *testing.T) { t.Fatalf("want ErrNotFound, got %v", err) } } + +// Every connection of the pool must enforce foreign keys and wait on a busy +// database, not only the first one: the pragmas live in the DSN. +func TestEveryConnectionHasThePragmas(t *testing.T) { + for _, path := range []string{":memory:", filepath.Join(t.TempDir(), "p.db")} { + s, err := Open(path) + if err != nil { + t.Fatalf("open %s: %v", path, err) + } + s.db.SetMaxOpenConns(2) + ctx := context.Background() + c1, err := s.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + c2, err := s.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + for i, c := range []*sql.Conn{c1, c2} { + var fk, busy int + if err := c.QueryRowContext(ctx, "PRAGMA foreign_keys").Scan(&fk); err != nil || fk != 1 { + t.Errorf("%s conn %d: foreign_keys = %d, %v", path, i, fk, err) + } + if err := c.QueryRowContext(ctx, "PRAGMA busy_timeout").Scan(&busy); err != nil || busy != 5000 { + t.Errorf("%s conn %d: busy_timeout = %d, %v", path, i, busy, err) + } + if path != ":memory:" { + var mode string + if err := c.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&mode); err != nil || mode != "wal" { + t.Errorf("%s conn %d: journal_mode = %q, %v", path, i, mode, err) + } + } + } + c1.Close() + c2.Close() + s.Close() + } +} From 28b73fa24a8226e5d3207d8894d5e489a256c408 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:49:32 +0100 Subject: [PATCH 02/30] test(api): every route but health answers 401 without the token routes() is now the single table New registers, so a route added later is covered by TestEveryRouteNeedsTheToken without editing it. - api: declare the routes in one table (routes) and register from it - api: table test walks it, 401 without a token, not 401 with one --- internal/api/api.go | 55 +++++++++++++++++++++++++--------------- internal/api/api_test.go | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 20 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index b40b9ab..e9e7200 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -40,29 +40,44 @@ func New(s *store.Store, token string, rel Releaser) http.Handler { func newHandler(s *store.Store, token string, rel Releaser, now func() time.Time) http.Handler { srv := &server{store: s, token: token, releaser: rel, lim: newLimiter(now)} mux := http.NewServeMux() - mux.HandleFunc("GET /api/v1/health", func(w http.ResponseWriter, _ *http.Request) { - writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) - }) - mux.Handle("GET /api/v1/lists", srv.auth(srv.listLists)) - mux.Handle("POST /api/v1/lists", srv.auth(srv.createList)) - mux.Handle("GET /api/v1/lists/{id}", srv.auth(srv.getList)) - mux.Handle("PATCH /api/v1/lists/{id}", srv.auth(srv.updateList)) - mux.Handle("DELETE /api/v1/lists/{id}", srv.auth(srv.deleteList)) - mux.Handle("GET /api/v1/lists/{id}/members", srv.auth(srv.listMembers)) - mux.Handle("POST /api/v1/lists/{id}/members", srv.auth(srv.addMember)) - mux.Handle("DELETE /api/v1/lists/{id}/members/{address}", srv.auth(srv.removeMember)) - mux.Handle("GET /api/v1/lists/{id}/archive", srv.auth(srv.listArchive)) - mux.Handle("POST /api/v1/lists/{id}/archive/import", srv.auth(srv.importArchive)) - mux.Handle("GET /api/v1/archive/{id}", srv.auth(srv.getArchived)) - mux.Handle("DELETE /api/v1/archive/{id}", srv.auth(srv.deleteArchived)) - mux.Handle("GET /api/v1/queue", srv.auth(srv.getQueue)) - mux.Handle("DELETE /api/v1/queue/{id}", srv.auth(srv.discardQueued)) - mux.Handle("GET /api/v1/lists/{id}/held", srv.auth(srv.listHeld)) - mux.Handle("POST /api/v1/held/{id}/approve", srv.auth(srv.approveHeld)) - mux.Handle("DELETE /api/v1/held/{id}", srv.auth(srv.discardHeld)) + for _, rt := range srv.routes() { + mux.Handle(rt.pattern, rt.handler) + } return mux } +type route struct { + pattern string + handler http.Handler +} + +// routes is the only place routes are declared. Everything but the health check goes through auth. +func (s *server) routes() []route { + public := func(h http.HandlerFunc) http.Handler { return h } + return []route{ + {"GET /api/v1/health", public(func(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) + })}, + {"GET /api/v1/lists", s.auth(s.listLists)}, + {"POST /api/v1/lists", s.auth(s.createList)}, + {"GET /api/v1/lists/{id}", s.auth(s.getList)}, + {"PATCH /api/v1/lists/{id}", s.auth(s.updateList)}, + {"DELETE /api/v1/lists/{id}", s.auth(s.deleteList)}, + {"GET /api/v1/lists/{id}/members", s.auth(s.listMembers)}, + {"POST /api/v1/lists/{id}/members", s.auth(s.addMember)}, + {"DELETE /api/v1/lists/{id}/members/{address}", s.auth(s.removeMember)}, + {"GET /api/v1/lists/{id}/archive", s.auth(s.listArchive)}, + {"POST /api/v1/lists/{id}/archive/import", s.auth(s.importArchive)}, + {"GET /api/v1/archive/{id}", s.auth(s.getArchived)}, + {"DELETE /api/v1/archive/{id}", s.auth(s.deleteArchived)}, + {"GET /api/v1/queue", s.auth(s.getQueue)}, + {"DELETE /api/v1/queue/{id}", s.auth(s.discardQueued)}, + {"GET /api/v1/lists/{id}/held", s.auth(s.listHeld)}, + {"POST /api/v1/held/{id}/approve", s.auth(s.approveHeld)}, + {"DELETE /api/v1/held/{id}", s.auth(s.discardHeld)}, + } +} + func (s *server) auth(next http.HandlerFunc) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ip := clientIP(r) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index ac1e3ad..984dda7 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -685,3 +685,50 @@ func TestQueueShowsFailedDeliveriesApartFromPendingOnes(t *testing.T) { } expect(t, do(t, h, "DELETE", "/api/v1/queue/"+strconv.FormatInt(due[2].ID, 10), "", true), 204) } + +// TestEveryRouteNeedsTheToken walks the route table New registers, so a route added later is +// covered without touching this test: all of them but the health check answer 401 without a token +// and something else with one. +func TestEveryRouteNeedsTheToken(t *testing.T) { + s, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { s.Close() }) + h := New(s, token, nil) + + rs := (&server{}).routes() + if len(rs) < 17 { + t.Fatalf("only %d routes in the table", len(rs)) + } + for i, rt := range rs { + method, pattern, _ := strings.Cut(rt.pattern, " ") + path := strings.NewReplacer("{id}", "1", "{address}", "a@example.com").Replace(pattern) + t.Run(rt.pattern, func(t *testing.T) { + // Each request comes from its own address, so the lockout never gets in the way. + addr := fmt.Sprintf("192.0.2.%d:4000", i+1) + call := func(auth bool) int { + req := httptest.NewRequest(method, path, strings.NewReader("{}")) + req.RemoteAddr = addr + if auth { + req.Header.Set("Authorization", "Bearer "+token) + } + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + return rec.Code + } + if pattern == "/api/v1/health" { + if got := call(false); got != 200 { + t.Fatalf("health without a token: %d", got) + } + return + } + if got := call(false); got != 401 { + t.Errorf("no token: status %d, want 401", got) + } + if got := call(true); got == 401 { + t.Errorf("good token: status %d", got) + } + }) + } +} From fddf14e285dfbb4d9766cb7c1673d4784a57d37f Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:50:01 +0100 Subject: [PATCH 03/30] fix(router): count only failed recipient-unknown DSNs as hard bounces - Parse the bounce as an RFC 3464 report: multipart/report with a message/delivery-status (or message/global-delivery-status) part. - Count a hard bounce only for a per-recipient block with Action: failed and a status of 5.1.1, 5.1.2, 5.1.3, 5.1.6 or 5.1.10. - Policy and content rejections (5.7.x), full mailboxes (5.2.x), delays, 4.x.x and Status lines in plain text or quoted text no longer count. - Tests with real-looking DSNs for each class, quoted text and multiple recipient blocks; the VERP token check is unchanged. --- internal/router/bounce_test.go | 121 +++++++++++++++++++++++++++++++++ internal/router/commands.go | 5 +- internal/router/dsn.go | 66 ++++++++++++++++++ internal/router/m2_test.go | 2 +- 4 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 internal/router/bounce_test.go create mode 100644 internal/router/dsn.go diff --git a/internal/router/bounce_test.go b/internal/router/bounce_test.go new file mode 100644 index 0000000..a8fd492 --- /dev/null +++ b/internal/router/bounce_test.go @@ -0,0 +1,121 @@ +package router + +import ( + "fmt" + "strings" + "testing" +) + +// dsn builds a delivery status notification as an MTA sends it (RFC 3464): a multipart/report with +// a human part, a message/delivery-status part and the returned headers. +func dsn(action, status string) []byte { + return []byte(strings.ReplaceAll(fmt.Sprintf(`From: MAILER-DAEMON@mx.example.com +To: dev+bounce@example.org +Subject: Undelivered Mail Returned to Sender +MIME-Version: 1.0 +Content-Type: multipart/report; report-type=delivery-status; + boundary="BOUNDARY" + +--BOUNDARY +Content-Type: text/plain; charset=us-ascii + +I'm sorry to have to inform you that your message could not be delivered. + +--BOUNDARY +Content-Type: message/delivery-status + +Reporting-MTA: dns; mx.example.com +Arrival-Date: Mon, 9 Nov 2026 10:00:00 +0100 + +Final-Recipient: rfc822; alice@example.com +Original-Recipient: rfc822;alice@example.com +Action: %s +Status: %s +Diagnostic-Code: smtp; 550 something + +--BOUNDARY +Content-Type: text/rfc822-headers + +From: bob@example.com +Subject: hello + +--BOUNDARY-- +`, action, status), "\n", "\r\n")) +} + +func bounceCount(t *testing.T, e *env) int { + t.Helper() + m, err := e.s.MemberByID(e.alice) + if err != nil { + t.Fatal(err) + } + return m.BounceCount +} + +func TestOnlyFailedRecipientUnknownDSNsCount(t *testing.T) { + cases := []struct { + action, status string + counts bool + }{ + {"failed", "5.1.1", true}, + {"failed", "5.1.2", true}, + {"failed", "5.1.3", true}, + {"failed", "5.1.6", true}, + {"failed", "5.1.10", true}, + {"FAILED (permanent)", "5.1.1 (bad destination mailbox address)", true}, + {"failed", "5.7.1", false}, // spam or policy rejection of the content + {"failed", "5.2.2", false}, // mailbox full + {"failed", "5.1.7", false}, // bad sender address + {"failed", "5.1.11", false}, // not in the list (prefix of 5.1.1x must not match) + {"failed", "4.1.1", false}, + {"delayed", "5.1.1", false}, + {"delayed", "4.4.1", false}, + {"delivered", "2.0.0", false}, + } + for _, c := range cases { + e := newEnv(t) + handled, err := e.command("", e.r.bounceAddress(e.list, e.alice), dsn(c.action, c.status)) + if !handled || err != nil { + t.Fatalf("%s %s: %v %v", c.action, c.status, handled, err) + } + if got := bounceCount(t, e) == 1; got != c.counts { + t.Errorf("%s %s: counted=%v, want %v", c.action, c.status, got, c.counts) + } + } +} + +func TestStatusLinesOutsideADeliveryStatusPartDoNotCount(t *testing.T) { + quoted := "From: carol@example.com\r\nSubject: Re: your bounce\r\nContent-Type: text/plain\r\n\r\n" + + "> Action: failed\r\n> Status: 5.1.1\r\nAction: failed\r\nStatus: 5.1.1\r\n" + // A report whose status sits in the human part, not in message/delivery-status. + human := strings.Replace(string(dsn("delayed", "4.4.1")), "I'm sorry", "Action: failed\r\nStatus: 5.1.1\r\n\r\nI'm sorry", 1) + // The returned original message quotes a DSN of its own. + returned := strings.Replace(string(dsn("delayed", "4.4.1")), "Subject: hello", "Subject: hello\r\nAction: failed\r\nStatus: 5.1.1", 1) + for name, msg := range map[string]string{ + "plain text reply": quoted, + "status in human part": human, + "status in returned": returned, + "not mime at all": "Action: failed\r\nStatus: 5.1.1\r\n", + "broken multipart": "Content-Type: multipart/report; boundary=x\r\n\r\nno parts here\r\nStatus: 5.1.1\r\n", + "empty": "", + } { + e := newEnv(t) + if _, err := e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)); err != nil { + t.Fatalf("%s: %v", name, err) + } + if n := bounceCount(t, e); n != 0 { + t.Errorf("%s: counted %d bounces", name, n) + } + } +} + +func TestAnyFailedRecipientBlockCounts(t *testing.T) { + // The first recipient block is a delay, the second a hard failure: the report counts once. + msg := strings.Replace(string(dsn("failed", "5.1.1")), "Final-Recipient:", + "Final-Recipient: rfc822; alice@example.com\r\nAction: delayed\r\nStatus: 4.4.1\r\n\r\nFinal-Recipient:", 1) + e := newEnv(t) + e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)) + if n := bounceCount(t, e); n != 1 { + t.Fatalf("counted %d", n) + } +} diff --git a/internal/router/commands.go b/internal/router/commands.go index 499c303..86459fb 100644 --- a/internal/router/commands.go +++ b/internal/router/commands.go @@ -8,7 +8,6 @@ import ( "fmt" "mime" "net/mail" - "regexp" "strconv" "strings" "time" @@ -16,8 +15,6 @@ import ( "codeberg.org/squidd/parsimony/internal/store" ) -var statusRe = regexp.MustCompile(`(?mi)^Status:\s*([245])\.\d+\.\d+`) - func (r *Router) secret() ([]byte, error) { r.secretOnce.Do(func() { r.secretVal, r.secretErr = r.store.Secret() }) return r.secretVal, r.secretErr @@ -164,7 +161,7 @@ func (r *Router) bounce(l store.List, tag string, data []byte) error { if err != nil { return err } - if sm := statusRe.FindSubmatch(data); sm != nil && string(sm[1]) == "5" { + if isHardBounce(data) { _, err = r.store.RecordHardBounce(m.ID, store.BounceThreshold) } return err diff --git a/internal/router/dsn.go b/internal/router/dsn.go new file mode 100644 index 0000000..a5e33b5 --- /dev/null +++ b/internal/router/dsn.go @@ -0,0 +1,66 @@ +package router + +import ( + "bufio" + "bytes" + "io" + "mime" + "mime/multipart" + "net/mail" + "net/textproto" + "strings" +) + +// recipientUnknown are the DSN status codes that say the recipient does not exist: 5.1.1 bad +// mailbox, 5.1.2 bad domain, 5.1.3 bad address syntax, 5.1.6 moved, 5.1.10 null MX. It is the +// same set as outbound.recipientUnknown, used for RCPT TO answers; keep the two in sync. +var recipientUnknown = map[string]bool{"5.1.1": true, "5.1.2": true, "5.1.3": true, "5.1.6": true, "5.1.10": true} + +// isHardBounce reports whether data is a delivery status notification (RFC 3464) that reports a +// recipient as non-existent: a multipart/report with a message/delivery-status part holding a +// per-recipient block with "Action: failed" and a status in recipientUnknown. Anything else (a +// policy or content rejection, a full mailbox, a delay, a status line in plain text) is not. +func isHardBounce(data []byte) bool { + msg, err := mail.ReadMessage(bytes.NewReader(data)) + if err != nil { + return false + } + mt, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) + if err != nil || mt != "multipart/report" || params["boundary"] == "" { + return false + } + mr := multipart.NewReader(msg.Body, params["boundary"]) + for { + p, err := mr.NextPart() + if err != nil { + return false + } + pt, _, _ := mime.ParseMediaType(p.Header.Get("Content-Type")) + if pt == "message/delivery-status" || pt == "message/global-delivery-status" { + return failedUnknownRecipient(p) + } + } +} + +// failedUnknownRecipient reads the fields of a delivery-status body: a per-message block, then one +// block per recipient, separated by blank lines. +func failedUnknownRecipient(r io.Reader) bool { + tr := textproto.NewReader(bufio.NewReader(r)) + for { + h, err := tr.ReadMIMEHeader() + if firstWord(h.Get("Action")) == "failed" && recipientUnknown[firstWord(h.Get("Status"))] { + return true + } + if err != nil { + return false + } + } +} + +// firstWord returns the lowercase value of a field without its trailing comment. +func firstWord(v string) string { + if f := strings.Fields(v); len(f) > 0 { + return strings.ToLower(f[0]) + } + return "" +} diff --git a/internal/router/m2_test.go b/internal/router/m2_test.go index 1a1d1c6..133206b 100644 --- a/internal/router/m2_test.go +++ b/internal/router/m2_test.go @@ -290,7 +290,7 @@ func TestUnknownCommandAndList(t *testing.T) { } func bounceMsg(status string) []byte { - return []byte(fmt.Sprintf("Subject: Undelivered Mail\r\nContent-Type: text/plain\r\n\r\nAction: failed\r\nStatus: %s\r\nDiagnostic-Code: smtp; 550 no such user\r\n", status)) + return dsn("failed", status) } func TestHardBouncesDisableMember(t *testing.T) { From b8fcbf9acf0d2260ed24422e31270053ae253697 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:50:40 +0100 Subject: [PATCH 04/30] fix(api): validate list name, domain and title on create createList accepted a title with CR/LF (every later post to the list then failed to rewrite and got 451) and names with '/', '<', '>' or tabs. - name: lowercased, 1 to 30 characters of a-z 0-9 . _ -, no leading or trailing dot, no ".." - domain: lowercased ASCII host name, labels of 1 to 63 characters - title: non-empty single line, at most 200 characters, no control characters - updateList uses the same title rule (it also refuses tabs and long titles now) - tests: refused names, domains and titles, the accepted limits, nothing created for a bad title --- internal/api/api.go | 72 ++++++++++++++++++++++++++++++++++---- internal/api/api_test.go | 74 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 7 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index e9e7200..0c96bb3 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -12,6 +12,8 @@ import ( "strconv" "strings" "time" + "unicode" + "unicode/utf8" "codeberg.org/squidd/parsimony/internal/archive" "codeberg.org/squidd/parsimony/internal/store" @@ -184,6 +186,53 @@ func (s *server) listLists(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, out) } +const ( + maxNameLen = 30 // keeps list+confirm- and list+bounce-- within 64 characters + maxTitleLen = 200 + titleHint = "title must be a non-empty single line of at most 200 characters, without control characters" +) + +// validName reports whether name (already lowercased) works as the local part of a list address. +func validName(name string) bool { + if name == "" || len(name) > maxNameLen || strings.HasPrefix(name, ".") || strings.HasSuffix(name, ".") || strings.Contains(name, "..") { + return false + } + for _, c := range name { + if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '.' && c != '_' && c != '-' { + return false + } + } + return true +} + +// validDomain reports whether d (already lowercased) is an ASCII host name: dot-separated labels of +// 1 to 63 letters, digits and hyphens, none starting or ending with a hyphen. +func validDomain(d string) bool { + if d == "" || len(d) > 253 { + return false + } + for _, label := range strings.Split(d, ".") { + if label == "" || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' { + return false + } + for _, c := range label { + if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' { + return false + } + } + } + return true +} + +// validTitle reports whether t is a usable list title: not empty, at most maxTitleLen characters, +// no line breaks or other control characters (it goes into mail headers). +func validTitle(t string) bool { + if t == "" || utf8.RuneCountInString(t) > maxTitleLen { + return false + } + return !strings.ContainsFunc(t, unicode.IsControl) +} + func (s *server) createList(w http.ResponseWriter, r *http.Request) { var in struct { Name string `json:"name"` @@ -193,13 +242,22 @@ func (s *server) createList(w http.ResponseWriter, r *http.Request) { if !decodeBody(w, r, &in) { return } - in.Name, in.Domain = strings.TrimSpace(in.Name), strings.TrimSpace(in.Domain) - if in.Name == "" || in.Domain == "" || strings.ContainsAny(in.Name+in.Domain, "@+ \r\n") { - writeError(w, http.StatusBadRequest, "name and domain are required and must not contain '@', '+' or spaces") + typedName := strings.TrimSpace(in.Name) + in.Name, in.Domain = strings.ToLower(typedName), strings.ToLower(strings.TrimSpace(in.Domain)) + if !validName(in.Name) { + writeError(w, http.StatusBadRequest, "name must be 1 to 30 characters of a-z, 0-9, '.', '_' or '-', without a dot at either end or two dots in a row") return } - if in.Title == "" { - in.Title = in.Name + if !validDomain(in.Domain) { + writeError(w, http.StatusBadRequest, "domain must be a host name such as lists.example.org") + return + } + if in.Title = strings.TrimSpace(in.Title); in.Title == "" { + in.Title = typedName + } + if !validTitle(in.Title) { + writeError(w, http.StatusBadRequest, titleHint) + return } l, err := s.store.CreateList(in.Name, in.Domain, in.Title) if err != nil { @@ -248,8 +306,8 @@ func (s *server) updateList(w http.ResponseWriter, r *http.Request) { } if in.Title != nil { title := strings.TrimSpace(*in.Title) - if title == "" || strings.ContainsAny(title, "\r\n") { - writeError(w, http.StatusBadRequest, "title must be a non-empty single line") + if !validTitle(title) { + writeError(w, http.StatusBadRequest, titleHint) return } l.Title = title diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 984dda7..19ddfdf 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -152,6 +152,34 @@ func TestCreateListValidation(t *testing.T) { `{"name":"a+b","domain":"example.org"}`, `{"name":"dev","domain":""}`, `{"name":"dev","domain":"example.org","bogus":1}`, + // Names that break an address: separators, brackets, whitespace, control characters, dots at the ends. + `{"name":"a/b","domain":"example.org"}`, + `{"name":"ab","domain":"example.org"}`, + `{"name":"a\tb","domain":"example.org"}`, + `{"name":"a b","domain":"example.org"}`, + `{"name":"a\"b","domain":"example.org"}`, + `{"name":"caffè","domain":"example.org"}`, + `{"name":".dev","domain":"example.org"}`, + `{"name":"dev.","domain":"example.org"}`, + `{"name":"a..b","domain":"example.org"}`, + `{"name":"` + strings.Repeat("a", 31) + `","domain":"example.org"}`, + // Domains that are not host names. + `{"name":"dev","domain":"exa mple.org"}`, + `{"name":"dev","domain":"example..org"}`, + `{"name":"dev","domain":"-example.org"}`, + `{"name":"dev","domain":"example-.org"}`, + `{"name":"dev","domain":"example.org."}`, + `{"name":"dev","domain":"ex_ample.org"}`, + `{"name":"dev","domain":"exämple.org"}`, + `{"name":"dev","domain":"a/b.org"}`, + `{"name":"dev","domain":"` + strings.Repeat("a", 64) + `.org"}`, + // Titles that are not one clean line. + `{"name":"dev","domain":"example.org","title":"Dev\r\nBcc: x@y.com"}`, + `{"name":"dev","domain":"example.org","title":"Dev\nlist"}`, + `{"name":"dev","domain":"example.org","title":"Dev\tlist"}`, + `{"name":"dev","domain":"example.org","title":"Dev\u0000list"}`, + `{"name":"dev","domain":"example.org","title":"` + strings.Repeat("x", 201) + `"}`, } { rec := do(t, h, "POST", "/api/v1/lists", body, true) if rec.Code != 400 { @@ -732,3 +760,49 @@ func TestEveryRouteNeedsTheToken(t *testing.T) { }) } } + +func TestCreateListAcceptsTheLimits(t *testing.T) { + h := newServer(t) + for _, body := range []string{ + `{"name":"` + strings.Repeat("a", 30) + `","domain":"example.org","title":"` + strings.Repeat("è", 200) + `"}`, + `{"name":"dev-team_1.eu","domain":"lists.sub-domain.example.org"}`, + `{"name":"Dev2","domain":"localhost","title":"Développeurs ☕"}`, + } { + if rec := do(t, h, "POST", "/api/v1/lists", body, true); rec.Code != 201 { + t.Errorf("%.80q: status %d, body %s", body, rec.Code, rec.Body.String()) + } + } +} + +// A title with a line break would make every later post to the list fail to rewrite, so the list +// must not come into existence at all. +func TestCreateListWithABadTitleLeavesNothingBehind(t *testing.T) { + h := newServer(t) + expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev\r\nX: y"}`, true), 400) + rec := do(t, h, "GET", "/api/v1/lists", "", true) + if strings.TrimSpace(rec.Body.String()) != "[]" { + t.Fatalf("the refused list was created: %s", rec.Body.String()) + } + // The name is free for a good request. + expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, true), 201) +} + +func TestCreateListLowercasesNameAndDomain(t *testing.T) { + h := newServer(t) + rec := do(t, h, "POST", "/api/v1/lists", `{"name":" Dev ","domain":"Example.ORG"}`, true) + expect(t, rec, 201) + var l listJSON + decode(t, rec, &l) + if l.Address != "dev@example.org" || l.Title != "Dev" { + t.Fatalf("%+v", l) + } +} + +func TestUpdateListTitleUsesTheSameRules(t *testing.T) { + h := newServer(t) + createList(t, h) + for _, title := range []string{`"a\tb"`, `"` + strings.Repeat("x", 201) + `"`, `" "`, `"a\r\nb"`} { + expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":`+title+`}`, true), 400) + } + expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":"`+strings.Repeat("x", 200)+`"}`, true), 200) +} From ca2a1d63d7f1d9266a61496834516ecea3394f47 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:50:49 +0100 Subject: [PATCH 05/30] fix(store): count only hard bounces of the last 30 days - Add members.bounce_at (unix time of the last counted bounce) with an additive migration; existing counts get 0, so they are treated as old. - RecordHardBounce restarts the count at 1 when the last bounce is older than BounceWindow (30 days); a disabled member stays disabled. - Tests for the window boundary, the restart and the migration of an old database. --- internal/store/bounce_test.go | 76 +++++++++++++++++++++++++++++++++++ internal/store/store.go | 21 +++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 internal/store/bounce_test.go diff --git a/internal/store/bounce_test.go b/internal/store/bounce_test.go new file mode 100644 index 0000000..69212a8 --- /dev/null +++ b/internal/store/bounce_test.go @@ -0,0 +1,76 @@ +package store + +import ( + "testing" + "time" +) + +func TestHardBouncesCountOnlyWithinTheWindow(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_900_000_000, 0) + s.Now = func() time.Time { return now } + l, _ := s.CreateList("dev", "example.org", "Dev") + s.AddMember(l.ID, "a@x.com", "") + ms, _ := s.Members(l.ID) + id := ms[0].ID + count := func() int { + t.Helper() + m, err := s.MemberByID(id) + if err != nil { + t.Fatal(err) + } + return m.BounceCount + } + + s.RecordHardBounce(id, 3) + now = now.Add(BounceWindow) // exactly the window: still recent + s.RecordHardBounce(id, 3) + if n := count(); n != 2 { + t.Fatalf("a bounce within the window adds up: %d", n) + } + now = now.Add(BounceWindow + time.Second) // the last bounce is now too old + if disabled, err := s.RecordHardBounce(id, 3); disabled || err != nil { + t.Fatalf("an old count must not disable: %v %v", disabled, err) + } + if n := count(); n != 1 { + t.Fatalf("a bounce after a quiet window restarts the count: %d", n) + } + now = now.Add(time.Hour) + s.RecordHardBounce(id, 3) + if disabled, _ := s.RecordHardBounce(id, 3); !disabled { + t.Fatal("three recent bounces disable") + } + // A disabled member stays disabled, even when a late bounce restarts the count. + now = now.Add(2 * BounceWindow) + if disabled, _ := s.RecordHardBounce(id, 3); !disabled { + t.Fatal("only re-adding re-enables a member") + } +} + +func TestMigrationAddsTheBounceTimeColumn(t *testing.T) { + path := t.TempDir() + "/old.db" + s, _ := Open(path) + l, _ := s.CreateList("dev", "example.org", "Dev") + s.AddMember(l.ID, "a@x.com", "") + s.db.Exec(`UPDATE members SET bounce_count=2`) + if _, err := s.db.Exec(`ALTER TABLE members DROP COLUMN bounce_at`); err != nil { + t.Fatal(err) + } + s.Close() + s2, err := Open(path) + if err != nil { + t.Fatalf("migrate: %v", err) + } + defer s2.Close() + ms, err := s2.Members(l.ID) + if err != nil || len(ms) != 1 || ms[0].BounceCount != 2 { + t.Fatalf("members survive the migration: %+v %v", ms, err) + } + // An old count has no date: it is older than the window, so the next bounce restarts it. + if disabled, err := s2.RecordHardBounce(ms[0].ID, 3); disabled || err != nil { + t.Fatalf("%v %v", disabled, err) + } + if m, _ := s2.MemberByID(ms[0].ID); m.BounceCount != 1 { + t.Fatalf("count: %d", m.BounceCount) + } +} diff --git a/internal/store/store.go b/internal/store/store.go index 5abaedf..0683169 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -58,6 +58,9 @@ var ( // BounceThreshold is the number of hard bounces after which a member is disabled. const BounceThreshold = 3 +// BounceWindow is how long a hard bounce counts: a bounce after a quiet window restarts the count. +const BounceWindow = 30 * 24 * time.Hour + const ( pendingTTL = 72 * time.Hour pendingRateLimit = time.Hour @@ -146,6 +149,7 @@ CREATE TABLE IF NOT EXISTS members ( display_name TEXT NOT NULL DEFAULT '', disabled INTEGER NOT NULL DEFAULT 0, bounce_count INTEGER NOT NULL DEFAULT 0, + bounce_at INTEGER NOT NULL DEFAULT 0, UNIQUE(list_id, address) ); CREATE TABLE IF NOT EXISTS held ( @@ -278,6 +282,15 @@ func migrate(db *sql.DB) error { } } } + if ok, err := hasColumn(db, "members", "id"); err != nil { + return err + } else if ok { + if has, _ := hasColumn(db, "members", "bounce_at"); !has { + if _, err := db.Exec(`ALTER TABLE members ADD COLUMN bounce_at INTEGER NOT NULL DEFAULT 0`); err != nil { + return err + } + } + } if ok, err := hasColumn(db, "outbox", "msg_id"); err != nil { return err } else if ok { @@ -545,9 +558,13 @@ func (s *Store) DeleteMember(id int64) error { } // RecordHardBounce counts a hard bounce and disables the member once the count -// reaches threshold. It reports whether the member is now disabled. +// reaches threshold. The count holds recent bounces only: when the last one is +// older than BounceWindow it restarts at 1. It reports whether the member is now disabled. func (s *Store) RecordHardBounce(memberID int64, threshold int) (bool, error) { - if err := affected(s.db.Exec(`UPDATE members SET bounce_count=bounce_count+1 WHERE id=?`, memberID)); err != nil { + now := s.Now() + if err := affected(s.db.Exec(`UPDATE members SET + bounce_count=CASE WHEN bounce_at=?`, memberID, threshold); err != nil { From 087bc92d413911e856281857bf9268172e1f6829 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:51:05 +0100 Subject: [PATCH 06/30] fix(router): RFC 2047 encode a non-ASCII List-Id phrase - headers: the title in List-Id goes through mime.QEncoding, the part stays ASCII - test: UTF-8 and 200-character titles read back through net/mail and the word decoder --- internal/router/headers.go | 4 ++- internal/router/router_test.go | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/internal/router/headers.go b/internal/router/headers.go index 8847dea..c9f5a3c 100644 --- a/internal/router/headers.go +++ b/internal/router/headers.go @@ -5,6 +5,7 @@ import ( "bytes" "fmt" "io" + "mime" "net/mail" "github.com/emersion/go-message/textproto" @@ -27,7 +28,8 @@ func rewrite(l store.List, data []byte) ([]byte, error) { } id := l.Name + "." + l.Domain - h.Set("List-Id", fmt.Sprintf("%s <%s>", l.Title, id)) + // The phrase is RFC 2047 encoded when it is not ASCII; the stays plain ASCII (list names and domains are). + h.Set("List-Id", fmt.Sprintf("%s <%s>", mime.QEncoding.Encode("utf-8", l.Title), id)) h.Set("List-Post", "") // List-Unsubscribe is per recipient: Router.Send adds it. h.Set("Precedence", "list") diff --git a/internal/router/router_test.go b/internal/router/router_test.go index 1cb7856..5e12516 100644 --- a/internal/router/router_test.go +++ b/internal/router/router_test.go @@ -3,6 +3,7 @@ package router import ( "bytes" "errors" + "mime" "net/mail" "strings" "testing" @@ -107,3 +108,51 @@ func TestRewriteKeepsExistingReplyTo(t *testing.T) { t.Errorf("Reply-To = %q", got) } } + +func TestListIdWithANonASCIITitleIsEncoded(t *testing.T) { + l := store.List{Name: "dev", Domain: "example.org", Title: "Développeurs ☕ \"café\""} + out, err := rewrite(l, []byte(sample)) + if err != nil { + t.Fatal(err) + } + for _, line := range strings.Split(string(out), "\r\n") { + if strings.HasPrefix(line, "List-Id:") { + for _, b := range []byte(line) { + if b >= 0x80 { + t.Fatalf("raw non-ASCII in %q", line) + } + } + } + } + m, err := mail.ReadMessage(bytes.NewReader(out)) + if err != nil { + t.Fatal(err) + } + raw := m.Header.Get("List-Id") + if !strings.HasSuffix(raw, " ") { + t.Fatalf("the id must stay a plain : %q", raw) + } + phrase, err := new(mime.WordDecoder).DecodeHeader(strings.TrimSuffix(raw, " ")) + if err != nil || phrase != l.Title { + t.Fatalf("phrase = %q (%v), want %q", phrase, err, l.Title) + } + // A long title is folded into several encoded words and still reads back. + long := store.List{Name: "dev", Domain: "example.org", Title: strings.Repeat("è", 200)} + out, err = rewrite(long, []byte(sample)) + if err != nil { + t.Fatal(err) + } + m, err = mail.ReadMessage(bytes.NewReader(out)) + if err != nil { + t.Fatal(err) + } + raw = m.Header.Get("List-Id") + if phrase, err := new(mime.WordDecoder).DecodeHeader(strings.TrimSuffix(raw, " ")); err != nil || phrase != long.Title { + t.Fatalf("long phrase = %q (%v)", phrase, err) + } + // An ASCII title is left as it is. + out, _ = rewrite(store.List{Name: "dev", Domain: "example.org", Title: "Dev"}, []byte(sample)) + if !strings.Contains(string(out), "List-Id: Dev \r\n") { + t.Fatalf("ascii title changed: %q", out) + } +} From 0a21e998e7cb62c706d8b4ecaf79d652bb2746f2 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:51:13 +0100 Subject: [PATCH 07/30] fix(archive): bound the multipart walk and distrust extreme dates - walk at most 10 nested multipart levels and 100 parts, then use what was found: the post is still delivered, the archive stores less - read a text part only while it can still become the body, and stop at the first non-blank plain part, so memory stays near MaxBody - a Date more than 24 hours after the time of parsing becomes that time; a Date before 1990 is treated as missing - tests: 20000-level nesting finishes fast, exact limits, allocation bound, pathological HTML, date clamps --- internal/archive/archive.go | 67 +++++++---- internal/archive/archive_test.go | 197 +++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+), 21 deletions(-) diff --git a/internal/archive/archive.go b/internal/archive/archive.go index afc76d9..b8a8827 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -24,6 +24,19 @@ import _ "github.com/emersion/go-message/charset" // MaxBody is the largest body stored for one message, in bytes. const MaxBody = 1 << 20 +// Limits on how much of a message's structure is walked to find its body: a +// hostile message cannot make the parser recurse or loop for long. +const ( + maxDepth = 10 // levels of nested multipart + maxParts = 100 // parts visited in one message +) + +// Dates outside these bounds are not trusted: they would pin a message to the +// top (or the bottom) of the archive for good. +const maxFuture = 24 * time.Hour + +var minDate = time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC) + // Entry is the archivable content of a message. type Entry struct { MessageID string @@ -45,8 +58,8 @@ var ( emailRe = regexp.MustCompile(`([A-Za-z0-9._%+\-]+)@[A-Za-z0-9\-]+(?:\.[A-Za-z0-9\-]+)*\.[A-Za-z]{2,}`) ) -// Parse reads an RFC 5322 message. now is used when the Date header is missing -// or invalid. +// Parse reads an RFC 5322 message. now is used when the Date header is missing, +// invalid, before 1990, or more than 24 hours after now. func Parse(raw []byte, now time.Time) (Entry, error) { ent, err := message.Read(bytes.NewReader(raw)) if err != nil && !message.IsUnknownCharset(err) { @@ -58,7 +71,7 @@ func Parse(raw []byte, now time.Time) (Entry, error) { h := mail.Header{Header: ent.Header} e := Entry{Date: now} - if d, err := h.Date(); err == nil && !d.IsZero() { + if d, err := h.Date(); err == nil && !d.Before(minDate) && !d.After(now.Add(maxFuture)) { e.Date = d } if e.Subject, _ = h.Subject(); strings.TrimSpace(e.Subject) == "" { @@ -79,7 +92,9 @@ func Parse(raw []byte, now time.Time) (Entry, error) { e.InReplyTo = refs[len(refs)-1] } - plain, htmlBody := bodies(ent) + w := &walker{} + w.walk(ent, 0) + plain, htmlBody := w.plain, w.html if strings.TrimSpace(plain) == "" && htmlBody != "" { plain = stripHTML(htmlBody) } @@ -99,45 +114,55 @@ func senderName(h mail.Header) string { return clean(local) } -// bodies returns the first plain text and the first HTML body found. -func bodies(e *message.Entity) (plain, htmlBody string) { +// walker collects the first plain text and the first HTML body of a message. +type walker struct { + plain, html string + parts int +} + +// done reports whether the plain text is found: nothing else is needed. +func (w *walker) done() bool { return strings.TrimSpace(w.plain) != "" } + +func (w *walker) walk(e *message.Entity, depth int) { ct, _, _ := e.Header.ContentType() if ct == "" { ct = "text/plain" } if strings.HasPrefix(ct, "multipart/") { + if depth >= maxDepth { + return + } mr := e.MultipartReader() if mr == nil { - return "", "" + return } - for { + for !w.done() && w.parts < maxParts { part, err := mr.NextPart() if err == io.EOF { - break + return } if err != nil && !message.IsUnknownCharset(err) { - break + return } + w.parts++ if disp, _, _ := part.Header.ContentDisposition(); disp == "attachment" { continue } - p, h := bodies(part) - if plain == "" { - plain = p - } - if htmlBody == "" { - htmlBody = h - } + w.walk(part, depth+1) } - return plain, htmlBody + return } + // Read a text part only if it can still become the body. switch ct { case "text/plain": - plain = readLimited(e.Body) + if w.plain == "" { + w.plain = readLimited(e.Body) + } case "text/html": - htmlBody = readLimited(e.Body) + if w.html == "" { + w.html = readLimited(e.Body) + } } - return plain, htmlBody } func readLimited(r io.Reader) string { diff --git a/internal/archive/archive_test.go b/internal/archive/archive_test.go index fa96e29..c2b50b2 100644 --- a/internal/archive/archive_test.go +++ b/internal/archive/archive_test.go @@ -1,6 +1,8 @@ package archive import ( + "fmt" + "runtime" "strings" "testing" "time" @@ -212,3 +214,198 @@ func TestCapNeverSplitsAMultibyteCharacter(t *testing.T) { t.Fatalf("body len %d", len(e.Body)) } } + +// nestedBomb is a message with a text part followed by a part that nests +// multipart/mixed levels deep. +func nestedBomb(levels int) string { + var b strings.Builder + b.WriteString("From: a@example.com\r\nSubject: bomb\r\nContent-Type: multipart/mixed; boundary=b0\r\n\r\n") + b.WriteString("--b0\r\nContent-Type: text/plain\r\n\r\nhello\r\n") + for i := 0; i < levels; i++ { + fmt.Fprintf(&b, "--b%d\r\nContent-Type: multipart/mixed; boundary=b%d\r\n\r\n", i, i+1) + } + b.WriteString("--b" + fmt.Sprint(levels) + "\r\nContent-Type: text/plain\r\n\r\ndeep\r\n") + return b.String() +} + +func parseWithin(t *testing.T, raw string, limit time.Duration) Entry { + t.Helper() + type result struct { + e Entry + err error + } + done := make(chan result, 1) + go func() { + e, err := Parse([]byte(raw), now) + done <- result{e, err} + }() + select { + case r := <-done: + if r.err != nil { + t.Fatalf("parse: %v", r.err) + } + return r.e + case <-time.After(limit): + t.Fatalf("Parse still running after %v", limit) + return Entry{} + } +} + +func TestDeeplyNestedMultipartIsBounded(t *testing.T) { + e := parseWithin(t, nestedBomb(20000), 2*time.Second) + if e.Body != "hello\n" && e.Body != "hello" { + t.Fatalf("body = %q", e.Body) + } +} + +func TestNestingDeeperThanTheLimitIsIgnored(t *testing.T) { + // text/plain at depth 3 is found, at depth 40 it is not. + shallow := "Content-Type: multipart/mixed; boundary=b0\r\n\r\n" + + "--b0\r\nContent-Type: multipart/mixed; boundary=b1\r\n\r\n" + + "--b1\r\nContent-Type: multipart/mixed; boundary=b2\r\n\r\n" + + "--b2\r\nContent-Type: text/plain\r\n\r\nfound\r\n" + if e := parse(t, "Subject: s\r\n"+shallow); !strings.Contains(e.Body, "found") { + t.Fatalf("shallow body = %q", e.Body) + } + if e := parse(t, strings.Replace(nestedBomb(40), "text/plain\r\n\r\nhello", "application/octet-stream\r\n\r\nhello", 1)); strings.Contains(e.Body, "deep") { + t.Fatalf("a part 40 levels deep was read: %q", e.Body) + } +} + +func TestManyPartsAreBounded(t *testing.T) { + var b strings.Builder + b.WriteString("Subject: many\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n") + for i := 0; i < 5000; i++ { + b.WriteString("--b\r\nContent-Type: application/octet-stream\r\n\r\nx\r\n") + } + b.WriteString("--b\r\nContent-Type: text/plain\r\n\r\nlast\r\n") + e := parseWithin(t, b.String(), 2*time.Second) + if strings.Contains(e.Body, "last") { + t.Fatalf("part 5001 was read: %q", e.Body) + } + + // A text part among the first parts is still found. + b.Reset() + b.WriteString("Subject: many\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n") + for i := 0; i < 50; i++ { + b.WriteString("--b\r\nContent-Type: application/octet-stream\r\n\r\nx\r\n") + } + b.WriteString("--b\r\nContent-Type: text/plain\r\n\r\nearly\r\n") + if e := parse(t, b.String()); !strings.Contains(e.Body, "early") { + t.Fatalf("body = %q", e.Body) + } +} + +func TestOnlyTheTextPartsThatAreNeededAreRead(t *testing.T) { + // 60 text parts of 1 MiB each: the first wins and the rest are not read. + big := strings.Repeat("a", MaxBody) + var b strings.Builder + b.WriteString("Subject: big\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n") + for i := 0; i < 60; i++ { + b.WriteString("--b\r\nContent-Type: text/plain\r\n\r\n" + big + "\r\n") + } + raw := []byte(b.String()) + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + e, err := Parse(raw, now) + runtime.ReadMemStats(&after) + if err != nil { + t.Fatal(err) + } + if len(e.Body) != MaxBody { + t.Fatalf("body = %d bytes", len(e.Body)) + } + if used := after.TotalAlloc - before.TotalAlloc; used > 24<<20 { + t.Fatalf("Parse allocated %d MiB for a 1 MiB body", used>>20) + } +} + +func TestPathologicalHTMLIsNotQuadratic(t *testing.T) { + for name, frag := range map[string]string{ + "open script": "