diff --git a/docs/reference.md b/docs/reference.md index c1bf1c1fd..a1a80a9cb 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -3573,6 +3573,7 @@ application: - /mcp - /me - /join + - /magpie-ca.crt services: magpie: image: ghcr.io/yetone/magpie:0.1.N @@ -3615,6 +3616,11 @@ the box's login page. Let nothing but the box's ingress reach the page's port (3430): not a TCP ingress (`application.ingress`) or the box's LAN port forwarding. There the box passes every header as the caller wrote it, and anyone who reaches that port could say they are the box's administrator. +magpie turns away a peer on a public address whatever it sends, but it +can't tell a private one from the ingress: LazyCat's docs +(developer.lazycat.cloud/en/http-request-headers.html) say the ingress sets +`X-HC-*` after authenticating and clears them when it fails, and name no +address for it. Another app's container reaches the app through the ingress alone, unless its manifest puts it in the box's own network (`network_mode: host`, as some of the box's own apps are): from there, as from the box itself, the diff --git a/internal/gateway/proxy_identity_test.go b/internal/gateway/proxy_identity_test.go index c21ce45c6..c07197adb 100644 --- a/internal/gateway/proxy_identity_test.go +++ b/internal/gateway/proxy_identity_test.go @@ -136,3 +136,30 @@ func TestPassthroughDropsProxyIdentity(t *testing.T) { t.Errorf("mcp: the MCP server was told %v", l) } } + +// The translated routes build the vendor's request themselves, so none of +// the caller's headers should reach it, a LazyCat box's X-HC-* among them: +// a Chat completion and an Anthropic Messages turn to a Chat vendor. +func TestTranslatedRoutesDropProxyIdentity(t *testing.T) { + fresh(t) + t.Setenv("MAGPIE_TRUST_PROXY", "1") + up := newVendorAPI(t) + saveVendor(t, provider.Provider{ID: "vendor", Name: "Vendor", Chat: up.URL + "/v1", Key: "vk", Models: []string{"m"}}) + h := nativeServe(t) + for _, c := range []struct{ path, body string }{ + {"/v1/chat/completions", `{"model":"vendor/m","messages":[{"role":"user","content":"hi"}]}`}, + {"/v1/messages", `{"model":"vendor/m","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`}, + } { + hdr := append(identityHeadersOn(), "Authorization", "Bearer "+Token) + nativeDo(h, "POST", "http://127.0.0.1:3425"+c.path, "127.0.0.1:50000", c.body, hdr...) + } + seen := up.asked() + if len(seen) == 0 { + t.Fatal("the vendor was never asked") + } + for _, s := range seen { + if l := leakedIdentity(s.header); len(l) > 0 { + t.Errorf("%s: the vendor was told %v", s.uri, l) + } + } +} diff --git a/internal/gui/web_lazycat.go b/internal/gui/web_lazycat.go index 25ea6c0e5..77a21b0ed 100644 --- a/internal/gui/web_lazycat.go +++ b/internal/gui/web_lazycat.go @@ -3,7 +3,9 @@ package gui import ( "errors" "fmt" + "net" "net/http" + "net/netip" "os" "github.com/yetone/magpie/internal/gateway" @@ -76,6 +78,12 @@ func webBoxSignIn() (boxSignIn, error) { // the box sends is read; X-HC-User-Groups least of all, which the ingress // passed on as the caller wrote it (lzcos 1.6.2). func lazyCatSignIn(r *http.Request) (why, short string) { + // The ingress reaches the app over the box's own network. LazyCat's + // docs don't give its address, so this can't name it; but a peer on a + // public address is never it, whatever headers it sends. + if !boxPeer(r.RemoteAddr) { + return "This request didn't come from the box's network, so the headers it carries (X-HC-*) are the caller's own, not the box's ingress's.", "peer " + r.RemoteAddr + " is not on the box's network" + } for _, k := range []string{"X-HC-User-ID", "X-HC-User-Role", "X-HC-Source"} { if n := len(r.Header.Values(k)); n > 1 { return fmt.Sprintf("This request names %s %d times, and the box's ingress names it once, in place of what the caller sent: it didn't come through the box's sign-in alone.", k, n), fmt.Sprintf("%s sent %d times", k, n) @@ -122,3 +130,19 @@ func boxFetched(r *http.Request) (why, short string) { return fmt.Sprintf("This browser says the request came from %q (Sec-Fetch-Site), which the box's sign-in doesn't take.", site), fmt.Sprintf("Sec-Fetch-Site %q", site) } } + +// boxPeer says addr, a request's peer, is on a private, loopback or +// link-local network, where a box's ingress is, and not a public address. An +// address it can't read is not. +func boxPeer(addr string) bool { + host, _, err := net.SplitHostPort(addr) + if err != nil { + host = addr + } + ip, err := netip.ParseAddr(host) + if err != nil { + return false + } + ip = ip.Unmap() + return ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() +} diff --git a/internal/gui/web_lazycat_test.go b/internal/gui/web_lazycat_test.go index ea02eb95c..fde0f3d1b 100644 --- a/internal/gui/web_lazycat_test.go +++ b/internal/gui/web_lazycat_test.go @@ -398,3 +398,47 @@ func TestWebBoxSignInChecksItsValue(t *testing.T) { t.Errorf("the box's headers without MAGPIE_WEB_SIGN_IN: %d, reached %v", w.Code, reached) } } + +// The box's headers are the caller's own from anywhere but the box's +// network: an administrator's headers, forged, from a public address (the +// app's port published past the ingress), or from a peer that isn't an +// address, open nothing, with Fetch Metadata that suits. +func TestLazyCatSignInIgnoresAPublicPeer(t *testing.T) { + var reached bool + h := lazyCatBox(t, &reached) + for _, peer := range []string{"203.0.113.9:50000", "[2001:db8::1]:50000", "8.8.8.8:1", "not-an-address", ""} { + reached = false + w := onePortCall(t, h, "GET", "/api/state", peer, lazyCatDomain, "", over(lazyCatSignedIn, lazyCatAPI...)...) + if w.Code != http.StatusUnauthorized || reached { + t.Errorf("peer %q: %d, reached %v", peer, w.Code, reached) + } + w = onePortCall(t, h, "GET", "/", peer, lazyCatDomain, "", over(lazyCatSignedIn, lazyCatPage...)...) + if w.Code != http.StatusUnauthorized || reached { + t.Errorf("peer %q, the page: %d, reached %v", peer, w.Code, reached) + } + } + for _, peer := range []string{"172.28.2.130:1", "10.0.0.2:1", "127.0.0.1:1", "[fd00::2]:1"} { + reached = false + if w := onePortCall(t, h, "GET", "/api/state", peer, lazyCatDomain, "", over(lazyCatSignedIn, lazyCatAPI...)...); w.Code != http.StatusTeapot || !reached { + t.Errorf("peer %q: %d, reached %v", peer, w.Code, reached) + } + } +} + +// A sandboxed frame's request (Origin: null) from the box's network, with +// the administrator's headers, is another page's request: it opens nothing. +func TestLazyCatSignInTurnsAwayOriginNull(t *testing.T) { + var reached bool + h := lazyCatBox(t, &reached) + for _, fetch := range [][]string{ + {"Origin", "null", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "cors", "Sec-Fetch-Dest", "empty"}, + {"Origin", "null", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "navigate", "Sec-Fetch-Dest", "iframe"}, + {"Origin", "null"}, + } { + reached = false + w := onePortCall(t, h, "POST", "/api/state", lazyCatPeer, lazyCatDomain, "", over(lazyCatSignedIn, fetch...)...) + if w.Code != http.StatusUnauthorized || reached { + t.Errorf("%v: %d, reached %v", fetch, w.Code, reached) + } + } +}