test: close gaps found by mutation checks on DKIM

- Cover headers outside the signed list changing in transit, quoted local parts with @, signing errors and lowercase domains
- Ignore *.pem so a private key is never committed
This commit is contained in:
tgiachi 2026-11-09 20:02:54 +01:00
commit e7cc9cacfb
3 changed files with 59 additions and 0 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@ parsimony
admin/node_modules
internal/adminui/dist/*
!internal/adminui/dist/.gitkeep
# Private keys must never be committed
*.pem

View file

@ -760,3 +760,19 @@ func TestDKIMCheckEd25519Key(t *testing.T) {
t.Fatalf("%d %q %q", code, out, errs)
}
}
func TestDKIMDomainIsLowercased(t *testing.T) {
keyFile := filepath.Join(t.TempDir(), "k.pem")
code, out, errs := noAuth(t, "dkim", "keygen", "Lists.Example.ORG", "--out", keyFile)
if code != 0 {
t.Fatalf("%d %s", code, errs)
}
if !strings.Contains(out, "mail._domainkey.lists.example.org.") || !strings.Contains(out, "domain: lists.example.org") {
t.Fatalf("DNS names and the config use lowercase:\n%s", out)
}
asked := withDNS(t, map[string][]string{})
noAuth(t, "dkim", "check", "Lists.Example.ORG", "--key", keyFile)
if len(*asked) != 1 || (*asked)[0] != "mail._domainkey.lists.example.org" {
t.Fatalf("lookup name: %v", *asked)
}
}

View file

@ -10,6 +10,7 @@ import (
"encoding/base64"
"encoding/pem"
"errors"
"io"
"os"
"path/filepath"
"regexp"
@ -402,3 +403,42 @@ func TestSplitTXTChunks(t *testing.T) {
}
}
}
func TestHeadersOutsideTheSignedListMayChangeInTransit(t *testing.T) {
k := rsaKey(t, 2048)
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
in := append([]byte("X-Transit: original\r\n"), message("dev@lists.example.org")...)
out, _ := s.Sign(in)
changed := bytes.Replace(out, []byte("X-Transit: original"), []byte("X-Transit: rewritten by a relay"), 1)
mustVerify(t, changed, "mail", "lists.example.org", &k.PublicKey)
withReceived := append([]byte("Received: from relay by mx; Mon, 01 Jan 2024 10:00:01 +0000\r\n"), out...)
mustVerify(t, withReceived, "mail", "lists.example.org", &k.PublicKey)
}
func TestFromAddressWithAnAtSignInTheLocalPart(t *testing.T) {
k := rsaKey(t, 2048)
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
out, _ := s.Sign(message(`"odd@name"@lists.example.org`))
mustVerify(t, out, "mail", "lists.example.org", &k.PublicKey)
}
// brokenSigner has an RSA public key but cannot sign.
type brokenSigner struct{ pub crypto.PublicKey }
func (b brokenSigner) Public() crypto.PublicKey { return b.pub }
func (brokenSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
return nil, errors.New("hsm unavailable")
}
func TestASigningErrorReturnsTheOriginalMessageAndTheError(t *testing.T) {
k := rsaKey(t, 2048)
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: brokenSigner{pub: &k.PublicKey}})
in := message("dev@lists.example.org")
out, err := s.Sign(in)
if err == nil || !strings.Contains(err.Error(), "lists.example.org") {
t.Fatalf("the error names the domain: %v", err)
}
if !bytes.Equal(out, in) {
t.Fatal("on error the caller still gets the original message to send unsigned")
}
}