mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
Every check that judges or compares a host now reads it through proxy.NormHost (internal/proxy/host.go), which also holds HostOK, splitHostPort, hostASCII and asciiEqualFold: a host of printable ASCII is lowercased in ASCII alone and loses its one trailing dot; any other host stays as it is, so HostOK refuses it and no Unicode fold makes another name match. A host with two trailing dots or more, or a dot after a closing bracket, keeps them, so NormHost is idempotent and reading a host twice can't turn evil.test.. into evil.test. A proxy.Host is what the matchers take (rule.matchHost, noProxyRule.matches, local, sniMatches). Brackets are strict (hostAddr): HostOK, Host.Addr and splitHostPort take an address bare or an IPv6 address in one pair of brackets, never a stray bracket ([[::1]], 1.2.3.4]) or brackets around a name or an IPv4 address ([evil.com]:443, [1.2.3.4]:443), which get 400. Migrated: splitHostPort, sniMatches, sniHost, parseHostRule, the policy's rulesFor, quiet, byName and coversOn, secrets.forHost, parseNoProxy, route and local in internal/proxy; netpolicy.RiskOf, Tickable and sameRule (devvm network rm and the duplicate check, so a Kelvin sign never names the rule with a k); secrets.CheckHost; from.publicHost (a name HostOK refuses isn't public); agentlogin.claudeHost; vm.urlHostPort and privateHost. vm.urlHost (cloneseed.go) keeps its own reading: it's a remote's ssh host, which ssh lowercases and keeps the trailing dot of, to find a VM's alias. Differential fuzz targets keep a frozen copy of each reader as it was and allow only the decided differences: FuzzHostSites (proxy), FuzzRiskSites, FuzzCheckHostSite, FuzzPublicHostSite, FuzzClaudeHostSite and FuzzPromisorHostSite. A rule line is the same rule or no rule exactly as before, so a broken deny stays a broken deny. TestFixtureHosts reads the frozen v0.1.0 state's secrets.policy, secrets.json and links.json hosts and finds them unchanged. The structure test's new hosts check refuses, outside host.go and tests: Trim, TrimLeft or TrimRight with a cutset holding ".", TrimSuffix or CutSuffix with "."; ToLower, ToUpper, EqualFold and the TrimFunc family on an argument named for a host; and a conversion to proxy.Host of anything but a literal. Exceptions are by file and function, each with why (request.go:trimDots, cloneseed.go:urlHost, cloneworktree.go:safeIndexPath, cloneconfig.go:transportHelper, lima/start.go:machines.fillPassword). It doesn't see slicing, strings.Map or variables not named for a host. The owners doc, the proxy README, structure.md, design.md (One form of a host), network.md and safety/network.md say so. Co-Authored-By: Claude Opus 5.5 <[email protected]>
101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package netpolicy
|
||
|
||
import (
|
||
"net/netip"
|
||
"slices"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/devvmhq/devvm/internal/proxy"
|
||
)
|
||
|
||
// legacyRiskOf and legacyTickable are RiskOf and Tickable as they were before proxy.NormHost read
|
||
// their hosts, frozen for FuzzRiskSites.
|
||
func legacyRiskOf(host string) Risk {
|
||
host = strings.ToLower(strings.TrimSuffix(host, "."))
|
||
if _, err := netip.ParseAddr(strings.Trim(host, "[]")); err == nil {
|
||
return RiskAddress
|
||
}
|
||
labels := strings.Split(host, ".")
|
||
if slices.ContainsFunc(labels, func(l string) bool { return strings.HasPrefix(l, "xn--") }) {
|
||
return RiskLookalike
|
||
}
|
||
for why, hosts := range riskyHosts() {
|
||
if slices.ContainsFunc(hosts, func(h string) bool { return host == h || strings.HasSuffix(host, "."+h) }) {
|
||
return why
|
||
}
|
||
}
|
||
n := len(labels)
|
||
aws := strings.HasSuffix(host, ".amazonaws.com") || strings.HasSuffix(host, ".amazonaws.com.cn")
|
||
switch {
|
||
case n >= 2 && labels[n-2] == "oast":
|
||
return RiskWebhook
|
||
case aws && slices.ContainsFunc(labels, func(l string) bool { return l == "s3" || strings.HasPrefix(l, "s3-") }):
|
||
return RiskStorage
|
||
case aws && slices.Contains(labels, "execute-api"):
|
||
return RiskHosting
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func legacyTickable(host string, port int) bool {
|
||
host = strings.ToLower(strings.TrimSuffix(host, "."))
|
||
if port != 443 {
|
||
return false
|
||
}
|
||
for name := range tickable() {
|
||
if host == name || strings.HasSuffix(host, "."+name) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// A line that isn't a rule is the same as another in ASCII's case alone: a letter whose Unicode
|
||
// fold is ASCII (the Kelvin sign) names no rule, so devvm network rm with it never takes out the
|
||
// rule with the k.
|
||
func TestSameRuleFold(t *testing.T) {
|
||
t.Parallel()
|
||
for _, c := range []struct {
|
||
text, rule string
|
||
want bool
|
||
}{
|
||
{"!kubernetes.io", "!Kubernetes.io", false},
|
||
{"kubernetes.io", "Kubernetes.io", false},
|
||
{"example.com:x", "EXAMPLE.com:X", true},
|
||
{"éx", "éx", true},
|
||
{"éx", "Éx", false},
|
||
{"Example.com:x.", "example.com:x", true},
|
||
} {
|
||
if got := sameRule(c.text, c.rule); got != c.want {
|
||
t.Errorf("sameRule(%q, %q) = %v, want %v", c.text, c.rule, got, c.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
// FuzzRiskSites: devvm network lock's risks and ticks read a host as they did. Divergences, for
|
||
// hosts the proxy never takes (proxy.HostOK), and devvm network lock only ever judges one it took
|
||
// (learning.add): a host with anything but printable ASCII isn't folded in Unicode (the Kelvin sign
|
||
// into k), so it's no risky or tickable name's; one with two trailing dots or more keeps them all
|
||
// (oast.. isn't oast.'s webhook); and one with a stray bracket (::[, [1.2.3.4]) is no address.
|
||
// Every host the proxy takes reads as it did.
|
||
func FuzzRiskSites(f *testing.F) {
|
||
for _, s := range []string{
|
||
"ngrok.io", "NGROK.io.", "x.ngrok.io..", "registry.npmjs.org.", "Registry.NPMJS.org", "Kubernetes.io",
|
||
"[::1]", "1.2.3.4.", "xn--80ak6aa92e.com", "a.oast.fun.", "b.s3.us-east-1.amazonaws.com.", "", "::[", "[1.2.3.4]",
|
||
} {
|
||
f.Add(s)
|
||
}
|
||
f.Fuzz(func(t *testing.T, s string) {
|
||
if !proxy.NormHost(s).OK() {
|
||
return
|
||
}
|
||
if got, was := RiskOf(s), legacyRiskOf(s); got != was {
|
||
t.Fatalf("RiskOf(%q) = %q, was %q", s, got, was)
|
||
}
|
||
if got, was := Tickable(s, 443), legacyTickable(s, 443); got != was {
|
||
t.Fatalf("Tickable(%q) = %v, was %v", s, got, was)
|
||
}
|
||
})
|
||
}
|