release: 1.9.2: Review pass: port-25 milters, DMARC validation, rate-limit fix
Review pass over `1.4.0`…`1.9.1`: port 25 no longer shares the submission milter chain, DMARC reports are validated before they are stored, and the auto rate limit is correct for a non-default level-1 window. Schema migration `0010` (index only). Upgrading is a tag bump. Added: - SQLite migration `0010_send_log_app_login_index.sql` — - DMARC aggregate reports are accepted as **zip** as well as gzip or plain XML - e2e: `checkPort25ConfigCombinations` regenerates the Postfix configuration Changed: - README hero line and a **Positioning** block in `docs/product.md` record the Fixed: - The setup account's password is hashed at bcrypt cost `12` like every other - Login/setup rate limiter: protecting blocked buckets from eviction is now - `postqueue -p` output is bounded to 4 MiB (the timeout alone left the size of - [schema-migrations.md](docs/schema-migrations.md): the schema section was - postqueue reader is now time-bounded: `postqueue -p` runs under a 5-second … (11 more in CHANGELOG) Changed: - [roadmap.md](docs/roadmap.md) now carries everything the 2026-08-19 code - Extracted `internal/configsafe` — the empty-value and forbidden-character - Build, deploy and migration comments no longer cite the archived - Dropped the `parseDomainRateLimitForm` / `parseAppRateLimitForm` pass-through - Extracted shared helpers: `internal/atomicfile` (atomic temp-write+rename) and … (7 more in CHANGELOG) Removed: - Unused legacy asset `docs/assets/icon.png` (superseded by the four brand SVGs - Plan documents for already-shipped features: `domain-stats-auto-ratelimit`, Full detail: CHANGELOG.md (1.9.2).
This commit is contained in:
@@ -33,3 +33,21 @@ func TestRenderDMARCMapsEmpty(t *testing.T) {
|
||||
t.Fatalf("expected empty maps")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderDMARCMapsRejectsInjectionTokens(t *testing.T) {
|
||||
_, _, _, err := renderDMARCMaps(DMARCMapsConfig{
|
||||
Recipients: []string{"dmarc@example.com\nattacker@example.com"},
|
||||
Domains: []string{"example.com"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected recipient token validation error")
|
||||
}
|
||||
|
||||
_, _, _, err = renderDMARCMaps(DMARCMapsConfig{
|
||||
Recipients: []string{"dmarc@example.com"},
|
||||
Domains: []string{"example.com attacker"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected domain token validation error")
|
||||
}
|
||||
}
|
||||
|
||||
+12
-11
@@ -2,11 +2,12 @@ package postfix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/configsafe"
|
||||
)
|
||||
|
||||
// InboundRoute is one inbound domain's Postfix map material: the relay
|
||||
@@ -83,12 +84,10 @@ func renderInboundMaps(routes []InboundRoute) (relay, transport, recipients, tls
|
||||
return []byte(relayB.String()), []byte(transportB.String()), []byte(recipB.String()), []byte(tlsB.String()), nil
|
||||
}
|
||||
|
||||
// inboundNexthop is the Postfix next-hop [host]:port form that disables MX
|
||||
// lookup for the explicit upstream.
|
||||
// inboundNexthop is the Postfix next-hop [host]:port form. The brackets are
|
||||
// what disable the MX lookup, so the operator's explicit upstream is used as
|
||||
// given; they also delimit a bare IPv6 address.
|
||||
func inboundNexthop(host string, port int) string {
|
||||
if ip := net.ParseIP(host); ip != nil && ip.To4() == nil {
|
||||
return "[" + host + "]:" + strconv.Itoa(port)
|
||||
}
|
||||
return "[" + host + "]:" + strconv.Itoa(port)
|
||||
}
|
||||
|
||||
@@ -118,13 +117,15 @@ func assertInboundRouteSafe(r InboundRoute) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// texthashForbidden is what may never appear in a texthash line: whitespace and
|
||||
// the newline that would end it, the comma that separates values, and the
|
||||
// backslash.
|
||||
const texthashForbidden = " \t\r\n,\\"
|
||||
|
||||
// assertMapToken rejects values that could break out of a texthash line.
|
||||
func assertMapToken(v, what string) error {
|
||||
if v == "" {
|
||||
return fmt.Errorf("postfix: empty %s", what)
|
||||
}
|
||||
if strings.ContainsAny(v, " \t\r\n,\\") {
|
||||
return fmt.Errorf("postfix: unsafe character in %s %q", what, v)
|
||||
if err := configsafe.Token(what, v, texthashForbidden); err != nil {
|
||||
return fmt.Errorf("postfix: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+17
-21
@@ -7,10 +7,12 @@ package postfix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/configsafe"
|
||||
"github.com/mixeme/selfpost/internal/supervisor"
|
||||
)
|
||||
|
||||
// Postfix manages the on-disk Postfix state the panel is responsible for. After
|
||||
@@ -122,6 +124,15 @@ func appendUnique(list []string, v string) []string {
|
||||
return append(list, v)
|
||||
}
|
||||
|
||||
// What may never reach a sender-map line: whitespace and the newline that would
|
||||
// end it, the comma that separates values, the colon a lookup key cannot carry,
|
||||
// and the backslash. A login additionally may not carry "@" — that would make it
|
||||
// look like an address.
|
||||
const (
|
||||
senderMapAddressForbidden = " \t\r\n,:\\"
|
||||
senderMapLoginForbidden = " \t\r\n,:@\\"
|
||||
)
|
||||
|
||||
// assertMapSafe rejects any address/login value that could break out of a single
|
||||
// map line or inject a directive. Addresses are validated to a strict whitelist
|
||||
// (letters, digits, '@', '.', '-', '_', '+') and logins to an even stricter one
|
||||
@@ -129,14 +140,11 @@ func appendUnique(list []string, v string) []string {
|
||||
// letting whitespace, a newline or a comma (the value separator) through into
|
||||
// the file (security.md).
|
||||
func assertMapSafe(address, login string) error {
|
||||
if address == "" || login == "" {
|
||||
return fmt.Errorf("postfix: empty address or login")
|
||||
if err := configsafe.Token("address", address, senderMapAddressForbidden); err != nil {
|
||||
return fmt.Errorf("postfix: %w", err)
|
||||
}
|
||||
if strings.ContainsAny(address, " \t\r\n,:\\") {
|
||||
return fmt.Errorf("postfix: unsafe character in address %q", address)
|
||||
}
|
||||
if strings.ContainsAny(login, " \t\r\n,:@\\") {
|
||||
return fmt.Errorf("postfix: unsafe character in login %q", login)
|
||||
if err := configsafe.Token("login", login, senderMapLoginForbidden); err != nil {
|
||||
return fmt.Errorf("postfix: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -154,17 +162,5 @@ func assertMapSafe(address, login string) error {
|
||||
// Arguments are fixed literals — no user input is interpolated into the command,
|
||||
// and it never goes through a shell (security.md).
|
||||
func reloadViaSupervisor() error {
|
||||
cmd := exec.Command("supervisorctl",
|
||||
"-c", "/etc/supervisor/supervisord.conf",
|
||||
"start", "postfix-reload")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// A reload already in flight is not a failure: that pending run reloads
|
||||
// Postfix after our file is in place (the file is written before this).
|
||||
if strings.Contains(string(out), "already started") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("reload postfix via supervisor: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return nil
|
||||
return supervisor.Start("postfix-reload")
|
||||
}
|
||||
|
||||
@@ -1,12 +1,46 @@
|
||||
package postfix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const postqueueTimeout = 5 * time.Second
|
||||
|
||||
// maxQueueOutput bounds how much of `postqueue -p` the panel keeps. A deferred
|
||||
// queue with a very large backlog prints one paragraph per message, and the
|
||||
// whole listing is held in memory and then rendered into a page — so the read
|
||||
// is capped and the remainder reported as truncated rather than being allowed
|
||||
// to size the panel's memory by the size of the queue.
|
||||
const maxQueueOutput = 4 << 20
|
||||
|
||||
// limitWriter keeps the first remaining bytes written to it and drops the rest,
|
||||
// recording that it did. Writes always report full consumption so postqueue is
|
||||
// never killed by a short write; the extra output is simply discarded.
|
||||
type limitWriter struct {
|
||||
buf bytes.Buffer
|
||||
remaining int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (l *limitWriter) Write(p []byte) (int, error) {
|
||||
keep := len(p)
|
||||
if keep > l.remaining {
|
||||
keep = l.remaining
|
||||
l.truncated = true
|
||||
}
|
||||
if keep > 0 {
|
||||
l.buf.Write(p[:keep])
|
||||
l.remaining -= keep
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Queue returns Postfix's own human-readable mail-queue listing
|
||||
// (architecture.md § Panel HTTP surface): active, deferred and held messages,
|
||||
// exactly as an administrator would see via the CLI. The command takes a
|
||||
@@ -14,12 +48,24 @@ import (
|
||||
// (security.md). The panel is responsible for escaping the output before
|
||||
// display (security.md); this function returns it as-is.
|
||||
func Queue() (string, error) {
|
||||
cmd := exec.Command("postqueue", "-p")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("postqueue -p: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), postqueueTimeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "postqueue", "-p")
|
||||
out := &limitWriter{remaining: maxQueueOutput}
|
||||
cmd.Stdout = out
|
||||
cmd.Stderr = out
|
||||
err := cmd.Run()
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
return "", fmt.Errorf("postqueue -p timed out after %s", postqueueTimeout)
|
||||
}
|
||||
return string(out), nil
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("postqueue -p: %w: %s", err, strings.TrimSpace(out.buf.String()))
|
||||
}
|
||||
text := out.buf.String()
|
||||
if out.truncated {
|
||||
text += fmt.Sprintf("\n[listing truncated at %d bytes]\n", maxQueueOutput)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// QueueIDs returns the set of queue ids Postfix is still holding — everything
|
||||
|
||||
@@ -41,3 +41,34 @@ func TestParseQueueIDsOnAnEmptyQueue(t *testing.T) {
|
||||
t.Errorf("got %v, want no ids", ids)
|
||||
}
|
||||
}
|
||||
|
||||
// The queue listing is held in memory and rendered into a page, so a very large
|
||||
// queue must not size the panel's allocation: the writer keeps the cap and
|
||||
// reports the rest as dropped, while still accepting every write so postqueue
|
||||
// itself is never cut short.
|
||||
func TestLimitWriterCapsOutput(t *testing.T) {
|
||||
w := &limitWriter{remaining: 8}
|
||||
n, err := w.Write([]byte("12345"))
|
||||
if n != 5 || err != nil {
|
||||
t.Fatalf("first write = %d, %v", n, err)
|
||||
}
|
||||
if w.truncated {
|
||||
t.Fatal("truncated before the cap was reached")
|
||||
}
|
||||
n, err = w.Write([]byte("67890"))
|
||||
if n != 5 || err != nil {
|
||||
t.Fatalf("second write = %d, %v", n, err)
|
||||
}
|
||||
if !w.truncated {
|
||||
t.Fatal("write past the cap was not recorded as truncated")
|
||||
}
|
||||
if got := w.buf.String(); got != "12345678" {
|
||||
t.Fatalf("kept %q, want the first 8 bytes", got)
|
||||
}
|
||||
if n, err := w.Write([]byte("more")); n != 4 || err != nil {
|
||||
t.Fatalf("write after the cap = %d, %v (must still accept)", n, err)
|
||||
}
|
||||
if got := w.buf.String(); got != "12345678" {
|
||||
t.Fatalf("buffer grew past the cap: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,14 @@
|
||||
package postfix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mixeme/selfpost/internal/atomicfile"
|
||||
)
|
||||
|
||||
// writeFileAtomic writes data to path via a temp file in the same directory
|
||||
// followed by a rename, so a concurrent Postfix reload only ever sees the
|
||||
// complete old or new map, never a partial write (security.md). It mirrors the
|
||||
// same primitive used for the OpenDKIM tables.
|
||||
// complete old or new map, never a partial write (security.md).
|
||||
func writeFileAtomic(path string, data []byte, perm os.FileMode) error {
|
||||
dir := filepath.Dir(path)
|
||||
tmp, err := os.CreateTemp(dir, ".tmp-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temp in %s: %w", dir, err)
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
cleanup := true
|
||||
defer func() {
|
||||
if cleanup {
|
||||
_ = os.Remove(tmpName)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("write temp %s: %w", tmpName, err)
|
||||
}
|
||||
if err := tmp.Chmod(perm); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("chmod temp %s: %w", tmpName, err)
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("sync temp %s: %w", tmpName, err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return fmt.Errorf("close temp %s: %w", tmpName, err)
|
||||
}
|
||||
if err := os.Rename(tmpName, path); err != nil {
|
||||
return fmt.Errorf("rename %s -> %s: %w", tmpName, path, err)
|
||||
}
|
||||
cleanup = false
|
||||
return nil
|
||||
return atomicfile.Write(path, data, perm)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user