Files
selfpost/internal/inbound/service_test.go
T
mixandClaude Opus 5.5 6b4106f60e upstream-tls-verify: Verified TLS to an inbound domain's upstream
Owner, 2026-10-18: "Оба предложения делаем"; the mockup is c10cd78. The
security audit's M7: Required is Postfix's encrypt level, which checks no
certificate - it keeps the mail from a listener, not from one who answers
in the upstream's place.

- A fourth TLS mode, Verified, stored as Postfix's level secure (migration
  0014 rebuilds inbound_transports with the wider CHECK; schema 14) and
  written to the TLS policy map like the others. Postfix matches the
  certificate against the next hop's name (smtp_tls_secure_cert_match =
  nexthop, dot-nexthop, the default; postconf(5)), so Verified is refused
  for an upstream given by address (checkSecureHost).
- Postfix's SMTP client trusts Debian's ca-certificates bundle
  (smtp_tls_CAfile): Postfix trusts none by default (DEF_SMTP_TLS_CA_FILE
  "", DEF_TLS_APPEND_DEF_CA 0). The levels may and encrypt only log the
  verdict, so mail to recipients' MX is unchanged.
- The page: the select's fourth option, a help line under it, the lead
  "over verified TLS", the list's tag "verified"; Help word for word with
  the mockup; guide § Inbound relay, with a posttls-finger command to try
  an upstream first.

Tests: TestVerifiedTLSNeedsTheUpstreamsName, TestRenderInboundMapsSecure,
TestUpstreamTLSModeSecure; schema tests at 14. e2e
inbound_upstream_verified_tls: an address refused, a name saved as
"[sink]:25 secure" in tls_policy, the page says verified, the CA file set
and present, and posttls-finger -l secure with it calls the stand's
self-signed port 25 "Untrusted" (the stand has no TLS upstream). make e2e
(65 passed) and go vet + go test (26 packages) on the test server for this
tree. Tried against the live upstream from the 2.14.5 container:
mc-ent.mixfed.ru gives "Verified TLS connection established".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 10:27:37 +03:00

95 lines
3.4 KiB
Go

package inbound
import (
"errors"
"strings"
"testing"
"github.com/mixeme/selfpost/internal/postfix"
"github.com/mixeme/selfpost/internal/store"
)
type nullMaps struct{}
func (nullMaps) RebuildInboundMaps([]postfix.InboundRoute) error { return nil }
// An upstream has one TLS mode, since Postfix keys the policy by the next
// hop (audit 2026-10-12, L5): the other domains forwarded to it take the mode
// saved for one of them, when the one saving may change them; one they may
// not is refused, and nothing is saved. Two domains on one upstream can
// change its mode (review of stage 4: they could not, each refused for the
// other).
func TestOneTLSModePerUpstream(t *testing.T) {
st, err := store.Open(t.TempDir() + "/test.db")
if err != nil {
t.Fatal(err)
}
defer st.Close()
s := NewService(st, nullMaps{})
a, _ := s.Add("a.example")
b, _ := s.Add("b.example")
c, _ := s.Add("c.example")
mode := func(id int64) string {
d, err := st.GetInboundDomain(id)
if err != nil {
t.Fatal(err)
}
return d.TLSMode
}
all := func(store.InboundDomain) bool { return true }
if _, err := s.SetTransportAs(a.ID, "10.0.0.8", "25", "encrypt", all); err != nil {
t.Fatal(err)
}
if n, err := s.SetTransportAs(b.ID, "[10.0.0.8]", "25", "encrypt", all); err != nil || n != 0 {
t.Fatalf("the same mode: %d, %v", n, err)
}
// Either domain changes the mode, for both.
if n, err := s.SetTransportAs(a.ID, "10.0.0.8", "25", "may", all); err != nil || n != 1 {
t.Fatalf("a changes the shared mode: %d, %v", n, err)
}
if mode(a.ID) != "may" || mode(b.ID) != "may" {
t.Errorf("modes after a's change: %s, %s", mode(a.ID), mode(b.ID))
}
// Another port is another next hop.
if n, err := s.SetTransportAs(c.ID, "10.0.0.8", "2525", "none", all); err != nil || n != 0 {
t.Errorf("another port: %d, %v", n, err)
}
// One that may not change b is refused, with b's mode, and saves nothing.
notB := func(d store.InboundDomain) bool { return d.ID != b.ID }
_, err = s.SetTransportAs(c.ID, "10.0.0.8", "25", "encrypt", notB)
var shared *UpstreamShared
if !errors.As(err, &shared) || shared.Mode != "may" || shared.Domain != "b.example" || shared.Upstream != "[10.0.0.8]:25" {
t.Fatalf("a mode for another's upstream = %v", err)
}
if d, _ := st.GetInboundDomain(c.ID); d.Port != 2525 || mode(a.ID) != "may" {
t.Errorf("the refused save changed something: c on %d, a %s", d.Port, mode(a.ID))
}
}
// Verified TLS (secure) is saved for an upstream given by name and refused for
// one given by address: Postfix checks the certificate against the next hop's
// name (upstream-tls-verify).
func TestVerifiedTLSNeedsTheUpstreamsName(t *testing.T) {
st, err := store.Open(t.TempDir() + "/test.db")
if err != nil {
t.Fatal(err)
}
defer st.Close()
s := NewService(st, nullMaps{})
d, _ := s.Add("in.example")
for _, host := range []string{"10.0.0.8", "[2001:db8::25]"} {
var v *ValidationError
if err := s.SetTransport(d.ID, host, "25", store.TLSModeSecure); !errors.As(err, &v) || !strings.Contains(err.Error(), "give the upstream by name") {
t.Errorf("Verified TLS to %s: %v, want refused", host, err)
}
}
if err := s.SetTransport(d.ID, "MX.Example.NET", "25", store.TLSModeSecure); err != nil {
t.Fatalf("Verified TLS to a name: %v", err)
}
if got, _ := st.GetInboundDomain(d.ID); got.TLSMode != store.TLSModeSecure || got.Host != "mx.example.net" {
t.Errorf("saved %s %s", got.Host, got.TLSMode)
}
}