Files
magpie/internal/gui/web_oneport_test.go
T
yetone 1b32bdcfcb gui: magpie web's port answers the gateway's /v1 routes too, so one proxy location serves both (zgcwkj on Discord)
zgcwkj puts magpie behind nginx on one domain and wanted the browser UI
(magpie web / serve --web, 127.0.0.1:3430) and the gateway (:3425) from
one port, so one location works. Now the web port hands every gateway
client route to the gateway: /v1/..., /v1beta/..., /models, /chat/completions,
/responses, /messages, /images, /embeddings, /rerank, /audio, /videos,
/muse-code/models, /backend-api/codex/..., the search path and /mcp/...
(gateway.ClientPath). Everything else stays the page's, behind webGuard as
before. /api/* stays split: on the web port it is the UI's API; Ollama's
/api/* and Claude Desktop's /api/hello still need the gateway port, since
the two can't share the path.

Security: a handed-over request goes through the gateway's whole own
stack, not a copy of it. gateway.Server.Front() is lanGuard(Handler()),
built once; ListenAndServe serves the same handler. When this process
serves the gateway, the web port calls Front() in process; when another
magpie does, toGateway reverse-proxies to gateway.URL(), keeps the Host,
copies Forwarded/X-Forwarded-* back (ReverseProxy strips them), and adds
X-Forwarded-For for a non-loopback peer, so remote, proxied, rebound,
Origin null, *.localhost and foreign-Origin requests are judged exactly as
on the gateway port. The web cookie is never a gateway key. Paths that
aren't clean (/v1/../api, /v1//x, /v1/./x) are not client paths, so they
never reach the gateway as one. The allow list (AllowGuard) still wraps
both.

Tests:
- TestClientPathCoversHandler reads every mux pattern Handler registers
  and fails when one is neither a ClientPath nor /, /api/* or /_magpie/*,
  so a new gateway route has to be placed.
- TestClientPathOnlyClean.
- TestWebOnePortServedHere / ServedElsewhere: with no web cookie, the UI
  API (/api/settings) is never reached for: no Origin (GET, POST),
  Origin null, a foreign Origin, a rebound Host (GET, POST), through a
  proxy (XFF), from another machine, /v1/../api, /v1/%2e%2e/api,
  /v1//api, and with a gateway key. The gateway routes on the web port:
  local agent 200; Origin null POST 403; foreign Origin 403; rebound Host
  403 (with the page's cookie too); proxy or remote without a key 401
  (with the page's cookie too); with a gateway key 200; not shared 403.
- TestWebAssetsAreNoClientPath: no embedded asset shadows a client path.

Run (full packages at the first rebase, on origin/main 0b72ccbef):
- go test ./internal/gateway ./internal/gui -count=1 -timeout 45m:
  ok (gateway 644s, gui 193s).
- Rebased again on origin/main 8b460833e (upstream touched catalog,
  provider, redact and usage only); at this head: the three builds and
  the vets below again, and go test -run 'TestClientPath|TestWeb'
  ./internal/gateway ./internal/gui: ok. The full packages were not
  rerun at this head.
- gofmt -l internal/gateway internal/gui: empty
- go vet ./internal/gateway ./internal/gui; GOOS=linux go vet -tags
  nogui ./internal/gateway ./internal/gui ./internal/cli; GOOS=windows go
  vet ./internal/gateway ./internal/gui: ok
- go build .; GOOS=linux go build -tags nogui .; GOOS=windows go build .: ok

Broken by hand, each red, then restored:
- no onePort (the old web handler): agent 401 not 200, Origin null 401
  not 403 (ServedHere).
- Front without lanGuard: rebound Host 200, proxy without a key 200.
- toGateway not copying the forwarding headers: proxy without a key 200,
  not shared 200 (ServedElsewhere).
- no X-Forwarded-For for a remote peer: another machine 200.
- Host not kept: rebound Host 200.
- ClientPath taking unclean paths: TestClientPathOnlyClean red (4 paths).
  The gui tests stay green under this break, since the gateway's mux
  answers /v1/../api/settings with a redirect, not the UI API; the clean
  check is a second wall.

Real try: the built binary, `magpie serve --web=127.0.0.1:3472` under a
sandbox HOME (MAGPIE_ADDR=127.0.0.1:3471, MAGPIE_NO_ACCOUNTS=1,
MAGPIE_TEST_SANDBOX), then Homebrew's nginx 1.31.7 (bottle unpacked in a
scratch folder, proxy_pass to 3472 with Host and X-Forwarded-For) on
127.0.0.1:3473. Direct on 3472: / 401, /?k= 303, /api/settings 401
without and 200 with the cookie, /v1/models 200, Origin null POST 403,
foreign Origin 403, X-Forwarded-For 403 (not shared). Through nginx, not
shared: /v1/models 403 with the "came through a proxy" reason; the UI
signs in with /?k= and /api/settings is 200 with its cookie. Sharing
turned on and a key added from the UI API through nginx: /v1/models 401
without a key and 401 with only the page's cookie, 200 with the key;
/v1/messages and a streamed /v1/chat/completions with the key reach the
gateway (404: the sandbox has no provider); /api/settings with the key
401; Origin null and foreign Origin without a key 401; /v1/../api/settings
401; /api/tags 401 (the page's API, not Ollama's). A rebound Host on
either port is refused only for a request that looks like a browser's
(Sec-Fetch-Site), the same as the gateway port before.

Not tried: a real model reply (and so a long stream) through the one
port, since the sandbox has no provider; https, WebSocket (Codex's
/backend-api/codex websocket) through nginx; Windows and Linux, other
than vet and build; the GUI suite (no GUI asset changed).
2026-10-25 04:01:52 +08:00

189 lines
7.8 KiB
Go

package gui
import (
"io/fs"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
)
// onePortHome is a fresh magpie home for one test: settings and keys of
// its own.
func onePortHome(t *testing.T) {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("MAGPIE_ADDR", "")
t.Setenv("MAGPIE_PUBLIC_URL", "")
t.Setenv("MAGPIE_TRUST_PROXY", "")
}
// onePortKey shares magpie and makes an enabled gateway key.
func onePortKey(t *testing.T) string {
t.Helper()
s := settings.Load()
s.LAN = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
k, err := access.Update("add-key", access.Change{Name: "nginx"})
if err != nil {
t.Fatal(err)
}
return k
}
// onePortCall is a request to h from peer, with hdr's pairs, the page's
// cookie added when cookie isn't "".
func onePortCall(t *testing.T, h http.Handler, method, target, peer, host, cookie string, hdr ...string) *httptest.ResponseRecorder {
t.Helper()
var body *strings.Reader
if method == "POST" {
body = strings.NewReader(`{"model":"none/none","messages":[{"role":"user","content":"hi"}]}`)
} else {
body = strings.NewReader("")
}
r := httptest.NewRequest(method, target, body)
r.RemoteAddr = peer
r.Host = host
r.Header.Set("Content-Type", "text/plain") // a page's "simple" POST
for i := 0; i+1 < len(hdr); i += 2 {
r.Header.Set(hdr[i], hdr[i+1])
}
if cookie != "" {
r.AddCookie(&http.Cookie{Name: "magpie_web_3430", Value: cookie})
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
var chromeMarks = []string{"User-Agent", "Mozilla/5.0 (Macintosh) Chrome/140", "Sec-Fetch-Site", "same-origin", "Sec-Fetch-Mode", "cors"}
// onePortCases checks what `magpie web`'s port answers, the gateway being
// reached through gw (this process's, or another magpie's): its client
// routes as the gateway's own port would, and the page's API never without
// the page's key, whatever route, Origin or Host a request takes.
func onePortCases(t *testing.T, h http.Handler, reached *bool) {
t.Helper()
const lo, here = "127.0.0.1:50001", "127.0.0.1:3430"
const evil = "rebind.attacker.example:3430"
api := func(name, method, target, peer, host string, hdr ...string) {
t.Helper()
*reached = false
w := onePortCall(t, h, method, target, peer, host, "", hdr...)
if *reached || w.Code < 300 || w.Code == http.StatusMovedPermanently {
t.Errorf("the page's API %s: %d, reached %v: %s", name, w.Code, *reached, w.Body.String())
}
}
// the page's API: no cookie, no way in
api("with no Origin", "GET", "/api/settings", lo, here)
api("with no Origin, a POST", "POST", "/api/settings", lo, here)
api("from Origin: null", "POST", "/api/settings", lo, here, "Origin", "null")
api("from a foreign Origin", "POST", "/api/settings", lo, here, "Origin", "https://evil.example")
api("under a rebound Host", "GET", "/api/settings", lo, evil, chromeMarks...)
api("under a rebound Host, a POST", "POST", "/api/settings", lo, evil, append([]string{"Origin", "http://" + evil}, chromeMarks...)...)
api("through a proxy", "GET", "/api/settings", lo, "magpie.example.com", "X-Forwarded-For", "203.0.113.9")
api("from another machine", "GET", "/api/settings", "10.0.0.5:50001", here)
// nor by a path that names a client route on its way to the API
api("by /v1/../api", "GET", "/v1/../api/settings", lo, here)
api("by /v1/%2e%2e/api", "GET", "/v1/%2e%2e/api/settings", lo, here)
api("by /v1//api", "GET", "/v1//api/settings", lo, here)
// a gateway key is no key to the page
key := onePortKey(t)
api("with a gateway key", "GET", "/api/settings", lo, here, "Authorization", "Bearer "+key)
*reached = false
if w := onePortCall(t, h, "POST", "/api/settings", lo, here, onePortWebKey); w.Code != http.StatusTeapot || !*reached {
t.Fatalf("the page's API with its cookie: %d", w.Code)
}
gw := func(name string, want int, method, target, peer, host, cookie string, hdr ...string) {
t.Helper()
*reached = false
w := onePortCall(t, h, method, target, peer, host, cookie, hdr...)
if w.Code != want || *reached {
t.Errorf("the gateway %s: %d, want %d (page reached %v): %s", name, w.Code, want, *reached, w.Body.String())
}
}
// the gateway's client routes, judged as on its own port
gw("for an agent here", 200, "GET", "/v1/models", lo, here, "")
gw("from Origin: null", 403, "POST", "/v1/chat/completions", lo, here, "", "Origin", "null")
gw("from a foreign Origin", 403, "POST", "/v1/chat/completions", lo, here, "", "Origin", "https://evil.example")
gw("under a rebound Host", 403, "GET", "/v1/models", lo, evil, "", chromeMarks...)
gw("under a rebound Host, a POST", 403, "POST", "/v1/chat/completions", lo, evil, "", append([]string{"Origin", "http://" + evil}, chromeMarks...)...)
// the page's own cookie is no gateway key either
gw("under a rebound Host with the page's cookie", 403, "GET", "/v1/models", lo, evil, onePortWebKey, chromeMarks...)
gw("through a proxy without a key", 401, "GET", "/v1/models", lo, "magpie.example.com", "", "X-Forwarded-For", "203.0.113.9")
gw("through a proxy with the page's cookie", 401, "GET", "/v1/models", lo, "magpie.example.com", onePortWebKey, "X-Forwarded-For", "203.0.113.9")
gw("from another machine without a key", 401, "GET", "/v1/models", "10.0.0.5:50001", here, "")
gw("through a proxy with a gateway key", 200, "GET", "/v1/models", lo, "magpie.example.com", "", "X-Forwarded-For", "203.0.113.9", "Authorization", "Bearer "+key)
gw("from another machine with a gateway key", 200, "GET", "/v1/models", "10.0.0.5:50001", here, "", "Authorization", "Bearer "+key)
// not shared: another machine, or a proxy, isn't answered at all
s := settings.Load()
s.LAN = false
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
gw("through a proxy, not shared", 403, "GET", "/v1/models", lo, "magpie.example.com", "", "X-Forwarded-For", "203.0.113.9", "Authorization", "Bearer "+key)
gw("from another machine, not shared", 403, "GET", "/v1/models", "10.0.0.5:50001", here, "", "Authorization", "Bearer "+key)
}
const onePortWebKey = "0123456789abcdef0123456789abcdef"
func onePortPage(reached *bool) http.Handler {
return webServed("3430", onePortWebKey, 0, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
*reached = true
rw.WriteHeader(http.StatusTeapot)
}))
}
// zgcwkj on Discord: one reverse-proxy location for the page and the
// agents. `magpie web`'s port hands the gateway's client routes to the
// gateway this process serves, through the gateway's own guards.
func TestWebOnePortServedHere(t *testing.T) {
onePortHome(t)
g := gateway.New()
prev := served.Swap(g)
t.Cleanup(func() { served.Store(prev) })
var reached bool
onePortCases(t, onePortPage(&reached), &reached)
}
// With another magpie serving the gateway (the service, the app), the page
// passes the client routes on to it, and they are judged there as they
// came: Host, Origin and a proxy's forwarding headers kept, and one from
// another machine marked as such.
func TestWebOnePortServedElsewhere(t *testing.T) {
onePortHome(t)
other := httptest.NewServer(gateway.New().Front())
t.Cleanup(other.Close)
u, _ := url.Parse(other.URL)
t.Setenv("MAGPIE_ADDR", u.Host)
prev := served.Swap(nil)
t.Cleanup(func() { served.Store(prev) })
var reached bool
onePortCases(t, onePortPage(&reached), &reached)
}
// No file of the page is at a client route, where the gateway would
// answer in its place.
func TestWebAssetsAreNoClientPath(t *testing.T) {
fs.WalkDir(staticFS(), ".", func(p string, d fs.DirEntry, err error) error {
if err == nil && gateway.ClientPath("/"+p) {
t.Errorf("the page's /%s is a gateway client route", p)
}
return nil
})
}