parsimony/internal/config/config.go
tgiachi 44b3f9d98a fix(web): key the subscribe limit on the real client
Behind a reverse proxy every visitor shared the proxy's address, so the
whole site got 10 signups an hour and one person could block everyone.
Over IPv6 each address of a /64 was a fresh key, so the limit did not
bind and the map grew by one entry per address.

- web.trusted_proxies (addresses or CIDR ranges, default empty) is
  validated at load and passed to the web handler. X-Forwarded-For is
  read only when the TCP peer is a trusted proxy; the client is then the
  right-most entry that is not itself a trusted proxy. An empty or
  malformed header falls back to the peer. With no trusted proxy the
  behaviour is unchanged: the header is ignored.
- IPv6 clients are keyed by their /64, IPv4 clients by address.
- The limiter remembers at most 100000 clients. When full it sweeps
  expired entries first and otherwise answers 429 to new clients until
  the oldest window ends, instead of growing.
2026-11-10 15:11:26 +01:00

262 lines
8.8 KiB
Go

// Package config loads the YAML configuration.
package config
import (
"bytes"
"crypto/x509"
"errors"
"fmt"
"math"
"net"
"net/url"
"os"
"strings"
"unicode"
"gopkg.in/yaml.v3"
)
// Config is the full daemon configuration.
type Config struct {
DB string `yaml:"db"` // SQLite path
BaseURL string `yaml:"base_url"` // public web URL, enables one-click unsubscribe
LMTP struct {
Network string `yaml:"network"` // tcp or unix
Addr string `yaml:"addr"`
// Allow limits which clients may connect (CIDR ranges or single addresses, TCP only).
// LMTP has no authentication: anyone who can connect can post as any member.
Allow []string `yaml:"allow"`
} `yaml:"lmtp"`
API struct {
Listen string `yaml:"listen"` // HTTP listen address; the bearer token comes from PARSIMONY_API_TOKEN
} `yaml:"api"`
Web struct {
Listen string `yaml:"listen"` // public archive and unsubscribe pages; empty disables them
// TrustedProxies are the reverse proxies (CIDR ranges or single addresses) whose
// X-Forwarded-For names the client of the subscribe form's rate limit. Empty, the default:
// the TCP peer is the client and the header is ignored.
TrustedProxies []string `yaml:"trusted_proxies"`
} `yaml:"web"`
// DKIM lists the signing keys, one per domain. The private keys stay in files.
DKIM []DKIMKey `yaml:"dkim"`
Relay struct {
Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay
Concurrency int `yaml:"concurrency"` // simultaneous deliveries
// RatePerSecond is the most sends started per second, for relays that throttle (Amazon
// SES). Zero, the default, is unlimited.
RatePerSecond Rate `yaml:"rate_per_second"`
// TLS is "none" (default: a local Postfix), "starttls" (required, never silently skipped)
// or "tls" (encrypted from the first byte, port 465).
TLS string `yaml:"tls"`
ServerName string `yaml:"server_name"` // name the certificate must match; the host of addr by default
CAFile string `yaml:"ca_file"` // PEM bundle of the CAs to trust; the system's by default
Helo string `yaml:"helo"` // EHLO name; this machine's host name by default
// Username and the content of PasswordFile are the SMTP AUTH PLAIN credentials. The password
// is never in the config itself.
Username string `yaml:"username"`
PasswordFile string `yaml:"password_file"`
} `yaml:"relay"`
}
// Rate is a number of sends per second. It exists so that a value that is not a number is reported
// with the name of the key, as the other checks are.
type Rate float64
// UnmarshalYAML reads a YAML number.
func (r *Rate) UnmarshalYAML(n *yaml.Node) error {
var f float64
if err := n.Decode(&f); err != nil {
return fmt.Errorf("line %d: relay.rate_per_second must be a number of sends per second, not %q", n.Line, n.Value)
}
*r = Rate(f)
return nil
}
// DKIMKey is the signing key of one domain.
type DKIMKey struct {
Domain string `yaml:"domain"`
Selector string `yaml:"selector"`
KeyFile string `yaml:"key_file"` // PEM private key
}
// LMTPAllowed returns the parsed lmtp.allow list (empty: every client is accepted).
func (c Config) LMTPAllowed() []*net.IPNet { return parseNets(c.LMTP.Allow) }
// WebTrustedProxies returns the parsed web.trusted_proxies list (empty: X-Forwarded-For is ignored).
func (c Config) WebTrustedProxies() []*net.IPNet { return parseNets(c.Web.TrustedProxies) }
// parseNets parses a list that validate has already checked.
func parseNets(list []string) []*net.IPNet {
nets := make([]*net.IPNet, 0, len(list))
for _, a := range list {
if n, err := parseNet(a); err == nil {
nets = append(nets, n)
}
}
return nets
}
// parseNet accepts "10.0.0.0/8" or a single address ("10.1.2.3", "::1").
func parseNet(s string) (*net.IPNet, error) {
s = strings.TrimSpace(s)
if strings.Contains(s, "/") {
_, n, err := net.ParseCIDR(s)
return n, err
}
ip := net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("not an IP address or CIDR range: %q", s)
}
bits := 128
if ip4 := ip.To4(); ip4 != nil {
ip, bits = ip4, 32
}
return &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)}, nil
}
func isLoopbackHost(host string) bool {
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}
// RelayPassword reads the relay password from its file (only the line ending is removed). It is
// empty when no username is configured.
func (c Config) RelayPassword() (string, error) {
if c.Relay.Username == "" {
return "", nil
}
b, err := os.ReadFile(c.Relay.PasswordFile)
if err != nil {
return "", fmt.Errorf("relay.password_file: %w", err)
}
pw := strings.TrimRight(string(b), "\r\n")
if pw == "" {
return "", fmt.Errorf("relay.password_file %s is empty", c.Relay.PasswordFile)
}
return pw, nil
}
// RelayRootCAs reads relay.ca_file. It returns nil when none is set: the system roots apply.
func (c Config) RelayRootCAs() (*x509.CertPool, error) {
if c.Relay.CAFile == "" {
return nil, nil
}
b, err := os.ReadFile(c.Relay.CAFile)
if err != nil {
return nil, fmt.Errorf("relay.ca_file: %w", err)
}
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("relay.ca_file %s contains no certificate", c.Relay.CAFile)
}
return pool, nil
}
func (c Config) validateRelay() error {
r := c.Relay
host, _, err := net.SplitHostPort(r.Addr)
if err != nil {
return fmt.Errorf("relay.addr %q must be host:port", r.Addr)
}
switch r.TLS {
case "", "none", "starttls", "tls":
default:
return fmt.Errorf("relay.tls must be none, starttls or tls, not %q", r.TLS)
}
if math.IsNaN(float64(r.RatePerSecond)) || math.IsInf(float64(r.RatePerSecond), 0) || r.RatePerSecond < 0 {
return fmt.Errorf("relay.rate_per_second must be a number of sends per second, 0 or more (0 is unlimited), not %v", r.RatePerSecond)
}
if (r.Username == "") != (r.PasswordFile == "") {
return errors.New("relay.username and relay.password_file go together")
}
if r.Username != "" && (r.TLS == "" || r.TLS == "none") && !isLoopbackHost(host) {
return fmt.Errorf("relay.username is set but relay.tls is not: the password would cross the network in clear to %s; set relay.tls to starttls or tls", host)
}
return nil
}
// validateBaseURL checks base_url, the public URL the one-click unsubscribe links are built from.
// RFC 8058 needs https; http is accepted for a loopback host (development and tests). Empty is fine:
// there is no one-click link then.
func (c Config) validateBaseURL() error {
if c.BaseURL == "" {
return nil
}
u, err := url.Parse(c.BaseURL)
switch {
case err != nil || strings.ContainsFunc(c.BaseURL, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }):
return fmt.Errorf("base_url %q is not a valid URL", c.BaseURL)
case u.Hostname() == "":
return fmt.Errorf("base_url %q needs a scheme and a host, such as https://lists.example.org", c.BaseURL)
case u.Scheme != "https" && !(u.Scheme == "http" && isLoopbackHost(u.Hostname())):
return fmt.Errorf("base_url %q must use https (one-click unsubscribe, RFC 8058); http is only accepted for localhost", c.BaseURL)
case u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || strings.Contains(c.BaseURL, "#"):
return fmt.Errorf("base_url %q must not contain credentials, a query or a fragment", c.BaseURL)
}
return nil
}
func (c Config) validate() error {
if err := c.validateBaseURL(); err != nil {
return err
}
if err := c.validateRelay(); err != nil {
return err
}
for i, a := range c.LMTP.Allow {
if _, err := parseNet(a); err != nil {
return fmt.Errorf("lmtp.allow[%d]: %w", i, err)
}
}
for i, a := range c.Web.TrustedProxies {
if _, err := parseNet(a); err != nil {
return fmt.Errorf("web.trusted_proxies[%d]: %w", i, err)
}
}
seen := map[string]bool{}
for i, k := range c.DKIM {
switch {
case strings.TrimSpace(k.Domain) == "":
return fmt.Errorf("dkim[%d]: domain is required", i)
case strings.TrimSpace(k.Selector) == "":
return fmt.Errorf("dkim[%d] (%s): selector is required", i, k.Domain)
case strings.TrimSpace(k.KeyFile) == "":
return fmt.Errorf("dkim[%d] (%s): key_file is required", i, k.Domain)
}
d := strings.ToLower(k.Domain)
if seen[d] {
return fmt.Errorf("dkim[%d]: more than one key for %s", i, k.Domain)
}
seen[d] = true
}
return nil
}
// Load reads path and applies defaults.
func Load(path string) (Config, error) {
var c Config
c.DB = "parsimony.db"
c.LMTP.Network = "tcp"
c.LMTP.Addr = "127.0.0.1:2424"
c.Relay.Addr = "127.0.0.1:25"
c.Relay.Concurrency = 4
c.API.Listen = "127.0.0.1:8080"
c.Web.Listen = "127.0.0.1:8081"
b, err := os.ReadFile(path)
if err != nil {
return c, err
}
dec := yaml.NewDecoder(bytes.NewReader(b))
dec.KnownFields(true)
if err := dec.Decode(&c); err != nil {
return c, fmt.Errorf("parse %s: %w", path, err)
}
if err := c.validate(); err != nil {
return c, fmt.Errorf("%s: %w", path, err)
}
return c, nil
}