gui: magpie web's page port judges every gateway route as from another machine (reported privately by Coffemon)
Since1b32bdcf(v0.1.1444) the page port of `magpie web` / `serve --web` hands the gateway's client routes to the gateway. The gateway counts a loopback request with no forwarding header as an agent on this computer, which needs no key. A request reaching the page port over loopback without such a header was answered that way, so the page port gave the gateway's routes without a gateway key, with sharing off. Before1b32bdcfevery request there needed the page's key. Now every request the page port hands over is marked (gateway.FromPagePort, the X-Magpie-Page-Port header) and local() is false for it: answered only while magpie is shared (or on the network), with an enabled gateway key, from loopback too, under any Host or browser marks, and with MAGPIE_TRUST_PROXY set. While not shared lanGuard refuses it 403, saying to use the gateway's own address. lanGuard moves the mark into the request's context and drops the header, so it never goes upstream. toGateway (another magpie serves the gateway) passes the mark on and adds the loopback peer as X-Forwarded-For when no forwarding header came, so an older gateway that doesn't know the mark still asks for a key. Agents on this computer keep the gateway's own port (3425), key-free from loopback as before. The page itself calls no client route (checked app.js and plugins.js: the /mcp and /v1 addresses there are copied, not fetched), so nothing in the GUI needs a key on the page port. The routes covered are all of gateway.ClientPath (/v1, /v1beta, the routes without /v1, the Codex backend path, the search path, /mcp), since the mark is set in onePort for each of them. docs/reference.md's one-port paragraph, gui-shell.md and gateway-routing.md now say the page port answers these routes as from another machine and that agents here use the gateway's port; the old text said a same-machine proxy's requests carry forwarding headers, which a forward that adds none doesn't. Tests: - TestWebOnePortServedHere / ServedElsewhere: a loopback request with no key and no forwarding header to /v1/chat/completions, /chat/completions, /v1/responses, /v1/messages, /v1/models, /v1beta/models, /mcp/github, /mcp/magpie/web-search and the Codex backend, under Host 127.0.0.1:3430, 127.0.0.1, localhost:3430 and a public name, with a browser's marks, a CLI's user agent and an IPv6 loopback peer: 403 not shared, 401 shared. Red on the old code (404 "knows no model" / "isn't signed in to an MCP server": the handlers were reached). The old "for an agent here" 200 is now 401; loopback with a gateway key 200. Origin null, foreign Origin and rebound Host without a key now 401 (lanGuard asks the key first); a foreign Origin with a key still 403. A 429 from the process's failed-key bucket counts as the 401 it replaces. - TestWebOnePortAgentsHereUseTheGatewayPort: the gateway's own Front from loopback without a key 200; the page port 403 with the reason and the gateway's URL; MAGPIE_TRUST_PROXY=1 doesn't make it local. - TestPagePortMarkIsNeverLocal (gateway): marked requests never local under MAGPIE_TRUST_PROXY; refused unshared; with a key the handler sees no header and local false. Broken by hand, each red, then restored: - onePort not marking: ServedHere and AgentsHere red; ServedElsewhere stays green on the X-Forwarded-For fallback alone (the older-gateway path). - local() ignoring the mark: ServedHere, AgentsHere, PagePortMark red. - lanGuard not taking the header off: PagePortMark red (handler saw it). Real try (built binaries, sandbox HOME, MAGPIE_NO_ACCOUNTS=1, MAGPIE_TEST_SANDBOX; gateway 127.0.0.1:3561, page 3562, a raw TCP forwarder (a Go listen/dial/copy loop, as socat would be) on 3563 -> 3562, a fake OpenAI-compatible upstream on 3564 added as a provider): - `serve --web` (gateway in process), origin/mainc37bcdbfcbinary: through the forward with no key, chat 200 (upstream called), /v1/models 200, /v1/responses and /v1/messages reached the provider, /mcp/magpie/web-search with Host 127.0.0.1 200; the page's /api/settings 401. 5 upstream calls. This change: every one of those 403 with the page-port reason; the gateway port's agent chat 200; 1 upstream call (the agent's). - `serve` plus a separate `magpie web` (served elsewhere): old binary 200 for chat and MCP through the forward, shared or not, with no key or the page's cookie only. This change: 403 not shared; shared, 401 with no key and with the page's cookie only, 200 with a gateway key; the page signs in through the forward and /api/settings is 200 with its cookie; the upstream request carries no X-Magpie-Page-Port. Run at this head (rebased on origin/mainf0ffee34c): - go test ./internal/gui ./internal/gateway -run 'TestWebOnePort|TestClientPath|TestWebAssets|TestPagePortMark|TestAllowList|TestLAN|TestRebound|TestProxied' -count=1: ok - gofmt -l internal/gateway internal/gui: empty; go vet (darwin), GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet -tags nogui ./... (before the rebase), GOOS=linux/windows go vet of gateway, gui and cli at this head: ok - The full suite (go test -tags nogui -count=1 -timeout 45m ./..., one per machine, waiting behind another session's run) was not run before this push: a security fix ships at once. It runs at this head next; any red it names gets its own commit. Not tried: Windows and Linux beyond vet; nginx (its XFF path is unchanged and covered by the "through a proxy" cases); a real vendor account; the GUI suite (no GUI asset changed).
This commit is contained in:
+21
-11
@@ -1896,17 +1896,27 @@ page's port (`127.0.0.1:3430`) answers the gateway's client routes as well
|
||||
(`/v1/…`, `/v1beta/…`, `/mcp/…`, `/backend-api/codex/…` and the same
|
||||
routes without `/v1`), handing them to the gateway, so a proxy that sends
|
||||
everything to the page's port serves the page at `https://magpie.example.com/`
|
||||
and the agents at `https://magpie.example.com/v1`. Those routes are judged
|
||||
there exactly as on the gateway's port (the allow list, a gateway key from
|
||||
another machine or through the proxy, the Origin and Host checks); the
|
||||
page's sign-in cookie is no gateway key, and the page and its `/api` still
|
||||
need the page's key. Ollama's `/api/*` and Claude Desktop's `/api/hello`
|
||||
are the page's own namespace on that port, so a client of those is given
|
||||
the gateway's own port. When the
|
||||
proxy and magpie run on the same machine, a request it forwards over
|
||||
loopback carries a forwarding header and needs a gateway key as one from
|
||||
the network does (see [Providers and the gateway](#providers-and-the-gateway)); a proxy that
|
||||
authenticates its clients itself can be trusted with `MAGPIE_TRUST_PROXY=1`.
|
||||
and the agents at `https://magpie.example.com/v1`. On the page's port every
|
||||
one of those routes is answered as a request from another machine, from
|
||||
loopback too: only while Settings → Network and sharing → Share on local
|
||||
network is on (or `MAGPIE_ADDR` is on the network), and with an enabled
|
||||
gateway key, after the allow list. A forward to that port that adds no
|
||||
forwarding header (frp's `tcp`, `ssh -R`, socat, an nginx `proxy_pass`
|
||||
without `X-Forwarded-For`) reaches it from loopback, so magpie can't tell
|
||||
it from an agent on this computer and never answers it as one;
|
||||
`MAGPIE_TRUST_PROXY=1` doesn't change that. The page's sign-in cookie is
|
||||
no gateway key, and the page and its `/api` still need the page's key.
|
||||
Agents on this computer call the gateway's own port (`127.0.0.1:3425`),
|
||||
which needs no key from loopback. Ollama's `/api/*` and Claude Desktop's
|
||||
`/api/hello` are the page's own namespace on that port, so a client of
|
||||
those is given the gateway's own port. On the gateway's own port, a
|
||||
request a proxy on the same machine forwards over loopback with a
|
||||
forwarding header needs a gateway key as one from the network does (see
|
||||
[Providers and the gateway](#providers-and-the-gateway)), and one without
|
||||
a forwarding header looks like an agent here: put the page's port, not
|
||||
the gateway's, behind a forward that adds none. A proxy that authenticates
|
||||
its clients itself can be trusted on the gateway's port with
|
||||
`MAGPIE_TRUST_PROXY=1`.
|
||||
|
||||
It exposes:
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -249,6 +249,7 @@ func lanGuard(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
r = takePagePort(r)
|
||||
// a WSL distro's agent, at the address Windows has on its network
|
||||
// (wsl.go), is one of this computer's
|
||||
if fromWSL(r) {
|
||||
@@ -266,6 +267,11 @@ func lanGuard(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
if remote && !remoteKeyed() {
|
||||
if viaPagePort(r) {
|
||||
log.Printf("refused %s %s on magpie web's page port: magpie isn't shared", r.Method, r.URL.Path)
|
||||
http.Error(w, "this request came to magpie web's page port, which answers the gateway's routes as from another machine (the port may sit behind a proxy or a forward): only while Settings → Network and sharing → Share on local network is on, with an enabled gateway key (Gateway → Gateway keys) sent as Authorization: Bearer <key> or x-api-key. An agent on this computer calls the gateway's own address ("+URL()+") instead, which needs no key", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if proxied(r) {
|
||||
log.Printf("refused %s %s through a proxy or tunnel: magpie isn't shared", r.Method, r.URL.Path)
|
||||
http.Error(w, "this request came through a proxy or tunnel (it carries a forwarding header such as X-Forwarded-For or Cf-Connecting-IP), so magpie answers it as one from another machine: only while Settings → Network and sharing → Share on local network is on, with an enabled gateway key (Gateway → Gateway keys) sent as Authorization: Bearer <key> or x-api-key", http.StatusForbidden)
|
||||
@@ -555,14 +561,15 @@ func refusedKey(key string) string {
|
||||
}
|
||||
|
||||
// local is a request from this computer: one that reached the gateway on
|
||||
// loopback and wasn't passed on by a proxy or tunnel running here.
|
||||
// loopback, wasn't passed on by a proxy or tunnel running here, and didn't
|
||||
// come through `magpie web`'s page port (viaPagePort).
|
||||
func local(r *http.Request) bool {
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && ip.IsLoopback() && !proxied(r)
|
||||
return ip != nil && ip.IsLoopback() && !proxied(r) && !viaPagePort(r)
|
||||
}
|
||||
|
||||
// forwardHeaders are what a proxy or tunnel adds to a request it passes
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
@@ -46,3 +47,47 @@ func ClientPath(p string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PagePortHeader marks a request `magpie web`'s page port handed to the
|
||||
// gateway (FromPagePort). The page's port is the one put behind a proxy
|
||||
// or a forward, and a raw TCP forward (frp's tcp, ssh -R, socat) or a
|
||||
// proxy that adds no forwarding header reaches it from loopback with
|
||||
// nothing to tell it from an agent here. So no request through it is
|
||||
// local: each is judged as one from another machine, answered only while
|
||||
// magpie is shared and with an enabled gateway key, whatever peer, Host
|
||||
// or browser marks it has, and MAGPIE_TRUST_PROXY doesn't change that
|
||||
// (reported privately by Coffemon). Agents here call the gateway's own
|
||||
// port, which needs no key from loopback, as before the page's port
|
||||
// answered client routes. The header only ever takes a request's
|
||||
// locality away, so one a client sends itself costs nobody else; lanGuard
|
||||
// moves it into the request's context, so it never goes on upstream.
|
||||
const PagePortHeader = "X-Magpie-Page-Port"
|
||||
|
||||
// pagePort is the context key of a request the page's port handed over.
|
||||
type pagePort struct{}
|
||||
|
||||
// FromPagePort is r marked as handed over by the page's port: for the
|
||||
// gateway this process serves (Front), or the header toGateway passes on.
|
||||
func FromPagePort(r *http.Request) *http.Request {
|
||||
r = r.Clone(r.Context())
|
||||
r.Header.Set(PagePortHeader, "1")
|
||||
return r
|
||||
}
|
||||
|
||||
// viaPagePort: r came through `magpie web`'s page port (FromPagePort).
|
||||
func viaPagePort(r *http.Request) bool {
|
||||
ok, _ := r.Context().Value(pagePort{}).(bool)
|
||||
return ok || r.Header.Get(PagePortHeader) != ""
|
||||
}
|
||||
|
||||
// takePagePort moves the page port's mark from r's headers into its
|
||||
// context, so nothing passes the header on to a vendor.
|
||||
func takePagePort(r *http.Request) *http.Request {
|
||||
if r.Header.Get(PagePortHeader) == "" {
|
||||
return r
|
||||
}
|
||||
r = r.WithContext(context.WithValue(r.Context(), pagePort{}, true))
|
||||
r.Header = r.Header.Clone()
|
||||
r.Header.Del(PagePortHeader)
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/access"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
@@ -74,3 +77,52 @@ func TestClientPathOnlyClean(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A request the page's port marked (FromPagePort, or the header toGateway
|
||||
// passes on) is never local, and lanGuard takes the header off before any
|
||||
// handler, so nothing passes it on upstream. One without the mark, from
|
||||
// loopback, is an agent here as before.
|
||||
func TestPagePortMarkIsNeverLocal(t *testing.T) {
|
||||
fresh(t)
|
||||
t.Setenv("MAGPIE_TRUST_PROXY", "1")
|
||||
var sawLocal, reached, sawHeader bool
|
||||
h := lanGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
sawLocal, reached, sawHeader = local(r), true, r.Header.Get(PagePortHeader) != ""
|
||||
}))
|
||||
r := httptest.NewRequest("GET", "/v1/models", nil)
|
||||
r.RemoteAddr = "127.0.0.1:50001"
|
||||
h.ServeHTTP(httptest.NewRecorder(), r)
|
||||
if !sawLocal {
|
||||
t.Fatal("an agent here, unmarked: not local")
|
||||
}
|
||||
if local(FromPagePort(r)) {
|
||||
t.Fatal("marked by the page's port: local")
|
||||
}
|
||||
for _, m := range []*http.Request{FromPagePort(r), takePagePort(FromPagePort(r))} {
|
||||
if !viaPagePort(m) {
|
||||
t.Fatal("the mark was lost")
|
||||
}
|
||||
}
|
||||
if takePagePort(FromPagePort(r)).Header.Get(PagePortHeader) != "" {
|
||||
t.Fatal("takePagePort left the header on")
|
||||
}
|
||||
reached = false
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, FromPagePort(r))
|
||||
if w.Code != http.StatusForbidden || reached {
|
||||
t.Fatalf("marked, not shared: %d, handler reached %v", w.Code, reached)
|
||||
}
|
||||
// shared, with a key: answered, the mark kept in the context alone
|
||||
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3499")
|
||||
secret, err := access.Update("add-key", access.Change{Name: "Laptop"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
k := FromPagePort(r)
|
||||
k.Header.Set("Authorization", "Bearer "+secret)
|
||||
w = httptest.NewRecorder()
|
||||
h.ServeHTTP(w, k)
|
||||
if !reached || sawLocal || sawHeader {
|
||||
t.Fatalf("marked, with a key: %d, handler reached %v, saw local %v, the header %v", w.Code, reached, sawLocal, sawHeader)
|
||||
}
|
||||
}
|
||||
|
||||
+17
-8
@@ -407,16 +407,21 @@ func isWeb(w Windows) bool { _, ok := w.(webHost); return ok }
|
||||
// /v1beta/…, /mcp/…) on the page's port to the gateway, so the page and the
|
||||
// agents share one address and a reverse proxy needs one location for both
|
||||
// (zgcwkj on Discord). Each such request is judged by the gateway's own
|
||||
// guards (Server.Front), not the page's key: an agent has no cookie, and a
|
||||
// request from another machine or through a proxy needs a gateway key
|
||||
// while magpie is shared, as on the gateway's port. Everything else (the
|
||||
// page, its /api/*) stays behind the page's allow list and key.
|
||||
// guards (Server.Front), not the page's key, and always as one from
|
||||
// another machine (gateway.FromPagePort): it needs a gateway key and
|
||||
// magpie shared, from loopback too. This port is the one put behind a
|
||||
// proxy or a forward, and a raw TCP forward or a proxy that adds no
|
||||
// forwarding header arrives from loopback looking like an agent here
|
||||
// (reported privately by Coffemon). Agents here use the gateway's own
|
||||
// port. Everything else (the page, its /api/*) stays behind the page's
|
||||
// allow list and key.
|
||||
func onePort(page http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if !gateway.ClientPath(r.URL.Path) {
|
||||
page.ServeHTTP(rw, r)
|
||||
return
|
||||
}
|
||||
r = gateway.FromPagePort(r)
|
||||
if gw := served.Load(); gw != nil {
|
||||
gw.Front().ServeHTTP(rw, r)
|
||||
return
|
||||
@@ -428,10 +433,11 @@ func onePort(page http.Handler) http.Handler {
|
||||
// toGateway passes a client route on to the gateway another magpie serves
|
||||
// here (the service, the app), as it came: its Host, so a rebound page's
|
||||
// name is still refused, and its forwarding headers, so a request a proxy
|
||||
// passed on is still one from another machine. One from another machine
|
||||
// straight to this port gets the peer's address added as X-Forwarded-For,
|
||||
// as the gateway would otherwise see this process's loopback call and ask
|
||||
// it no key.
|
||||
// passed on is still one from another machine. It carries the page
|
||||
// port's mark (gateway.PagePortHeader, set by onePort), so the gateway
|
||||
// judges it as from another machine wherever it came from; and it always
|
||||
// carries an X-Forwarded-For (the peer's address added), so a gateway of
|
||||
// an older magpie, which doesn't know the mark, still does.
|
||||
var toGateway = &httputil.ReverseProxy{
|
||||
Rewrite: func(pr *httputil.ProxyRequest) {
|
||||
u, _ := url.Parse(gateway.URL())
|
||||
@@ -445,6 +451,9 @@ var toGateway = &httputil.ReverseProxy{
|
||||
}
|
||||
if !loopbackPeer(pr.In.RemoteAddr) {
|
||||
pr.SetXForwarded()
|
||||
} else if pr.Out.Header.Get("X-Forwarded-For") == "" {
|
||||
h, _, _ := net.SplitHostPort(pr.In.RemoteAddr)
|
||||
pr.Out.Header.Set("X-Forwarded-For", h)
|
||||
}
|
||||
},
|
||||
FlushInterval: -1, // streams as they come
|
||||
|
||||
@@ -98,6 +98,23 @@ func onePortCases(t *testing.T, h http.Handler, reached *bool) {
|
||||
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)
|
||||
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...)
|
||||
// a 401's refusal for want of a key turns 429 once the process's
|
||||
// failed-key bucket (auththrottle.go) is spent: these are many
|
||||
if w.Code != want && !(want == http.StatusUnauthorized && w.Code == http.StatusTooManyRequests) || *reached {
|
||||
t.Errorf("the gateway %s: %d, want %d (page reached %v): %s", name, w.Code, want, *reached, w.Body.String())
|
||||
}
|
||||
}
|
||||
// A raw TCP forward to the page's port (frp's tcp, ssh -R, socat), or
|
||||
// a proxy that adds no forwarding header, arrives from loopback with
|
||||
// nothing an agent here wouldn't send: no client route there is
|
||||
// answered as local, so with magpie not shared none is answered at all
|
||||
// (reported privately by Coffemon). Browser marks change nothing.
|
||||
forwarded(t, gw, 403, lo)
|
||||
|
||||
// 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)
|
||||
@@ -107,22 +124,19 @@ func onePortCases(t *testing.T, h http.Handler, reached *bool) {
|
||||
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 gateway's client routes, judged as from another machine: shared
|
||||
// now, each needs a gateway key, from loopback too
|
||||
forwarded(t, gw, 401, lo)
|
||||
gw("from loopback with the page's cookie", 401, "GET", "/v1/models", lo, here, onePortWebKey)
|
||||
gw("from loopback with a gateway key", 200, "GET", "/v1/models", lo, here, "", "Authorization", "Bearer "+key)
|
||||
gw("from loopback with a gateway key as x-api-key", 200, "GET", "/v1/models", lo, "127.0.0.1", "", "x-api-key", key)
|
||||
gw("from Origin: null", 401, "POST", "/v1/chat/completions", lo, here, "", "Origin", "null")
|
||||
gw("from a foreign Origin", 401, "POST", "/v1/chat/completions", lo, here, "", "Origin", "https://evil.example")
|
||||
gw("from a foreign Origin with a gateway key", 403, "POST", "/v1/chat/completions", lo, here, "", "Origin", "https://evil.example", "Authorization", "Bearer "+key)
|
||||
gw("under a rebound Host", 401, "GET", "/v1/models", lo, evil, "", chromeMarks...)
|
||||
gw("under a rebound Host, a POST", 401, "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("under a rebound Host with the page's cookie", 401, "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, "")
|
||||
@@ -139,6 +153,29 @@ func onePortCases(t *testing.T, h http.Handler, reached *bool) {
|
||||
gw("from another machine, not shared", 403, "GET", "/v1/models", "10.0.0.5:50001", here, "", "Authorization", "Bearer "+key)
|
||||
}
|
||||
|
||||
// forwarded checks what a request a raw TCP forward or a proxy without
|
||||
// forwarding headers passes on to the page's port gets on the client
|
||||
// routes, peer on loopback and no key: want, under any Host and whatever a
|
||||
// browser's marks say, on the chat, Responses, Messages, model and MCP
|
||||
// routes.
|
||||
func forwarded(t *testing.T, gw func(string, int, string, string, string, string, string, ...string), want int, peer string) {
|
||||
t.Helper()
|
||||
for _, host := range []string{"127.0.0.1:3430", "127.0.0.1", "localhost:3430", "magpie.example.com"} {
|
||||
gw("forwarded to "+host+": chat completions", want, "POST", "/v1/chat/completions", peer, host, "")
|
||||
gw("forwarded to "+host+": chat completions without /v1", want, "POST", "/chat/completions", peer, host, "")
|
||||
gw("forwarded to "+host+": responses", want, "POST", "/v1/responses", peer, host, "")
|
||||
gw("forwarded to "+host+": messages", want, "POST", "/v1/messages", peer, host, "")
|
||||
gw("forwarded to "+host+": models", want, "GET", "/v1/models", peer, host, "")
|
||||
gw("forwarded to "+host+": Gemini", want, "GET", "/v1beta/models", peer, host, "")
|
||||
gw("forwarded to "+host+": an MCP server", want, "POST", "/mcp/github", peer, host, "")
|
||||
gw("forwarded to "+host+": magpie's web search MCP", want, "POST", "/mcp/magpie/web-search", peer, host, "")
|
||||
gw("forwarded to "+host+": the Codex backend", want, "POST", gateway.CodexPath+"/responses", peer, host, "")
|
||||
}
|
||||
gw("forwarded, a browser's marks", want, "POST", "/v1/chat/completions", peer, "127.0.0.1:3430", "", chromeMarks...)
|
||||
gw("forwarded, a CLI's user agent", want, "POST", "/v1/chat/completions", peer, "127.0.0.1:3430", "", "User-Agent", "claude-cli/2.0.0 (external, cli)")
|
||||
gw("forwarded, an IPv6 loopback peer", want, "POST", "/v1/chat/completions", "[::1]:50001", "[::1]:3430", "")
|
||||
}
|
||||
|
||||
const onePortWebKey = "0123456789abcdef0123456789abcdef"
|
||||
|
||||
func onePortPage(reached *bool) http.Handler {
|
||||
@@ -186,3 +223,34 @@ func TestWebAssetsAreNoClientPath(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// An agent here keeps the gateway's own port, key-free from loopback as
|
||||
// before; only the page's port judges every client route as from another
|
||||
// machine, and MAGPIE_TRUST_PROXY (a proxy that signs its clients in)
|
||||
// doesn't make the page's port local again.
|
||||
func TestWebOnePortAgentsHereUseTheGatewayPort(t *testing.T) {
|
||||
onePortHome(t)
|
||||
g := gateway.New()
|
||||
prev := served.Swap(g)
|
||||
t.Cleanup(func() { served.Store(prev) })
|
||||
var reached bool
|
||||
page := onePortPage(&reached)
|
||||
if w := onePortCall(t, g.Front(), "GET", "/v1/models", "127.0.0.1:50001", "127.0.0.1:3425", ""); w.Code != 200 {
|
||||
t.Fatalf("an agent here on the gateway's port: %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
w := onePortCall(t, page, "GET", "/v1/models", "127.0.0.1:50001", "127.0.0.1:3430", "")
|
||||
if w.Code != 403 || !strings.Contains(w.Body.String(), "page port") || !strings.Contains(w.Body.String(), gateway.URL()) {
|
||||
t.Fatalf("an agent here on the page's port, not shared: %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
t.Setenv("MAGPIE_TRUST_PROXY", "1")
|
||||
if w := onePortCall(t, page, "POST", "/v1/chat/completions", "127.0.0.1:50001", "127.0.0.1:3430", ""); w.Code != 403 {
|
||||
t.Fatalf("with MAGPIE_TRUST_PROXY, the page's port: %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
key := onePortKey(t)
|
||||
if w := onePortCall(t, page, "POST", "/mcp/magpie/web-search", "127.0.0.1:50001", "127.0.0.1", ""); w.Code != 401 && w.Code != 429 {
|
||||
t.Fatalf("with MAGPIE_TRUST_PROXY, shared, the page's port's MCP without a key: %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w := onePortCall(t, page, "GET", "/v1/models", "127.0.0.1:50001", "127.0.0.1", "", "Authorization", "Bearer "+key); w.Code != 200 {
|
||||
t.Fatalf("the page's port with a gateway key: %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user