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
This commit is contained in:
parent
b09ee2e419
commit
9a64146fce
6 changed files with 224 additions and 21 deletions
|
|
@ -249,6 +249,13 @@ lists.example.org lmtp:inet:127.0.0.1:2424
|
|||
and `Reply-To` come from the header; so a post whose header names someone else (or has no usable `From`, or several
|
||||
addresses) is held for moderation instead of delivered, with the reason shown to the admin (API, CLI, UI). Let Postfix
|
||||
reject forged senders too (SPF, DKIM, DMARC checks on inbound mail).
|
||||
- **Loops and robots.** Mail that carries the list's own `List-Id` (a loop) or is a genuine auto-reply
|
||||
(`Auto-Submitted: auto-replied`, `Precedence: auto_reply`) is answered `250` and dropped without a trace, so the
|
||||
sender's server never bounces it. Other machine-generated mail (`Auto-Submitted: auto-generated`, cron, CI and
|
||||
monitoring posts) is a normal post: the membership, `From` and moderation checks apply. The `join`, `leave` and
|
||||
`confirm` addresses ignore (`250`, nothing happens, the token is kept) a mail with a null sender, any
|
||||
`Auto-Submitted` other than `no`, a `Precedence` of `auto_reply`, `bulk` or `junk`, or the list's own `List-Id`.
|
||||
This stops an auto-responder from confirming a subscription someone forged in your name.
|
||||
- **The moderation queue is bounded.** A list holds at most 500 messages and 200 MiB, and one held message at most 10 MiB; a
|
||||
full queue answers `452` (the sender retries later), a message over 10 MiB `552`. Messages nobody moderated for 30 days
|
||||
are dropped. Listings (API, CLI, UI) show a summary and never read the bodies. The held view shows the From header
|
||||
|
|
|
|||
|
|
@ -255,7 +255,7 @@ func TestModeratedPostIsAcceptedButNotQueued(t *testing.T) {
|
|||
|
||||
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"} {
|
||||
for _, h := range []string{"Auto-Submitted: auto-replied\r\n", "Precedence: auto_reply\r\n", "List-Id: Dev <dev.example.org>\r\n"} {
|
||||
conn, err := net.Dial("tcp", st.addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -276,6 +276,29 @@ func TestAutoReplyAndLoopsAreAcceptedButNotDelivered(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAutoGeneratedPostsAreDelivered(t *testing.T) {
|
||||
st := newStack(t, nil)
|
||||
for _, h := range []string{"Auto-Submitted: auto-generated\r\n", "Auto-Submitted: auto-notified\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: nightly report\r\nMessage-ID: <" + strings.TrimSpace(h) + "@x>\r\n\r\nbody\r\n"))
|
||||
if _, err := w.CloseWithLMTPResponse(); err != nil {
|
||||
t.Fatalf("%q: %v", h, err)
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
if st.queued(t) == 0 {
|
||||
t.Fatal("a cron or CI post from a member must be queued for delivery")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAFullModerationQueueAnswers452(t *testing.T) {
|
||||
st := newStack(t, nil)
|
||||
l, _ := st.store.FindList("dev@example.org")
|
||||
|
|
|
|||
106
internal/router/auto_test.go
Normal file
106
internal/router/auto_test.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"mime"
|
||||
"net/mail"
|
||||
"regexp"
|
||||
|
|
@ -63,6 +64,13 @@ func (r *Router) Command(sender, rcpt string, data []byte) (handled bool, err er
|
|||
return true, err
|
||||
}
|
||||
|
||||
if tag == "join" || tag == "leave" || strings.HasPrefix(tag, "confirm-") {
|
||||
if why, robot := robotReason(l, sender, data); robot {
|
||||
log.Printf("ignore a subscription command for %s from %q: %s", l.Address(), sender, why)
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case tag == "join":
|
||||
return true, r.join(l, sender)
|
||||
|
|
@ -79,9 +87,6 @@ func (r *Router) Command(sender, rcpt string, data []byte) (handled bool, err er
|
|||
}
|
||||
|
||||
func (r *Router) join(l store.List, sender string) error {
|
||||
if sender == "" {
|
||||
return nil
|
||||
}
|
||||
if l.JoinPolicy != store.JoinOpen {
|
||||
return ErrJoinClosed
|
||||
}
|
||||
|
|
@ -92,9 +97,6 @@ func (r *Router) join(l store.List, sender string) error {
|
|||
}
|
||||
|
||||
func (r *Router) leave(l store.List, sender string) error {
|
||||
if sender == "" {
|
||||
return nil
|
||||
}
|
||||
if ok, err := r.store.IsMember(l.ID, sender); err != nil || !ok {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,8 +31,7 @@ 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",
|
||||
"Auto-Submitted: Auto-Replied (vacation)\r\n",
|
||||
"Precedence: auto_reply\r\n",
|
||||
"precedence: AUTO_REPLY\r\n",
|
||||
} {
|
||||
|
|
@ -40,9 +39,13 @@ func TestAutoRepliesAreDiscarded(t *testing.T) {
|
|||
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",
|
||||
"",
|
||||
|
|
@ -53,6 +56,23 @@ func TestAutoRepliesAreDiscarded(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
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")
|
||||
|
|
|
|||
|
|
@ -251,26 +251,71 @@ 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).
|
||||
// ownListID reports whether the message carries this list's own List-Id: it is list mail coming back.
|
||||
func ownListID(l store.List, h mail.Header) bool {
|
||||
own := l.Name + "." + l.Domain
|
||||
for _, v := range h["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 true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// autoSubmitted returns the lowercase Auto-Submitted keyword (RFC 3834), or "" when the header is absent.
|
||||
func autoSubmitted(h mail.Header) string {
|
||||
if f := strings.Fields(h.Get("Auto-Submitted")); len(f) > 0 {
|
||||
return strings.ToLower(f[0])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// precedence returns the lowercase Precedence value.
|
||||
func precedence(h mail.Header) string {
|
||||
return strings.ToLower(strings.TrimSpace(h.Get("Precedence")))
|
||||
}
|
||||
|
||||
// discardReason spots mail that must not be distributed: our own list mail coming back (a loop)
|
||||
// and genuine automatic replies such as vacation messages (RFC 3834). Other machine-generated
|
||||
// mail (Auto-Submitted: auto-generated, cron, CI, monitoring) is a legitimate post and goes
|
||||
// through the normal checks.
|
||||
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 ownListID(l, m.Header) {
|
||||
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 autoSubmitted(m.Header) == "auto-replied" {
|
||||
return "Auto-Submitted: auto-replied", true
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(m.Header.Get("Precedence")), "auto_reply") {
|
||||
if precedence(m.Header) == "auto_reply" {
|
||||
return "Precedence: auto_reply", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// robotReason spots a request mail (join, leave, confirm) that a person did not write: a null
|
||||
// sender, any Auto-Submitted other than "no", a bulk, junk or auto_reply Precedence, or the list's
|
||||
// own List-Id. A robot must not drive a subscription change or get a confirmation mail.
|
||||
func robotReason(l store.List, sender string, data []byte) (string, bool) {
|
||||
if strings.TrimSpace(sender) == "" {
|
||||
return "null sender", true
|
||||
}
|
||||
m, err := mail.ReadMessage(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
if ownListID(l, m.Header) {
|
||||
return "it carries this list's own List-Id", true
|
||||
}
|
||||
if kind := autoSubmitted(m.Header); kind != "" && kind != "no" {
|
||||
return "Auto-Submitted: " + kind, true
|
||||
}
|
||||
switch p := precedence(m.Header); p {
|
||||
case "auto_reply", "bulk", "junk":
|
||||
return "Precedence: " + p, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue