parsimony/internal/router/auto_test.go
tgiachi 9a64146fce fix(router): deliver automated posts, stop robots from confirming subscriptions
- discardReason drops only the own-List-Id loop, Auto-Submitted: auto-replied and Precedence: auto_reply; auto-generated, auto-notified and unknown values take the normal path (membership, From, moderation)
- join, leave and confirm commands ignore (250, token kept) a null sender, any Auto-Submitted other than no, Precedence auto_reply/bulk/junk and the list's own List-Id
- tests: loop_test, new auto_test, ingest LMTP test
- README: loops and robots paragraph in the security notes
2026-11-10 11:54:08 +01:00

106 lines
3.6 KiB
Go

package router
import "testing"
// automatedMails are mails a robot sends: none of them may drive a join, a leave or a confirmation.
var automatedMails = []string{
"Auto-Submitted: auto-replied\r\n",
"Auto-Submitted: auto-generated\r\n",
"auto-submitted: Auto-Notified (vacation)\r\n",
"Auto-Submitted: something-new\r\n",
"Precedence: auto_reply\r\n",
"precedence: AUTO_REPLY\r\n",
"Precedence: bulk\r\n",
"Precedence: junk\r\n",
"List-Id: Dev <dev.example.org>\r\n",
}
func TestConfirmIgnoresAutomatedMailAndKeepsTheToken(t *testing.T) {
e := newEnv(t)
e.command("carol@x.com", "dev+join@example.org", nil)
tok := token(t, lastMail(t, e.out))
rcpt := "dev+confirm-" + tok + "@example.org"
for _, h := range automatedMails {
handled, err := e.command("carol@x.com", rcpt, withHeaders(h))
if !handled || err != nil {
t.Fatalf("%q: must be accepted and ignored: %v %v", h, handled, err)
}
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
t.Fatalf("%q: a robot must not confirm a subscription", h)
}
}
// A null sender is a robot too, whatever the headers say.
if handled, err := e.command("", rcpt, withHeaders("")); !handled || err != nil {
t.Fatalf("null sender: %v %v", handled, err)
}
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
t.Fatal("a null sender must not confirm a subscription")
}
// The token survived all of that: a human reply still confirms.
if _, err := e.command("carol@x.com", rcpt, withHeaders("Auto-Submitted: no\r\n")); err != nil {
t.Fatalf("a human reply must confirm: %v", err)
}
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok {
t.Fatal("carol must be a member after a human reply")
}
}
func TestHumanReplyConfirmsLeave(t *testing.T) {
e := newEnv(t)
e.command("alice@example.com", "dev+leave@example.org", nil)
tok := token(t, lastMail(t, e.out))
if _, err := e.command("", "dev+confirm-"+tok+"@example.org", withHeaders("")); err != nil {
t.Fatal(err)
}
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); !ok {
t.Fatal("a null sender must not confirm a leave")
}
if _, err := e.command("alice@example.com", "dev+confirm-"+tok+"@example.org", withHeaders("")); err != nil {
t.Fatal(err)
}
if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); ok {
t.Fatal("a plain reply must confirm the leave")
}
}
func TestJoinAndLeaveIgnoreAutomatedMail(t *testing.T) {
e := newEnv(t)
for _, h := range automatedMails {
for _, rcpt := range []string{"dev+join@example.org", "dev+leave@example.org"} {
sender := "carol@x.com"
if rcpt == "dev+leave@example.org" {
sender = "alice@example.com"
}
handled, err := e.command(sender, rcpt, withHeaders(h))
if !handled || err != nil {
t.Fatalf("%q %s: %v %v", h, rcpt, handled, err)
}
}
}
if len(e.out.Sent) != 0 {
t.Fatalf("no confirmation mail goes back to a robot, sent %d", len(e.out.Sent))
}
// Null sender with real headers: still nothing.
e.command("", "dev+join@example.org", withHeaders(""))
e.command("", "dev+leave@example.org", withHeaders(""))
if len(e.out.Sent) != 0 {
t.Fatal("a null sender gets no mail")
}
// A human request still gets its confirmation mail.
e.command("carol@x.com", "dev+join@example.org", withHeaders("Auto-Submitted: no\r\n"))
if len(e.out.Sent) != 1 {
t.Fatalf("a human join must be answered, sent %d", len(e.out.Sent))
}
}
func TestAnotherListsIDDoesNotBlockCommands(t *testing.T) {
e := newEnv(t)
if _, err := e.command("carol@x.com", "dev+join@example.org", withHeaders("List-Id: Ops <ops.example.org>\r\n")); err != nil {
t.Fatal(err)
}
if len(e.out.Sent) != 1 {
t.Fatal("a mail from another list's thread is a normal request")
}
}