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>
This commit is contained in:
BaaRaiandCoff0xc committed 2026-11-28 23:07:44 +01:00
1 parent bfdb026e36
commit 096eead53c
2 files changed
+91 -3

No files matched your search

+18 -3
View File
@@ -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)
+73
View File
@@ -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)
}
}