Compare commits

..
2 Commits
Author SHA1 Message Date
clawbot 6259832fb3 Allow underscores in allowlist_hosts and referer_blocklist host names
check / check (push) Failing after 1s
The shared entry check refused host names with an underscore, though
both lists can match them. The check's comment now says that what it
refuses can never match a host name that resolves.

Model: opus-5-5
2026-10-04 19:46:10 +00:00
clawbot 29ff9e4463 Add failing tests for host names with an underscore
Both allowlist_hosts and referer_blocklist should accept a host name
with an underscore, such as my_bucket.example.com, since pixa fetches
from such hosts and pages are served from them.

Model: opus-5-5
2026-10-04 19:46:10 +00:00
5 changed files with 56 additions and 16 deletions

No files matched your search

+3 -3
View File
@@ -396,9 +396,9 @@ and the URL is
`images.example.com`, and `example.com` `images.example.com`, and `example.com`
An IP address is matched exactly; write an IPv6 address without brackets. An An IP address is matched exactly; write an IPv6 address without brackets. An
entry that is neither a host name (letters, digits, hyphens and dots, with at entry that is neither a host name (letters, digits, hyphens, underscores and
most one leading dot) nor an IP address, such as one with a port or a `*.` dots, with at most one leading dot) nor an IP address, such as one with a port
wildcard, aborts startup. or a `*.` wildcard, aborts startup.
### Configuration ### Configuration
+9 -9
View File
@@ -34,15 +34,15 @@ P2: security: per-IP rate limiting on the image routes
- 2026-10-04 referer blocklist (closes #90): `referer_blocklist` - 2026-10-04 referer blocklist (closes #90): `referer_blocklist`
(`PIXA_REFERER_BLOCKLIST`) lists hosts, written and matched as for (`PIXA_REFERER_BLOCKLIST`) lists hosts, written and matched as for
`allowlist_hosts` with the same matcher; an entry of either list that is `allowlist_hosts` with the same matcher; an entry of either list that is
neither a host name (letters, digits, hyphens and dots, with at most one neither a host name (letters, digits, hyphens, underscores and dots, with at
leading dot) nor an IP address, such as one with a port or a `*.` wildcard, most one leading dot) nor an IP address, such as one with a port or a `*.`
aborts startup naming the setting and the entry. Both image routes refuse a wildcard, aborts startup naming the setting and the entry. Both image routes
request whose `Referer` names a listed host with 403 and a JSON error before refuse a request whose `Referer` names a listed host with 403 and a JSON error
the signature, the cache and the upstream fetch, so it fetches nothing and is before the signature, the cache and the upstream fetch, so it fetches nothing
refused whether or not the image is cached. A request with no `Referer`, or and is refused whether or not the image is cached. A request with no
one that does not parse as a URL with a host, is served, so the list is easily `Referer`, or one that does not parse as a URL with a host, is served, so the
got around; `README.md` and `configs/config.example.yml` say so. It does not list is easily got around; `README.md` and `configs/config.example.yml` say
apply to the login and generator pages. so. It does not apply to the login and generator pages.
- 2026-10-04 fewer files in the repository root (closes #97): - 2026-10-04 fewer files in the repository root (closes #97):
`config.example.yml` moved unchanged to `configs/config.example.yml`, and `config.example.yml` moved unchanged to `configs/config.example.yml`, and
`README.md`, the comments in `internal/config/config.go` and the startup error `README.md`, the comments in `internal/config/config.go` and the startup error
+5 -4
View File
@@ -742,14 +742,15 @@ func (c *Config) validateConcurrencyLimits() error {
return nil return nil
} }
// hostNamePattern matches a host name: letters, digits, hyphens and dots, // hostNamePattern matches a host name: letters, digits, hyphens, underscores
// optionally after one leading dot. // and dots, optionally after one leading dot.
var hostNamePattern = regexp.MustCompile(`^\.?[A-Za-z0-9-][A-Za-z0-9.-]*$`) var hostNamePattern = regexp.MustCompile(`^\.?[A-Za-z0-9_-][A-Za-z0-9_.-]*$`)
// validateHostPattern checks that an entry of the named key, allowlist_hosts // validateHostPattern checks that an entry of the named key, allowlist_hosts
// or referer_blocklist, is an IP address or a host name, the host name // or referer_blocklist, is an IP address or a host name, the host name
// optionally with one leading dot for suffix matching. Anything else, such as // optionally with one leading dot for suffix matching. Anything else, such as
// a URL, a port or a "*." wildcard, can never match a host, so it is refused. // a URL, a port or a "*." wildcard, can never match a host name that resolves,
// so it is refused.
// So is "." alone: the allowlist matcher would match it against any host // So is "." alone: the allowlist matcher would match it against any host
// written with a trailing dot, which in allowlist_hosts disables URL signing. // written with a trailing dot, which in allowlist_hosts disables URL signing.
func validateHostPattern(key, host string) error { func validateHostPattern(key, host string) error {
@@ -5,6 +5,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"testing" "testing"
"time" "time"
@@ -216,6 +217,25 @@ func TestCommaSeparatedAllowlistStillSupported(t *testing.T) {
} }
} }
// TestAllowlistHostsAcceptsUnderscore checks that an upstream host name with
// an underscore, which pixa can fetch from, is accepted as an entry.
func TestAllowlistHostsAcceptsUnderscore(t *testing.T) {
t.Parallel()
c, err := configFromYAML(t, signingKeyLine+`allowlist_hosts:
- my_bucket.example.com
- .my_bucket.example.org
`)
if err != nil {
t.Fatalf("host names with an underscore should load, got error: %v", err)
}
want := []string{"my_bucket.example.com", ".my_bucket.example.org"}
if !slices.Equal(c.AllowlistHosts, want) {
t.Errorf("AllowlistHosts = %v, want %v", c.AllowlistHosts, want)
}
}
// runAbortCases asserts that each case's config aborts startup with an // runAbortCases asserts that each case's config aborts startup with an
// error message mentioning every expected substring. // error message mentioning every expected substring.
func runAbortCases(t *testing.T, cases []abortCase) { func runAbortCases(t *testing.T, cases []abortCase) {
@@ -43,6 +43,25 @@ func TestRefererBlocklistAcceptsIPAddresses(t *testing.T) {
} }
} }
// TestRefererBlocklistAcceptsUnderscore checks that a host name with an
// underscore, which a page can be served from, is accepted as an entry.
func TestRefererBlocklistAcceptsUnderscore(t *testing.T) {
t.Parallel()
c, err := configFromYAML(t, signingKeyLine+`referer_blocklist:
- my_site.leech.example
- .my_site.hotlinker.example
`)
if err != nil {
t.Fatalf("host names with an underscore should load, got error: %v", err)
}
want := []string{"my_site.leech.example", ".my_site.hotlinker.example"}
if !slices.Equal(c.RefererBlocklist, want) {
t.Errorf("RefererBlocklist = %v, want %v", c.RefererBlocklist, want)
}
}
// TestRefererBlocklistOmittedIsEmpty checks that an omitted key blocks no // TestRefererBlocklistOmittedIsEmpty checks that an omitted key blocks no
// referer. // referer.
func TestRefererBlocklistOmittedIsEmpty(t *testing.T) { func TestRefererBlocklistOmittedIsEmpty(t *testing.T) {