Files
w3cjandClaude Opus 5.5 4e925492ca fix(proxy): every host is read in one form, proxy.NormHost, guarded by the structure test
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]>
2026-11-15 19:36:28 -07:00

159 lines
4.7 KiB
Go

package structure
import (
"fmt"
"go/ast"
"go/token"
"maps"
"path"
"slices"
"strings"
)
// A hostRule is where a host is read in one form (checkHosts): the file that owns the
// normalization, and the functions outside it that may drop a dot or fold a case of their own,
// each with why.
type hostRule struct {
owner string // the file, from the module's root, that normalizes hosts
free map[string]string // "file:function", the file from the module's root: why it may
}
// hostish reports whether an expression names a host: an identifier, a field or a method whose
// name holds "host" in any case (host, h.Host, u.Hostname()), or is sni.
func hostish(e ast.Expr) bool {
found := false
ast.Inspect(e, func(n ast.Node) bool {
if id, ok := n.(*ast.Ident); ok {
l := strings.ToLower(id.Name)
found = found || strings.Contains(l, "host") || l == "sni"
}
return !found
})
return found
}
// checkHosts: outside tests and r.hosts.owner, nothing reads a host in a form of its own. These
// calls to package strings fail, outside the functions free lists ("file:function"):
// - Trim, TrimLeft or TrimRight with a cutset that holds ".", and TrimSuffix or CutSuffix with
// ".", which drop a trailing dot;
// - ToLower, ToUpper, EqualFold, TrimFunc, TrimLeftFunc or TrimRightFunc of an argument that
// names a host (host, sni, u.Hostname()), which fold its case or trim it a way of their own.
//
// So does a conversion to the owner's Host of anything but a literal: proxy.NormHost reads a host,
// as the proxy does.
//
// It goes by names and by calls, so it doesn't see a host's case folded in a variable named
// otherwise (name, s), a dot dropped by slicing (h[:len(h)-1]), or a host mapped rune by rune
// (strings.Map, unicode.ToLower).
func checkHosts(m *module, r rules) []violation {
o := r.hosts
if o.owner == "" {
return nil
}
ownerDir := path.Dir(o.owner)
var vs []violation
used := map[string]bool{}
c := stringConsts(m)
for _, f := range m.files {
if f.test || f.name == o.owner {
continue
}
pkg := importName(f, "strings")
host := importName(f, m.path+"/"+ownerDir) + ".Host"
if f.dir == ownerDir {
host = "Host"
}
for _, d := range f.ast.Decls {
site := f.name + ":" + declName(d)
ast.Inspect(d, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
if isConversion(call, host) {
if lit, ok := call.Args[0].(*ast.BasicLit); !ok || lit.Kind != token.STRING {
vs = append(vs, violation{ruleOwners, m.pos(call), "makes a Host of a string: read it with NormHost (" + o.owner + ")"})
}
return true
}
why := hostCall(m, f, c, pkg, call)
switch {
case why == "":
case o.free[site] != "":
used[site] = true
default:
vs = append(vs, violation{ruleOwners, m.pos(call), fmt.Sprintf("%s: read the host with proxy.NormHost (%s)", why, o.owner)})
}
return true
})
}
}
for _, site := range slices.Sorted(maps.Keys(o.free)) {
if !used[site] {
vs = append(vs, violation{ruleOwners, "hosts", site + " reads no host of its own: take it out"})
}
}
return vs
}
// hostCall is why call, in f, reads a host a way of its own (checkHosts), or "".
func hostCall(m *module, f srcFile, c consts, pkg string, call *ast.CallExpr) string {
if pkg == "" || len(call.Args) == 0 {
return ""
}
last := call.Args[len(call.Args)-1]
switch callName(call) {
case pkg + ".Trim", pkg + ".TrimLeft", pkg + ".TrimRight":
if v, ok := c.value(m, f, last); ok && strings.Contains(v, ".") {
return "drops a dot"
}
case pkg + ".TrimSuffix", pkg + ".CutSuffix":
if v, ok := c.value(m, f, last); ok && v == "." {
return "drops a trailing dot"
}
case pkg + ".TrimFunc", pkg + ".TrimLeftFunc", pkg + ".TrimRightFunc":
if hostish(call.Args[0]) {
return "trims a host"
}
case pkg + ".ToLower", pkg + ".ToUpper", pkg + ".EqualFold":
if slices.ContainsFunc(call.Args, hostish) {
return "folds a host's case"
}
}
return ""
}
// declName is a declaration's function's name, Type.Name for a method, or "" for any other.
func declName(d ast.Decl) string {
fd, ok := d.(*ast.FuncDecl)
if !ok {
return ""
}
if fd.Recv == nil || len(fd.Recv.List) == 0 {
return fd.Name.Name
}
t := fd.Recv.List[0].Type
if s, ok := t.(*ast.StarExpr); ok {
t = s.X
}
if id, ok := t.(*ast.Ident); ok {
return id.Name + "." + fd.Name.Name
}
return fd.Name.Name
}
// isConversion reports whether call is name(x), one argument, name an identifier or pkg.Name.
func isConversion(call *ast.CallExpr, name string) bool {
if len(call.Args) != 1 {
return false
}
switch fun := call.Fun.(type) {
case *ast.Ident:
return fun.Name == name
case *ast.SelectorExpr:
x, ok := fun.X.(*ast.Ident)
return ok && x.Name+"."+fun.Sel.Name == name
}
return false
}