Files
w3cjandClaude Opus 5.5 47593e21ab fix(proxy): NormHost keeps an address's trailing dot, its IPv6 zone's, and Public calls no zoned address the internet's
NormHost dropped one trailing dot from any host, so an IPv6 address whose
zone is "." (::1%.) became ::1%, an empty zone, which netip reads as no
address. The proxy refused both forms (HostOK), but a partial clone's
promisor warning (privateHost) no longer saw ::1%. as private, as
FuzzPromisorHostSite found. NormHost now leaves an address as it is, its
dot included, so every judge reads such a host as the zoned address it
is. Public now calls no address with a zone the internet's: netip's
prefixes never contain a zoned address, so the ranges alone missed, for
example, a zoned documentation address, which --from's registry check
took as public.

No VM could reach anything through such a host: every way in (CONNECT,
absolute-form requests, a transparent connection's Host and its TLS
server name) refused it with 400 before any lookup or connection, on
every tier, the broker refuses a lookup of it and a connection to a
zoned address, and no rule or NO_PROXY entry names one.

Tests: TestZonedHostsRefused sends each zoned and empty-zone host
through CONNECT, plain HTTP and the transparent path on the open,
internet, allowlist, none and loopback-allowing tiers, checking each
refusal is the proxy's own where net/http lets the request through,
with a control on each path that [::1] itself reaches the dialer where
allowed; it passed before the fix too, and fails when HostOK takes a
zone. TestNormHost, TestBrokerLookup, TestBrokerDial, TestInternal and
TestPublicAddr gain zoned cases; FuzzHostSites' splitSite and
policySite list the new divergence (zoneDot), and FuzzPromisorHostSite
lists a zoned address now counting as private; its crash input and the
zoned-unspecified one ([::%[]:) are added. design.md's list of the
host readers' differences names both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-20 17:36:57 -07:00

88 lines
3.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package vm
import (
"net/netip"
"net/url"
"strconv"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/proxy"
)
// legacyURLHostPort and legacyPrivateHost are urlHostPort and privateHost as they were before
// proxy.NormHost read their hosts, frozen for FuzzPromisorHostSite.
func legacyURLHostPort(u string) (host string, port int, local bool) {
scheme, _, hasScheme := strings.Cut(u, "://")
if !hasScheme {
if h, _, ok := scpHost(u); ok {
return strings.ToLower(h), 22, false
}
return "", 0, true
}
if strings.EqualFold(scheme, "file") {
return "", 0, true
}
p, err := url.Parse(u)
if err != nil || p.Hostname() == "" {
return "", 0, false
}
port = map[string]int{"http": 80, "https": 443, "ssh": 22, "git+ssh": 22, "ssh+git": 22}[strings.ToLower(scheme)]
if p.Port() != "" {
n, err := strconv.Atoi(p.Port())
if err != nil {
return "", 0, false
}
port = n
}
if port == 0 {
return "", 0, false
}
return strings.ToLower(p.Hostname()), port, false
}
func legacyPrivateHost(host string) bool {
if a, err := netip.ParseAddr(strings.Trim(host, "[]")); err == nil {
return !proxy.Public(a)
}
return host == "localhost" || strings.HasSuffix(host, ".localhost")
}
// FuzzPromisorHostSite: a partial clone's promisor remote's host, which its warnings judge by the
// VM's policies, reads as it did, but as the proxy reads it (proxy.NormHost), which the policies
// did already. Divergences: its one trailing dot is dropped, so a warning names example.com, not
// example.com., and localhost. or 127.0.0.1. counts as private, which the proxy refuses as it
// refuses localhost; and a host with anything but printable ASCII isn't folded in Unicode (the
// Kelvin sign into k), as the proxy refuses such a host. A host private before is private still,
// but one with a stray bracket ([::, from the scp-like [[::]:path), which strings.Trim read as an
// address and is none: git reaches no such host, and the proxy refuses it. An IPv6 address with a
// zone is private, as proxy.Public calls none the internet's, where a zoned public address wasn't;
// and one whose zone is "." keeps its dot (::1%.), so it's the address it is, private as it was.
func FuzzPromisorHostSite(f *testing.F) {
for _, s := range []string{
"https://github.com/x/y.git", "https://GitHub.com./x", "[email protected].:x", "git@localhost.:x",
"https://127.0.0.1./x", "ssh://[::1]/x", "[::1]:x", "https://Kubernetes.io/x", "https://a..b/x",
"https://x.localhost../x", "file:///x", "/x", "https://h:bad/x", "[[::]:x",
} {
f.Add(s)
}
f.Fuzz(func(t *testing.T, u string) {
host, port, local := urlHostPort(u)
lhost, lport, llocal := legacyURLHostPort(u)
if port != lport || local != llocal {
t.Fatalf("urlHostPort(%q) = %q, %d, %v; was %q, %d, %v", u, host, port, local, lhost, lport, llocal)
}
ascii := !strings.ContainsFunc(host, func(r rune) bool { return r <= ' ' || r > '~' })
if ascii && host != proxy.NormHost(lhost).String() {
t.Fatalf("urlHostPort(%q) = %q; was %q", u, host, lhost)
}
if legacyPrivateHost(lhost) && !privateHost(host) && !strings.ContainsAny(lhost, "[]") {
t.Fatalf("%q: %q was private, %q isn't", u, lhost, host)
}
a, isAddr := proxy.NormHost(host).Addr()
if privateHost(host) && !legacyPrivateHost(lhost) && !strings.HasSuffix(lhost, ".") && (!isAddr || a.Zone() == "") {
t.Fatalf("%q: %q is private, %q wasn't", u, host, lhost)
}
})
}