- 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.
703 lines
22 KiB
Go
703 lines
22 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.kmalloc.org/squid/parsimony/internal/outbound"
|
|
"git.kmalloc.org/squid/parsimony/internal/queue"
|
|
"git.kmalloc.org/squid/parsimony/internal/store"
|
|
)
|
|
|
|
type env struct {
|
|
r *Router
|
|
s *store.Store
|
|
out *outbound.Fake
|
|
list store.List
|
|
ctx context.Context
|
|
alice int64
|
|
}
|
|
|
|
func newEnv(t *testing.T) *env {
|
|
t.Helper()
|
|
s, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.AddMember(l.ID, "alice@example.com", "Alice")
|
|
s.AddMember(l.ID, "bob@example.com", "Bob")
|
|
ms, _ := s.Members(l.ID)
|
|
return &env{r: New(s), s: s, out: &outbound.Fake{}, list: l, ctx: context.Background(), alice: ms[0].ID}
|
|
}
|
|
|
|
// flush sends everything queued, as the background worker would.
|
|
func (e *env) flush() {
|
|
w := &queue.Worker{Store: e.s, Sender: e.out}
|
|
for {
|
|
st, err := w.RunOnce(e.ctx)
|
|
if err != nil || st == (queue.Stats{}) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (e *env) send(res Result) error {
|
|
if err := e.r.Deliver(res); err != nil {
|
|
return err
|
|
}
|
|
e.flush()
|
|
return nil
|
|
}
|
|
|
|
func (e *env) command(sender, rcpt string, data []byte) (bool, error) {
|
|
handled, err := e.r.Command(sender, rcpt, data)
|
|
e.flush()
|
|
return handled, err
|
|
}
|
|
|
|
func (e *env) release(id int64) error {
|
|
err := e.r.Release(id)
|
|
e.flush()
|
|
return err
|
|
}
|
|
|
|
func lastMail(t *testing.T, out *outbound.Fake) outbound.Sent {
|
|
t.Helper()
|
|
if len(out.Sent) == 0 {
|
|
t.Fatal("no mail was sent")
|
|
}
|
|
return out.Sent[len(out.Sent)-1]
|
|
}
|
|
|
|
func TestSendUsesVERPPerRecipient(t *testing.T) {
|
|
e := newEnv(t)
|
|
res, err := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := e.send(res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(e.out.Sent) != 2 {
|
|
t.Fatalf("want one send per recipient, got %d", len(e.out.Sent))
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, m := range e.out.Sent {
|
|
if len(m.To) != 1 {
|
|
t.Fatalf("to = %v", m.To)
|
|
}
|
|
if !strings.HasPrefix(m.From, "dev+bounce-") || !strings.HasSuffix(m.From, "@example.org") {
|
|
t.Fatalf("envelope from = %q", m.From)
|
|
}
|
|
seen[m.From] = true
|
|
}
|
|
if len(seen) != 2 {
|
|
t.Fatal("each recipient needs a distinct bounce address")
|
|
}
|
|
}
|
|
|
|
func TestDisabledMembersAreSkipped(t *testing.T) {
|
|
e := newEnv(t)
|
|
for i := 0; i < 3; i++ {
|
|
e.s.RecordHardBounce(e.alice, 3)
|
|
}
|
|
res, err := e.r.Route("bob@example.com", "dev@example.org", postFrom("Bob <bob@example.com>"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(res.Recipients) != 1 || res.Recipients[0].Address != "bob@example.com" {
|
|
t.Fatalf("recipients: %+v", res.Recipients)
|
|
}
|
|
}
|
|
|
|
func TestModeratedListHoldsPosts(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, store.PostModerated, store.JoinOpen, store.ArchivePrivate)
|
|
for _, sender := range []string{"alice@example.com", "stranger@x.com"} {
|
|
_, err := e.r.Route(sender, "dev@example.org", []byte(sample))
|
|
if !errors.Is(err, ErrHeld) {
|
|
t.Fatalf("%s: want ErrHeld, got %v", sender, err)
|
|
}
|
|
}
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
if len(held) != 2 {
|
|
t.Fatalf("held: %d", len(held))
|
|
}
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatal("nothing may be sent while held")
|
|
}
|
|
}
|
|
|
|
func TestReleaseHeld(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, store.PostModerated, store.JoinOpen, store.ArchivePrivate)
|
|
e.r.Route("stranger@x.com", "dev@example.org", []byte(sample))
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
|
|
if err := e.release(held[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(e.out.Sent) != 2 {
|
|
t.Fatalf("sent = %d", len(e.out.Sent))
|
|
}
|
|
if !strings.Contains(string(e.out.Sent[0].Data), "List-Id: Dev <dev.example.org>") {
|
|
t.Error("released mail must carry list headers")
|
|
}
|
|
if rest, _ := e.s.ListHeld(e.list.ID); len(rest) != 0 {
|
|
t.Fatal("released message must leave the queue")
|
|
}
|
|
if err := e.release(held[0].ID); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("want ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func token(t *testing.T, m outbound.Sent) string {
|
|
t.Helper()
|
|
const marker = "Reply-To: dev+confirm-"
|
|
i := strings.Index(string(m.Data), marker)
|
|
if i < 0 {
|
|
t.Fatalf("no confirm address in %q", m.Data)
|
|
}
|
|
rest := string(m.Data)[i+len(marker):]
|
|
return rest[:strings.Index(rest, "@")]
|
|
}
|
|
|
|
func TestJoinFlow(t *testing.T) {
|
|
e := newEnv(t)
|
|
handled, err := e.command("carol@x.com", "dev+join@example.org", nil)
|
|
if !handled || err != nil {
|
|
t.Fatalf("join: %v %v", handled, err)
|
|
}
|
|
m := lastMail(t, e.out)
|
|
if len(m.To) != 1 || m.To[0] != "carol@x.com" {
|
|
t.Fatalf("confirmation must go to the requester: %+v", m.To)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
|
|
t.Fatal("not a member before confirming")
|
|
}
|
|
tok := token(t, m)
|
|
|
|
handled, err = e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil)
|
|
if !handled || err != nil {
|
|
t.Fatalf("confirm: %v %v", handled, err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok {
|
|
t.Fatal("carol must be a member after confirming")
|
|
}
|
|
_, err = e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil)
|
|
if !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("reused token: want ErrBadToken, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestJoinRateLimitedAndSilentForMembers(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("carol@x.com", "dev+join@example.org", nil)
|
|
e.command("carol@x.com", "dev+join@example.org", nil)
|
|
if len(e.out.Sent) != 1 {
|
|
t.Fatalf("second request must not send mail, sent %d", len(e.out.Sent))
|
|
}
|
|
e.command("alice@example.com", "dev+join@example.org", nil)
|
|
if len(e.out.Sent) != 1 {
|
|
t.Fatal("existing members get no mail")
|
|
}
|
|
e.command("", "dev+join@example.org", nil)
|
|
if len(e.out.Sent) != 1 {
|
|
t.Fatal("empty sender is ignored")
|
|
}
|
|
}
|
|
|
|
func TestJoinClosed(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate)
|
|
_, err := e.command("carol@x.com", "dev+join@example.org", nil)
|
|
if !errors.Is(err, ErrJoinClosed) {
|
|
t.Fatalf("want ErrJoinClosed, got %v", err)
|
|
}
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatal("no mail for closed lists")
|
|
}
|
|
}
|
|
|
|
func TestConfirmAfterListClosed(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("carol@x.com", "dev+join@example.org", nil)
|
|
tok := token(t, lastMail(t, e.out))
|
|
e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate)
|
|
_, err := e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil)
|
|
if !errors.Is(err, ErrJoinClosed) {
|
|
t.Fatalf("want ErrJoinClosed, got %v", err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
|
|
t.Fatal("must not join a closed list")
|
|
}
|
|
// The refusal did not use the token up: once the list is open again the same mail works.
|
|
e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinOpen, store.ArchivePrivate)
|
|
if _, err := e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil); err != nil {
|
|
t.Fatalf("the token must survive a refusal: %v", err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok {
|
|
t.Fatal("carol must join once the list is open")
|
|
}
|
|
}
|
|
|
|
func TestConfirmWithTokenOfAnotherListKeepsItUsable(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.CreateList("ops", "example.org", "Ops")
|
|
e.command("carol@x.com", "dev+join@example.org", nil)
|
|
tok := token(t, lastMail(t, e.out))
|
|
if _, err := e.command("carol@x.com", "ops+confirm-"+tok+"@example.org", nil); !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
if _, err := e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil); err != nil {
|
|
t.Fatalf("the right list still accepts it: %v", err)
|
|
}
|
|
}
|
|
|
|
// A member disabled by bounces must be able to rejoin: +join sends the confirmation, confirming
|
|
// re-enables the subscription and clears the bounce state.
|
|
func TestDisabledMemberCanRejoin(t *testing.T) {
|
|
e := newEnv(t)
|
|
for i := 0; i < store.BounceThreshold; i++ {
|
|
e.s.RecordHardBounce(e.alice, store.BounceThreshold)
|
|
}
|
|
if m, _ := e.s.MemberByID(e.alice); !m.Disabled {
|
|
t.Fatal("setup: alice must be disabled")
|
|
}
|
|
n := len(e.out.Sent)
|
|
if _, err := e.command("alice@example.com", "dev+join@example.org", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(e.out.Sent) != n+1 {
|
|
t.Fatalf("a disabled member must get the confirmation, sent %d new mails", len(e.out.Sent)-n)
|
|
}
|
|
m := lastMail(t, e.out)
|
|
if len(m.To) != 1 || m.To[0] != "alice@example.com" {
|
|
t.Fatalf("to = %v", m.To)
|
|
}
|
|
if got, _ := e.s.MemberByID(e.alice); !got.Disabled {
|
|
t.Fatal("not re-enabled before confirming")
|
|
}
|
|
if _, err := e.command("alice@example.com", "dev+confirm-"+token(t, m)+"@example.org", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := e.s.MemberByID(e.alice)
|
|
if err != nil || got.Disabled || got.BounceCount != 0 || got.ID != e.alice {
|
|
t.Fatalf("after confirming: %+v %v", got, err)
|
|
}
|
|
// She receives list mail again.
|
|
res, err := e.r.Route("bob@example.com", "dev@example.org", postFrom("Bob <bob@example.com>"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := false
|
|
for _, r := range res.Recipients {
|
|
seen = seen || r.Address == "alice@example.com"
|
|
}
|
|
if !seen {
|
|
t.Fatalf("alice must receive posts again: %+v", res.Recipients)
|
|
}
|
|
}
|
|
|
|
// An active member still gets nothing: no mail that would reveal membership.
|
|
func TestJoinStaysSilentForActiveMembers(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("alice@example.com", "dev+join@example.org", nil)
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatal("active members get no mail")
|
|
}
|
|
}
|
|
|
|
func TestLeaveFlow(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("alice@example.com", "dev+leave@example.org", nil)
|
|
m := lastMail(t, e.out)
|
|
if m.To[0] != "alice@example.com" {
|
|
t.Fatalf("to = %v", m.To)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); !ok {
|
|
t.Fatal("still a member before confirming")
|
|
}
|
|
e.command("alice@example.com", "dev+confirm-"+token(t, m)+"@example.org", nil)
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); ok {
|
|
t.Fatal("alice must be gone")
|
|
}
|
|
// Leaving when not a member sends nothing.
|
|
n := len(e.out.Sent)
|
|
e.command("zed@x.com", "dev+leave@example.org", nil)
|
|
if len(e.out.Sent) != n {
|
|
t.Fatal("non-members get no mail")
|
|
}
|
|
}
|
|
|
|
func TestConfirmTokenFromOtherList(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.CreateList("ops", "example.org", "Ops")
|
|
e.command("carol@x.com", "dev+join@example.org", nil)
|
|
tok := token(t, lastMail(t, e.out))
|
|
_, err := e.command("carol@x.com", "ops+confirm-"+tok+"@example.org", nil)
|
|
if !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCommandNotHandledForPlainAddress(t *testing.T) {
|
|
e := newEnv(t)
|
|
handled, err := e.command("a@x.com", "dev@example.org", nil)
|
|
if handled || err != nil {
|
|
t.Fatalf("plain address: %v %v", handled, err)
|
|
}
|
|
}
|
|
|
|
func TestUnknownCommandAndList(t *testing.T) {
|
|
e := newEnv(t)
|
|
for _, rcpt := range []string{"dev+bogus@example.org", "nope+join@example.org"} {
|
|
handled, err := e.command("a@x.com", rcpt, nil)
|
|
if !handled || !errors.Is(err, ErrUnknownList) {
|
|
t.Errorf("%s: %v %v", rcpt, handled, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func bounceMsg(status string) []byte {
|
|
return dsn("failed", status)
|
|
}
|
|
|
|
func TestHardBouncesDisableMember(t *testing.T) {
|
|
e := newEnv(t)
|
|
addr := e.r.bounceAddress(e.list, e.alice)
|
|
for i := 0; i < 3; i++ {
|
|
handled, err := e.command("", addr, bounceMsg("5.1.1"))
|
|
if !handled || err != nil {
|
|
t.Fatalf("bounce: %v %v", handled, err)
|
|
}
|
|
}
|
|
m, _ := e.s.MemberByID(e.alice)
|
|
if !m.Disabled || m.BounceCount != 3 {
|
|
t.Fatalf("member: %+v", m)
|
|
}
|
|
}
|
|
|
|
func TestSoftBounceIgnored(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("", e.r.bounceAddress(e.list, e.alice), bounceMsg("4.2.2"))
|
|
if m, _ := e.s.MemberByID(e.alice); m.BounceCount != 0 {
|
|
t.Fatalf("soft bounce counted: %+v", m)
|
|
}
|
|
}
|
|
|
|
func TestForgedBounceRejected(t *testing.T) {
|
|
e := newEnv(t)
|
|
addr := fmt.Sprintf("dev+bounce-%d-000000000000@example.org", e.alice)
|
|
_, err := e.command("", addr, bounceMsg("5.1.1"))
|
|
if !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
if m, _ := e.s.MemberByID(e.alice); m.BounceCount != 0 {
|
|
t.Fatal("forged bounce must not count")
|
|
}
|
|
}
|
|
|
|
func TestBounceFromOtherListRejected(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.CreateList("ops", "example.org", "Ops")
|
|
ops, _ := e.s.FindList("ops@example.org")
|
|
addr := e.r.bounceAddress(ops, e.alice) // valid MAC but alice is not in ops
|
|
_, err := e.command("", addr, bounceMsg("5.1.1"))
|
|
if !errors.Is(err, ErrBadToken) {
|
|
t.Fatalf("want ErrBadToken, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSystemBounceAddressDiscarded(t *testing.T) {
|
|
e := newEnv(t)
|
|
handled, err := e.command("", "dev+bounce@example.org", bounceMsg("5.1.1"))
|
|
if !handled || err != nil {
|
|
t.Fatalf("%v %v", handled, err)
|
|
}
|
|
}
|
|
|
|
func TestDeliverQueuesOneEntryPerRecipientAtomically(t *testing.T) {
|
|
e := newEnv(t)
|
|
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
|
|
if err := e.r.Deliver(res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatal("Deliver only queues; the worker sends")
|
|
}
|
|
due, _ := e.s.DueOutbox(10)
|
|
if len(due) != 2 {
|
|
t.Fatalf("queued = %d", len(due))
|
|
}
|
|
if due[0].MsgID != due[1].MsgID {
|
|
t.Fatal("recipients must share one stored body")
|
|
}
|
|
for _, d := range due {
|
|
if d.MemberID == 0 || !strings.HasPrefix(d.From, "dev+bounce-") || !strings.Contains(string(d.Prefix), "List-Unsubscribe:") {
|
|
t.Fatalf("item: %+v", d)
|
|
}
|
|
}
|
|
body, _ := e.s.OutboxData(due[0].MsgID)
|
|
if !strings.Contains(string(body), "List-Id:") || strings.Contains(string(body), "List-Unsubscribe:") {
|
|
t.Fatalf("shared body has the common headers only: %q", body)
|
|
}
|
|
}
|
|
|
|
func TestDeliverFailsCleanlyWhenTheQueueIsUnavailable(t *testing.T) {
|
|
e := newEnv(t)
|
|
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
|
|
e.s.Close()
|
|
if err := e.r.Deliver(res); err == nil {
|
|
t.Fatal("want an error so the MTA retries")
|
|
}
|
|
}
|
|
|
|
func TestDeliverWithoutRecipientsQueuesNothing(t *testing.T) {
|
|
e := newEnv(t)
|
|
woke := 0
|
|
e.r.OnEnqueue = func() { woke++ }
|
|
if err := e.r.Deliver(Result{List: e.list, Data: []byte("x")}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st, _ := e.s.OutboxStats(); st.Pending != 0 || woke != 0 {
|
|
t.Fatalf("pending=%d woke=%d", st.Pending, woke)
|
|
}
|
|
}
|
|
|
|
func TestQueuedMailWakesTheSender(t *testing.T) {
|
|
e := newEnv(t)
|
|
woke := 0
|
|
e.r.OnEnqueue = func() { woke++ }
|
|
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
|
|
e.r.Deliver(res)
|
|
e.r.Command("carol@x.com", "dev+join@example.org", nil)
|
|
if woke != 2 {
|
|
t.Fatalf("OnEnqueue called %d times, want once per queued batch (2)", woke)
|
|
}
|
|
}
|
|
|
|
func TestConfirmationMailIsQueuedAsSystemMail(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.r.Command("carol@x.com", "dev+join@example.org", nil)
|
|
due, _ := e.s.DueOutbox(10)
|
|
if len(due) != 1 || due[0].Rcpt != "carol@x.com" || due[0].MemberID != 0 || due[0].From != "dev+bounce@example.org" {
|
|
t.Fatalf("queue: %+v", due)
|
|
}
|
|
}
|
|
|
|
func TestIncomingListHeadersAreStripped(t *testing.T) {
|
|
r := setup(t) // no BaseURL: One-Click must not survive from the outside
|
|
msg := "From: Alice <alice@example.com>\r\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n" +
|
|
"List-Unsubscribe: <https://evil.example/u>\r\nList-Post: <mailto:evil@x.com>\r\nPrecedence: bulk\r\n\r\nx\r\n"
|
|
res, err := r.Route("alice@example.com", "dev@example.org", []byte(msg))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := header(t, res.Data)
|
|
if h.Get("List-Unsubscribe-Post") != "" {
|
|
t.Error("spoofed List-Unsubscribe-Post survived")
|
|
}
|
|
if strings.Contains(h.Get("List-Unsubscribe"), "evil") || strings.Contains(h.Get("List-Post"), "evil") {
|
|
t.Errorf("spoofed list headers survived: %v", h)
|
|
}
|
|
if n := len(h["Precedence"]); n != 1 || h.Get("Precedence") != "list" {
|
|
t.Errorf("Precedence = %v", h["Precedence"])
|
|
}
|
|
}
|
|
|
|
// A local part is at most 64 characters (RFC 5321): with the longest list name the API accepts, the
|
|
// confirm and the VERP bounce addresses must still fit.
|
|
func TestCommandAddressesFitTheLocalPartLimitForALongListName(t *testing.T) {
|
|
e := newEnv(t)
|
|
name := strings.Repeat("a", 30)
|
|
l, err := e.s.CreateList(name, "example.org", "Long")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.s.AddMember(l.ID, "alice@example.com", "Alice")
|
|
|
|
if _, err := e.command("carol@x.com", name+"+join@example.org", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := lastMail(t, e.out)
|
|
const marker = "Reply-To: "
|
|
i := strings.Index(string(m.Data), marker)
|
|
if i < 0 {
|
|
t.Fatalf("no Reply-To in %q", m.Data)
|
|
}
|
|
reply := strings.SplitN(string(m.Data)[i+len(marker):], "\r\n", 2)[0]
|
|
local, _, _ := strings.Cut(reply, "@")
|
|
if len(local) > 64 {
|
|
t.Fatalf("confirm local part is %d characters: %s", len(local), local)
|
|
}
|
|
tok := strings.TrimPrefix(local, name+"+confirm-")
|
|
if len(tok) != 20 {
|
|
t.Fatalf("token %q has %d characters, want 20 hex digits", tok, len(tok))
|
|
}
|
|
// The shortened token still confirms.
|
|
if _, err := e.command("carol@x.com", reply, nil); err != nil {
|
|
t.Fatalf("confirm: %v", err)
|
|
}
|
|
if ok, _ := e.s.IsMember(l.ID, "carol@x.com"); !ok {
|
|
t.Fatal("carol must be a member")
|
|
}
|
|
|
|
// VERP: the member id can have up to 12 digits.
|
|
for _, id := range []int64{1, 123456789012} {
|
|
addr := e.r.bounceAddress(l, id)
|
|
local, _, _ := strings.Cut(addr, "@")
|
|
if len(local) > 64 {
|
|
t.Fatalf("bounce local part for id %d is %d characters: %s", id, len(local), local)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConfirmTokensAreUnpredictable(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
seen := map[string]bool{}
|
|
for i := 0; i < 50; i++ {
|
|
tok, created, err := s.CreatePending(l.ID, fmt.Sprintf("u%d@x.com", i), store.ActionJoin)
|
|
if err != nil || !created || len(tok) != 20 || seen[tok] {
|
|
t.Fatalf("token %q created=%v err=%v", tok, created, err)
|
|
}
|
|
seen[tok] = true
|
|
}
|
|
}
|
|
|
|
// Closing a list to new members must not stop a member from leaving.
|
|
func TestLeaveConfirmationWorksOnAClosedList(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.command("alice@example.com", "dev+leave@example.org", nil)
|
|
tok := token(t, lastMail(t, e.out))
|
|
e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate)
|
|
if _, err := e.command("alice@example.com", "dev+confirm-"+tok+"@example.org", nil); err != nil {
|
|
t.Fatalf("leave on a closed list: %v", err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); ok {
|
|
t.Fatal("alice must be gone")
|
|
}
|
|
}
|
|
|
|
func TestIncomingListManagementAndAuthenticationHeadersAreStripped(t *testing.T) {
|
|
r := setup(t)
|
|
msg := "From: Alice <alice@example.com>\r\n" +
|
|
"List-Help: <mailto:evil@x.com?subject=help>\r\nList-Archive: <https://evil.example/a>\r\n" +
|
|
"List-Owner: <mailto:evil@x.com>\r\nList-Subscribe: <https://evil.example/s>\r\n" +
|
|
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=x; h=from; bh=aa; b=bb\r\n" +
|
|
"Authentication-Results: mx.example.com; dkim=pass\r\n" +
|
|
"ARC-Seal: i=1; a=rsa-sha256; cv=none; d=example.com; s=x; b=cc\r\n" +
|
|
"ARC-Message-Signature: i=1; a=rsa-sha256; d=example.com; s=x; b=dd\r\n" +
|
|
"ARC-Authentication-Results: i=1; mx.example.com; dkim=pass\r\n" +
|
|
"Subject: hi\r\n\r\nx\r\n"
|
|
res, err := r.Route("alice@example.com", "dev@example.org", []byte(msg))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := header(t, res.Data)
|
|
for _, k := range []string{"List-Help", "List-Archive", "List-Owner", "List-Subscribe", "DKIM-Signature",
|
|
"Authentication-Results", "ARC-Seal", "ARC-Message-Signature", "ARC-Authentication-Results"} {
|
|
if v := h.Get(k); v != "" { // Get canonicalizes the key
|
|
t.Errorf("%s survived: %v", k, v)
|
|
}
|
|
}
|
|
if h.Get("Subject") != "hi" {
|
|
t.Errorf("unrelated headers must stay: %v", h)
|
|
}
|
|
}
|
|
|
|
func TestDroppingARCHeadersKeepsSimilarlyNamedOnes(t *testing.T) {
|
|
r := setup(t)
|
|
msg := "From: Alice <alice@example.com>\r\nARC-Seal: i=1\r\nArcade: keep\r\nX-ARC-Note: keep\r\nARC-Seal: i=2\r\n\r\nx\r\n"
|
|
res, err := r.Route("alice@example.com", "dev@example.org", []byte(msg))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := header(t, res.Data)
|
|
if v := h["Arc-Seal"]; len(v) != 0 {
|
|
t.Errorf("every ARC-Seal must go: %v", v)
|
|
}
|
|
if h.Get("Arcade") != "keep" || h.Get("X-Arc-Note") != "keep" {
|
|
t.Errorf("only the ARC- prefix is dropped: %v", h)
|
|
}
|
|
}
|
|
|
|
func TestRequestJoinQueuesTheConfirmationMail(t *testing.T) {
|
|
e := newEnv(t)
|
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.flush()
|
|
m := lastMail(t, e.out)
|
|
if len(m.To) != 1 || m.To[0] != "carol@x.com" {
|
|
t.Fatalf("confirmation must go to the requester: %+v", m.To)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
|
|
t.Fatal("not a member before confirming")
|
|
}
|
|
// The mail is the usual one: replying to it confirms.
|
|
if _, err := e.command("carol@x.com", "dev+confirm-"+token(t, m)+"@example.org", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok {
|
|
t.Fatal("carol must be a member after confirming")
|
|
}
|
|
}
|
|
|
|
// Nothing may tell the caller whether the list exists, is closed, or the address is known.
|
|
func TestRequestJoinRevealsNothing(t *testing.T) {
|
|
e := newEnv(t)
|
|
closed, _ := e.s.CreateList("closed", "example.org", "Closed")
|
|
e.s.SetPolicies(closed.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate)
|
|
for _, c := range []struct{ name, list, addr string }{
|
|
{"unknown list", "nope@example.org", "carol@x.com"},
|
|
{"closed list", "closed@example.org", "carol@x.com"},
|
|
{"active member", "dev@example.org", "alice@example.com"},
|
|
} {
|
|
if err := e.r.RequestJoin(c.list, c.addr); err != nil {
|
|
t.Errorf("%s: want nil, got %v", c.name, err)
|
|
}
|
|
}
|
|
e.flush()
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatalf("no mail for these cases, sent %d", len(e.out.Sent))
|
|
}
|
|
// Asking twice is rate limited by the store: still nil, still one mail.
|
|
e.r.RequestJoin("dev@example.org", "carol@x.com")
|
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err != nil {
|
|
t.Fatalf("rate limited: want nil, got %v", err)
|
|
}
|
|
e.flush()
|
|
if len(e.out.Sent) != 1 {
|
|
t.Fatalf("one mail expected, sent %d", len(e.out.Sent))
|
|
}
|
|
}
|
|
|
|
func TestRequestJoinLetsABounceDisabledMemberRejoin(t *testing.T) {
|
|
e := newEnv(t)
|
|
for i := 0; i < store.BounceThreshold; i++ {
|
|
e.s.RecordHardBounce(e.alice, store.BounceThreshold)
|
|
}
|
|
if err := e.r.RequestJoin("dev@example.org", "alice@example.com"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.flush()
|
|
if m := lastMail(t, e.out); len(m.To) != 1 || m.To[0] != "alice@example.com" {
|
|
t.Fatalf("to = %v", m.To)
|
|
}
|
|
}
|
|
|
|
func TestRequestJoinReportsAnInternalFailure(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.Close()
|
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err == nil {
|
|
t.Fatal("a database failure must be reported")
|
|
}
|
|
}
|