feat(router): rewrite headers for list delivery

- Add List-Id, List-Post, List-Unsubscribe and Precedence headers
- Add one-click unsubscribe (RFC 8058) when a base URL is configured
- Strip incoming List-* headers to prevent spoofing
- Rewrite From as "Name via List" and keep the original in Reply-To
- Copy the message body byte for byte
This commit is contained in:
tgiachi 2026-11-09 14:50:56 +01:00
commit d8f31d9b3e
5 changed files with 181 additions and 3 deletions

5
go.mod
View file

@ -2,7 +2,10 @@ module codeberg.org/squidd/parsimony
go 1.27.1
require modernc.org/sqlite v1.62.0
require (
github.com/emersion/go-message v0.18.2
modernc.org/sqlite v1.62.0
)
require (
github.com/dustin/go-humanize v1.1.0 // indirect

33
go.sum
View file

@ -1,5 +1,7 @@
github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg=
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
github.com/emersion/go-message v0.18.2 h1:rl55SQdjd9oJcIoQNhubD2Acs1E6IzlZISRTK7x/Lpg=
github.com/emersion/go-message v0.18.2/go.mod h1:XpJyL70LwRvq2a8rVbHXikPgKj8+aI0kGdHlg16ibYA=
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs=
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@ -12,14 +14,45 @@ github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL
github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs=
golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE=
modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4=

View file

@ -0,0 +1,64 @@
package router
import (
"bufio"
"bytes"
"fmt"
"io"
"net/mail"
"strings"
"github.com/emersion/go-message/textproto"
"codeberg.org/squidd/parsimony/internal/store"
)
// rewrite adds list headers and rewrites From so DMARC alignment holds.
// The body is copied byte for byte.
func rewrite(l store.List, baseURL string, data []byte) ([]byte, error) {
br := bufio.NewReader(bytes.NewReader(data))
h, err := textproto.ReadHeader(br)
if err != nil {
return nil, fmt.Errorf("parse header: %w", err)
}
// Never trust list headers coming from outside.
for _, k := range []string{"List-Id", "List-Post", "List-Unsubscribe", "List-Unsubscribe-Post", "Precedence"} {
h.Del(k)
}
id := l.Name + "." + l.Domain
h.Set("List-Id", fmt.Sprintf("%s <%s>", l.Title, id))
h.Set("List-Post", "<mailto:"+l.Address()+">")
unsub := "<mailto:" + l.Name + "+unsubscribe@" + l.Domain + ">"
if baseURL != "" {
unsub = "<" + strings.TrimRight(baseURL, "/") + "/unsubscribe/" + id + ">, " + unsub
h.Set("List-Unsubscribe-Post", "List-Unsubscribe=One-Click")
}
h.Set("List-Unsubscribe", unsub)
h.Set("Precedence", "list")
if orig := h.Get("From"); orig != "" {
name := orig
if a, err := mail.ParseAddress(orig); err == nil {
name = a.Name
if name == "" {
name = a.Address
}
}
if h.Get("Reply-To") == "" {
h.Set("Reply-To", orig)
}
from := mail.Address{Name: name + " via " + l.Title, Address: l.Address()}
h.Set("From", from.String())
}
var out bytes.Buffer
if err := textproto.WriteHeader(&out, h); err != nil {
return nil, err
}
if _, err := io.Copy(&out, br); err != nil {
return nil, err
}
return out.Bytes(), nil
}

View file

@ -22,7 +22,11 @@ type Result struct {
}
// Router applies list policy to incoming messages.
type Router struct{ store *store.Store }
type Router struct {
store *store.Store
// BaseURL is the public web URL; enables one-click unsubscribe when set.
BaseURL string
}
// New creates a Router backed by s.
func New(s *store.Store) *Router { return &Router{store: s} }
@ -51,5 +55,9 @@ func (r *Router) Route(sender, rcpt string, data []byte) (Result, error) {
for i, m := range members {
rcpts[i] = m.Address
}
return Result{List: l, Recipients: rcpts, Data: data}, nil
out, err := rewrite(l, r.BaseURL, data)
if err != nil {
return Result{}, err
}
return Result{List: l, Recipients: rcpts, Data: out}, nil
}

View file

@ -1,7 +1,10 @@
package router
import (
"bytes"
"errors"
"net/mail"
"strings"
"testing"
"codeberg.org/squidd/parsimony/internal/store"
@ -56,3 +59,70 @@ func TestRouteUnknownList(t *testing.T) {
t.Fatalf("want ErrUnknownList, got %v", err)
}
}
func header(t *testing.T, data []byte) mail.Header {
t.Helper()
m, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
t.Fatalf("parse: %v", err)
}
return m.Header
}
const sample = "From: Alice <alice@example.com>\r\nTo: dev@example.org\r\nSubject: hi\r\nList-Id: <spoof.example>\r\n\r\nbody line\r\n"
func TestRewriteHeaders(t *testing.T) {
r := setup(t)
r.BaseURL = "https://lists.example.org"
res, err := r.Route("alice@example.com", "dev@example.org", []byte(sample))
if err != nil {
t.Fatal(err)
}
h := header(t, res.Data)
if got := h.Get("List-Id"); got != "Dev <dev.example.org>" {
t.Errorf("List-Id = %q", got)
}
if got := h.Get("List-Post"); got != "<mailto:dev@example.org>" {
t.Errorf("List-Post = %q", got)
}
if got := h.Get("List-Unsubscribe"); !strings.Contains(got, "<https://lists.example.org/unsubscribe/dev.example.org>") ||
!strings.Contains(got, "<mailto:dev+unsubscribe@example.org>") {
t.Errorf("List-Unsubscribe = %q", got)
}
if got := h.Get("List-Unsubscribe-Post"); got != "List-Unsubscribe=One-Click" {
t.Errorf("List-Unsubscribe-Post = %q", got)
}
if got := h.Get("From"); got != `"Alice via Dev" <dev@example.org>` {
t.Errorf("From = %q", got)
}
if got := h.Get("Reply-To"); got != "Alice <alice@example.com>" {
t.Errorf("Reply-To = %q", got)
}
if !strings.HasSuffix(string(res.Data), "\r\n\r\nbody line\r\n") {
t.Errorf("body altered: %q", res.Data)
}
if n := len(h["List-Id"]); n != 1 {
t.Errorf("want a single List-Id, got %d", n)
}
}
func TestRewriteKeepsExistingReplyTo(t *testing.T) {
r := setup(t)
msg := "From: Alice <alice@example.com>\r\nReply-To: team@example.com\r\n\r\nx\r\n"
res, _ := r.Route("alice@example.com", "dev@example.org", []byte(msg))
if got := header(t, res.Data).Get("Reply-To"); got != "team@example.com" {
t.Errorf("Reply-To = %q", got)
}
}
func TestNoOneClickWithoutBaseURL(t *testing.T) {
r := setup(t)
res, _ := r.Route("alice@example.com", "dev@example.org", []byte(sample))
h := header(t, res.Data)
if h.Get("List-Unsubscribe-Post") != "" {
t.Error("one-click header needs a base URL")
}
if got := h.Get("List-Unsubscribe"); got != "<mailto:dev+unsubscribe@example.org>" {
t.Errorf("List-Unsubscribe = %q", got)
}
}