From 096eead53c77968ab2b9253bb02a5ff2b2f11565 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:38:34 +0100 Subject: [PATCH] fix(egress): canonicalize host keys across IP/IDNA/port/scheme forms (#657) Applies #657 (canonical netip/Unmap, IDNA ASCII, port normalization) and normalizes scheme case before defaulting ports. Co-authored-by: Coff0xc <184964491+Coff0xc@users.noreply.github.com> --- egress/egress.go | 21 ++++++++++-- egress/hostkey_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 egress/hostkey_test.go diff --git a/egress/egress.go b/egress/egress.go index b95abdc4..b2ba5832 100644 --- a/egress/egress.go +++ b/egress/egress.go @@ -17,6 +17,7 @@ import ( "math/rand/v2" "net" "net/http" + "net/netip" "net/url" "os" "sort" @@ -28,6 +29,7 @@ import ( "unicode" "github.com/Autumn-27/artex/intercept" + "golang.org/x/net/idna" ) // Profile is one noise level. The zero Profile is off. Later levers (scanner @@ -177,10 +179,23 @@ func (g *Governor) off() bool { return g.p.RPS <= 0 } // Profile is the profile g was built for. func (g *Governor) Profile() Profile { return g.p } -// HostKey is the lowercased host, with the port when it is not the default. +// HostKey groups equivalent DNS names and IP literals into one pacing bucket. +// It does not resolve DNS: distinct virtual hosts keep independent budgets. func HostKey(u *url.URL) string { - h, p := strings.ToLower(u.Hostname()), u.Port() - if p == "" || (u.Scheme == "http" && p == "80") || (u.Scheme == "https" && p == "443") { + h, p := u.Hostname(), u.Port() + if addr, err := netip.ParseAddr(h); err == nil { + h = addr.Unmap().String() // preserve a scoped IPv6 address's zone + } else { + h = strings.TrimSuffix(strings.ToLower(h), ".") + if ascii, err := idna.Lookup.ToASCII(h); err == nil { + h = strings.TrimSuffix(ascii, ".") + } + } + if port, err := strconv.ParseUint(p, 10, 16); err == nil { + p = strconv.FormatUint(port, 10) + } + scheme := strings.ToLower(u.Scheme) + if p == "" || (scheme == "http" && p == "80") || (scheme == "https" && p == "443") { return h } return net.JoinHostPort(h, p) diff --git a/egress/hostkey_test.go b/egress/hostkey_test.go new file mode 100644 index 00000000..fdc3f284 --- /dev/null +++ b/egress/hostkey_test.go @@ -0,0 +1,73 @@ +package egress + +import ( + "context" + "errors" + "net/url" + "testing" + "time" +) + +func TestHostKeyEquivalentSpellings(t *testing.T) { + for _, group := range [][]string{ + {"http://example.test", "http://EXAMPLE.test./", "http://example.test:00080", "HTTP://EXAMPLE.test:80"}, + {"https://example.test:8443", "https://EXAMPLE.test.:08443"}, + {"http://[2001:db8::1]", "http://[2001:0db8:0:0:0:0:0:1]"}, + {"http://192.0.2.1", "http://[::ffff:192.0.2.1]"}, + {"https://bücher.example", "https://xn--bcher-kva.example."}, + } { + first, _ := url.Parse(group[0]) + for _, raw := range group[1:] { + u, err := url.Parse(raw) + if err != nil { + t.Fatal(err) + } + if got, want := HostKey(u), HostKey(first); got != want { + t.Errorf("HostKey(%q) = %q, want %q", raw, got, want) + } + } + } + for _, pair := range [][2]string{ + {"http://a.example", "http://b.example"}, + {"http://example.test:8080", "http://example.test:8081"}, + {"http://[fe80::1%25eth0]", "http://[fe80::1%25eth1]"}, + {"http://[fe80::1%25ETH0]", "http://[fe80::1%25eth0]"}, + } { + a, _ := url.Parse(pair[0]) + b, _ := url.Parse(pair[1]) + if HostKey(a) == HostKey(b) { + t.Errorf("distinct destinations share a key: %v", pair) + } + } +} + +func TestHostAliasesShareConcurrencyAndBackoff(t *testing.T) { + a, _ := url.Parse("https://example.test") + b, _ := url.Parse("https://EXAMPLE.test.") + g := New(Profile{RPS: 1000, Burst: 10, MaxPerHost: 1, MaxGlobal: 2}) + release, err := g.acquire(t.Context(), HostKey(a)) + if err != nil { + t.Fatal(err) + } + defer release() + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Millisecond) + defer cancel() + if otherRelease, err := g.acquire(ctx, HostKey(b)); !errors.Is(err, context.DeadlineExceeded) { + if otherRelease != nil { + otherRelease() + } + t.Fatalf("alias escaped the in-flight limit: %v", err) + } + release() + g.mu.Lock() + g.hosts[HostKey(a)].notBefore = time.Now().Add(time.Hour) + g.mu.Unlock() + ctx, cancel = context.WithTimeout(t.Context(), 30*time.Millisecond) + defer cancel() + if otherRelease, err := g.acquire(ctx, HostKey(b)); !errors.Is(err, context.DeadlineExceeded) { + if otherRelease != nil { + otherRelease() + } + t.Fatalf("alias escaped the retry backoff: %v", err) + } +}