84 lines
3.6 KiB
Go
84 lines
3.6 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/tasktools"
|
|
)
|
|
|
|
type proxyTaskTools struct{ calls int }
|
|
|
|
func (b *proxyTaskTools) List(context.Context, string) ([]*mcp.Tool, error) {
|
|
b.calls++
|
|
return []*mcp.Tool{}, nil
|
|
}
|
|
|
|
func (*proxyTaskTools) Call(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) {
|
|
return &mcp.CallToolResult{}, nil
|
|
}
|
|
func (*proxyTaskTools) Close(context.Context, string) error { return nil }
|
|
|
|
func TestTaskToolsProxyConfinement(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name, target, auth string
|
|
task, runtime, revoked bool
|
|
want int
|
|
}{
|
|
{name: "runtime", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusOK},
|
|
{name: "install", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, want: http.StatusForbidden},
|
|
{name: "setup", target: tasktools.Endpoint, auth: tasktools.Sentinel, runtime: true, want: http.StatusForbidden},
|
|
{name: "revoked", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, revoked: true, want: http.StatusProxyAuthRequired},
|
|
{name: "forged bearer", target: tasktools.Endpoint, auth: "forged", task: true, runtime: true, want: http.StatusUnauthorized},
|
|
{name: "other path", target: "http://archie.tools/other", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
|
{name: "query", target: tasktools.Endpoint + "?target=victim", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
|
{name: "other port", target: "http://archie.tools:8080/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
|
{name: "other host", target: "http://attacker.example/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
backend := &proxyTaskTools{}
|
|
proxy := NewProxy(nil, ProxyOptions{TaskTools: backend})
|
|
opts := SessionOptions{Token: "proxy-password"}
|
|
if tt.task {
|
|
opts.RunCredential = "run-grant"
|
|
}
|
|
session, err := proxy.Register(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tt.runtime {
|
|
session.EnterRuntime()
|
|
}
|
|
if tt.revoked {
|
|
proxy.Revoke(t.Context(), session.Token())
|
|
}
|
|
if session.gatewayToken != "" && (session.gatewayToken == session.Token() || session.gatewayToken == tasktools.Sentinel) {
|
|
t.Fatal("gateway token is container-visible")
|
|
}
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, tt.target, strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}`))
|
|
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("archie:"+session.Token())))
|
|
req.Header.Set("Authorization", "Bearer "+tt.auth)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json, text/event-stream")
|
|
response := httptest.NewRecorder()
|
|
proxy.ServeHTTP(response, req)
|
|
if response.Code != tt.want {
|
|
t.Fatalf("status %d, want %d: %s", response.Code, tt.want, response.Body.String())
|
|
}
|
|
if tt.want != http.StatusOK && backend.calls != 0 {
|
|
t.Fatal("rejected request reached gateway")
|
|
}
|
|
if session.gatewayToken != "" && strings.Contains(response.Body.String(), session.gatewayToken) {
|
|
t.Fatal("gateway token escaped in response")
|
|
}
|
|
})
|
|
}
|
|
}
|