Files
ARTEX/mcphttp/ssrf_test.go

147 lines
4.6 KiB
Go

package mcphttp
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
// Every other test in this package uses httptest on 127.0.0.1. The guard is a
// default, not an absolute ban, so the suite opts out globally here; the tests
// below turn it back on explicitly to check the default.
func TestMain(m *testing.M) {
if os.Getenv(AllowLocalTargetsEnv) == "" {
_ = os.Setenv(AllowLocalTargetsEnv, "1")
}
os.Exit(m.Run())
}
func TestLocalMCPTargetBlockedByDefault(t *testing.T) {
t.Setenv(AllowLocalTargetsEnv, "")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
t.Error("the MCP endpoint was dialled despite the guard")
}))
defer srv.Close()
ctx := context.Background()
_, err := New(ctx, "blocked", srv.URL, nil, false, 1)
if err == nil || !strings.Contains(err.Error(), "refusing to dial loopback") {
t.Fatalf("streamable HTTP: want dial refusal, got %v", err)
}
_, err = NewSSE(ctx, "blocked", srv.URL+"/sse", nil, false)
if err == nil || !strings.Contains(err.Error(), "refusing to dial loopback") {
t.Fatalf("legacy SSE: want dial refusal, got %v", err)
}
}
func TestLocalMCPTargetAllowedWithOptOut(t *testing.T) {
t.Setenv(AllowLocalTargetsEnv, "1")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":%q,"capabilities":{},"serverInfo":{"name":"local","version":"0"}}}`, protocolVersion)
}))
defer srv.Close()
c, err := New(context.Background(), "local", srv.URL, nil, false, 1)
if err != nil {
t.Fatalf("connect with opt-out: %v", err)
}
_ = c.Close()
}
func TestIsBlockedDialIPMetadataAndNAT64(t *testing.T) {
cases := []struct {
ip string
want bool
}{
{"127.0.0.1", true},
{"169.254.169.254", true},
{"100.100.100.200", true},
{"::ffff:169.254.169.254", true},
{"64:ff9b::169.254.169.254", true},
{"64:ff9b::127.0.0.1", true},
{"64:ff9b::100.100.100.200", true},
{"64:ff9b::8.8.8.8", false},
{"10.0.0.5", false},
{"192.168.1.20", false},
// AWS IMDS over IPv6, the Azure WireServer, and the RFC 8215
// local-use NAT64 prefix.
{"fd00:ec2::254", true},
{"FD00:EC2:0:0:0:0:0:254", true},
{"fd00:ec2::253", false},
{"fd00::1", false},
{"168.63.129.16", true},
{"::ffff:168.63.129.16", true},
{"64:ff9b::168.63.129.16", true},
{"168.63.129.17", false},
{"64:ff9b:1::1", true},
{"64:ff9b:1::a9fe:a9fe", true},
{"64:ff9b:1:ffff:ffff:ffff:ffff:ffff", true},
{"64:ff9b:2::1", false},
}
for _, tc := range cases {
ip := net.ParseIP(tc.ip)
if got := isBlockedDialIP(ip); got != tc.want {
t.Errorf("isBlockedDialIP(%q) = %v, want %v", tc.ip, got, tc.want)
}
}
}
// ARTEX_MCP_ALLOW_LOCAL is the opt-out for a local MCP server: it exempts
// loopback and nothing else. Metadata, link-local, unspecified and multicast
// addresses stay refused with it set.
func TestAllowLocalExemptsOnlyLoopback(t *testing.T) {
cases := []struct {
ip string
allowed, allowedOn bool // without / with ARTEX_MCP_ALLOW_LOCAL
}{
{"127.0.0.1", false, true},
{"127.10.20.30", false, true},
{"::1", false, true},
{"::ffff:127.0.0.1", false, true},
{"169.254.169.254", false, false},
{"::ffff:169.254.169.254", false, false},
{"169.254.10.1", false, false},
{"fe80::1", false, false},
{"fd00:ec2::254", false, false},
{"100.100.100.200", false, false},
{"168.63.129.16", false, false},
{"64:ff9b::169.254.169.254", false, false},
{"64:ff9b::127.0.0.1", false, false},
{"64:ff9b:1::1", false, false},
{"0.0.0.0", false, false},
{"::", false, false},
{"224.0.0.1", false, false},
{"10.0.0.5", true, true},
{"8.8.8.8", true, true},
}
for _, tc := range cases {
ip := net.ParseIP(tc.ip)
if got := dialAllowed(ip, false); got != tc.allowed {
t.Errorf("dialAllowed(%q, allowLocal=false) = %v, want %v", tc.ip, got, tc.allowed)
}
if got := dialAllowed(ip, true); got != tc.allowedOn {
t.Errorf("dialAllowed(%q, allowLocal=true) = %v, want %v", tc.ip, got, tc.allowedOn)
}
}
// Through the real dial hook, with the environment variable set.
t.Setenv(AllowLocalTargetsEnv, "1")
if err := blockInternalDial("tcp", "127.0.0.1:80", nil); err != nil {
t.Errorf("loopback with opt-out refused: %v", err)
}
for _, addr := range []string{"169.254.169.254:80", "[fd00:ec2::254]:80", "168.63.129.16:80", "[fe80::1]:80"} {
if err := blockInternalDial("tcp", addr, nil); err == nil {
t.Errorf("%s dialled with %s=1; only loopback is exempt", addr, AllowLocalTargetsEnv)
}
}
}