fix(config): check base_url at load

- config: empty is fine; otherwise https with a host, http only for localhost/127.0.0.1/::1, no
  userinfo, query, fragment or whitespace; the error names base_url
- test: accepted and refused URLs
- docs: README states the rule
This commit is contained in:
tgiachi 2026-11-10 11:51:38 +01:00
commit f340078aed
3 changed files with 67 additions and 1 deletions

View file

@ -41,7 +41,9 @@ Three listeners, each configurable:
| `web` | `127.0.0.1:8081` | public archive and unsubscribe pages (`""` disables) |
Put the `web` listener behind a reverse proxy with TLS and set `base_url` to its public URL:
the unsubscribe links in every message use it. Keep the `api` listener private.
the unsubscribe links in every message use it. It must be an `https://` URL (RFC 8058 one-click unsubscribe needs
it; `http://` is accepted only for `localhost`, `127.0.0.1` and `::1`) with a host and no credentials, query or fragment;
the daemon refuses to start otherwise. Leave it empty to turn the one-click links off. Keep the `api` listener private.
```
curl -H "Authorization: Bearer $PARSIMONY_API_TOKEN" -d '{"name":"dev","domain":"example.org","title":"Dev"}' localhost:8080/api/v1/lists

View file

@ -7,8 +7,10 @@ import (
"errors"
"fmt"
"net"
"net/url"
"os"
"strings"
"unicode"
"gopkg.in/yaml.v3"
)
@ -145,7 +147,31 @@ func (c Config) validateRelay() error {
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
}

View file

@ -246,3 +246,41 @@ func testCAPEM(t *testing.T) []byte {
}
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
}
func TestBaseURLIsChecked(t *testing.T) {
for _, ok := range []string{
"", // one-click unsubscribe is simply off
"https://lists.example.org",
"https://lists.example.org/",
"https://lists.example.org:8443/sub/path",
"http://localhost:8081",
"http://127.0.0.1:8081",
"http://[::1]:8081",
} {
if _, err := Load(write(t, "base_url: \""+ok+"\"\n")); err != nil {
t.Errorf("%q must be accepted: %v", ok, err)
}
}
for _, bad := range []string{
"http://lists.example.org", // RFC 8058 needs https
"http://10.0.0.5:8081", // only loopback may use http
"ftp://lists.example.org", // not http(s) at all
"lists.example.org", // no scheme
"//lists.example.org", // no scheme
"https://", // no host
"https:///path", // no host
"https://lists.example.org?x=1", // query
"https://lists.example.org/#top", // fragment
"https://user:pw@lists.example.org", // userinfo
"https://user@lists.example.org",
"https://lists.example.org/a b",
"https://exa mple.org",
} {
_, err := Load(write(t, "base_url: \""+bad+"\"\n"))
if err == nil {
t.Errorf("%q must be rejected", bad)
} else if !strings.Contains(err.Error(), "base_url") {
t.Errorf("%q: the error must name base_url: %v", bad, err)
}
}
}