parsimony/internal/router/loop_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

99 lines
3.6 KiB
Go

package router
import (
"errors"
"testing"
)
func withHeaders(extra string) []byte {
return []byte("From: Alice <alice@example.com>\r\n" + extra + "Subject: hi\r\nMessage-ID: <l1@x>\r\n\r\nbody\r\n")
}
func TestOwnListIDMeansALoop(t *testing.T) {
e := newEnv(t)
for _, h := range []string{
"List-Id: Dev <dev.example.org>\r\n",
"List-Id: <dev.example.org>\r\n",
"List-Id: <DEV.Example.ORG>\r\n",
"List-Id: some other <foo.example>\r\nList-Id: Dev <dev.example.org>\r\n",
} {
if _, err := e.r.Route("alice@example.com", "dev@example.org", withHeaders(h)); !errors.Is(err, ErrDiscarded) {
t.Errorf("%q: want ErrDiscarded, got %v", h, err)
}
}
// Another list's id is fine: a member may reply from a thread of another list.
if _, err := e.r.Route("alice@example.com", "dev@example.org", withHeaders("List-Id: Ops <ops.example.org>\r\n")); err != nil {
t.Fatalf("another list's id must not block a post: %v", err)
}
}
func TestAutoRepliesAreDiscarded(t *testing.T) {
e := newEnv(t)
for _, h := range []string{
"Auto-Submitted: auto-replied\r\n",
"Auto-Submitted: Auto-Replied (vacation)\r\n",
"Precedence: auto_reply\r\n",
"precedence: AUTO_REPLY\r\n",
} {
if _, err := e.r.Route("alice@example.com", "dev@example.org", withHeaders(h)); !errors.Is(err, ErrDiscarded) {
t.Errorf("%q: want ErrDiscarded, got %v", h, err)
}
}
// Machine-generated mail is not a reply: cron, CI and monitoring posts must be delivered.
for _, h := range []string{
"Auto-Submitted: no\r\n",
"Auto-Submitted: NO\r\n",
"Auto-Submitted: auto-generated\r\n",
"auto-submitted: Auto-Notified (vacation)\r\n",
"Auto-Submitted: something-new\r\n",
"Precedence: bulk\r\n",
"Precedence: list\r\n",
"",
} {
if _, err := e.r.Route("alice@example.com", "dev@example.org", withHeaders(h)); err != nil {
t.Errorf("%q must be delivered: %v", h, err)
}
}
}
func TestAutomatedPostsFollowTheNormalPath(t *testing.T) {
e := newEnv(t)
h := withHeaders("Auto-Submitted: auto-generated\r\n")
if _, err := e.r.Route("stranger@x.com", "dev@example.org", h); !errors.Is(err, ErrNotMember) {
t.Fatalf("a non-member's automated post is refused like any other: %v", err)
}
e.s.SetPolicies(e.list.ID, "moderated", "open", "public")
if _, err := e.r.Route("alice@example.com", "dev@example.org", h); !errors.Is(err, ErrHeld) {
t.Fatalf("an automated post to a moderated list is held: %v", err)
}
e.s.SetPolicies(e.list.ID, "members", "open", "public")
other := withHeaders("Auto-Submitted: auto-generated\r\n")
if _, err := e.r.Route("bob@example.com", "dev@example.org", other); !errors.Is(err, ErrHeld) {
t.Fatalf("a From header that does not match the sender still holds the post: %v", err)
}
}
func TestDiscardedMailLeavesNoTrace(t *testing.T) {
e := newEnv(t)
e.s.SetPolicies(e.list.ID, "moderated", "open", "public")
e.r.Route("alice@example.com", "dev@example.org", withHeaders("Auto-Submitted: auto-replied\r\n"))
e.r.Route("stranger@x.com", "dev@example.org", withHeaders("List-Id: <dev.example.org>\r\n"))
if held, _ := e.s.ListHeld(e.list.ID); len(held) != 0 {
t.Fatalf("held: %d", len(held))
}
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 0 {
t.Fatalf("archived: %d", total)
}
if st, _ := e.s.OutboxStats(); st.Pending != 0 {
t.Fatalf("queued: %d", st.Pending)
}
}
func TestAutoRepliesFromStrangersAreDiscardedNotBounced(t *testing.T) {
e := newEnv(t)
// A bounce for an auto-reply would start the loop we are trying to avoid.
_, err := e.r.Route("stranger@x.com", "dev@example.org", withHeaders("Auto-Submitted: auto-replied\r\n"))
if !errors.Is(err, ErrDiscarded) {
t.Fatalf("want ErrDiscarded, got %v", err)
}
}