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.
54 lines
2.5 KiB
Go
54 lines
2.5 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|