parsimony/internal/router/loop_test.go
tgiachi ba627a50b8 fix(security): discard mail loops and automatic replies
- Drop posts that carry the list's own List-Id, Auto-Submitted other than no, or Precedence: auto_reply
- Acknowledge them to the MTA instead of refusing, so no bounce starts another loop
- Never archive, hold or queue a discarded message
2026-11-09 20:24:16 +01:00

79 lines
2.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-generated\r\n",
"auto-submitted: Auto-Notified (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)
}
}
for _, h := range []string{
"Auto-Submitted: no\r\n",
"Auto-Submitted: NO\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 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)
}
}