Files
selfpost/internal/store/sendercheck_test.go
T
mixandClaude Opus 5.5 c171fb5bc7
test / test (push) Canceled after 0s
from-header-binding: the sender check, what it costs, its pages; guide, architecture, security
A message's From: line is held against the application that sends it, under
the rule Postfix holds the envelope sender to. OpenDKIM chooses the key by
that line, so until now an application could have mail signed as another
domain of the server or as the server itself.

The check (internal/milter/sender.go)
- A role of the panel on a socket of its own (SENDER_CHECK_SOCKET), first in
  smtpd_milters and strict: default_action=tempfail. The journal milter keeps
  its socket and stays fail-open. postfix-wrapper waits for the new socket.
- It reads no database: what an application may send as comes from Postfix's
  own sender_login_maps, what the server signs for from OpenDKIM's
  SigningTable. Both are read again when they change; one that cannot be
  read defers mail.
- One From: field, one address, allowed by the envelope's lookup; then no
  other name of the SigningTable may appear anywhere in the raw line.
  Refused: 550 5.7.1 naming the line, reduced to printable ASCII.
- A refusal is a send-log row with reason "from" and the header address
  (migration 0006), written by another goroutine and not waited for. The
  journal's refusals record their reason too (limit, client), and the
  notifier takes only a limit's.

What it costs (docs/plans/from-header-binding.md, Statistics)
- The role marks itself alive every 2 s; at start the gap since the mark is
  downtime, counted from Postfix's start if later and not at all when
  supervisord says Postfix is not running.
- The log tailer counts the connections Postfix turned away: a warning that
  names the socket, then milter-reject "Service unavailable" from the same
  smtpd. Seen on the stand: the client is refused from CONNECT on and leaves
  at AUTH (454), so no MAIL line is ever written.
- sender_check_days, a row per UTC day, kept a year.

Pages
- Health: the card "Sender check", the socket's row, and the cards layout -
  boxes numbered by importance in two stacks read in rows; the left stack
  polls, the three readings of the right one are swapped in by id.
- Outbound log: a refused row says why in its tag (wrong From, wrong client,
  rate-limited); the message's page shows the From: line and what the
  application may send as.
- Help: topic "Sender check", word for word the mockup's.

Found by the stand, fixed here: the names for the second look came from the
sender bindings, and a domain with a key and no application yet was not
among them.

e2e: negative_journal_milter_fail_open is gone - it proved that mail passes
with the panel down, which is no longer so. In its place
sender_check_holds_the_from_line (another domain's From:, the hostname's, a
stranger's, an address off the list, another sender in the display name -
refused; own addresses - delivered) and panel_down_defers_mail (4xx with the
panel stopped, the container alive, mail again after, downtime and one
deferred message on Health, the card warning).

Test server, this tree before the last cosmetic edits (a fixture row, a
fact's size, docs): go vet ok, go test 23 packages ok, make e2e ok (337 s).
Locally after them: internal/web/view ok.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 19:35:43 +03:00

115 lines
3.8 KiB
Go

package store
import (
"path/filepath"
"testing"
"time"
)
func TestSenderCheckStats(t *testing.T) {
st, err := Open(filepath.Join(t.TempDir(), "selfpost.db"))
if err != nil {
t.Fatal(err)
}
defer st.Close()
now := time.Date(2026, 10, 20, 15, 0, 0, 0, time.UTC)
day := func(n int) time.Time { return now.AddDate(0, 0, -n) }
// Nothing counted yet: zeros, and no first day.
got, err := st.SenderCheckStats(now)
if err != nil {
t.Fatal(err)
}
if got != (SenderCheckStats{}) {
t.Fatalf("empty statistics = %+v", got)
}
// The first day is recorded once.
if err := st.StartSenderCheckStats(day(40)); err != nil {
t.Fatal(err)
}
if err := st.StartSenderCheckStats(now); err != nil {
t.Fatal(err)
}
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
must(st.AddSenderCheckDown(now, 3*time.Second))
must(st.AddSenderCheckDown(now, 1500*time.Millisecond)) // whole seconds: one
must(st.AddSenderCheckDown(now, 400*time.Millisecond)) // nothing
must(st.AddSenderCheckDeferred(now.Add(-time.Hour), 2))
must(st.AddSenderCheckDown(day(10), 30*time.Second))
must(st.AddSenderCheckDeferred(day(10), 1))
must(st.AddSenderCheckDown(day(29), time.Minute))
must(st.AddSenderCheckDown(day(30), time.Hour)) // the 31st day back: the year's only
must(st.AddSenderCheckDeferred(day(200), 5))
must(st.AddSenderCheckDown(day(365), 24*time.Hour)) // a year and a day: nobody's
got, err = st.SenderCheckStats(now)
if err != nil {
t.Fatal(err)
}
want := SenderCheckStats{
Today: SenderCheckTotals{Down: 4 * time.Second, Deferred: 2},
Month: SenderCheckTotals{Down: 94 * time.Second, Deferred: 3},
Year: SenderCheckTotals{Down: 94*time.Second + time.Hour, Deferred: 8},
Since: time.Date(2026, 9, 10, 0, 0, 0, 0, time.UTC),
DeferredAt: now.Add(-time.Hour),
}
if got != want {
t.Fatalf("statistics\n got %+v\nwant %+v", got, want)
}
// The last deferral is the latest one seen, not the last one written: a
// log read again after an unclean stop does not move it back.
if got.DeferredAt != now.Add(-time.Hour) {
t.Fatalf("an older deferral moved the mark to %s", got.DeferredAt)
}
// A year on, the old days are gone.
must(st.PruneSenderCheckDays(now.AddDate(1, 0, 0)))
var n int
if err := st.db.QueryRow(`SELECT COUNT(*) FROM sender_check_days`).Scan(&n); err != nil || n != 1 {
t.Fatalf("%d days left after a year's prune (%v), want the newest alone", n, err)
}
}
// A refused row keeps why it was refused and, for its From: line, what the
// line said; an accepted row has neither.
func TestRefusedRowKeepsItsReason(t *testing.T) {
st, err := Open(filepath.Join(t.TempDir(), "selfpost.db"))
if err != nil {
t.Fatal(err)
}
defer st.Close()
if err := st.InsertRejected(SendLogEntry{Domain: "shop.example.org", AppLogin: "shop", From: "shop@shop.example.org",
To: "k.berg@web.de", Subject: "s", Reason: ReasonFrom, HeaderFrom: "director@example.org"}); err != nil {
t.Fatal(err)
}
if err := st.InsertQueued(SendLogEntry{QueueID: "4XcB7k2Jm9z1", Domain: "shop.example.org", AppLogin: "shop",
From: "shop@shop.example.org", To: "k.berg@web.de"}); err != nil {
t.Fatal(err)
}
rows, err := st.QuerySendLog(SendLogFilter{AllDomains: true}, 10, 0)
if err != nil || len(rows) != 2 {
t.Fatalf("rows = %d, %v", len(rows), err)
}
queued, refused := rows[0], rows[1]
if refused.Status != StatusRejected || refused.Reason != ReasonFrom || refused.HeaderFrom != "director@example.org" || refused.To != "k.berg@web.de" {
t.Errorf("refused row = %+v", refused)
}
if queued.Reason != "" || queued.HeaderFrom != "" {
t.Errorf("queued row carries a refusal: %+v", queued)
}
one, err := st.GetSendLog(refused.ID)
if err != nil || one.Reason != ReasonFrom || one.HeaderFrom != "director@example.org" {
t.Errorf("GetSendLog = %+v, %v", one, err)
}
}