95ae900a58
docs/plans/06-mcp-support.md asks for the host direction — Maven connects OUT
to MCP servers and consumes what they offer. This is the client half: the
protocol, the transports, the connection manager, the config block. Nothing is
wired into a turn yet, and nothing here exposes Maven's own capabilities to an
outside caller.
internal/mcp:
- hand-rolled JSON-RPC 2.0 (the wire format is four fields, and the repo
vendors its deps, so a library would cost more than it saves);
- two transports: a stdio subprocess on this box, and streamable HTTP, which
accepts a plain JSON reply or an SSE frame because servers disagree about
which they send;
- Client: initialize handshake, tools/list, tools/call, resources/list,
resources/read. Text content only — everything downstream is a sentence;
- Manager: lazy dial, per-server failure that never blocks boot or the other
servers, backoff reconnect, Status for a web surface, graceful Close;
- the allowlist encoding: a discovered tool becomes the store row
"vikunja_list_tasks" with cmd ["mcp","vikunja","list_tasks"], scope
"mcp:vikunja". No new column, no migration, and ProposeTool, EnableTool,
the act matcher and the confirm turn all keep working untouched.
Constraints held, in code rather than in prose:
- OFF unless configured, and a server is dark until "enabled": true.
- A url server goes through internal/webfetch, so the SSRF guard, the size
cap, the redirect cap and the per-host rate limit apply. Reaching loopback
needs allow_private on THAT server, and each server gets its own fetcher so
one loopback exemption cannot become a hole for a public endpoint.
- readOnlyHint decides destructive: no hint means "assume it mutates", which
will route the call through the existing confirm turn. Guessing wrong in
that direction only costs a question.
- The catalogue stays small on purpose — allow_tools, and max_tools=12 per
server. The resident model is a 1.7B with a 4096-token context; a tool name
it half-remembers is a wrong act.
- Only the tool name and the router's arguments are sent. There is no API
here through which a note, a fact or the persona block could travel.
webfetch grows Post (JSON-RPC cannot be a GET) and surfaces response headers
for Mcp-Session-Id. It shares Get's guards exactly: a body buys a caller
nothing, a POST to the LAN is refused for the same reason a GET is.
Verified against the real Vikunja MCP server on homesrv
(http://localhost:9100/mcp): handshake, three discovered tools with update_task
correctly NOT read-only, a live list_projects call, a tool excluded by
allow_tools refused, and the same server refused outright once allow_private
was dropped. Tests cover both transports (the stdio one against a real
subprocess), SSE and JSON framing, session echo, reconnect, and the config
validation.
300 lines
10 KiB
Go
300 lines
10 KiB
Go
package webfetch
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The limits in this package are the reason a crawler is allowed to exist on
|
|
// this box at all, so each one has a test that fails loudly if it is removed.
|
|
|
|
func TestPrivateAddressesAreRefused(t *testing.T) {
|
|
// The wireguard range (10.42.0.0/24), the LAN (192.168.1.0/24) and the
|
|
// cloud metadata address are the three that matter here; the rest come
|
|
// along for free.
|
|
for _, s := range []string{
|
|
"127.0.0.1", "127.1.2.3", "10.42.0.7", "10.0.0.5", "192.168.1.104",
|
|
"172.16.4.4", "169.254.169.254", "100.64.1.1", "0.0.0.0",
|
|
"::1", "fc00::1", "fd12:3456::1", "fe80::1",
|
|
} {
|
|
if !IsPrivateIP(net.ParseIP(s)) {
|
|
t.Errorf("IsPrivateIP(%s) = false, want true", s)
|
|
}
|
|
}
|
|
for _, s := range []string{"8.8.8.8", "1.1.1.1", "93.184.216.34", "2606:2800:220:1::1"} {
|
|
if IsPrivateIP(net.ParseIP(s)) {
|
|
t.Errorf("IsPrivateIP(%s) = true, want false", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetRefusesPrivateLiteral(t *testing.T) {
|
|
f := New(Config{})
|
|
for _, u := range []string{
|
|
"http://127.0.0.1:8034/search",
|
|
"http://10.42.0.1/",
|
|
"http://192.168.1.104/dash",
|
|
"http://[::1]:9100/mcp",
|
|
} {
|
|
if _, err := f.Get(context.Background(), u); !errors.Is(err, ErrPrivate) {
|
|
t.Errorf("Get(%s) error = %v, want ErrPrivate", u, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A hostname that resolves into private space must fail too — that is the
|
|
// rebinding case, and it is why the check lives in the dialer.
|
|
func TestGetRefusesPrivateResolution(t *testing.T) {
|
|
f := New(Config{})
|
|
if _, err := f.Get(context.Background(), "http://localhost:8034/"); !errors.Is(err, ErrPrivate) {
|
|
t.Fatalf("Get(localhost) error = %v, want ErrPrivate", err)
|
|
}
|
|
}
|
|
|
|
func TestGetRefusesNonHTTPSchemes(t *testing.T) {
|
|
f := New(Config{})
|
|
for _, u := range []string{"file:///etc/passwd", "ftp://example.com/x", "gopher://example.com"} {
|
|
if _, err := f.Get(context.Background(), u); !errors.Is(err, ErrScheme) {
|
|
t.Errorf("Get(%s) error = %v, want ErrScheme", u, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// testFetcher — a fetcher pointed at an httptest server, which necessarily
|
|
// listens on loopback. AllowPrivate is the test-only escape hatch.
|
|
func testFetcher(t *testing.T, cfg Config) *Fetcher {
|
|
t.Helper()
|
|
cfg.AllowPrivate = true
|
|
if cfg.HostInterval == 0 {
|
|
cfg.HostInterval = time.Nanosecond
|
|
}
|
|
return New(cfg)
|
|
}
|
|
|
|
func TestAllowAndDenyLists(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := testFetcher(t, Config{AllowHosts: []string{"example.com"}})
|
|
if _, err := f.Get(context.Background(), srv.URL); !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("off-allowlist host: error = %v, want ErrBlocked", err)
|
|
}
|
|
f = testFetcher(t, Config{DenyHosts: []string{"127.0.0.1"}})
|
|
if _, err := f.Get(context.Background(), srv.URL); !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("denied host: error = %v, want ErrBlocked", err)
|
|
}
|
|
f = testFetcher(t, Config{AllowHosts: []string{"127.0.0.1"}})
|
|
if _, err := f.Get(context.Background(), srv.URL); err != nil {
|
|
t.Fatalf("allowlisted host: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestHostMatchesSubdomains(t *testing.T) {
|
|
pats := []string{"example.com", "*.news.org"}
|
|
for _, h := range []string{"example.com", "news.example.com", "a.b.example.com", "news.org", "feeds.news.org"} {
|
|
if !HostMatches(h, pats) {
|
|
t.Errorf("HostMatches(%q) = false, want true", h)
|
|
}
|
|
}
|
|
for _, h := range []string{"notexample.com", "example.com.evil.net", "org"} {
|
|
if HostMatches(h, pats) {
|
|
t.Errorf("HostMatches(%q) = true, want false", h)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSizeCap(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte(strings.Repeat("x", 5000)))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := testFetcher(t, Config{MaxBytes: 100})
|
|
if _, err := f.Get(context.Background(), srv.URL); !errors.Is(err, ErrTooLarge) {
|
|
t.Fatalf("error = %v, want ErrTooLarge", err)
|
|
}
|
|
f = testFetcher(t, Config{MaxBytes: 6000})
|
|
resp, err := f.Get(context.Background(), srv.URL)
|
|
if err != nil {
|
|
t.Fatalf("under the cap: %v", err)
|
|
}
|
|
if len(resp.Body) != 5000 {
|
|
t.Fatalf("body = %d bytes, want 5000", len(resp.Body))
|
|
}
|
|
}
|
|
|
|
func TestRedirectCap(t *testing.T) {
|
|
var srv *httptest.Server
|
|
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, srv.URL+"/again", http.StatusFound)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := testFetcher(t, Config{MaxRedirects: 2})
|
|
if _, err := f.Get(context.Background(), srv.URL); !errors.Is(err, ErrRedirects) {
|
|
t.Fatalf("error = %v, want ErrRedirects", err)
|
|
}
|
|
}
|
|
|
|
// A redirect off the allowlist is the interesting redirect: the first hop is
|
|
// permitted, the second must not be.
|
|
func TestRedirectRecheckedAgainstDenylist(t *testing.T) {
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("secret"))
|
|
}))
|
|
defer target.Close()
|
|
hop := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, target.URL, http.StatusFound)
|
|
}))
|
|
defer hop.Close()
|
|
|
|
// Reach the hop under the name "localhost" and allow only that name; the
|
|
// redirect lands on the same box under its literal address, which the
|
|
// allowlist does not cover. Without the CheckRedirect hook this fetch
|
|
// succeeds and returns "secret".
|
|
f := testFetcher(t, Config{AllowHosts: []string{"localhost"}})
|
|
viaName := strings.Replace(hop.URL, "127.0.0.1", "localhost", 1)
|
|
if _, err := f.Get(context.Background(), viaName); !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("error = %v, want ErrBlocked", err)
|
|
}
|
|
}
|
|
|
|
func TestPerHostRateLimit(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
f := testFetcher(t, Config{HostInterval: 60 * time.Millisecond})
|
|
start := time.Now()
|
|
for i := 0; i < 3; i++ {
|
|
if _, err := f.Get(context.Background(), srv.URL); err != nil {
|
|
t.Fatalf("request %d: %v", i, err)
|
|
}
|
|
}
|
|
if elapsed := time.Since(start); elapsed < 120*time.Millisecond {
|
|
t.Fatalf("three requests took %s, want at least 120ms of spacing", elapsed)
|
|
}
|
|
}
|
|
|
|
func TestRateLimitHonoursContext(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
defer srv.Close()
|
|
|
|
f := testFetcher(t, Config{HostInterval: 10 * time.Second})
|
|
if _, err := f.Get(context.Background(), srv.URL); err != nil {
|
|
t.Fatalf("first request: %v", err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
if _, err := f.Get(ctx, srv.URL); !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("error = %v, want DeadlineExceeded", err)
|
|
}
|
|
}
|
|
|
|
func TestNon2xxIsAnError(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "nope", http.StatusInternalServerError)
|
|
}))
|
|
defer srv.Close()
|
|
f := testFetcher(t, Config{})
|
|
if _, err := f.Get(context.Background(), srv.URL); !errors.Is(err, ErrStatus) {
|
|
t.Fatalf("error = %v, want ErrStatus", err)
|
|
}
|
|
}
|
|
|
|
func TestUserAgentIsSent(t *testing.T) {
|
|
got := make(chan string, 1)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got <- r.Header.Get("User-Agent")
|
|
}))
|
|
defer srv.Close()
|
|
f := testFetcher(t, Config{UserAgent: "Maven/test"})
|
|
if _, err := f.Get(context.Background(), srv.URL); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ua := <-got; ua != "Maven/test" {
|
|
t.Fatalf("user-agent = %q", ua)
|
|
}
|
|
}
|
|
|
|
func TestPostSendsBodyAndHeaders(t *testing.T) {
|
|
type seen struct {
|
|
method, ctype, accept, ua, custom string
|
|
body []byte
|
|
}
|
|
ch := make(chan seen, 1)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
ch <- seen{r.Method, r.Header.Get("Content-Type"), r.Header.Get("Accept"),
|
|
r.Header.Get("User-Agent"), r.Header.Get("X-Thing"), b}
|
|
w.Header().Set("Mcp-Session-Id", "sess-9")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ok":true}`))
|
|
}))
|
|
defer srv.Close()
|
|
f := testFetcher(t, Config{UserAgent: "Maven/test"})
|
|
resp, err := f.Post(context.Background(), srv.URL, "application/json",
|
|
[]byte(`{"jsonrpc":"2.0"}`), map[string]string{"Accept": "text/event-stream", "X-Thing": "1"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(resp.Body) != `{"ok":true}` {
|
|
t.Fatalf("body = %q", resp.Body)
|
|
}
|
|
if resp.Header["Mcp-Session-Id"] != "sess-9" {
|
|
t.Fatalf("response headers not surfaced: %+v", resp.Header)
|
|
}
|
|
s := <-ch
|
|
if s.method != http.MethodPost {
|
|
t.Fatalf("method = %s", s.method)
|
|
}
|
|
if string(s.body) != `{"jsonrpc":"2.0"}` {
|
|
t.Fatalf("request body = %q", s.body)
|
|
}
|
|
if s.ctype != "application/json" {
|
|
t.Fatalf("content-type = %q", s.ctype)
|
|
}
|
|
if s.accept != "text/event-stream" || s.custom != "1" {
|
|
t.Fatalf("caller headers dropped: %+v", s)
|
|
}
|
|
if s.ua != "Maven/test" {
|
|
t.Fatalf("user-agent = %q — a caller must not be able to override it", s.ua)
|
|
}
|
|
}
|
|
|
|
// The whole point of routing MCP through webfetch: a POST is guarded exactly
|
|
// like a GET. A body does not buy a caller a way onto the LAN.
|
|
func TestPostRefusesPrivateAddress(t *testing.T) {
|
|
f := New(Config{}) // no AllowPrivate
|
|
_, err := f.Post(context.Background(), "http://127.0.0.1:9100/mcp", "application/json", []byte(`{}`), nil)
|
|
if !errors.Is(err, ErrPrivate) {
|
|
t.Fatalf("error = %v, want ErrPrivate", err)
|
|
}
|
|
}
|
|
|
|
func TestPostRefusesNonHTTPScheme(t *testing.T) {
|
|
f := New(Config{})
|
|
if _, err := f.Post(context.Background(), "file:///etc/passwd", "application/json", nil, nil); !errors.Is(err, ErrScheme) {
|
|
t.Fatalf("error = %v, want ErrScheme", err)
|
|
}
|
|
}
|
|
|
|
func TestPostObeysDenylist(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
defer srv.Close()
|
|
f := testFetcher(t, Config{DenyHosts: []string{"127.0.0.1"}})
|
|
if _, err := f.Post(context.Background(), srv.URL, "application/json", []byte(`{}`), nil); !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("error = %v, want ErrBlocked", err)
|
|
}
|
|
}
|