From bcca031ea3140ab9a121e326513bf253cf5b514d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:40:31 +0100 Subject: [PATCH] feat: show who really wrote a held message in the CLI and the UI - Add SENDER and FROM columns to held ls, flagging a From header that differs from the sender - Show the From header and a badge in the Held tab; keep the envelope sender visible --- admin/src/lib/api.ts | 4 ++++ admin/src/pages/ListPage.test.tsx | 2 +- admin/src/tabs/HeldTab.test.tsx | 25 +++++++++++++++++++++++++ admin/src/tabs/HeldTab.tsx | 12 +++++++++++- internal/cli/cli_test.go | 29 ++++++++++++++++++++++++++++- internal/cli/commands.go | 13 +++++++++++-- 6 files changed, 80 insertions(+), 5 deletions(-) diff --git a/admin/src/lib/api.ts b/admin/src/lib/api.ts index eb5a5c8..0f25352 100644 --- a/admin/src/lib/api.ts +++ b/admin/src/lib/api.ts @@ -30,6 +30,10 @@ export interface Held { created_at: string /** Why the message was held, for the admin. */ reason: string + /** The From header as received: the author readers will see. */ + from: string + /** The From header names someone other than the envelope sender, or nobody usable. */ + mismatch: boolean } export interface Message { diff --git a/admin/src/pages/ListPage.test.tsx b/admin/src/pages/ListPage.test.tsx index be9710e..f2a8f11 100644 --- a/admin/src/pages/ListPage.test.tsx +++ b/admin/src/pages/ListPage.test.tsx @@ -50,7 +50,7 @@ describe('list page', () => { installFakeApi( newState({ lists: [dev()], - held: { 1: [{ id: 5, sender: 'a@x.com', subject: 's', size: 10, created_at: '2030-01-01T00:00:00Z', reason: '' }] }, + held: { 1: [{ id: 5, sender: 'a@x.com', subject: 's', size: 10, created_at: '2030-01-01T00:00:00Z', reason: '', from: 'a@x.com', mismatch: false }] }, }), ) renderApp('/lists/1', { token: 'good' }) diff --git a/admin/src/tabs/HeldTab.test.tsx b/admin/src/tabs/HeldTab.test.tsx index 3a0c169..5abdc84 100644 --- a/admin/src/tabs/HeldTab.test.tsx +++ b/admin/src/tabs/HeldTab.test.tsx @@ -11,6 +11,8 @@ const held = (id: number, over: Partial = {}): Held => ({ size: 2048, created_at: '2030-01-02T03:04:05Z', reason: 'the list is moderated', + from: `Sender ${id} `, + mismatch: false, ...over, }) @@ -46,6 +48,29 @@ describe('held tab', () => { expect(screen.getByRole('columnheader', { name: /reason/i })).toBeInTheDocument() }) + it('shows the author readers will see, and flags a From header that is not the sender', async () => { + await setup([ + held(1), + held(2, { sender: 'bob@example.com', from: 'Eve ', mismatch: true }), + held(3, { sender: 'bob@example.com', from: '', mismatch: true }), + ]) + const honest = (await screen.findByText('Subject 1')).closest('tr')! + expect(within(honest).getByText('Sender 1 ')).toBeInTheDocument() + expect(within(honest).getByText('s1@example.com')).toBeInTheDocument() + expect(within(honest).queryByText(/differs from the sender/i)).not.toBeInTheDocument() + + const passing = screen.getByText('Subject 2').closest('tr')! + expect(within(passing).getByText('Eve ')).toBeInTheDocument() + expect(within(passing).getByText('bob@example.com')).toBeInTheDocument() // who really sent it + expect(within(passing).getByText(/differs from the sender/i)).toBeInTheDocument() + + const noFrom = screen.getByText('Subject 3').closest('tr')! + expect(within(noFrom).getByText(/differs from the sender/i)).toBeInTheDocument() + expect(within(noFrom).getByText('(none)')).toBeInTheDocument() + expect(screen.getByRole('columnheader', { name: /^from \(header\)$/i })).toBeInTheDocument() + expect(screen.getByRole('columnheader', { name: /^sender$/i })).toBeInTheDocument() + }) + it('says nothing is waiting', async () => { await setup([]) expect(await screen.findByText(/nothing is waiting/i)).toBeInTheDocument() diff --git a/admin/src/tabs/HeldTab.tsx b/admin/src/tabs/HeldTab.tsx index bccaa48..fa3c0e6 100644 --- a/admin/src/tabs/HeldTab.tsx +++ b/admin/src/tabs/HeldTab.tsx @@ -11,6 +11,7 @@ import { AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog' +import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' import { api, type Held, type List } from '@/lib/api' @@ -60,7 +61,8 @@ export function HeldTab() { Subject - From + From (header) + Sender Received Size Reason @@ -71,6 +73,14 @@ export function HeldTab() { {items.map((h) => ( {h.subject || '(no subject)'} + + {h.from || (none)} + {h.mismatch && ( + + Differs from the sender + + )} + {h.sender} {formatDate(h.created_at)} {formatBytes(h.size)} diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 05aca26..ff494f8 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -319,7 +319,7 @@ func TestHeldQueue(t *testing.T) { 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", "REASON", "bob@x.com", "First held", "eve@x.com", "Second held", "(no subject)", "the list is moderated"} { + for _, want := range []string{"ID", "SENDER", "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) } @@ -797,3 +797,30 @@ func TestQueueShowsFailedDeliveries(t *testing.T) { t.Fatalf("a failed delivery can be cleared:\n%s", out) } } + +func TestHeldListingWarnsWhenTheHeaderDiffersFromTheSender(t *testing.T) { + e := newEnv(t) + e.ok(t, "list", "add", "dev@example.org") + l, _ := e.s.FindList("dev@example.org") + e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob \r\nSubject: honest\r\n\r\nb"), "r") + e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Eve \r\nSubject: passing as eve\r\n\r\nb"), "r") + e.s.HoldMessage(l.ID, "bob@x.com", []byte("Subject: no from\r\n\r\nb"), "r") + out := e.ok(t, "held", "ls", "dev@example.org") + lines := map[string]string{} + for _, l := range strings.Split(out, "\n") { + for _, key := range []string{"honest", "passing as eve", "no from"} { + if strings.Contains(l, key) { + lines[key] = l + } + } + } + if strings.Contains(lines["honest"], "differs") { + t.Fatalf("a matching header is not flagged: %q", lines["honest"]) + } + if !strings.Contains(lines["passing as eve"], "Eve ") || !strings.Contains(lines["passing as eve"], "differs") { + t.Fatalf("the author readers will see is shown and flagged: %q", lines["passing as eve"]) + } + if !strings.Contains(lines["no from"], "differs") { + t.Fatalf("no usable From is flagged too: %q", lines["no from"]) + } +} diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 0c31e02..0a09494 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -375,6 +375,8 @@ func (c *cmd) heldLs(args []string) int { Size int `json:"size"` CreatedAt time.Time `json:"created_at"` Reason string `json:"reason"` + From string `json:"from"` + Mismatch bool `json:"mismatch"` } if err := json.Unmarshal(raw, &hs); err != nil { return c.fail(err) @@ -383,13 +385,20 @@ 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", "REASON"}} + rows := [][]string{{"ID", "RECEIVED", "SENDER", "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), h.Reason}) + from := h.From + if from == "" { + from = "-" + } + if h.Mismatch { + from += " (differs)" // readers would see someone other than the sender + } + rows = append(rows, []string{strconv.FormatInt(h.ID, 10), formatDate(h.CreatedAt), h.Sender, from, subject, formatBytes(h.Size), h.Reason}) } c.table(rows) return 0