feat(egress): Docker task containers reach allowed hosts through the egress proxy (#1967)
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.
This commit is contained in:
16 files changed
+188
-94
No files matched your search
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
@@ -28,7 +30,9 @@ type modelEgress struct {
|
||||
secrets *secret.Registry
|
||||
}
|
||||
|
||||
func (m modelEgress) Open(_ context.Context, token, org string, providers map[string]config.Provider) (daemon.ModelSession, error) {
|
||||
// Open registers the task's session: the providers' hosts with their keys
|
||||
// substituted, and extra hosts the task's tools may reach.
|
||||
func (m modelEgress) Open(_ context.Context, token, org string, providers map[string]config.Provider, extra []string) (daemon.ModelSession, error) {
|
||||
// Each keyed provider's credential variable: the operator's, or the name
|
||||
// derived for a provider that carries only a secret reference.
|
||||
keyed := make(map[string]string, len(providers))
|
||||
@@ -38,7 +42,7 @@ func (m modelEgress) Open(_ context.Context, token, org string, providers map[st
|
||||
}
|
||||
}
|
||||
names := slices.Sorted(maps.Keys(keyed))
|
||||
if len(names) == 0 {
|
||||
if len(names) == 0 && len(extra) == 0 {
|
||||
return daemon.ModelSession{}, nil
|
||||
}
|
||||
bound := m.config.Get().Containers.BoundCredentials(org, names, names)
|
||||
@@ -65,9 +69,18 @@ func (m modelEgress) Open(_ context.Context, token, org string, providers map[st
|
||||
hosts = append(hosts, host)
|
||||
env = append(env, keyed[name]+"="+egress.SentinelFor(name))
|
||||
}
|
||||
if _, err := m.proxy.Register(egress.SessionOptions{Token: token, Allow: hosts, Substitutions: subs, Secrets: secrets}); err != nil {
|
||||
allow := slices.Clone(hosts)
|
||||
for _, entry := range extra {
|
||||
host, port, err := config.EgressHost(entry)
|
||||
if err != nil {
|
||||
return daemon.ModelSession{}, err
|
||||
}
|
||||
allow = append(allow, net.JoinHostPort(host, strconv.Itoa(port)))
|
||||
}
|
||||
if _, err := m.proxy.Register(egress.SessionOptions{Token: token, Allow: allow, Substitutions: subs, Secrets: secrets}); err != nil {
|
||||
return daemon.ModelSession{}, err
|
||||
}
|
||||
env = append(env, egress.ProxyEnv(token, modelProxyCAPath)...)
|
||||
return daemon.ModelSession{
|
||||
Env: append(env, egress.ModelProxyEnvFor(m.addr, token, hosts, modelProxyCAPath)...),
|
||||
Mounts: []storage.Mount{{Type: storage.MountTypeBind, Source: m.caFile, Destination: modelProxyCAPath, Shared: true}},
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestModelEgressResolvesProviderSecretRefs(t *testing.T) {
|
||||
proxy: proxy, secrets: secret.NewRegistry(),
|
||||
config: config.NewHolder(config.Config{Containers: config.ContainerConfig{Credentials: tc.bindings}}),
|
||||
}
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", map[string]config.Provider{"used": tc.provider})
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", map[string]config.Provider{"used": tc.provider}, nil)
|
||||
if tc.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
||||
t.Fatalf("err = %v, want one naming %q", err, tc.wantErr)
|
||||
@@ -97,6 +97,7 @@ func TestModelEgressSubstitutesTheProviderKey(t *testing.T) {
|
||||
// The resolver answers nothing: a session key is what the substitution uses.
|
||||
proxy := egress.NewProxy(nil, egress.ProxyOptions{
|
||||
Resolver: unboundResolver{},
|
||||
Lookup: func(context.Context, string) ([]net.IP, error) { return []net.IP{net.ParseIP("203.0.113.7")}, nil },
|
||||
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
||||
},
|
||||
@@ -108,7 +109,7 @@ func TestModelEgressSubstitutesTheProviderKey(t *testing.T) {
|
||||
"alpha": {Class: "openai", BaseURL: "http://api.alpha.example", APIKey: config.SecretRef{Engine: "env", Key: "PROVIDER_KEY"}},
|
||||
},
|
||||
}.ModelProviders()
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", providers)
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", providers, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("open model egress: %v", err)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func newOpenShellModelEgress(client gatewayProviders, workspace, instance string
|
||||
return &openShellModelEgress{client: client, workspace: workspace, instance: instance, secrets: secrets, bindings: bindings, log: log, owned: map[string][]string{}}
|
||||
}
|
||||
|
||||
func (m *openShellModelEgress) Open(ctx context.Context, token, org string, providers map[string]config.Provider) (daemon.ModelSession, error) {
|
||||
func (m *openShellModelEgress) Open(ctx context.Context, token, org string, providers map[string]config.Provider, _ []string) (daemon.ModelSession, error) {
|
||||
var session daemon.ModelSession
|
||||
for _, name := range slices.Sorted(maps.Keys(providers)) {
|
||||
p := providers[name]
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestOpenShellModelEgressHoldsKeysOnTheGateway(t *testing.T) {
|
||||
gw := fakeGateway{stubProviders{ProviderInterface: fc.Providers(), profiles: stubProfiles{known: map[string]bool{}, items: &imported}}}
|
||||
m := newOpenShellModelEgress(gw, "ws", "Test Inst", secret.NewRegistry(), fixedBindings("bound-fake-key"), slog.New(slog.DiscardHandler))
|
||||
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", tc.providers)
|
||||
session, err := m.Open(t.Context(), "run-credential", "org-test", tc.providers, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -723,10 +723,6 @@ type OpenShellConfig struct {
|
||||
// Store by when those are addressed by IP or loopback: sandbox policy
|
||||
// admits TCP only to DNS names, and never to loopback.
|
||||
ServicesHost string `toml:"services_host" yaml:"services_host" json:"services_host"`
|
||||
// EgressHosts are the hosts, "host" or "host:port" (443 by default), any
|
||||
// tool in a sandbox may reach, such as package registries. A one-label
|
||||
// wildcard ("*.example.com") matches subdomains.
|
||||
EgressHosts []string `toml:"egress_hosts" yaml:"egress_hosts" json:"egress_hosts"`
|
||||
}
|
||||
|
||||
// ContainerConfig configures Docker sandbox execution of archie-agent.
|
||||
@@ -753,6 +749,10 @@ type ContainerConfig struct {
|
||||
// OpenShell is the gateway task sandboxes run on when Runtime is
|
||||
// RuntimeOpenShell. Applies on restart.
|
||||
OpenShell OpenShellConfig `toml:"openshell" yaml:"openshell"`
|
||||
// EgressHosts are the hosts, "host" or "host:port" (443 by default), any
|
||||
// tool in a task's sandbox may reach on either runtime, such as package
|
||||
// registries. A one-label wildcard ("*.example.com") matches subdomains.
|
||||
EgressHosts []string `toml:"egress_hosts" yaml:"egress_hosts" json:"egress_hosts"`
|
||||
// Profiles are named agent profiles a workflow selects with `profile:`.
|
||||
// Stored as their own control-plane resource.
|
||||
Profiles map[string]AgentProfile `toml:"profiles" yaml:"profiles" json:"-"`
|
||||
|
||||
@@ -89,7 +89,8 @@ func (d *Daemon) acquireTaskContainer(
|
||||
return nil, "", nil, false
|
||||
}
|
||||
|
||||
model, closeModel, err := d.openModelEgress(ctx, task, credential)
|
||||
hosts := d.taskEgressHosts(ctx, profile)
|
||||
model, closeModel, err := d.openModelEgress(ctx, task, credential, hosts)
|
||||
if err != nil {
|
||||
revokeCredential()
|
||||
d.teardownStorage(ctx, task, repo, workDir)
|
||||
@@ -108,7 +109,7 @@ func (d *Daemon) acquireTaskContainer(
|
||||
Env: append([]string{fmt.Sprintf("WORKTREE_UID=%d", os.Getuid()), fmt.Sprintf("WORKTREE_GID=%d", os.Getgid())}, model.Env...),
|
||||
Worker: d.workerEndpoints(credential),
|
||||
Providers: model.Providers,
|
||||
Hosts: d.taskEgressHosts(ctx, profile),
|
||||
Hosts: hosts,
|
||||
})
|
||||
if err != nil {
|
||||
revokeCredential()
|
||||
@@ -144,7 +145,7 @@ func (d *Daemon) workerEndpoints(stateStoreToken string) egress.Endpoints {
|
||||
// left out with a warning rather than failing every task.
|
||||
func (d *Daemon) taskEgressHosts(ctx context.Context, profile config.AgentProfile) []string {
|
||||
cfg := d.Cfg.Get()
|
||||
hosts := slices.Clone(cfg.Containers.OpenShell.EgressHosts)
|
||||
hosts := slices.Clone(cfg.Containers.EgressHosts)
|
||||
for _, server := range cfg.Tools.MCPServers {
|
||||
if len(profile.Tools) > 0 && !slices.Contains(profile.Tools, server.Name) {
|
||||
continue
|
||||
|
||||
@@ -10,11 +10,12 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// ModelEgress opens a native task's model proxy session. The container holds
|
||||
// a sentinel under each provider's key variable and reaches the providers
|
||||
// through the egress proxy, which swaps in the org's bound key.
|
||||
// ModelEgress opens a task's egress session. A Docker container holds a
|
||||
// sentinel under each provider's key variable and reaches the providers, and
|
||||
// the extra hosts, through the egress proxy, which swaps in the org's bound
|
||||
// key and refuses every other host.
|
||||
type ModelEgress interface {
|
||||
Open(ctx context.Context, token, org string, providers map[string]config.Provider) (ModelSession, error)
|
||||
Open(ctx context.Context, token, org string, providers map[string]config.Provider, hosts []string) (ModelSession, error)
|
||||
Close(ctx context.Context, token string)
|
||||
}
|
||||
|
||||
@@ -62,29 +63,34 @@ type ModelProvider struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
// openModelEgress opens task's model session, or refuses: provider keys
|
||||
// openModelEgress opens task's egress session, or refuses: provider keys
|
||||
// never reach a container, so without the proxy no model call can run. A
|
||||
// workflow with no model step needs no session and no provider credential.
|
||||
func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) {
|
||||
if !workflow.UsesModel(task.WorkflowDefinitionYAML) {
|
||||
return ModelSession{}, func() {}, nil
|
||||
}
|
||||
cfg, err := d.taskConfig(ctx, task)
|
||||
if err != nil {
|
||||
return ModelSession{}, nil, err
|
||||
// workflow with no model step needs no provider credential, only the session
|
||||
// that lets its tools reach hosts.
|
||||
func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string, hosts []string) (ModelSession, func(), error) {
|
||||
var providers map[string]config.Provider
|
||||
if workflow.UsesModel(task.WorkflowDefinitionYAML) {
|
||||
cfg, err := d.taskConfig(ctx, task)
|
||||
if err != nil {
|
||||
return ModelSession{}, nil, err
|
||||
}
|
||||
providers = cfg.Providers
|
||||
}
|
||||
if d.ModelEgress == nil {
|
||||
for _, p := range cfg.Providers {
|
||||
for _, p := range providers {
|
||||
if p.APIKeyEnv != "" {
|
||||
return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, fmt.Errorf("model egress proxy is unavailable on this daemon, and provider keys never enter a container")}
|
||||
}
|
||||
}
|
||||
return ModelSession{}, func() {}, nil
|
||||
}
|
||||
if len(providers) == 0 && len(hosts) == 0 {
|
||||
return ModelSession{}, func() {}, nil
|
||||
}
|
||||
if credential == "" {
|
||||
return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one")
|
||||
}
|
||||
session, err := d.ModelEgress.Open(ctx, credential, task.OrgOrDefault(), cfg.Providers)
|
||||
session, err := d.ModelEgress.Open(ctx, credential, task.OrgOrDefault(), providers, hosts)
|
||||
if err != nil {
|
||||
return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, err}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ type recordingModelEgress struct {
|
||||
providers map[string]config.Provider
|
||||
}
|
||||
|
||||
func (r *recordingModelEgress) Open(_ context.Context, _, _ string, providers map[string]config.Provider) (ModelSession, error) {
|
||||
func (r *recordingModelEgress) Open(_ context.Context, _, _ string, providers map[string]config.Provider, _ []string) (ModelSession, error) {
|
||||
r.providers = providers
|
||||
return ModelSession{}, nil
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func TestOpenModelEgressCarriesOnlyReferencedProviders(t *testing.T) {
|
||||
})
|
||||
task := &workflow.Task{ID: 7, Identity: string(sys), WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"}
|
||||
|
||||
if _, _, err := d.openModelEgress(t.Context(), task, "run-credential"); err != nil {
|
||||
if _, _, err := d.openModelEgress(t.Context(), task, "run-credential", nil); err != nil {
|
||||
t.Fatalf("open model egress: %v", err)
|
||||
}
|
||||
if _, ok := egressProxy.providers["unused"]; ok {
|
||||
|
||||
@@ -338,8 +338,8 @@ func applyContainerDefaults(cfg *config.Config) {
|
||||
cfg.Containers.PullPolicy = defaultPullPolicy
|
||||
}
|
||||
// Nil, not empty: an operator who clears the list keeps it empty.
|
||||
if cfg.Containers.OpenShell.EgressHosts == nil {
|
||||
cfg.Containers.OpenShell.EgressHosts = slices.Clone(defaultEgressHosts)
|
||||
if cfg.Containers.EgressHosts == nil {
|
||||
cfg.Containers.EgressHosts = slices.Clone(defaultEgressHosts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ func validateRuntime(settings config.ContainerConfig) error {
|
||||
if (settings.OpenShell.Token.Engine == "") != (settings.OpenShell.Token.Key == "") {
|
||||
return fmt.Errorf("the openshell token reference needs both an engine and a key")
|
||||
}
|
||||
for _, host := range settings.OpenShell.EgressHosts {
|
||||
for _, host := range settings.EgressHosts {
|
||||
if _, _, err := config.EgressHost(host); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -166,14 +166,17 @@ func (n *Networks) Remove(ctx context.Context, name string) error {
|
||||
|
||||
// ProxyEnv is the environment a sandbox container needs to send its egress
|
||||
// through the relay under session token and to trust the daemon CA mounted
|
||||
// at caPath. Both spellings are set because tools disagree on case.
|
||||
// at caPath. Both spellings are set because tools disagree on case. The relay
|
||||
// itself is exempt, so the State Store dial (grpc-go honours HTTPS_PROXY) and
|
||||
// NATS reach their relay ports directly.
|
||||
func ProxyEnv(token, caPath string) []string {
|
||||
proxy := (&url.URL{
|
||||
Scheme: "http",
|
||||
User: url.UserPassword(proxyUser, token),
|
||||
Host: net.JoinHostPort(RelayAlias, strconv.Itoa(RelayPort)),
|
||||
}).String()
|
||||
env := []string{"NO_PROXY=", "no_proxy="}
|
||||
noProxy := RelayAlias + ",localhost,127.0.0.1"
|
||||
env := []string{"NO_PROXY=" + noProxy, "no_proxy=" + noProxy}
|
||||
for _, name := range []string{"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"} {
|
||||
env = append(env, name+"="+proxy, strings.ToLower(name)+"="+proxy)
|
||||
}
|
||||
|
||||
@@ -76,6 +76,17 @@ func (r rules) allows(host string, port int) bool {
|
||||
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 {
|
||||
|
||||
@@ -40,7 +40,12 @@ type Session struct {
|
||||
// upstream TLS against the system roots.
|
||||
type ProxyOptions struct {
|
||||
UpstreamRoots *x509.CertPool
|
||||
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
// Lookup resolves an upstream host name; the default is the system
|
||||
// resolver. Every address it returns passes the upstream guard before
|
||||
// Dial sees it.
|
||||
Lookup func(ctx context.Context, host string) ([]net.IP, error)
|
||||
// Dial connects to a guarded upstream address.
|
||||
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
// Resolver supplies the keys a session does not carry itself. Without
|
||||
// one, a substitution for such a key is refused.
|
||||
Resolver Resolver
|
||||
@@ -69,6 +74,7 @@ type SessionOptions struct {
|
||||
type Proxy struct {
|
||||
ca *CA
|
||||
dial func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
lookup func(ctx context.Context, host string) ([]net.IP, error)
|
||||
transport *http.Transport
|
||||
resolver Resolver
|
||||
|
||||
@@ -79,7 +85,13 @@ type Proxy struct {
|
||||
type sessionKey struct{}
|
||||
|
||||
func NewProxy(ca *CA, opts ProxyOptions) *Proxy {
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, sessions: map[string]*Session{}}
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, lookup: opts.Lookup, resolver: opts.Resolver, sessions: map[string]*Session{}}
|
||||
if p.dial == nil {
|
||||
p.dial = (&net.Dialer{Timeout: handshakeTimeout}).DialContext
|
||||
}
|
||||
if p.lookup == nil {
|
||||
p.lookup = lookupIPs
|
||||
}
|
||||
p.transport = &http.Transport{
|
||||
DialContext: p.dialUpstream,
|
||||
TLSClientConfig: &tls.Config{RootCAs: opts.UpstreamRoots, MinVersion: tls.VersionTLS12},
|
||||
@@ -241,53 +253,56 @@ func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Requ
|
||||
rp.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// dialUpstream resolves and dials an upstream, refusing addresses that
|
||||
// would turn the proxy into a path back into the host or its networks.
|
||||
func (p *Proxy) dialUpstream(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
if p.dial != nil {
|
||||
return p.dial(ctx, network, addr)
|
||||
// lookupIPs resolves host with the system resolver.
|
||||
func lookupIPs(ctx context.Context, host string) ([]net.IP, error) {
|
||||
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ips := make([]net.IP, 0, len(addrs))
|
||||
for _, a := range addrs {
|
||||
ips = append(ips, a.IP)
|
||||
}
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
// dialUpstream resolves and dials an upstream, refusing addresses that
|
||||
// would turn the proxy into a path back into the host or its networks. A
|
||||
// host the session's allow list names exactly is trusted to resolve to an
|
||||
// internal address, such as an MCP server on the LAN; a wildcard match is not.
|
||||
func (p *Proxy) dialUpstream(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s, _ := ctx.Value(sessionKey{}).(*Session)
|
||||
var ips []net.IP
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
ips = []net.IP{ip}
|
||||
} else {
|
||||
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
||||
if err != nil {
|
||||
ips := []net.IP{net.ParseIP(host)}
|
||||
if ips[0] == nil {
|
||||
if ips, err = p.lookup(ctx, host); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ips = append(ips, a.IP)
|
||||
}
|
||||
}
|
||||
var guardErr error
|
||||
guardErr := fmt.Errorf("%s resolved to no address", host)
|
||||
for _, ip := range ips {
|
||||
named := s != nil && s.allow.namesIP(ip)
|
||||
if guardErr = guardUpstream(ip, named); guardErr != nil {
|
||||
trusted := s != nil && (s.allow.namesIP(ip) || s.allow.namesHost(normalizeHost(host)))
|
||||
if guardErr = guardUpstream(ip, trusted); guardErr != nil {
|
||||
continue
|
||||
}
|
||||
return (&net.Dialer{Timeout: handshakeTimeout}).DialContext(ctx, network, net.JoinHostPort(ip.String(), port))
|
||||
}
|
||||
if guardErr == nil {
|
||||
guardErr = fmt.Errorf("%s resolved to no address", host)
|
||||
return p.dial(ctx, network, net.JoinHostPort(ip.String(), port))
|
||||
}
|
||||
return nil, guardErr
|
||||
}
|
||||
|
||||
// guardUpstream refuses loopback and unspecified addresses always, and
|
||||
// private, link-local, shared and multicast addresses unless the policy
|
||||
// named that exact IP.
|
||||
func guardUpstream(ip net.IP, namedByPolicy bool) error {
|
||||
// named that IP or the host it came from.
|
||||
func guardUpstream(ip net.IP, trusted bool) error {
|
||||
if ip.IsLoopback() || ip.IsUnspecified() {
|
||||
return fmt.Errorf("upstream address %s is the host itself", ip)
|
||||
}
|
||||
internal := ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||
|
||||
ip.IsInterfaceLocalMulticast() || ip.IsMulticast() || cgnat.Contains(ip)
|
||||
if internal && !namedByPolicy {
|
||||
if internal && !trusted {
|
||||
return fmt.Errorf("upstream address %s is internal and not named by the network policy", ip)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The proxy admits a request only to a host the session lists, and refuses an
|
||||
// internal address unless the host was named exactly. It guards against a
|
||||
// task reaching the host's networks through a name that resolves privately,
|
||||
// while a LAN MCP server the operator listed still works.
|
||||
func TestProxyUpstreamPolicy(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
allow []string
|
||||
target string
|
||||
resolve string
|
||||
want int
|
||||
}{
|
||||
{name: "listed host resolving public passes", allow: []string{"mcp.example.com:80"}, target: "mcp.example.com", resolve: "203.0.113.7", want: http.StatusOK},
|
||||
{name: "unlisted host is refused", allow: []string{"mcp.example.com:80"}, target: "other.example.com", resolve: "203.0.113.7", want: http.StatusForbidden},
|
||||
{name: "listed host resolving private passes", allow: []string{"mcp.lan.example:80"}, target: "mcp.lan.example", resolve: "192.168.1.20", want: http.StatusOK},
|
||||
{name: "wildcard match resolving private is refused", allow: []string{"*.lan.example:80"}, target: "mcp.lan.example", resolve: "192.168.1.20", want: http.StatusBadGateway},
|
||||
{name: "everything resolving private is refused", allow: []string{"*"}, target: "mcp.lan.example", resolve: "10.0.0.5", want: http.StatusBadGateway},
|
||||
{name: "listed host resolving loopback is refused", allow: []string{"mcp.lan.example:80"}, target: "mcp.lan.example", resolve: "127.0.0.1", want: http.StatusBadGateway},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer upstream.Close()
|
||||
proxy := NewProxy(nil, ProxyOptions{
|
||||
Lookup: func(context.Context, string) ([]net.IP, error) { return []net.IP{net.ParseIP(tc.resolve)}, nil },
|
||||
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
||||
},
|
||||
})
|
||||
if _, err := proxy.Register(SessionOptions{Token: "run", Allow: tc.allow}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+tc.target+"/", nil)
|
||||
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(proxyUser+":run")))
|
||||
rec := httptest.NewRecorder()
|
||||
proxy.ServeHTTP(rec, req)
|
||||
if rec.Code != tc.want {
|
||||
t.Fatalf("status = %d (%s), want %d", rec.Code, rec.Body.String(), tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ func (t *Tunnel) dialGuarded(ctx context.Context, network, addr string) (net.Con
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
named := t.namesHost(normalizeHost(host))
|
||||
named := t.rules.namesHost(normalizeHost(host))
|
||||
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -144,12 +144,3 @@ func (t *Tunnel) dialGuarded(ctx context.Context, network, addr string) (net.Con
|
||||
}
|
||||
return nil, guardErr
|
||||
}
|
||||
|
||||
func (t *Tunnel) namesHost(host string) bool {
|
||||
for _, p := range t.rules.allow {
|
||||
if !p.everything && p.suffix == "" && p.host == host {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -55,8 +55,8 @@ interface ContainerRuntime {
|
||||
cert_file: string;
|
||||
key_file: string;
|
||||
services_host: string;
|
||||
egress_hosts: string[];
|
||||
};
|
||||
egress_hosts: string[];
|
||||
}
|
||||
|
||||
const store = useSettingsStore();
|
||||
@@ -242,27 +242,6 @@ function formatHours(seconds: number): string {
|
||||
</div>
|
||||
</Field>
|
||||
</form.Field>
|
||||
|
||||
<form.Field v-slot="{ field }" name="openshell.egress_hosts">
|
||||
<Field orientation="responsive">
|
||||
<FieldContent><FieldLabel>Egress hosts</FieldLabel></FieldContent>
|
||||
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
|
||||
<TagsInput
|
||||
:model-value="field.state.value ?? []"
|
||||
class="font-mono"
|
||||
aria-label="Egress hosts"
|
||||
@update:model-value="(v) => field.handleChange(v as string[])"
|
||||
>
|
||||
<TagsInputItem v-for="h in field.state.value ?? []" :key="h" :value="h">
|
||||
<TagsInputItemText />
|
||||
<TagsInputItemDelete />
|
||||
</TagsInputItem>
|
||||
<TagsInputInput placeholder="*.example.com:443" />
|
||||
</TagsInput>
|
||||
<DraftHint :kind="KIND" path="openshell.egress_hosts" />
|
||||
</div>
|
||||
</Field>
|
||||
</form.Field>
|
||||
</FieldGroup>
|
||||
|
||||
<FieldGroup v-if="form" class="@container/field-group">
|
||||
@@ -372,6 +351,27 @@ function formatHours(seconds: number): string {
|
||||
</div>
|
||||
</Field>
|
||||
</form.Field>
|
||||
|
||||
<form.Field v-slot="{ field }" name="egress_hosts">
|
||||
<Field orientation="responsive">
|
||||
<FieldContent><FieldLabel>Egress hosts</FieldLabel></FieldContent>
|
||||
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
|
||||
<TagsInput
|
||||
:model-value="field.state.value ?? []"
|
||||
class="font-mono"
|
||||
aria-label="Egress hosts"
|
||||
@update:model-value="(v) => field.handleChange(v as string[])"
|
||||
>
|
||||
<TagsInputItem v-for="h in field.state.value ?? []" :key="h" :value="h">
|
||||
<TagsInputItemText />
|
||||
<TagsInputItemDelete />
|
||||
</TagsInputItem>
|
||||
<TagsInputInput placeholder="*.example.com:443" />
|
||||
</TagsInput>
|
||||
<DraftHint :kind="KIND" path="egress_hosts" />
|
||||
</div>
|
||||
</Field>
|
||||
</form.Field>
|
||||
</FieldGroup>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user