Files
archie/internal/config/egress_host.go
T
sam 2096ae8267 feat(openshell): each sandbox's network policy admits only what its task needs (#1864, #1967)
Runtimes now take a task launch: image, mounts, env, the daemon's NATS
and State Store with the task's credentials, its model providers and the
hosts its tools may reach. Docker addresses them through the egress relay
as before. An OpenShell sandbox reaches NATS and the State Store over
plain TCP, the model providers with their credential bound, and the
task's remote MCP hosts and the allowed package registries; services
addressed by IP or loopback are named by the configured services host,
since sandbox policy admits only DNS names.
2026-11-14 03:24:37 +11:00

39 lines
1.3 KiB
Go

package config
import (
"fmt"
"net"
"strconv"
"strings"
)
// EgressHost parses an egress host entry, "host" or "host:port", into its
// host and port; the port is 443 when none is given. The host is a DNS name,
// optionally with a whole first-label wildcard ("*.example.com") over at
// least three labels: sandbox policy names hosts, never addresses.
func EgressHost(entry string) (string, int, error) {
entry = strings.TrimSpace(entry)
host, port := entry, 443
if h, p, err := net.SplitHostPort(entry); err == nil {
n, err := strconv.Atoi(p)
if err != nil || n < 1 || n > 65535 {
return "", 0, fmt.Errorf("egress host %q: port must be 1 to 65535", entry)
}
host, port = h, n
}
host = strings.ToLower(strings.TrimSuffix(host, "."))
if host == "" || net.ParseIP(host) != nil {
return "", 0, fmt.Errorf("egress host %q: want a DNS name", entry)
}
labels := strings.Split(host, ".")
for i, label := range labels {
if label == "" || (strings.Contains(label, "*") && (i != 0 || label != "*")) {
return "", 0, fmt.Errorf("egress host %q: a wildcard must be the whole first label", entry)
}
}
if labels[0] == "*" && len(labels) < 3 {
return "", 0, fmt.Errorf("egress host %q: a wildcard host needs at least three labels", entry)
}
return host, port, nil
}