fix(security): cap confirmation mail per address and in total

This commit is contained in:
tgiachi 2026-11-09 20:25:53 +01:00
commit bad3c9bc10
2 changed files with 63 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package store
import (
"database/sql"
"errors"
"fmt"
"path/filepath"
"testing"
"time"
@ -219,3 +220,52 @@ func TestHoldingAnEmptyMessage(t *testing.T) {
t.Fatalf("%+v %v", h, err)
}
}
func TestConfirmationMailIsCappedPerAddressAcrossLists(t *testing.T) {
s := newTestStore(t)
now := time.Unix(1_000_000, 0)
s.Now = func() time.Time { return now }
var lists []List
for _, n := range []string{"a", "b", "c", "d", "e"} {
l, _ := s.CreateList(n, "example.org", n)
lists = append(lists, l)
}
// A third party can name a victim's address on every list: only a few mails may reach it per hour.
for i := 0; i < maxPendingPerAddress; i++ {
if _, created, _ := s.CreatePending(lists[i].ID, "victim@x.com", "join"); !created {
t.Fatalf("request %d must be allowed", i+1)
}
}
if _, created, _ := s.CreatePending(lists[maxPendingPerAddress].ID, "Victim@X.com", "join"); created {
t.Fatal("another list, same address, same hour: refused")
}
if _, created, _ := s.CreatePending(lists[0].ID, "victim@x.com", "leave"); created {
t.Fatal("a leave request counts as well")
}
if _, created, _ := s.CreatePending(lists[0].ID, "someone-else@x.com", "join"); !created {
t.Fatal("other addresses are not affected")
}
now = now.Add(time.Hour + time.Second)
if _, created, _ := s.CreatePending(lists[maxPendingPerAddress].ID, "victim@x.com", "join"); !created {
t.Fatal("the allowance returns after an hour")
}
}
func TestConfirmationMailHasAGlobalCap(t *testing.T) {
s := newTestStore(t)
now := time.Unix(1_000_000, 0)
s.Now = func() time.Time { return now }
l, _ := s.CreateList("dev", "example.org", "Dev")
for i := 0; i < maxPendingPerHour; i++ {
if _, created, err := s.CreatePending(l.ID, fmt.Sprintf("u%d@x.com", i), "join"); err != nil || !created {
t.Fatalf("request %d: %v %v", i+1, created, err)
}
}
if _, created, _ := s.CreatePending(l.ID, "one-more@x.com", "join"); created {
t.Fatal("the daemon must not become a confirmation-mail cannon")
}
now = now.Add(time.Hour + time.Second)
if _, created, _ := s.CreatePending(l.ID, "one-more@x.com", "join"); !created {
t.Fatal("the global allowance returns after an hour")
}
}

View file

@ -47,6 +47,12 @@ const BounceThreshold = 3
const (
pendingTTL = 72 * time.Hour
pendingRateLimit = time.Hour
// Caps on confirmation mail, per hour. Anyone can name any address in a join request, so the
// daemon must not mail one victim many times (maxPendingPerAddress, across all lists) or send
// an unbounded amount in total (maxPendingPerHour).
maxPendingPerAddress = 3
maxPendingPerHour = 300
)
// List is a mailing list.
@ -519,6 +525,13 @@ func (s *Store) CreatePending(listID int64, addr, action string) (token string,
if err != nil || n > 0 {
return "", false, err
}
since := now.Add(-pendingRateLimit).Unix()
if err = s.db.QueryRow(`SELECT COUNT(*) FROM pending WHERE address=? AND created_at>?`, addr, since).Scan(&n); err != nil || n >= maxPendingPerAddress {
return "", false, err
}
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)
if err != nil {
return "", false, err