web: a LazyCat sign-in takes no peer on a public address; the manifest lists /magpie-ca.crt (forgetable024 on X, #5987)

Follows Rainier's two commits (cherry-picked with authorship, #5987).

What LazyCat's own docs say (developer.lazycat.cloud/en/http-request-headers.html,
read today): lzc-ingress authenticates a request, and after a success sets
X-HC-User-ID, -User-Role (NORMAL or ADMIN), -Device-*, -Login-Time; "if
authentication fails, it will clear the above X-HC-XX headers", and "lzcapp
developers ... can directly trust X-HC-User-ID". X-HC-User-Groups is said to
have the caller's own removed. X-HC-Source is in no page of the docs; the
sign-in's use of it only narrows who gets in. The docs name no address for
the ingress, so the peer can't be named; a TCP ingress or port forward passes
headers as sent (the docs for it say no authentication), which the manifest
section already says to keep off the page's port.

This commit: lazyCatSignIn turns away a peer that isn't on a private,
loopback or link-local address (or isn't an address), whatever headers it
sends. The client-routes list gains /magpie-ca.crt, which main added after
the branch's base (TestLazyCatManifestListsTheClientRoutes was red on it).
TestTranslatedRoutesDropProxyIdentity adds the chat and messages routes to
the vendor-leak tests (native, Codex relay and MCP were covered).

Run (mote, go1.26.8): gofmt clean; go vet on gui, gateway and cli for darwin,
linux nogui and windows; LazyCat, ProxyIdentity, OnePort and Web tests in
internal/gui and internal/gateway ok.
Broken by hand: with the peer check off, TestLazyCatSignInIgnoresAPublicPeer
is red; with X-Hc- out of proxyIdentity, TestNativeAnyDropsProxyIdentity and
TestPassthroughDropsProxyIdentity are red.
Not tried: a real LazyCat box (none here); the plugin fetch route builds its
headers from the request magpie made, read, not run. Full suite and GUI
tests follow the tag.

Co-Authored-By: Rainier <74943233+Rmosser@users.noreply.github.com>
This commit is contained in:
yetone
2026-11-11 10:15:40 +08:00
co-authored by Rainier
parent 4d9b798d50
commit 5c05ac41b9
4 changed files with 101 additions and 0 deletions
+6
View File
@@ -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
+27
View File
@@ -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)
}
}
}
+24
View File
@@ -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()
}
+44
View File
@@ -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)
}
}
}