Files
selfpost/internal/postfix/inbound_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

154 lines
4.8 KiB
Go

package postfix
import (
"os"
"path/filepath"
"testing"
)
func TestRenderInboundMaps(t *testing.T) {
routes := []InboundRoute{
{
Domain: "zeta.example", Host: "192.0.2.20", Port: 25,
TLSMode: "none", RecipientMode: "any",
},
{
Domain: "lists.example.com", Host: "10.0.0.8", Port: 25,
TLSMode: "encrypt", RecipientMode: "list",
Recipients: []string{"staff@lists.example.com", "abuse@lists.example.com"},
},
{
Domain: "pending.example", Host: "", Port: 25,
TLSMode: "may", RecipientMode: "list",
},
}
relay, transport, recipients, tlsPolicy, err := renderInboundMaps(routes)
if err != nil {
t.Fatal(err)
}
wantRelay := "lists.example.com OK\nzeta.example OK\n"
if string(relay) != wantRelay {
t.Errorf("relay_domains =\n%q\nwant\n%q", relay, wantRelay)
}
wantTransport := "lists.example.com smtp:[10.0.0.8]:25\nzeta.example smtp:[192.0.2.20]:25\n"
if string(transport) != wantTransport {
t.Errorf("transport =\n%q\nwant\n%q", transport, wantTransport)
}
wantRecipients := "abuse@lists.example.com OK\nstaff@lists.example.com OK\n@zeta.example OK\n"
if string(recipients) != wantRecipients {
t.Errorf("relay_recipients =\n%q\nwant\n%q", recipients, wantRecipients)
}
wantTLS := "[10.0.0.8]:25 encrypt\n[192.0.2.20]:25 none\n"
if string(tlsPolicy) != wantTLS {
t.Errorf("tls_policy =\n%q\nwant\n%q", tlsPolicy, wantTLS)
}
}
func TestRenderInboundMapsIPv6(t *testing.T) {
routes := []InboundRoute{{
Domain: "v6.example", Host: "2001:db8::1", Port: 25,
TLSMode: "may", RecipientMode: "any",
}}
_, transport, _, tlsPolicy, err := renderInboundMaps(routes)
if err != nil {
t.Fatal(err)
}
if string(transport) != "v6.example smtp:[2001:db8::1]:25\n" {
t.Errorf("transport = %q", transport)
}
if string(tlsPolicy) != "[2001:db8::1]:25 may\n" {
t.Errorf("tls_policy = %q", tlsPolicy)
}
}
func TestRenderInboundMapsRejectsInjection(t *testing.T) {
bad := []InboundRoute{
{Domain: "ex ample.com", Host: "10.0.0.1", Port: 25, TLSMode: "may", RecipientMode: "any"},
{Domain: "example.com", Host: "10.0.0.1\nrelay", Port: 25, TLSMode: "may", RecipientMode: "any"},
{Domain: "example.com", Host: "10.0.0.1", Port: 25, TLSMode: "evil", RecipientMode: "any"},
{Domain: "example.com", Host: "10.0.0.1", Port: 25, TLSMode: "may", RecipientMode: "list",
Recipients: []string{"a@example.com OK\nb@evil.com"}},
}
for i, r := range bad {
if _, _, _, _, err := renderInboundMaps([]InboundRoute{r}); err == nil {
t.Errorf("case %d: expected injection rejection", i)
}
}
}
func TestRebuildInboundMapsWritesAndReloads(t *testing.T) {
p, reloads := newTestPostfix(t)
err := p.RebuildInboundMaps([]InboundRoute{{
Domain: "lists.example.com", Host: "10.0.0.8", Port: 25,
TLSMode: "encrypt", RecipientMode: "any",
}})
if err != nil {
t.Fatal(err)
}
if *reloads != 1 {
t.Errorf("reload called %d times, want 1", *reloads)
}
rd, _, _, _ := p.inboundMapPaths()
data, err := os.ReadFile(rd)
if err != nil {
t.Fatal(err)
}
if string(data) != "lists.example.com OK\n" {
t.Errorf("relay_domains file = %q", data)
}
if filepath.Base(rd) != "relay_domains" {
t.Errorf("unexpected path %s", rd)
}
}
func TestRebuildInboundMapsEmptyOmitsPending(t *testing.T) {
p, _ := newTestPostfix(t)
if err := p.RebuildInboundMaps(nil); err != nil {
t.Fatal(err)
}
rd, tr, rc, tl := p.inboundMapPaths()
for _, path := range []string{rd, tr, rc, tl} {
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if len(data) != 0 {
t.Errorf("%s not empty: %q", path, data)
}
}
}
// Two domains forwarded to one next hop share its TLS policy line: the map
// holds the key once (audit 2026-10-12, L5).
func TestRenderInboundMapsOneTLSLinePerNextHop(t *testing.T) {
routes := []InboundRoute{
{Domain: "b.example", Host: "10.0.0.8", Port: 25, TLSMode: "encrypt", RecipientMode: "any"},
{Domain: "a.example", Host: "10.0.0.8", Port: 25, TLSMode: "encrypt", RecipientMode: "any"},
}
_, transport, _, tlsPolicy, err := renderInboundMaps(routes)
if err != nil {
t.Fatal(err)
}
if string(transport) != "a.example smtp:[10.0.0.8]:25\nb.example smtp:[10.0.0.8]:25\n" {
t.Errorf("transport = %q", transport)
}
if string(tlsPolicy) != "[10.0.0.8]:25 encrypt\n" {
t.Errorf("tls_policy = %q, want the next hop once", tlsPolicy)
}
}
// Verified TLS is Postfix's secure level, written as the others are; the
// certificate is matched against the next hop's name by the main.cf default
// (smtp_tls_secure_cert_match = nexthop, dot-nexthop).
func TestRenderInboundMapsSecure(t *testing.T) {
_, _, _, tlsPolicy, err := renderInboundMaps([]InboundRoute{
{Domain: "in.example.com", Host: "mx.example.net", Port: 25, TLSMode: "secure", RecipientMode: "any"},
})
if err != nil {
t.Fatal(err)
}
if string(tlsPolicy) != "[mx.example.net]:25 secure\n" {
t.Errorf("tls_policy = %q", tlsPolicy)
}
}