diff --git a/internal/router/m2_test.go b/internal/router/m2_test.go index 669a6e6..5d934cb 100644 --- a/internal/router/m2_test.go +++ b/internal/router/m2_test.go @@ -510,3 +510,64 @@ func TestIncomingListHeadersAreStripped(t *testing.T) { 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 + } +} diff --git a/internal/store/store.go b/internal/store/store.go index 832aa38..873ab92 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -699,7 +699,9 @@ func (s *Store) CreatePending(listID int64, addr, action string) (token string, if err = s.db.QueryRow(`SELECT COUNT(*) FROM pending WHERE created_at>?`, since).Scan(&n); err != nil || n >= maxPendingPerHour { return "", false, err } - token, err = randomHex(16) + // 80 bits: unguessable within the 72 h life of a token, and short enough that + // name(<=30)+"+confirm-"+token fits the 64 characters of a local part. + token, err = randomHex(10) if err != nil { return "", false, err }