- Rename the Go module from codeberg.org/squidd/parsimony to git.kmalloc.org/squid/parsimony in go.mod and every import. - Fix .gitignore: the bare `parsimony` pattern also ignored the cmd/parsimony folder, so the main package was never committed and a clean checkout could not be built. Anchor it to the root (`/parsimony`) and add cmd/parsimony.
221 lines
7.2 KiB
Go
221 lines
7.2 KiB
Go
package router
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.kmalloc.org/squid/parsimony/internal/store"
|
|
)
|
|
|
|
const post = "From: Alice Smith <alice@example.com>\r\nTo: dev@example.org\r\nSubject: Hello list\r\n" +
|
|
"Message-ID: <hello@example.com>\r\nContent-Type: text/plain\r\n\r\nthe body\r\n"
|
|
|
|
func TestAcceptedPostIsArchivedFromTheOriginal(t *testing.T) {
|
|
e := newEnv(t)
|
|
if _, err := e.r.Route("alice@example.com", "dev@example.org", []byte(post)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, total, _ := e.s.ListMessages(e.list.ID, 10, 0)
|
|
if total != 1 {
|
|
t.Fatalf("archived = %d", total)
|
|
}
|
|
m, _ := e.s.GetMessage(ms[0].ID)
|
|
if m.Subject != "Hello list" || m.Body != "the body\n" || m.MessageID != "<hello@example.com>" {
|
|
t.Fatalf("message: %+v", m)
|
|
}
|
|
if m.Sender != "Alice Smith" {
|
|
t.Fatalf("sender must be the original author, not the list rewrite: %q", m.Sender)
|
|
}
|
|
}
|
|
|
|
func TestArchivePolicyOffStoresNothing(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinOpen, store.ArchiveOff)
|
|
e.r.Route("alice@example.com", "dev@example.org", []byte(post))
|
|
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 0 {
|
|
t.Fatalf("archived = %d", total)
|
|
}
|
|
}
|
|
|
|
func TestRetryDoesNotDuplicateTheArchive(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.r.Route("alice@example.com", "dev@example.org", []byte(post))
|
|
e.r.Route("alice@example.com", "dev@example.org", []byte(post))
|
|
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 1 {
|
|
t.Fatalf("archived = %d", total)
|
|
}
|
|
}
|
|
|
|
func TestRejectedAndHeldPostsAreNotArchived(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.r.Route("mallory@evil.com", "dev@example.org", []byte(post)) // rejected
|
|
e.s.SetPolicies(e.list.ID, store.PostModerated, store.JoinOpen, store.ArchivePrivate)
|
|
e.r.Route("alice@example.com", "dev@example.org", []byte(post)) // held
|
|
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 0 {
|
|
t.Fatalf("archived = %d", total)
|
|
}
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
if err := e.release(held[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 1 {
|
|
t.Fatalf("released post must be archived, got %d", total)
|
|
}
|
|
}
|
|
|
|
func TestAPostWithoutHeadersIsHeldNotDelivered(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, err := e.r.Route("alice@example.com", "dev@example.org", []byte("\r\nno headers at all\r\n"))
|
|
if !errors.Is(err, ErrHeld) {
|
|
t.Fatalf("a message without a From header is for the admin to judge: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAnArchiveFailureNeverBreaksDelivery(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, "members", "open", "private")
|
|
// archive() is best effort: a message the archive cannot read is logged and skipped.
|
|
e.r.archive(e.list, []byte("\r\nno headers at all\r\n"))
|
|
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 0 {
|
|
t.Fatalf("nothing archived: %d", total)
|
|
}
|
|
}
|
|
|
|
var unsubURL = regexp.MustCompile(`<https://lists\.example\.org/unsubscribe/([0-9]+-[0-9a-f]{12})>`)
|
|
|
|
func sendPost(t *testing.T, e *env) {
|
|
t.Helper()
|
|
res, err := e.r.Route("alice@example.com", "dev@example.org", []byte(post))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := e.send(res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestEachRecipientGetsItsOwnUnsubscribeLink(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.r.BaseURL = "https://lists.example.org"
|
|
sendPost(t, e)
|
|
tokens := map[string]bool{}
|
|
for _, m := range e.out.Sent {
|
|
h := header(t, m.Data)
|
|
lu := h.Get("List-Unsubscribe")
|
|
sub := unsubURL.FindStringSubmatch(lu)
|
|
if sub == nil || !strings.Contains(lu, "<mailto:dev+leave@example.org>") {
|
|
t.Fatalf("List-Unsubscribe = %q", lu)
|
|
}
|
|
tokens[sub[1]] = true
|
|
if h.Get("List-Unsubscribe-Post") != "List-Unsubscribe=One-Click" {
|
|
t.Errorf("List-Unsubscribe-Post = %q", h.Get("List-Unsubscribe-Post"))
|
|
}
|
|
if n := len(h["List-Unsubscribe"]); n != 1 {
|
|
t.Errorf("want one List-Unsubscribe header, got %d", n)
|
|
}
|
|
if h.Get("List-Id") == "" || !strings.HasSuffix(string(m.Data), "\r\n\r\nthe body\r\n") {
|
|
t.Errorf("rest of the message was damaged: %q", m.Data)
|
|
}
|
|
}
|
|
if len(tokens) != 2 {
|
|
t.Fatalf("each recipient needs a distinct link, got %v", tokens)
|
|
}
|
|
}
|
|
|
|
func TestNoOneClickWithoutBaseURL(t *testing.T) {
|
|
e := newEnv(t)
|
|
sendPost(t, e)
|
|
h := header(t, e.out.Sent[0].Data)
|
|
if h.Get("List-Unsubscribe-Post") != "" {
|
|
t.Error("one-click needs a base URL")
|
|
}
|
|
if got := h.Get("List-Unsubscribe"); got != "<mailto:dev+leave@example.org>" {
|
|
t.Errorf("List-Unsubscribe = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestSpoofedUnsubscribeHeadersNeverReachRecipients(t *testing.T) {
|
|
e := newEnv(t)
|
|
msg := "From: Alice <alice@example.com>\r\nList-Unsubscribe: <https://evil.example/u>\r\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n\r\nx\r\n"
|
|
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(msg))
|
|
e.send(res)
|
|
for _, m := range e.out.Sent {
|
|
h := header(t, m.Data)
|
|
if strings.Contains(h.Get("List-Unsubscribe"), "evil") || h.Get("List-Unsubscribe-Post") != "" {
|
|
t.Fatalf("spoofed header survived: %v", h)
|
|
}
|
|
}
|
|
}
|
|
|
|
func tokenFor(t *testing.T, e *env, memberID int64) string {
|
|
t.Helper()
|
|
e.r.BaseURL = "https://lists.example.org"
|
|
return e.r.UnsubscribeToken(memberID)
|
|
}
|
|
|
|
func TestUnsubscribeToken(t *testing.T) {
|
|
e := newEnv(t)
|
|
tok := tokenFor(t, e, e.alice)
|
|
l, err := e.r.CheckUnsubscribe(tok)
|
|
if err != nil || l.ID != e.list.ID {
|
|
t.Fatalf("check: %+v %v", l, err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); !ok {
|
|
t.Fatal("checking must not unsubscribe")
|
|
}
|
|
if err := e.r.Unsubscribe(tok); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); ok {
|
|
t.Fatal("alice must be gone")
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "bob@example.com"); !ok {
|
|
t.Fatal("bob must stay")
|
|
}
|
|
// Mail clients may repeat the request.
|
|
if err := e.r.Unsubscribe(tok); err != nil {
|
|
t.Fatalf("repeat must succeed: %v", err)
|
|
}
|
|
if _, err := e.r.CheckUnsubscribe(tok); !errors.Is(err, ErrGone) {
|
|
t.Fatalf("want ErrGone, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnsubscribeRejectsForgedTokens(t *testing.T) {
|
|
e := newEnv(t)
|
|
for _, tok := range []string{"", "x", "1", "1-", "-1", fmt.Sprintf("%d-000000000000", e.alice), "abc-123456789012",
|
|
fmt.Sprintf("%d-%s", e.alice+1, strings.TrimPrefix(tokenFor(t, e, e.alice), fmt.Sprintf("%d-", e.alice)))} {
|
|
if err := e.r.Unsubscribe(tok); !errors.Is(err, ErrBadToken) {
|
|
t.Errorf("token %q: want ErrBadToken, got %v", tok, err)
|
|
}
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); !ok {
|
|
t.Fatal("forged tokens must not unsubscribe anyone")
|
|
}
|
|
}
|
|
|
|
func TestBounceAndUnsubscribeTokensAreNotInterchangeable(t *testing.T) {
|
|
e := newEnv(t)
|
|
bounceMAC := e.r.mac("bounce", e.alice)
|
|
unsubMAC := e.r.mac("unsub", e.alice)
|
|
if bounceMAC == unsubMAC {
|
|
t.Fatal("MACs must be bound to their purpose")
|
|
}
|
|
if err := e.r.Unsubscribe(fmt.Sprintf("%d-%s", e.alice, bounceMAC)); !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("a bounce MAC must not unsubscribe: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnsubscribeRejectsMemberIDZero(t *testing.T) {
|
|
e := newEnv(t)
|
|
tok := fmt.Sprintf("0-%s", e.r.mac("unsub", 0)) // correctly signed, but not a real member id
|
|
if err := e.r.Unsubscribe(tok); !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
if _, err := e.r.CheckUnsubscribe(tok); !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
}
|