fix(security): hold posts whose From header does not match the sender
- Judge membership on the envelope sender and require the From header to name the same address - Hold mismatches (and missing, unparsable or multiple From addresses) for the admin instead of delivering - Record why a message was held: new held.reason column, migrated, shown in the API, CLI and UI - Accept an empty message in the held queue - Document who may post
This commit is contained in:
parent
064e8beed8
commit
eb9e915805
15 changed files with 252 additions and 34 deletions
|
|
@ -208,6 +208,11 @@ lists.example.org lmtp:inet:127.0.0.1:2424
|
|||
|
||||
The daemon logs a warning at startup when LMTP is reachable beyond the machine and `lmtp.allow` is empty.
|
||||
`allow` does not apply to unix sockets: use the permissions of the socket file.
|
||||
- **Who is posting.** A post is accepted when the SMTP sender is a member *and* the `From` header names the same
|
||||
address. The SMTP sender can be forged by anyone who can send mail to your MX, and the archive, the rewritten `From`
|
||||
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).
|
||||
- The API and the admin UI use one bearer token and no TLS: put them behind a reverse proxy with TLS and keep the `api`
|
||||
listener private.
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ export interface Held {
|
|||
subject: string
|
||||
size: number
|
||||
created_at: string
|
||||
/** Why the message was held, for the admin. */
|
||||
reason: string
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ const held = (id: number, over: Partial<Held> = {}): Held => ({
|
|||
subject: `Subject ${id}`,
|
||||
size: 2048,
|
||||
created_at: '2030-01-02T03:04:05Z',
|
||||
reason: 'the list is moderated',
|
||||
...over,
|
||||
})
|
||||
|
||||
|
|
@ -30,6 +31,21 @@ describe('held tab', () => {
|
|||
expect(screen.getByText('(no subject)')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('tells the admin why each message was held', async () => {
|
||||
await setup([
|
||||
held(1),
|
||||
held(2, { reason: 'the From header does not match the sender of the message (From: bob@example.com)' }),
|
||||
held(3, { reason: '' }),
|
||||
])
|
||||
const one = (await screen.findByText('Subject 1')).closest('tr')!
|
||||
expect(within(one).getByText('the list is moderated')).toBeInTheDocument()
|
||||
const two = screen.getByText('Subject 2').closest('tr')!
|
||||
expect(within(two).getByText(/From header does not match the sender/)).toBeInTheDocument()
|
||||
const three = screen.getByText('Subject 3').closest('tr')!
|
||||
expect(within(three).getByText('—')).toBeInTheDocument() // messages held before this was recorded
|
||||
expect(screen.getByRole('columnheader', { name: /reason/i })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('says nothing is waiting', async () => {
|
||||
await setup([])
|
||||
expect(await screen.findByText(/nothing is waiting/i)).toBeInTheDocument()
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ export function HeldTab() {
|
|||
<TableHead>From</TableHead>
|
||||
<TableHead>Received</TableHead>
|
||||
<TableHead>Size</TableHead>
|
||||
<TableHead>Reason</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
|
@ -73,6 +74,7 @@ export function HeldTab() {
|
|||
<TableCell>{h.sender}</TableCell>
|
||||
<TableCell>{formatDate(h.created_at)}</TableCell>
|
||||
<TableCell>{formatBytes(h.size)}</TableCell>
|
||||
<TableCell className="max-w-xs whitespace-normal text-muted-foreground">{h.reason || '—'}</TableCell>
|
||||
<TableCell className="space-x-2 text-right">
|
||||
<Button size="sm" disabled={approve.isPending} onClick={() => approve.mutate(h)}>
|
||||
Approve
|
||||
|
|
|
|||
|
|
@ -338,6 +338,7 @@ type heldDTO struct {
|
|||
Subject string `json:"subject"`
|
||||
Size int `json:"size"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Reason string `json:"reason"` // why the message was held
|
||||
}
|
||||
|
||||
func subjectOf(data []byte) string {
|
||||
|
|
@ -368,7 +369,7 @@ func (s *server) listHeld(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
out := make([]heldDTO, 0, len(hs))
|
||||
for _, h := range hs {
|
||||
out = append(out, heldDTO{ID: h.ID, Sender: h.Sender, Subject: subjectOf(h.Data), Size: len(h.Data), CreatedAt: h.CreatedAt})
|
||||
out = append(out, heldDTO{ID: h.ID, Sender: h.Sender, Subject: subjectOf(h.Data), Size: len(h.Data), CreatedAt: h.CreatedAt, Reason: h.Reason})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -264,7 +264,7 @@ func heldServer(t *testing.T) (http.Handler, *store.Store, *fakeReleaser) {
|
|||
s, _ := store.Open(":memory:")
|
||||
t.Cleanup(func() { s.Close() })
|
||||
l, _ := s.CreateList("dev", "example.org", "Dev")
|
||||
s.HoldMessage(l.ID, "Bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: Hello there\r\n\r\nbody"))
|
||||
s.HoldMessage(l.ID, "Bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: Hello there\r\n\r\nbody"), "the list is moderated")
|
||||
rel := &fakeReleaser{}
|
||||
return New(s, token, rel), s, rel
|
||||
}
|
||||
|
|
@ -274,6 +274,7 @@ type heldJSON struct {
|
|||
Sender string `json:"sender"`
|
||||
Subject string `json:"subject"`
|
||||
Size int `json:"size"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func TestHeldQueueAPI(t *testing.T) {
|
||||
|
|
@ -282,7 +283,7 @@ func TestHeldQueueAPI(t *testing.T) {
|
|||
expect(t, rec, 200)
|
||||
var hs []heldJSON
|
||||
decode(t, rec, &hs)
|
||||
if len(hs) != 1 || hs[0].Sender != "bob@x.com" || hs[0].Subject != "Hello there" || hs[0].Size == 0 {
|
||||
if len(hs) != 1 || hs[0].Sender != "bob@x.com" || hs[0].Subject != "Hello there" || hs[0].Size == 0 || hs[0].Reason != "the list is moderated" {
|
||||
t.Fatalf("held: %+v", hs)
|
||||
}
|
||||
expect(t, do(t, h, "GET", "/api/v1/lists/99/held", "", true), 404)
|
||||
|
|
|
|||
|
|
@ -315,11 +315,11 @@ func TestHeldQueue(t *testing.T) {
|
|||
if out := e.ok(t, "held", "ls", "dev@example.org"); !strings.Contains(out, "Nothing is held") {
|
||||
t.Fatalf("empty: %q", out)
|
||||
}
|
||||
id1, _ := e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: First held\r\n\r\nbody"))
|
||||
id2, _ := e.s.HoldMessage(l.ID, "eve@x.com", []byte("Subject: Second held\r\n\r\nbody"))
|
||||
id3, _ := e.s.HoldMessage(l.ID, "anon@x.com", []byte("From: anon@x.com\r\n\r\nno subject here"))
|
||||
id1, _ := e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: First held\r\n\r\nbody"), "the list is moderated")
|
||||
id2, _ := e.s.HoldMessage(l.ID, "eve@x.com", []byte("Subject: Second held\r\n\r\nbody"), "")
|
||||
id3, _ := e.s.HoldMessage(l.ID, "anon@x.com", []byte("From: anon@x.com\r\n\r\nno subject here"), "")
|
||||
out := e.ok(t, "held", "ls", "dev@example.org")
|
||||
for _, want := range []string{"ID", "FROM", "SUBJECT", "SIZE", "bob@x.com", "First held", "eve@x.com", "Second held", "(no subject)"} {
|
||||
for _, want := range []string{"ID", "FROM", "SUBJECT", "SIZE", "REASON", "bob@x.com", "First held", "eve@x.com", "Second held", "(no subject)", "the list is moderated"} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Fatalf("held ls lacks %q:\n%s", want, out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -374,6 +374,7 @@ func (c *cmd) heldLs(args []string) int {
|
|||
Subject string `json:"subject"`
|
||||
Size int `json:"size"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &hs); err != nil {
|
||||
return c.fail(err)
|
||||
|
|
@ -382,13 +383,13 @@ func (c *cmd) heldLs(args []string) int {
|
|||
fmt.Fprintln(c.io.Out, "Nothing is held for moderation.")
|
||||
return 0
|
||||
}
|
||||
rows := [][]string{{"ID", "RECEIVED", "FROM", "SUBJECT", "SIZE"}}
|
||||
rows := [][]string{{"ID", "RECEIVED", "FROM", "SUBJECT", "SIZE", "REASON"}}
|
||||
for _, h := range hs {
|
||||
subject := h.Subject
|
||||
if subject == "" {
|
||||
subject = "(no subject)"
|
||||
}
|
||||
rows = append(rows, []string{strconv.FormatInt(h.ID, 10), formatDate(h.CreatedAt), h.Sender, subject, formatBytes(h.Size)})
|
||||
rows = append(rows, []string{strconv.FormatInt(h.ID, 10), formatDate(h.CreatedAt), h.Sender, subject, formatBytes(h.Size), h.Reason})
|
||||
}
|
||||
c.table(rows)
|
||||
return 0
|
||||
|
|
|
|||
97
internal/router/identity_test.go
Normal file
97
internal/router/identity_test.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func postFrom(from string) []byte {
|
||||
h := ""
|
||||
if from != "" {
|
||||
h = "From: " + from + "\r\n"
|
||||
}
|
||||
return []byte(h + "Subject: hi\r\nMessage-ID: <id1@x>\r\n\r\nbody\r\n")
|
||||
}
|
||||
|
||||
func TestAMatchingFromHeaderIsDelivered(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
for _, from := range []string{
|
||||
"alice@example.com",
|
||||
"Alice <alice@example.com>",
|
||||
`"Alice S." <ALICE@Example.COM>`,
|
||||
`"bob@example.com" <alice@example.com>`, // another address in the display name is just text
|
||||
} {
|
||||
if _, err := e.r.Route("alice@example.com", "dev@example.org", postFrom(from)); err != nil {
|
||||
t.Errorf("From %q: %v", from, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestADifferentFromHeaderIsHeldForModeration(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
cases := map[string]string{
|
||||
"another member": "Bob <bob@example.com>",
|
||||
"a stranger": "Carol <carol@example.com>",
|
||||
"a plus-address": "alice+x@example.com",
|
||||
"no From header": "",
|
||||
"unparsable From": "not an address",
|
||||
"several From address": "alice@example.com, bob@example.com",
|
||||
"empty address": "<>",
|
||||
}
|
||||
for name, from := range cases {
|
||||
_, err := e.r.Route("alice@example.com", "dev@example.org", postFrom(from))
|
||||
if !errors.Is(err, ErrHeld) {
|
||||
t.Errorf("%s: want ErrHeld, got %v", name, err)
|
||||
}
|
||||
}
|
||||
held, _ := e.s.ListHeld(e.list.ID)
|
||||
if len(held) != len(cases) {
|
||||
t.Fatalf("held = %d, want %d", len(held), len(cases))
|
||||
}
|
||||
if held[0].Sender != "alice@example.com" {
|
||||
t.Fatalf("the held message keeps the envelope sender: %+v", held[0])
|
||||
}
|
||||
if !strings.Contains(held[0].Reason, "From") || !strings.Contains(held[0].Reason, "sender") {
|
||||
t.Fatalf("the admin is told why: %q", held[0].Reason)
|
||||
}
|
||||
if len(e.out.Sent) != 0 {
|
||||
t.Fatal("nothing may be delivered")
|
||||
}
|
||||
if st, _ := e.s.OutboxStats(); st.Pending != 0 {
|
||||
t.Fatal("nothing may be queued")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonMembersAreStillRefusedNotHeld(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
_, err := e.r.Route("mallory@evil.com", "dev@example.org", postFrom("alice@example.com"))
|
||||
if !errors.Is(err, ErrNotMember) {
|
||||
t.Fatalf("a stranger is refused whatever From says: %v", err)
|
||||
}
|
||||
if held, _ := e.s.ListHeld(e.list.ID); len(held) != 0 {
|
||||
t.Fatal("refused mail is not held")
|
||||
}
|
||||
}
|
||||
|
||||
func TestModeratedListsSayThatTheListIsModerated(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.s.SetPolicies(e.list.ID, "moderated", "open", "private")
|
||||
e.r.Route("alice@example.com", "dev@example.org", postFrom("alice@example.com"))
|
||||
held, _ := e.s.ListHeld(e.list.ID)
|
||||
if len(held) != 1 || !strings.Contains(held[0].Reason, "moderated") {
|
||||
t.Fatalf("held: %+v", held)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnApprovedIdentityMismatchIsDelivered(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.r.Route("alice@example.com", "dev@example.org", postFrom("Bob <bob@example.com>"))
|
||||
held, _ := e.s.ListHeld(e.list.ID)
|
||||
if err := e.release(held[0].ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(e.out.Sent) != 2 {
|
||||
t.Fatalf("the admin approved it: sent %d", len(e.out.Sent))
|
||||
}
|
||||
}
|
||||
|
|
@ -106,7 +106,7 @@ func TestDisabledMembersAreSkipped(t *testing.T) {
|
|||
for i := 0; i < 3; i++ {
|
||||
e.s.RecordHardBounce(e.alice, 3)
|
||||
}
|
||||
res, err := e.r.Route("bob@example.com", "dev@example.org", []byte(sample))
|
||||
res, err := e.r.Route("bob@example.com", "dev@example.org", postFrom("Bob <bob@example.com>"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,14 +66,21 @@ func TestRejectedAndHeldPostsAreNotArchived(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnparseablePostIsStillDelivered(t *testing.T) {
|
||||
func TestAPostWithoutHeadersIsHeldNotDelivered(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
res, err := e.r.Route("alice@example.com", "dev@example.org", []byte("\r\nno headers at all\r\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("archive failures must not block delivery: %v", err)
|
||||
_, err := e.r.Route("alice@example.com", "dev@example.org", []byte("\r\nno headers at all\r\n"))
|
||||
if !errors.Is(err, ErrHeld) {
|
||||
t.Fatalf("a message without a From header is for the admin to judge: %v", err)
|
||||
}
|
||||
if len(res.Recipients) != 2 {
|
||||
t.Fatalf("recipients: %+v", res.Recipients)
|
||||
}
|
||||
|
||||
func TestAnArchiveFailureNeverBreaksDelivery(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.s.SetPolicies(e.list.ID, "members", "open", "private")
|
||||
// archive() is best effort: a message the archive cannot read is logged and skipped.
|
||||
e.r.archive(e.list, []byte("\r\nno headers at all\r\n"))
|
||||
if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 0 {
|
||||
t.Fatalf("nothing archived: %d", total)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"log"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -67,10 +69,7 @@ func (r *Router) Route(sender, rcpt string, data []byte) (Result, error) {
|
|||
return Result{}, err
|
||||
}
|
||||
if l.PostPolicy == store.PostModerated {
|
||||
if _, err := r.store.HoldMessage(l.ID, sender, data); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
return Result{}, ErrHeld
|
||||
return Result{}, r.hold(l, sender, data, "the list is moderated")
|
||||
}
|
||||
ok, err := r.store.IsMember(l.ID, sender)
|
||||
if err != nil {
|
||||
|
|
@ -79,9 +78,23 @@ func (r *Router) Route(sender, rcpt string, data []byte) (Result, error) {
|
|||
if !ok {
|
||||
return Result{}, ErrNotMember
|
||||
}
|
||||
// Membership is judged on the envelope sender, which anyone can forge, while the archive, the
|
||||
// rewritten From and Reply-To come from the From header. Only trust a post whose header names
|
||||
// the same address: otherwise a member could pass as someone else. An admin decides on the rest.
|
||||
if from, ok := fromHeaderAddress(data); !ok || !strings.EqualFold(strings.TrimSpace(sender), from) {
|
||||
return Result{}, r.hold(l, sender, data, "the From header does not match the sender of the message ("+displayFrom(data, from, ok)+")")
|
||||
}
|
||||
return r.prepare(l, data)
|
||||
}
|
||||
|
||||
// hold queues a message for the admin and reports it as held.
|
||||
func (r *Router) hold(l store.List, sender string, data []byte, reason string) error {
|
||||
if _, err := r.store.HoldMessage(l.ID, sender, data, reason); err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrHeld
|
||||
}
|
||||
|
||||
// prepare builds the delivery plan for a list.
|
||||
func (r *Router) prepare(l store.List, data []byte) (Result, error) {
|
||||
members, err := r.store.ActiveMembers(l.ID)
|
||||
|
|
@ -181,3 +194,32 @@ func (r *Router) Release(heldID int64) error {
|
|||
}
|
||||
return r.store.DeleteHeld(heldID)
|
||||
}
|
||||
|
||||
// fromHeaderAddress returns the lowercase address of the From header when there is exactly one.
|
||||
func fromHeaderAddress(data []byte) (string, bool) {
|
||||
m, err := mail.ReadMessage(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
addrs, err := m.Header.AddressList("From")
|
||||
if err != nil || len(addrs) != 1 || addrs[0].Address == "" {
|
||||
return "", false
|
||||
}
|
||||
return strings.ToLower(addrs[0].Address), true
|
||||
}
|
||||
|
||||
// displayFrom describes the From header for the held-message reason.
|
||||
func displayFrom(data []byte, addr string, ok bool) string {
|
||||
if ok {
|
||||
return "From: " + addr
|
||||
}
|
||||
if m, err := mail.ReadMessage(bytes.NewReader(data)); err == nil {
|
||||
if raw := strings.TrimSpace(m.Header.Get("From")); raw != "" {
|
||||
if len(raw) > 80 {
|
||||
raw = raw[:80] + "…"
|
||||
}
|
||||
return "From: " + raw
|
||||
}
|
||||
}
|
||||
return "no usable From header"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func setup(t *testing.T) *Router {
|
|||
|
||||
func TestRouteMemberPost(t *testing.T) {
|
||||
r := setup(t)
|
||||
res, err := r.Route("alice@example.com", "dev@example.org", []byte("Subject: hi\r\n\r\nbody\r\n"))
|
||||
res, err := r.Route("alice@example.com", "dev@example.org", []byte("From: alice@example.com\r\nSubject: hi\r\n\r\nbody\r\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("route: %v", err)
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ func TestRouteMemberPost(t *testing.T) {
|
|||
|
||||
func TestRouteSenderCaseInsensitive(t *testing.T) {
|
||||
r := setup(t)
|
||||
if _, err := r.Route("ALICE@Example.com", "dev@example.org", nil); err != nil {
|
||||
if _, err := r.Route("ALICE@Example.com", "dev@example.org", []byte("From: Alice <alice@example.com>\r\n\r\nx\r\n")); err != nil {
|
||||
t.Fatalf("route: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,16 +32,16 @@ func TestListPolicies(t *testing.T) {
|
|||
func TestHeldQueue(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
l, _ := s.CreateList("dev", "example.org", "Dev")
|
||||
id, err := s.HoldMessage(l.ID, "bob@x.com", []byte("Subject: hi\r\n\r\nx"))
|
||||
id, err := s.HoldMessage(l.ID, "bob@x.com", []byte("Subject: hi\r\n\r\nx"), "list is moderated")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hs, err := s.ListHeld(l.ID)
|
||||
if err != nil || len(hs) != 1 || hs[0].ID != id || hs[0].Sender != "bob@x.com" {
|
||||
if err != nil || len(hs) != 1 || hs[0].ID != id || hs[0].Sender != "bob@x.com" || hs[0].Reason != "list is moderated" {
|
||||
t.Fatalf("held: %+v %v", hs, err)
|
||||
}
|
||||
h, err := s.GetHeld(id)
|
||||
if err != nil || string(h.Data) != "Subject: hi\r\n\r\nx" || h.ListID != l.ID {
|
||||
if err != nil || string(h.Data) != "Subject: hi\r\n\r\nx" || h.ListID != l.ID || h.Reason != "list is moderated" {
|
||||
t.Fatalf("get: %+v %v", h, err)
|
||||
}
|
||||
if err := s.DeleteHeld(id); err != nil {
|
||||
|
|
@ -189,3 +189,33 @@ INSERT INTO members(list_id, address, display_name) VALUES(1,'a@x.com','A'),(1,'
|
|||
}
|
||||
s2.Close()
|
||||
}
|
||||
|
||||
func TestMigrationAddsTheHeldReasonColumn(t *testing.T) {
|
||||
path := t.TempDir() + "/old.db"
|
||||
s, _ := Open(path)
|
||||
l, _ := s.CreateList("dev", "example.org", "Dev")
|
||||
s.db.Exec(`INSERT INTO held(list_id, sender, data, created_at, reason) VALUES(?, 'a@x.com', x'41', 1, 'x')`, l.ID)
|
||||
s.db.Exec(`ALTER TABLE held DROP COLUMN reason`)
|
||||
s.Close()
|
||||
s2, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
defer s2.Close()
|
||||
hs, err := s2.ListHeld(l.ID)
|
||||
if err != nil || len(hs) != 1 || hs[0].Reason != "" {
|
||||
t.Fatalf("an old held message has an empty reason: %+v %v", hs, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHoldingAnEmptyMessage(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
l, _ := s.CreateList("dev", "example.org", "Dev")
|
||||
id, err := s.HoldMessage(l.ID, "a@x.com", nil, "r")
|
||||
if err != nil {
|
||||
t.Fatalf("an empty message must be holdable: %v", err)
|
||||
}
|
||||
if h, err := s.GetHeld(id); err != nil || len(h.Data) != 0 {
|
||||
t.Fatalf("%+v %v", h, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,9 +77,10 @@ type Member struct {
|
|||
type Held struct {
|
||||
ID int64
|
||||
ListID int64
|
||||
Sender string
|
||||
Sender string // envelope sender
|
||||
Data []byte
|
||||
CreatedAt time.Time
|
||||
Reason string // why the message was held, for the admin
|
||||
}
|
||||
|
||||
// Pending is an unconfirmed subscribe or unsubscribe request.
|
||||
|
|
@ -122,7 +123,8 @@ CREATE TABLE IF NOT EXISTS held (
|
|||
list_id INTEGER NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
sender TEXT NOT NULL,
|
||||
data BLOB NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
created_at INTEGER NOT NULL,
|
||||
reason TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS pending (
|
||||
token TEXT PRIMARY KEY,
|
||||
|
|
@ -242,6 +244,15 @@ func migrate(db *sql.DB) error {
|
|||
}
|
||||
}
|
||||
}
|
||||
if ok, err := hasColumn(db, "held", "list_id"); err != nil {
|
||||
return err
|
||||
} else if ok {
|
||||
if has, _ := hasColumn(db, "held", "reason"); !has {
|
||||
if _, err := db.Exec(`ALTER TABLE held ADD COLUMN reason TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := db.Exec(schema)
|
||||
return err
|
||||
}
|
||||
|
|
@ -434,10 +445,13 @@ func (s *Store) RecordHardBounce(memberID int64, threshold int) (bool, error) {
|
|||
|
||||
// Held messages.
|
||||
|
||||
// HoldMessage queues a message for moderation and returns its id.
|
||||
func (s *Store) HoldMessage(listID int64, sender string, data []byte) (int64, error) {
|
||||
res, err := s.db.Exec(`INSERT INTO held(list_id, sender, data, created_at) VALUES(?,?,?,?)`,
|
||||
listID, norm(sender), data, s.Now().Unix())
|
||||
// HoldMessage queues a message for moderation and returns its id. reason tells the admin why.
|
||||
func (s *Store) HoldMessage(listID int64, sender string, data []byte, reason string) (int64, error) {
|
||||
if data == nil {
|
||||
data = []byte{} // the column is NOT NULL
|
||||
}
|
||||
res, err := s.db.Exec(`INSERT INTO held(list_id, sender, data, created_at, reason) VALUES(?,?,?,?,?)`,
|
||||
listID, norm(sender), data, s.Now().Unix(), reason)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
@ -447,7 +461,7 @@ func (s *Store) HoldMessage(listID int64, sender string, data []byte) (int64, er
|
|||
func scanHeld(row interface{ Scan(...any) error }) (Held, error) {
|
||||
var h Held
|
||||
var ts int64
|
||||
err := row.Scan(&h.ID, &h.ListID, &h.Sender, &h.Data, &ts)
|
||||
err := row.Scan(&h.ID, &h.ListID, &h.Sender, &h.Data, &ts, &h.Reason)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Held{}, ErrNotFound
|
||||
}
|
||||
|
|
@ -457,7 +471,7 @@ func scanHeld(row interface{ Scan(...any) error }) (Held, error) {
|
|||
|
||||
// ListHeld returns the held messages of a list, oldest first.
|
||||
func (s *Store) ListHeld(listID int64) ([]Held, error) {
|
||||
rows, err := s.db.Query(`SELECT id, list_id, sender, data, created_at FROM held WHERE list_id=? ORDER BY id`, listID)
|
||||
rows, err := s.db.Query(`SELECT id, list_id, sender, data, created_at, reason FROM held WHERE list_id=? ORDER BY id`, listID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -475,7 +489,7 @@ func (s *Store) ListHeld(listID int64) ([]Held, error) {
|
|||
|
||||
// GetHeld returns a held message by id.
|
||||
func (s *Store) GetHeld(id int64) (Held, error) {
|
||||
return scanHeld(s.db.QueryRow(`SELECT id, list_id, sender, data, created_at FROM held WHERE id=?`, id))
|
||||
return scanHeld(s.db.QueryRow(`SELECT id, list_id, sender, data, created_at, reason FROM held WHERE id=?`, id))
|
||||
}
|
||||
|
||||
// DeleteHeld discards a held message.
|
||||
|
|
|
|||
Loading…
Reference in a new issue