fix: shorten the confirmation token to 20 hex characters

name(<=30) + "+confirm-" + 20 = 59 characters, inside the 64 of a local part
(a 30-character name with the old 32-digit token made 71).

- store: CreatePending makes an 80-bit token
- tests: a 30-character list name keeps the confirm and the VERP bounce address (12-digit id) within 64,
  and the shorter token still confirms
This commit is contained in:
tgiachi 2026-11-10 11:54:28 +01:00
commit 555578bac5
2 changed files with 64 additions and 1 deletions

View file

@ -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
}
}

View file

@ -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
}