findings of the reviews of audit stage 2
test / test (push) Canceled after 0s

Technical review (Opus) and security review (Fable) of a59e3d4..cb5a4a2;
reports in the session's scratchpad. Nothing critical, high or medium; no
outright bug. Fixed:

- F2, F3 (0008 backfill, before it runs on the live database): rows are
  filled by the rule new rows follow - the application's domain, then the
  name's - and only by a domain and application that existed when the row
  was written (created_at); the inbound log likewise. Test: the migration's
  UPDATEs re-run on prepared rows (before the domain, after, null sender,
  application gone, a stranger's name). Dry run on a copy of the live
  database: 68/68 and 10/10 rows filled, none disagreeing with its login's
  domain, integrity ok.
- V2, F4: an application's statistics and level-2 count are its login's
  rows of its domain (appByLogin), and StatsWindow starts no earlier than
  the entity's creation; the message page's "may send as" only for a login
  still of the row's domain. Test: a login taken again in another domain
  starts at zero.
- F7: MAIL FROM:<> is held to the domain limit of its login
  (ApplicationByLogin fills DomainName).
- V3: an imported DKIM selector is held to DNS labels (validate.Selector).
- F5, F6: an imported auto limit's numbers are dropped and recalculated
  right after the import; its mode is stored trimmed.
- V6: the store refuses a role that is neither global nor domain.
- F9: SaveUser keeps the stored hash when no new password is given (the
  form no longer writes back the hash it read). F10: a reach naming a
  domain deleted meanwhile is ErrNoSuchDomain, answered on the form.
- V4, F12: from-lines-other.txt - lines naming another sender ({other}) in
  a quoted string, a quoted pair, capitals, a comment, an encoded word, a
  quoted local part; the unit test wants each refused, the e2e
  (lines_naming_another_sender_never_signed_for_it) wants each refused and
  none signed for it. The passed list gained a.{DOMAIN}, a comment after
  the angle address, a quoted local part, and "b@ {other}" - passed by
  design (security.md), and the e2e shows it signed for the own domain.
- F13: e2e login_in_other_capitals_does_not_authenticate - AUTH with the
  login upper-cased gets 535.

Not changed, written down: V1/F1 - L8 is half closed (refused or passed
still tells an application which domains are hosted here); security.md's
accepted risks and the CHANGELOG say so, and the owner is asked whether to
refuse every foreign address in a display name (changes the 2.4.0 rule).
F11 - deleting a domain rewrites its rows' domain_id (seconds at 1e5 rows),
accepted at this size. F8 (test cannot tell write-first from read-first;
the reviewer verified it with two stores, 30/30), V5, V7, P2: noted.

Evidence on the test server for this tree: go vet clean, go test ./... 23
packages ok, make e2e exit 0 with the three new cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mix
2026-10-14 16:17:57 +03:00
co-authored by Claude Opus 5.5
parent 8b53d63160
commit ef18ef21a0
26 changed files with 552 additions and 40 deletions
+11 -1
View File
@@ -37,6 +37,8 @@ type Application struct {
Addresses []string
AuthIPRestrict bool // when true, only AuthAllowedIPs may submit as this login
AuthAllowedIPs []string // client IPs permitted when AuthIPRestrict is set
// DomainName is the name of DomainID, filled by ApplicationByLogin only.
DomainName string
}
// AllowsAuthFromIP reports whether a client at ip may submit mail authenticated
@@ -205,8 +207,16 @@ func (s *Store) GetApplicationByLogin(login string) (Application, error) {
}
// ApplicationByLogin is the milter-facing alias for GetApplicationByLogin.
//
// DomainName is filled too: the milter holds a message with an empty
// envelope sender to the limit of its login's domain.
func (s *Store) ApplicationByLogin(login string) (Application, error) {
return s.GetApplicationByLogin(login)
a, err := s.GetApplicationByLogin(login)
if err != nil {
return a, err
}
err = s.db.QueryRow("SELECT name FROM domains WHERE id = ?", a.DomainID).Scan(&a.DomainName)
return a, err
}
// ListApplicationsByDomain returns a domain's applications ordered by login,
+93
View File
@@ -0,0 +1,93 @@
package store
import (
"strings"
"testing"
)
// backfill0008 runs the UPDATE statements of migration 0008 again: the store
// is already at its head, so the rows a test prepares get the backfill as
// they would have on an upgrade.
func backfill0008(t *testing.T, st *Store) {
t.Helper()
raw, err := migrationsFS.ReadFile("migrations/0008_tenant_boundary.sql")
if err != nil {
t.Fatal(err)
}
var lines []string
for _, l := range strings.Split(string(raw), "\n") {
if !strings.HasPrefix(strings.TrimSpace(l), "--") {
lines = append(lines, l)
}
}
for _, stmt := range strings.Split(strings.Join(lines, "\n"), ";") {
if strings.HasPrefix(strings.TrimSpace(stmt), "UPDATE") {
if _, err := st.db.Exec(stmt); err != nil {
t.Fatalf("%s: %v", stmt, err)
}
}
}
}
// The backfill follows the rule new rows follow - the application's domain,
// then the domain of the row's name - and gives a row only to a domain that
// already existed when it was written (review of audit stage 2).
func TestBackfillOf0008(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "a.example")
if _, err := st.AddApplication(d.ID, "shop", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
in, err := st.AddInboundDomain("in.example")
if err != nil {
t.Fatal(err)
}
// The domain, its application and the inbound domain were created on 1 Oct.
for _, q := range []string{
`UPDATE domains SET created_at = '2026-10-01T00:00:00Z'`,
`UPDATE applications SET created_at = '2026-10-01T00:00:00Z'`,
`UPDATE inbound_domains SET created_at = '2026-10-01T00:00:00Z'`,
} {
if _, err := st.db.Exec(q); err != nil {
t.Fatal(err)
}
}
rows := []struct{ queue, domain, login, at string }{
{"BEFORE", "a.example", "shop", "2026-09-01T00:00:00Z"}, // a predecessor's: nobody's
{"AFTER", "a.example", "shop", "2026-10-02T00:00:00Z"}, // the application's
{"NULLSENDER", "", "shop", "2026-10-02T00:00:00Z"}, // no domain of its own: the application's
{"GONEAPP", "a.example", "gone", "2026-10-02T00:00:00Z"}, // the application is gone: by name
{"STRANGER", "b.example", "gone", "2026-10-02T00:00:00Z"},
}
for _, r := range rows {
if _, err := st.db.Exec(`INSERT INTO send_log (queue_id, domain, app_login, from_addr, to_addr, subject, status, created_at, updated_at)
VALUES (?, ?, ?, '', 'x@example.net', '', 'sent', ?, ?)`, r.queue, r.domain, r.login, r.at, r.at); err != nil {
t.Fatal(err)
}
}
for _, at := range []string{"2026-09-01T00:00:00Z", "2026-10-02T00:00:00Z"} {
if _, err := st.db.Exec(`INSERT INTO inbound_spam_log (inbound_domain, client_ip, from_addr, to_addr, subject, decision, engine, detail, created_at)
VALUES ('in.example', '203.0.113.7', 's@x', 'r@in.example', '', 'accept', 'rspamd', '', ?)`, at); err != nil {
t.Fatal(err)
}
}
backfill0008(t, st)
want := map[string]int64{"BEFORE": 0, "AFTER": d.ID, "NULLSENDER": d.ID, "GONEAPP": d.ID, "STRANGER": 0}
for queue, id := range want {
var got int64
if err := st.db.QueryRow(`SELECT COALESCE(domain_id, 0) FROM send_log WHERE queue_id = ?`, queue).Scan(&got); err != nil {
t.Fatal(err)
}
if got != id {
t.Errorf("%s: domain_id %d, want %d", queue, got, id)
}
}
var before, after int64
st.db.QueryRow(`SELECT COALESCE(inbound_domain_id, 0) FROM inbound_spam_log WHERE created_at = '2026-09-01T00:00:00Z'`).Scan(&before)
st.db.QueryRow(`SELECT COALESCE(inbound_domain_id, 0) FROM inbound_spam_log WHERE created_at = '2026-10-02T00:00:00Z'`).Scan(&after)
if before != 0 || after != in.ID {
t.Errorf("inbound rows: before %d, after %d; want 0 and %d", before, after, in.ID)
}
}
+15
View File
@@ -76,3 +76,18 @@ func TestCountQueuedOlderThan(t *testing.T) {
t.Fatalf("queued rows before an hour ago = %d, %v; want 0", n, err)
}
}
// Statistics start no earlier than the entity: what came before is a
// predecessor's of the same name (review of audit stage 2).
func TestStatsWindowStartsAtTheEntity(t *testing.T) {
created := time.Now().UTC().Add(-48 * time.Hour)
since, _, _ := StatsWindow(90, created)
if !since.Equal(created) {
t.Fatalf("since = %s, want the entity's creation %s", since, created)
}
old := time.Now().UTC().AddDate(-1, 0, 0)
since, _, _ = StatsWindow(90, old)
if since.Before(time.Now().UTC().AddDate(0, 0, -31)) {
t.Fatalf("since = %s, want within the 30-day window", since)
}
}
+6
View File
@@ -161,6 +161,12 @@ func scanDomain(r scanRow) (Domain, error) {
return d, nil
}
// isForeignKeyViolation reports whether err is a SQLite FOREIGN KEY conflict.
func isForeignKeyViolation(err error) bool {
var se *sqlite.Error
return errors.As(err, &se) && se.Code() == sqlite3.SQLITE_CONSTRAINT_FOREIGNKEY
}
// isUniqueViolation reports whether err is a SQLite UNIQUE/PRIMARY-KEY conflict,
// so callers can turn a duplicate insert into a friendly domain-level error.
func isUniqueViolation(err error) bool {
@@ -16,13 +16,26 @@ CREATE UNIQUE INDEX idx_applications_login_nocase ON applications (login COLLATE
-- the domain of the login the row was written for; a row of a domain deleted
-- since - and every row older than this migration whose domain is gone - is
-- NULL and nobody's but the global role's. The name stays for display and for
-- the global view. Filled once from the registry as it is now.
-- the global view.
--
-- Filled once, by the rule new rows follow: the domain of the row's
-- application, then - for a row whose application is gone - the domain of the
-- row's name. Either only if it already existed when the row was written: a
-- domain deleted and added again before this migration must not inherit the
-- rows of the one before (review of audit stage 2). Times are RFC3339 UTC
-- text, so they compare as times.
ALTER TABLE send_log ADD COLUMN domain_id INTEGER REFERENCES domains(id) ON DELETE SET NULL;
UPDATE send_log SET domain_id = (SELECT d.id FROM domains d WHERE d.name = send_log.domain);
UPDATE send_log SET domain_id = COALESCE(
(SELECT a.domain_id FROM applications a JOIN domains d ON d.id = a.domain_id
WHERE a.login = send_log.app_login
AND a.created_at <= send_log.created_at AND d.created_at <= send_log.created_at),
(SELECT d.id FROM domains d
WHERE d.name = send_log.domain AND d.created_at <= send_log.created_at));
CREATE INDEX idx_send_log_domain_id_created_at ON send_log (domain_id, created_at);
-- The inbound journal follows the same rule, so the two logs do not follow two
-- (security.md's accepted risk of the inbound log, struck).
ALTER TABLE inbound_spam_log ADD COLUMN inbound_domain_id INTEGER REFERENCES inbound_domains(id) ON DELETE SET NULL;
UPDATE inbound_spam_log SET inbound_domain_id = (SELECT d.id FROM inbound_domains d WHERE d.name = inbound_spam_log.inbound_domain);
UPDATE inbound_spam_log SET inbound_domain_id = (SELECT d.id FROM inbound_domains d
WHERE d.name = inbound_spam_log.inbound_domain AND d.created_at <= inbound_spam_log.created_at);
CREATE INDEX idx_inbound_spam_log_domain_id_created_at ON inbound_spam_log (inbound_domain_id, created_at);
+1 -1
View File
@@ -180,7 +180,7 @@ func (s *Store) CountMessages(scope, ref string, since time.Time) (int64, error)
case RateLimitScopeDomain:
match = domainByName
case RateLimitScopeApp:
match = "app_login = ?"
match = appByLogin
default:
return 0, fmt.Errorf("unknown rate-limit scope %q", scope)
}
+33
View File
@@ -401,3 +401,36 @@ func TestADomainAddedAgainStartsWithAnEmptyLog(t *testing.T) {
t.Fatalf("statistics = %+v, %v; want one message", stats, err)
}
}
// A deleted application's login taken again in another domain starts with
// none of the old one's numbers (review of audit stage 2).
func TestALoginTakenAgainStartsWithNoNumbers(t *testing.T) {
st := openTestStore(t)
a := addTestDomain(t, st, "a.example")
b := addTestDomain(t, st, "b.example")
old, err := st.AddApplication(a.ID, "news", AddressModeWildcard, nil)
if err != nil {
t.Fatal(err)
}
if err := st.InsertQueued(SendLogEntry{QueueID: "OLD", Domain: "a.example", AppLogin: "news", To: "x@example.net"}); err != nil {
t.Fatal(err)
}
if _, err := st.DeleteApplication(old.ID); err != nil {
t.Fatal(err)
}
if _, err := st.AddApplication(b.ID, "news", AddressModeWildcard, nil); err != nil {
t.Fatal(err)
}
if n, err := st.CountMessages(RateLimitScopeApp, "news", time.Now().Add(-time.Hour)); err != nil || n != 0 {
t.Fatalf("level-2 count of the new news = %d, %v; want 0", n, err)
}
if stats, err := st.AppSendStats("news", 90, time.Now().Add(-time.Hour)); err != nil || stats.Total != 0 {
t.Fatalf("statistics of the new news = %+v, %v; want none", stats, err)
}
if err := st.InsertQueued(SendLogEntry{QueueID: "NEW", Domain: "b.example", AppLogin: "news", To: "x@example.net"}); err != nil {
t.Fatal(err)
}
if n, _ := st.CountMessages(RateLimitScopeApp, "news", time.Now().Add(-time.Hour)); n != 1 {
t.Fatalf("level-2 count of the new news = %d, want its own one", n)
}
}
+13 -1
View File
@@ -42,6 +42,11 @@ func StatsWindow(retentionDays int, entityCreated time.Time) (since time.Time, h
if hours < 1 {
hours = 1
}
// Nothing before the entity was created is its own: rows from then are a
// predecessor's of the same name or login (review of audit stage 2).
if c := entityCreated.UTC(); c.After(since) {
since = c
}
return since, hours, windowDays
}
@@ -62,7 +67,7 @@ func (s *Store) DomainSendStats(name string, retentionDays int, createdAt time.T
// AppSendStats returns send statistics for an application login.
func (s *Store) AppSendStats(login string, retentionDays int, createdAt time.Time) (SendStats, error) {
since, hours, windowDays := StatsWindow(retentionDays, createdAt)
stats, err := s.sendStats("app_login = ?", login, since, hours)
stats, err := s.sendStats(appByLogin, login, since, hours)
if err != nil {
return SendStats{}, err
}
@@ -75,6 +80,13 @@ func (s *Store) AppSendStats(login string, retentionDays int, createdAt time.Tim
// registered under the name it is given.
const domainByName = "domain_id = (SELECT d.id FROM domains d WHERE d.name = ?)"
// appByLogin selects an application's send-log rows: its login's rows of its
// domain. A login is unique while it exists, but a deleted application's
// login can be taken again in another domain, and its numbers - statistics,
// the level-2 count - must not start with the old one's (review of audit
// stage 2). ?1 twice; a plain ? after it takes the next number.
const appByLogin = "app_login = ?1 AND domain_id = (SELECT a.domain_id FROM applications a WHERE a.login = ?1)"
// sendStats counts the rows that match, a condition with one placeholder
// taking ref.
func (s *Store) sendStats(match, ref string, since time.Time, hours float64) (SendStats, error) {
+4
View File
@@ -19,6 +19,10 @@ func TestSendStatsTotalPeakAvg(t *testing.T) {
now := time.Now().UTC()
hour := now.Format("2006-01-02T15")
prevHour := now.Add(-2 * time.Hour).Format("2006-01-02T15")
// Both were created before the messages below: statistics start at the
// entity's creation.
created := now.Add(-3 * time.Hour)
d.CreatedAt, a.CreatedAt = created, created
// Hour 1: two messages (Q1 two recipients + Q2).
for _, to := range []string{"a@x.net", "b@x.net"} {
+30 -2
View File
@@ -16,6 +16,10 @@ var ErrUserNotFound = errors.New("user not found")
// ErrUserExists is returned when a username is already taken.
var ErrUserExists = errors.New("username already taken")
// ErrNoSuchDomain is returned when a user is assigned a domain that does not
// exist - one deleted while the users form was open.
var ErrNoSuchDomain = errors.New("an assigned domain does not exist")
// ErrLastGlobal is returned when deleting or demoting the last global user.
var ErrLastGlobal = errors.New("cannot remove last global administrator")
@@ -225,7 +229,8 @@ func (s *Store) CreateUser(username, passwordHash string, role Role, reach Reach
}
// UserChange is everything the users form sets on a user, written at once by
// AddUser and SaveUser. ID is ignored by AddUser.
// AddUser and SaveUser. ID is ignored by AddUser; SaveUser keeps the stored
// password hash when PasswordHash is empty.
type UserChange struct {
ID int64
Username, PasswordHash, Email string
@@ -238,6 +243,9 @@ type UserChange struct {
// and a failure part way leaves no user rather than one without it (audit
// 2026-10-12, M9).
func (s *Store) AddUser(c UserChange) (int64, error) {
if err := knownRole(c.Role); err != nil {
return 0, err
}
if c.Role == RoleDomain && c.Reach.Empty() {
return 0, ErrEmptyReach
}
@@ -282,6 +290,9 @@ func (s *Store) AddUser(c UserChange) (int64, error) {
// after the write, so two administrators demoting each other at once cannot
// both succeed (M7).
func (s *Store) SaveUser(c UserChange) error {
if err := knownRole(c.Role); err != nil {
return err
}
if c.Role == RoleDomain && c.Reach.Empty() {
return ErrEmptyReach
}
@@ -292,8 +303,10 @@ func (s *Store) SaveUser(c UserChange) error {
defer tx.Rollback()
// A write first: it takes the write lock, so what is counted below is
// what is committed.
// An empty hash keeps the stored one: the form read it when it was
// opened, and the user may have changed it on Account since.
res, err := tx.Exec(
"UPDATE users SET username = ?, password_hash = ?, email = ?, role = ? WHERE id = ?",
"UPDATE users SET username = ?, password_hash = COALESCE(NULLIF(?, ''), password_hash), email = ?, role = ? WHERE id = ?",
c.Username, c.PasswordHash, c.Email, c.Role, c.ID,
)
if err != nil {
@@ -321,6 +334,15 @@ func (s *Store) SaveUser(c UserChange) error {
return nil
}
// knownRole refuses a role that is neither global nor domain. The handlers
// check the form first; this is the second line.
func knownRole(r Role) error {
if r != RoleGlobal && r != RoleDomain {
return fmt.Errorf("unknown role %q", r)
}
return nil
}
// requireAGlobal is ErrLastGlobal when the transaction has left no global
// user.
func requireAGlobal(tx *sql.Tx) error {
@@ -510,6 +532,9 @@ func writeUserReach(tx *sql.Tx, userID int64, reach Reach) error {
if !reach.AllDomains {
for _, id := range reach.DomainIDs {
if _, err := tx.Exec("INSERT INTO user_domains (user_id, domain_id) VALUES (?, ?)", userID, id); err != nil {
if isForeignKeyViolation(err) {
return ErrNoSuchDomain
}
return fmt.Errorf("set user reach insert: %w", err)
}
}
@@ -517,6 +542,9 @@ func writeUserReach(tx *sql.Tx, userID int64, reach Reach) error {
if !reach.AllInbound {
for _, id := range reach.InboundDomainIDs {
if _, err := tx.Exec("INSERT INTO user_inbound_domains (user_id, inbound_domain_id) VALUES (?, ?)", userID, id); err != nil {
if isForeignKeyViolation(err) {
return ErrNoSuchDomain
}
return fmt.Errorf("set user reach insert inbound: %w", err)
}
}
+43
View File
@@ -149,3 +149,46 @@ func TestCreateGlobalUserOnlyOnce(t *testing.T) {
t.Fatalf("%d setups succeeded, %d global users; want 1 and 1", ok, n)
}
}
// The store refuses a role that is neither global nor domain.
func TestTheStoreKnowsOnlyTwoRoles(t *testing.T) {
st := openTestStore(t)
if _, err := st.AddUser(UserChange{Username: "x", PasswordHash: "h", Role: "admin"}); err == nil {
t.Fatal("AddUser took the role admin")
}
id, err := st.CreateUser("y", "h", RoleGlobal, Reach{})
if err != nil {
t.Fatal(err)
}
if err := st.SaveUser(UserChange{ID: id, Username: "y", PasswordHash: "h", Role: ""}); err == nil {
t.Fatal("SaveUser took an empty role")
}
}
// A save without a new password keeps the stored hash - one the user may have
// changed on Account since the form was opened - and a reach naming a domain
// deleted meanwhile is ErrNoSuchDomain (review of audit stage 2).
func TestSaveUserKeepsTheHashAndNamesAMissingDomain(t *testing.T) {
st := openTestStore(t)
d := addTestDomain(t, st, "example.com")
if _, err := st.CreateUser("root", "hash", RoleGlobal, Reach{}); err != nil {
t.Fatal(err)
}
id, err := st.CreateUser("shop", "old", RoleDomain, Reach{DomainIDs: []int64{d.ID}})
if err != nil {
t.Fatal(err)
}
if err := st.UpdateUser(id, "shop", "changed-on-account", ""); err != nil {
t.Fatal(err)
}
if err := st.SaveUser(UserChange{ID: id, Username: "shop", Role: RoleDomain, Reach: Reach{DomainIDs: []int64{d.ID}}}); err != nil {
t.Fatal(err)
}
u, _ := st.GetUser(id)
if u.PasswordHash != "changed-on-account" {
t.Fatalf("the hash became %q", u.PasswordHash)
}
if err := st.SaveUser(UserChange{ID: id, Username: "shop", Role: RoleDomain, Reach: Reach{DomainIDs: []int64{9999}}}); !errors.Is(err, ErrNoSuchDomain) {
t.Fatalf("a reach to a deleted domain = %v, want ErrNoSuchDomain", err)
}
}