Since 1b32bdcf (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. Before 1b32bdcf every
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/main c37bcdbfc binary: 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/main f0ffee34c):
- 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).