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
This commit is contained in:
parent
eb9e915805
commit
ba627a50b8
4 changed files with 134 additions and 1 deletions
|
|
@ -86,7 +86,7 @@ func (s *session) deliver(rcpt string, data []byte) error {
|
|||
// status turns a router result into an LMTP reply.
|
||||
func (s *session) status(rcpt string, err error) error {
|
||||
switch {
|
||||
case err == nil, errors.Is(err, router.ErrHeld):
|
||||
case err == nil, errors.Is(err, router.ErrHeld), errors.Is(err, router.ErrDiscarded):
|
||||
return nil
|
||||
case errors.Is(err, router.ErrUnknownList):
|
||||
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such list"}
|
||||
|
|
|
|||
|
|
@ -252,3 +252,26 @@ func TestModeratedPostIsAcceptedButNotQueued(t *testing.T) {
|
|||
t.Fatalf("held = %d", len(h))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoReplyAndLoopsAreAcceptedButNotDelivered(t *testing.T) {
|
||||
st := newStack(t, nil)
|
||||
for _, h := range []string{"Auto-Submitted: auto-replied\r\n", "List-Id: Dev <dev.example.org>\r\n"} {
|
||||
conn, err := net.Dial("tcp", st.addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := smtp.NewClientLMTP(conn)
|
||||
c.Hello("localhost")
|
||||
c.Mail("alice@example.com", nil)
|
||||
c.Rcpt("dev@example.org", nil)
|
||||
w, _ := c.Data()
|
||||
w.Write([]byte("From: Alice <alice@example.com>\r\n" + h + "Subject: x\r\n\r\nbody\r\n"))
|
||||
if _, err := w.CloseWithLMTPResponse(); err != nil {
|
||||
t.Fatalf("%q: a discarded message is acknowledged so the sender gets no bounce: %v", h, err)
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
if st.queued(t) != 0 {
|
||||
t.Fatal("nothing may be queued")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
79
internal/router/loop_test.go
Normal file
79
internal/router/loop_test.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,9 @@ import (
|
|||
var (
|
||||
// ErrUnknownList means the recipient address is not a list or a list command.
|
||||
ErrUnknownList = errors.New("unknown list")
|
||||
// ErrDiscarded means the message was dropped on purpose (a loop or an automatic reply). It is
|
||||
// acknowledged to the MTA: refusing it would make the sender's server bounce it, starting a new loop.
|
||||
ErrDiscarded = errors.New("discarded")
|
||||
// ErrNotMember means the sender may not post to the list.
|
||||
ErrNotMember = errors.New("sender is not a member")
|
||||
// ErrHeld means the message was queued for moderation. It is not a failure.
|
||||
|
|
@ -68,6 +71,10 @@ func (r *Router) Route(sender, rcpt string, data []byte) (Result, error) {
|
|||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if why, drop := discardReason(l, data); drop {
|
||||
log.Printf("discard post to %s from %s: %s", l.Address(), sender, why)
|
||||
return Result{}, ErrDiscarded
|
||||
}
|
||||
if l.PostPolicy == store.PostModerated {
|
||||
return Result{}, r.hold(l, sender, data, "the list is moderated")
|
||||
}
|
||||
|
|
@ -223,3 +230,27 @@ func displayFrom(data []byte, addr string, ok bool) string {
|
|||
}
|
||||
return "no usable From header"
|
||||
}
|
||||
|
||||
// discardReason spots mail that must not be distributed: our own list mail coming back (a
|
||||
// loop), and automatic replies (vacation messages and the like, RFC 3834).
|
||||
func discardReason(l store.List, data []byte) (string, bool) {
|
||||
m, err := mail.ReadMessage(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
own := l.Name + "." + l.Domain
|
||||
for _, v := range m.Header["List-Id"] {
|
||||
if i, j := strings.LastIndex(v, "<"), strings.LastIndex(v, ">"); i >= 0 && j > i && strings.EqualFold(strings.TrimSpace(v[i+1:j]), own) {
|
||||
return "it carries this list's own List-Id: a mail loop", true
|
||||
}
|
||||
}
|
||||
if v := strings.TrimSpace(m.Header.Get("Auto-Submitted")); v != "" {
|
||||
if kind := strings.ToLower(strings.Fields(v)[0]); kind != "no" {
|
||||
return "Auto-Submitted: " + kind, true
|
||||
}
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(m.Header.Get("Precedence")), "auto_reply") {
|
||||
return "Precedence: auto_reply", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue