The task session carries containers.egress_hosts and the profile's remote MCP hosts, and the container gets ProxyEnv with the relay exempt from the proxy so the State Store and NATS dials bypass it. A host the session lists exactly may resolve to a private address; wildcard and any-host matches may not. egress_hosts moves from openshell to containers, as it applies to both runtimes.
102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package egress
|
|
|
|
import (
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// pattern is one allow-list entry: an exact host, a host on one port,
|
|
// a single-label wildcard, or everything.
|
|
type pattern struct {
|
|
everything bool
|
|
suffix string // ".example.com" for "*.example.com"
|
|
host string
|
|
port int // 0 means any port
|
|
}
|
|
|
|
// rules is a compiled allow list. An empty one allows nothing.
|
|
type rules struct {
|
|
allow []pattern
|
|
}
|
|
|
|
func compilePatterns(entries []string) []pattern {
|
|
out := make([]pattern, 0, len(entries))
|
|
for _, e := range entries {
|
|
out = append(out, compilePattern(e))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func compilePattern(entry string) pattern {
|
|
entry = strings.TrimSpace(entry)
|
|
if entry == "*" || entry == "**" {
|
|
return pattern{everything: true}
|
|
}
|
|
var p pattern
|
|
if h, portText, err := net.SplitHostPort(entry); err == nil {
|
|
if port, err := strconv.Atoi(portText); err == nil {
|
|
entry, p.port = h, port
|
|
}
|
|
}
|
|
entry = normalizeHost(entry)
|
|
if rest, ok := strings.CutPrefix(entry, "*."); ok {
|
|
p.suffix = "." + rest
|
|
return p
|
|
}
|
|
p.host = entry
|
|
return p
|
|
}
|
|
|
|
func normalizeHost(host string) string {
|
|
return strings.TrimSuffix(strings.ToLower(host), ".")
|
|
}
|
|
|
|
func (p pattern) matches(host string, port int) bool {
|
|
if p.everything {
|
|
return true
|
|
}
|
|
if p.port != 0 && p.port != port {
|
|
return false
|
|
}
|
|
if p.suffix != "" {
|
|
label, ok := strings.CutSuffix(host, p.suffix)
|
|
return ok && label != "" && !strings.Contains(label, ".") && net.ParseIP(host) == nil
|
|
}
|
|
return host == p.host
|
|
}
|
|
|
|
func (r rules) allows(host string, port int) bool {
|
|
host = normalizeHost(host)
|
|
for _, p := range r.allow {
|
|
if p.matches(host, port) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// namesHost reports whether an allow entry names host exactly, as a DNS name
|
|
// and not through a wildcard.
|
|
func (r rules) namesHost(host string) bool {
|
|
for _, p := range r.allow {
|
|
if !p.everything && p.suffix == "" && p.host == host {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// namesIP reports whether an allow entry names ip exactly. Only an explicit
|
|
// IP entry opens an internal address; a wildcard or everything never does.
|
|
func (r rules) namesIP(ip net.IP) bool {
|
|
for _, p := range r.allow {
|
|
if !p.everything && p.suffix == "" {
|
|
if named := net.ParseIP(p.host); named != nil && named.Equal(ip) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|