Commit Graph
26 Commits
Author SHA1 Message Date
yetone 361da10ee9 web: the page's API answers its own origin only, not a page on another port or a sibling subdomain
With browser access on (Settings > Browser access, or magpie web), the
sign-in cookie magpie_web_<port> is SameSite=Lax, which a browser sends
with any same-site request: another port of 127.0.0.1 or of the LAN
address, or a sibling name behind the same reverse proxy. apiFramed
refused Origin null, frames and cross-site, but not same-site, and /api
decodes JSON from any Content-Type. So such a page could POST a stdio MCP
server to /api/library/servers/save (text/plain, no-cors) and start it
with /api/library/mcp/check: code run as the user.

webOwnPage (in webHandler, behind webGuard, /api/ only) now judges every
write (all methods but GET/HEAD/OPTIONS) with net/http's
CrossOriginProtection: Sec-Fetch-Site same-origin or none passes, any
other value is refused; from a browser without Sec-Fetch-Site, Origin must
be absent or name the request's Host (or MAGPIE_PUBLIC_URL, for a proxy
that rewrites Host). Any /api request, a GET too, whose Sec-Fetch-Site is
same-site or cross-site is refused, so no GET route with a side effect
(GET /api/import/{id} reads once and deletes) is another page's to call,
without auditing each one. A Sec-Fetch-Site same-origin with an Origin
that doesn't match the Host passes: the browser compared the origins
itself, and nginx's default proxy_set_header Host $proxy_host would
otherwise break every write behind it. The desktop window's /api is served
in-process over no port with no cookie, so it isn't wrapped and its
headers are unchanged. No Content-Type rule: every write the page makes
goes through api() as application/json, but multipart and text/plain are
both CORS-safelisted, so the rule would add nothing the origin check
doesn't already give.

Run on mote at 49c9491b7 + this commit; after the rebase onto e1eeeed73 (38 commits, none in web.go/pluginpage.go/api.go) gofmt, the three vets of internal/gui and -run TestWeb were run again: ok.
- TestWebAPIAnswersItsOwnPageOnly, through webServed (onePort ->
  AllowGuard -> webGuard -> webOwnPage) with the page's cookie and no
  gateway key. Refused: same-site Origin on another port, cross-port
  Origin without Sec-Fetch-Site, localhost on another port, the LAN
  address on another port (both), a sibling subdomain (both), cross-site,
  Origin null (both), no Origin with Sec-Fetch-Site same-site and
  cross-site, GETs from another port, a sibling and a cross-site link, a
  cross-port preflight. Allowed: the page's own origin (with and without
  Sec-Fetch-Site), on the LAN, behind a proxy keeping or rewriting the
  Host, MAGPIE_PUBLIC_URL with an old browser, Sec-Fetch-Site none, a
  non-browser client with the cookie.
- Real browsers: magpie web built from origin/main and from this commit
  (-tags nogui, sandbox HOME, MAGPIE_WEB_KEY), an attacker page on
  127.0.0.1:3497 / localhost:3497 running the no-cors text/plain POST.
  Old: the attacker's server was saved in Chromium and WebKit. New: not
  saved in either, logged "turned away ... Sec-Fetch-Site"; the page's
  own JSON write answered 200 in both.
- go test -tags nogui ./internal/gui -timeout 45m: ok
- gofmt -l: nothing; go vet ./internal/gui/... ./internal/cli/...,
  GOOS=windows go vet ./internal/gui/..., GOOS=linux go vet -tags nogui
  ./internal/gui/...: ok

Broken by hand: webOwnPage taken out of webHandler -> 29 cases red
("reached true, want reached false"). The read rule taken out -> the
three GETs and the preflight red. cop.Check skipped -> the 8 old-browser
cases red.

Not tried: the full ./... suite (the change is one handler in
internal/gui); the GUI Playwright suite (its pages mock /api with
page.route and never reach this server; the real-browser check above
stands in); a real reverse proxy; the Windows and Linux app pages; the
MAGPIE_DEV_UI dev server, which serves /api with no key and isn't
wrapped.
2026-11-06 22:17:04 +08:00
yetone f4c664a09c plugin, gui: magpie web ends its plugin hosts, waited for, before it runs the new version (#3285)
The second way acezan's 57 <defunct> magpie-bun (v0.1.2819, a Linux
systemd service, 47 hours) pile up: magpie web's restart to update
(AutoRestart) runs the new version with syscall.Exec in the same pid.
The running plugin host was the old image's child, and the old image's
host.read held the only Wait for it. After the exec nothing waits for
it, so it ends on stdin EOF and stays <defunct> under that pid, one per
restart to update. skygunner reproduced it and AAEE86 counted 22 zombies
for 23 images under one pid (#3285). With about a hundred releases a
day, an AutoRestart service makes about one a hour, as acezan's did.

- plugin.EndHosts (host.go) stops every host started and not yet
  reaped, including the retiring ones, waits until each one has been
  waited for, and refuses to start another (errHostsEnded). startOn
  starts the host and records it under one lock (live), so a host that
  starts during EndHosts isn't missed.
- Web.Wait calls it right before update.Reexec.

Run at 72bc741ef (on origin/main 40a287b21, after the previous commit):
- mote (macOS): gofmt -l clean; go vet ./internal/gui ./internal/plugin,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok.
  go test -count=1 -timeout 45m ./internal/plugin: ok; -run Web
  ./internal/gui: ok.
- WSL magpie-desktop (Linux): ./internal/plugin ok, including
  TestEndHostsLeavesNoChild and TestHostReapedWhileItsChildHoldsStdout
  with a real Bun host; -race -count=3 of both: ok; go vet -tags nogui
  and -run Web of ./internal/gui: ok.

Both commits were then rebased onto 0acba61a3, which changes only
app.js and plugin-checkin.test.cjs; no Go file these runs built moved,
and they were not run again there.

Broken by hand: EndHosts returning at once makes
TestEndHostsLeavesNoChild fail on Linux ("children left once EndHosts
returned: [16459 S bun]").

Not tried: a real magpie web restart to update (a downloaded update
exec'd in the same pid) on Linux. The test covers EndHosts, not the
exec. Not changed: anything else alive at the exec (pii's helper,
awake's caffeinate, #4188) still outlives it the same way.

Reported by acezan on Discord. Diagnosed in #3285 by skygunner and
AAEE86.
2026-11-03 17:58:31 +08:00
yetone 5eab14a8cc plugin pages: magpie web reaches a page's local service on its own address, so the chat page works from another computer
aigcfree (Discord DM) runs `magpie web --addr 0.0.0.0:3430` on an Ubuntu
server behind nginx and opens it from a Windows PC. Plugins -> 对话
(@magpie-community/page-chat) showed "127.0.0.1 refused to connect": the
page sends its frame to the chat server on 127.0.0.1, which in that
browser is the Windows PC.

A plugin now declares its page's service (package.json
magpie.page.service {port, option}: the port, or the plugin option that
holds it, from its plugins.json options; plugin.ServiceSpec). Served to a
browser (isWeb), /api/plugin-pages gives the page `service`:
_magpie/service/ under its token, and the bridge hands the frame
magpie.service as an absolute address. The desktop window and slot pages
get none, and the routes answer 404 there, so the local path is as before.

servePageService reverse-proxies /_magpie/plugin/<token>/_magpie/service/…
to 127.0.0.1:<port>. The service stays on loopback. The way in is the
page's own: the per-run token that only a signed-in /api hands out,
behind AllowGuard. Nothing is weakened:
- Host is set to 127.0.0.1:<port>, which the service's rebinding check
  wants.
- The window's cookie is taken off.
- Origin must be absent or null (403 otherwise).
- Set-Cookie is dropped.
- Every reply is sandboxed (sandbox allow-scripts; frame-ancestors
  'self'), with Referrer-Policy no-referrer and nosniff.
- The gateway's own port is never served, since a request from this
  server is loopback and the gateway takes that without a key.
- Event streams flush as they come (FlushInterval -1, X-Accel-Buffering:
  no for nginx). webCompressed no longer gzips text/event-stream, which
  held a stream back until gzip's buffer filled.
- A redirect to the service's own address is made relative.
- The entry starts the plugin host as the page's HTML does (pageHost).
- A service not up yet answers its frame with a 502 page that tries
  again in 2s.

page-chat 0.1.2 (magpie-community/plugins 4a46212, on dist) declares the
service, goes to magpie.service when given one (else 0.1.1's path:
127.0.0.1 on loopback, the SSH-tunnel note from elsewhere), and asks
api/… by relative address.

Docs: plugin-runtime.md P4 (the page row), gui-shell.md (no gzip for event
streams).

Tests: TestPluginPageServiceListedToBrowserOnly, TestPluginPageServiceSpec,
TestPluginPageServiceProxied (through webHandler: Host, cookie, Set-Cookie,
CSP, escaped path and query, foreign Origin 403, made-up token 404,
redirects, SSE first event before the end and not gzipped),
TestPluginPageServiceDown, TestServiceLocation; plugin-page.test.cjs
checks magpie.service in a browser page and its absence in a slot.

Run at 276683d85, this change on origin/main e61e10535. It was rebased
since onto c1660481c, a one-line WorkBuddy mover min in internal/provider.
181fd0a68
(another session's proxy, path _service/, {portOption}) is reverted
there by 3870ddf0a, and nothing of it is left:
- go test ./internal/gui/ ./internal/plugin/ -timeout 45m, native on the
  Windows box: every new test ok. 8 reds, none in code this touches:
  - 6 fail the same at origin/main c1660481c, run there by name:
    TestGatewayHeldByAContainer, TestLodyImportHandoff,
    TestImportStatusUnreadableIsUnknown, TestAionUiImportRow,
    TestUpdateThatWontStart, TestPiPackage. All are in #4115.
  - TestLoadConcurrentWithSavesAndEdits is red 2 of 3 alone here and red
    at c1660481c too (a rename "Access is denied"); it is in #1827.
  - TestDesktopSessionTitlesOnPages passes alone (3 of 3). In the package
    run it listed the box's real WSL Codex session. Filed as #4185.
- Earlier, at bc247a8cc, on mote: the new Go tests, and go vet of both
  packages.
- Not run at this tree: the full go test ./... . mote was at load 300+
  with three other sessions' full suites, and the Linux box had one. A
  run at an older base (fa5ff2fec) had 25 packages ok, internal/agent
  among them, and none red when it was stopped for the rebase.
- gofmt -l: clean. go vet ./internal/gui ./internal/plugin: darwin on
  mote, native Windows on xiaomi-gpu. A Linux vet from mote fails in wails'
  cgo files, and in WSL for lack of gtk4/webkitgtk-6.0 headers, both
  unrelated to this change; CI vets Linux with its headers.
- plugin-page.test.cjs on the Windows box, both engines: WebKit 3/3.
  Chromium's first test is #4180, which fails the same on origin/main
  f463a8aaf. The next commit fixes #4180 at its cause.
- Real: a sandbox magpie web on mote (0.0.0.0:3561, sandbox HOME,
  MAGPIE_WEB_KEY, the real chat package from a folder, a fake gateway).
  Playwright on the Windows box opened Plugins -> chat over the LAN in
  Chromium and WebKit. The frame went to
  http://mote.local:3561/_magpie/plugin/<tok>/_magpie/service/?theme=…
  with origin null. Its fonts.css and style.css came through the proxy. A
  sent message streamed back "remote chat works", and the fake gateway
  logged both POSTs.
  - From the Mac over the LAN: /api/plugin-pages without the cookie
    gives 401. A made-up token gives 404. A foreign Origin gives 403.
    POST api/chat without the chat token gives 403 (the service's own
    check). mote.local:3563 directly is refused (loopback only).
  - Before bun was reachable in the sandbox, the frame got the 502 retry
    page.
- Broken by hand, each red then restored:
  - Without the event-stream exclusion, the stream arrives gzipped.
  - Without the Host rewrite, the cookie strip or the web gate, each
    assertion is red.
  - With the bridge's service dropped, plugin-page.test.cjs is red in
    WebKit ("magpie.service").

Not tried:
- A real nginx in front.
- A WebSocket through the proxy (httputil handles the upgrade; the chat
  page uses none).
- TLS (--tls-cert).
- The desktop app with page-chat 0.1.2: its fallback is 0.1.1's path,
  covered by the plugin's launcher tests.
2026-10-29 14:28:24 +08:00
yetone 27bf14790f gui: the page's links for other computers are at the gateway's addresses
A user's computer has a static public address from its ISP on the
network card itself (203.236.50.70) as well as a private one
(10.77.70.2). Settings › Network & sharing listed 203.236.50.70 as the
gateway's address but only 10.77.70.2 under "Open magpie in a browser",
and they asked why the two differ. The gateway's list
(gateway.LANURLs) took every IPv4 address on an interface that is up;
the page's (gui.LANAddrs) took private ones only, through
net.InterfaceAddrs, down interfaces included. The page server in LAN
mode listens on 0.0.0.0, so the public address serves it too.

Now one list makes both: gateway.LANHosts, every interface that is up,
IPv4, not loopback or link-local, in the interfaces' order.
gui.LANAddrs (and so NetworkLinks, the CLI's `magpie web` and daemon
links) returns it, MAGPIE_PUBLIC_URL's host still first when set.
The page's links now also show a CGNAT/Tailscale 100.64.x address, as
the gateway's already did.

Run, on mote: go vet ./internal/gateway ./internal/gui ./internal/cli;
GOOS=windows go vet ./internal/gateway ./internal/gui; GOOS=linux go vet
./internal/gateway; go test -run
"LAN|Lan|URL|TLS|Public|Network|Share|Allow|Container"
./internal/gateway ok; go test ./internal/gui ./internal/cli: cli ok,
gui ok but for TestMainWindowCorners/macOS_26_SDK, #3949 (macOS 27,
not about addresses). On xiaomi-gpu (Windows): the gateway's LANHosts,
PublicURL and TLS address tests ok.
New: TestLANHostsKeepAPublicAddress (a stubbed public, private, CGNAT,
link-local, loopback and IPv6 address) and
TestNetworkLinksAtTheGatewaysHosts.
Broken by hand: LANHosts keeping private addresses only, as the page's
list did, turns TestLANHostsKeepAPublicAddress red (got [10.77.70.2]).
Not run: GOOS=linux vet of internal/gui (needs cgo; the WSL box lacks
gtk4 and webkitgtk-6.0 headers); not tried on the reporter's machine.
2026-10-28 19:54:53 +08:00
yetone e056779a67 gui: the app serves its page to a browser, behind its key (Settings › Browser access)
jiakun_zhao on X ran `magpie web` in a terminal beside the desktop app,
to manage it through a Cloudflare tunnel. Settings › Network and sharing ›
Browser access now has the app serve the same page itself: Off, This
computer (127.0.0.1, for a tunnel or SSH forward) or Local network.

Access is magpie web's, unchanged: the key in the link is traded for the
cookie (webGuard), the allow list holds, and the gateway's client routes
on the page's port need a gateway key (onePort). A rebound Host and
Origin: null get no cookie. There is no way to serve it without the key.
The key and choice live in web-page.json (0600) beside settings.json, so
a sync never carries them. GET/POST /api/web-page are the app windows'
alone: a browser on the page can't read the key or turn the page off. A
busy port (magpie web, the magpie before an update) is tried every 4s;
`magpie web` on the same port opens the app's link instead.

Run at d43b7c40d (this change on ef155a5ca), then rebased onto the four
newer main commits, which touch gateway, usage, proc and library only:
- gofmt -l internal/gui internal/cli: nothing. go vet, GOOS=linux go vet
  -tags nogui and GOOS=windows go vet of internal/gui and internal/cli:
  clean. go build: ok.
- go test -timeout 45m ./internal/gui/ ./internal/cli/: ok (180s, 70s).
- GUI, alone: browser-access.test.cjs and model-test-one.test.cjs pass in
  both engines; i18n-keys 23 of 24, its red is #3897 ("Runs as" from
  7f827b3b0), not this change. agent-stale-copies.test.cjs 80 of 80 at
  8d343a811 (this change before the rebase).
- Full GUI suite at 8d343a811: cut short. The disk filled (708MB free):
  browsers failed to launch with ENOSPC in agent-stale-copies, which then
  passed 80 of 80 alone, and node ended with exit 7 before the rest of
  the files ran. There is no full GUI record for this change.

Not tried: a real Cloudflare tunnel to the page, and Local network from
another machine.
2026-10-27 18:58:16 +08:00
yetone 6ce0f14b98 gui: magpie web's page port judges every gateway route as from another machine (reported privately by Coffemon)
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).
2026-10-27 15:15:29 +08:00
yetone 9022169f6a web: magpie web added to a phone's home screen opens as an app (ninja_s6114 on Discord)
ninja_s6114 runs magpie in Docker and put the web UI on an iPhone's home
screen; it opened with Safari's address and bookmarks bars. Now it opens
standalone, with no setup:

- index.html links manifest.json (display standalone, name magpie, start_url
  and scope "./", 180 and 512 px icons) and an apple-touch-icon, and sets
  apple-mobile-web-app-capable, mobile-web-app-capable, the title and status
  bar style "default". theme-color is set only under (display-mode:
  standalone), light #f4f4f6 and dark #1a1a1e, so a browser tab and the
  Wails windows look as before.
- webGuard serves exactly /manifest.json, /icons/apple-touch-icon.png and
  /icons/icon-512.png on GET/HEAD without the key, since a home screen may
  fetch them without the page's cookie. Everything else still needs it.
- The header's safe-area padding already in app.css (body.web .top) keeps it
  out from under the status bar and notch; app.css is unchanged.

Run, at this head (rebased on origin/main):
- go test ./internal/gui: ok, except TestRestartHandsTheGatewayOver, the
  handover EOF under load (#3451); it and TestWebAppAddsToHomeScreen pass
  3 of 3 alone.
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui, and
  GOOS=windows: ok; go build ok.
- GUI web-app-standalone + i18n-keys: chromium 8/8, webkit 6/6 (the
  standalone cases are Chromium only, as WebKit can't emulate
  display-mode standalone; they run under --app with iPhone 13 safe-area
  insets 47/34 and touch, portrait and landscape, light and dark).
- Before the rebase, 34 GUI files picked for strict fixtures/assert.fail on
  unexpected requests, plus menu-scroll, phone-web, mobile-web, web-page,
  old-webkit, usage-ledger and i18n-keys, in both engines: 563 of 576 pass.
  The reds fail the same on an origin/main copy and are filed:
  old-webkit Safari 15 (#3403), phone-web flick under load (#3180),
  gateway-key-models webkit under load (#2648), usage-ledger
  return-to-live (#3104). provider-detect-draft timed out under load and
  passes 20/20 alone.

Broken by hand:
- the guard exemption removed: the Go test fails with 401 on the manifest
  and the icon;
- the old index.html: the Go test fails on the missing tags, and the GUI
  test fails in WebKit too;
- theme-color media without display-mode: the GUI test fails in both
  engines;
- app.css's body.web .top safe-area padding removed: the standalone test
  fails, ".top .brand under the status bar: 8".

Not tried: a real iPhone or Android device and a real install; the whole
GUI suite (the machine is loaded and nearly out of disk; run afterwards).
iOS keeps the display mode it saw when the icon was added, so a user
removes the old icon and adds it again. A home-screen app on iOS may not
share Safari's cookie, so it can ask for the key once; MAGPIE_WEB_KEY keeps
the key the same across restarts. iOS 27's top blur over the header is
#3515, not changed here.
2026-10-27 14:27:12 +08:00
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
yetone 64b0f3a201 plugins: a community plugin's own page in magpie's window, shut in a sandboxed frame (P4)
A plugin's package.json may declare magpie.page {title, file, icon} and
magpie.permissions.reads. While the plugin is on, its page gets a tab in a
"Plugins" group after the built-in pages, drawn in an iframe sandboxed to
allow-scripts only (an opaque origin: no cookie, no storage, no parent).
Its files come from /_magpie/plugin/<token>/ (HMAC of the spec with a
per-process secret) under a CSP with connect-src 'none' and
frame-ancestors 'self'; a path that leaves the page's folder, a symlink
out, or an off plugin's token is refused. The window's postMessage bridge
answers only that frame (event.source checked) with theme and language,
the plugin's own options.get/set (throttled), and the reads it declared;
any other read is refused in the window and again by the server, and no
read carries a secret. /api refuses Origin: null and frame/iframe
fetch-dest requests (noFramedAPI), so the frame never reaches the GUI's
API even with the cookie. A page that throws or never says hello shows
the kernel's broken-page state with Try again. The Plugins page shows the
page and what it reads before the plugin is turned on.

Docs: gui-shell.md, plugin-runtime.md (P4 section), site docs en/zh/ja
with an example plugin; i18n zh, zh-TW, ja, de; fixture in
internal/gui/testdata/page-plugin.

Run (worktree at base 4df1ba0c6, -p 2, one package at a time):
- go test -count=1 -timeout 45m: internal/gui ok, internal/plugin ok,
  internal/pluginrt ok, plugins/... (budget groups middleware otel redact
  rotation search) ok.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean.
- Playwright (chromium + webkit, 5 languages, narrow widths): plugin-page 4/4,
  plugin-kernel 4/4, builtins 4/4, i18n-keys, gui-de, gui-ja, gui-zh-tw 3/3,
  plugin-middleware 24/24, plugin-own-icon 22/22, plugin-options 22/22,
  plugin-local 8/8, plugin-agent 22/22, click-scroll 22/22.

Broken by hand, each went red: CSP without connect-src; apiFramed always
false; the server's read check removed; the noFramedAPI wrap removed from
api.go (red through the real Handler); inside() letting every path
through; CORP same-origin re-added; in JS the client read check removed,
the ev.source check removed, and allow-same-origin added to the sandbox.
Without the Array.isArray guard on /api/plugin-pages, plugin-kernel,
builtins and plugin-middleware/own-icon/options went red (their mocks
answer a non-list) - that was found by the run, then fixed.

Real use: built magpie, `magpie web` on 127.0.0.1:3472 (gateway 3471) in a
sandbox HOME with the example plugin added. curl: the page file loads
with the CSP and no cookie, traversal and a bad token are 404, /api with
Origin: null or Sec-Fetch-Dest: iframe is 403 even with the cookie, an
undeclared read 403, a declared one 200. Chromium and WebKit at 1100 and
420: the page draws its declared read, the probe's fetch/absolute
fetch/top access/undeclared read are all refused, an <img> of /api is
refused, no page errors. Real use found that a
Cross-Origin-Resource-Policy: same-origin header blocked the frame's own
files (its origin is opaque); it is gone and a test guards it.

Not tried: the desktop (Wails) window. Market cards of plugins not yet
installed don't show a page's reads. The Playwright suite mirrors the
CSP in a mock route rather than the Go server (the Go tests and real use
cover the server).
2026-10-15 22:36:59 +08:00
yetone fbb7e2cbd4 gui, update: Settings' Restart automatically restarts into a downloaded update once no agent request is in flight (#2096, xiaozhu1337)
#2096 asks for a setting that installs and restarts whenever an update is
there. Settings > About gets "Restart automatically" (Off/On), off by
default: a restart ends the window, and whatever an agent sends during it
waits for the gateway to come back.

When on, and Automatic updates is on, the updater's minute loop sees a
"ready" update and starts the same idle wait the Install-when-idle button
uses (waitIdle: 10s with nothing in flight, at most 1h). Cancelling that
wait from the GUI skips that version, so the next one is restarted into
again. The app (window or tray) and `magpie web` are covered; serve and
daemon have no in-process updater and are not. On the Mac, a restart the
app made by itself reopens to the menu bar icon only (`open … --args tray`)
when no window was shown, rather than popping a window up unasked
(update.Relaunch takes a window flag; relaunchOpen builds the command).
With Automatic updates off the row is dimmed and reads "While automatic
updates are on", like Check every.

Run:
- go test: internal/gui (-tags nogui, full package, 273s), internal/update,
  internal/settings, internal/cli — ok, with set -o pipefail.
- New: TestAutoRestartWaitsForIdle, TestAutoRestartNeedsAReadyUpdate,
  TestAutoRestartCalledOff, TestRelaunchOpen.
- Broken by hand: without autoTick, WaitsForIdle and CalledOff fail;
  without the autoSkip assignment, CalledOff fails; restarting without
  the idle wait fails WaitsForIdle and CalledOff; without prefsKeep's
  autoRestart, update-restart.test.cjs fails 11 of 11.
- gofmt -l clean; go vet and go build for darwin, GOOS=linux -tags nogui
  and GOOS=windows on internal/gui, update, settings, cli.
- GUI: new update-restart.test.cjs, chromium and webkit, 1100 and 420,
  en/zh/zh-TW/ja/de (22 pass, twice); update-*, i18n-keys and settings*
  in both engines: 253 pass, 0 fail.
- Found on the way, filed as #2134: in WebKit at 420, turning Automatic
  updates off scrolls a view at its top by ~2px (the Version row's sub
  grows). The test checks the clicked control holds still instead.

Not tried: a real update or relaunch (no real install was touched), the
tray-only reopen on a real bundle, Windows/Linux restarts by hand.
2026-10-14 15:19:14 +08:00
yetone d9ca84a2f4 gateway: built-in TLS, magpie serve/web/daemon install --tls-cert --tls-key, reloaded on renewal (RoderickZH)
magpie serve, magpie web and magpie daemon install take --tls-cert <file>
--tls-key <file> (or MAGPIE_TLS_CERT / MAGPIE_TLS_KEY). The gateway and the
web UI on its listener then serve HTTPS to other computers. Without them
nothing changes.

- Half a pair, a file that doesn't parse, or a flag with no value is
  refused at start with a message naming the missing flag.
- The pair is re-read when either file's mtime or size changes (certbot
  renewal), with no restart. A renewal that doesn't parse keeps the
  certificate already served and is logged once.
- Other computers get HTTPS only. Plain HTTP from them gets net/http's
  400 "Client sent an HTTP request to an HTTPS server".
- This computer (loopback, or a peer address equal to the local one) may
  still use plain http on the same port. Its connections are sniffed on
  their own goroutine (a first byte of 0x16 is TLS) for up to 10s, so a
  silent client holds nobody else up. gateway.URL() stays
  http://127.0.0.1:<port>, which agents, MCP servers, ServedBy, the
  healthcheck and daemon status all use, and no certificate covers
  loopback.
- WSL NAT and containers count as other computers.
- ALPN is http/1.1 only (hijacked connections and websockets need it), with
  TLS 1.2 as the minimum.
- Printed links, LAN addresses, publicURL, the serve/web banners and
  daemon install show https:// for other computers' addresses.
- The DNS-rebinding Host checks (foreignPage, rebound, the MCP
  loopbackHost) don't depend on the scheme and are unchanged.
- daemon install carries MAGPIE_TLS_* into the unit or plist. A reinstall
  or `daemon web on` that names no certificate keeps the installed pair,
  and --no-tls removes it.
- Docs: reference.md gets an HTTPS section that names Caddy as the
  alternative, gateway-routing.md gets a TLS row, and docker.html (en,
  zh, ja) gets the env rows.

What was run:
- New tests: internal/gateway/tls_test.go (others HTTPS-only, this
  computer both, a silent local client holds no other, renewed
  certificate served, CheckTLS, addresses, Relisten keeps TLS),
  internal/cli/tls_cli_test.go and daemon TestInstallCarriesTLS (linux
  and darwin, paths with space, %, & and ").
- Each was broken by hand and went red: no wrap in listen, no reload, a
  synchronous sniff, a bad renewal dropping the certificate, http in
  publicURL or the pinned LANURLs, the carried entries removed, and
  keepTLS as a no-op.
- Real use under a sandbox HOME, with a self-signed pair, on
  0.0.0.0:3461:
  - Half a pair, a garbage certificate, a flag with no value, and the
    env set to half a pair each exit 1 with the message.
  - curl -k https://127.0.0.1 and https://<LAN IP>/v1/models with a key:
    200. Plain http on loopback: 200.
  - The cert files were swapped for a second pair while it ran.
    openssl s_client then showed the new CN and serial, and curl
    --cacert with the new pair got 200. A junk key file then kept the
    second pair, logged once.
  - From the Windows box over the LAN: plain http got 400; https with
    the key got 200, and without it 401.
  - magpie web --addr 0.0.0.0:3462: https network links. From Windows,
    http got 400 and https with the key got 303.
- gofmt -l clean. go vet passed on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -tags nogui for ./internal/cli, ./internal/daemon, and the TLS
  tests of ./internal/gateway and ./internal/gui: all pass at the rebased
  head.
- Two reds in the full suite (`go test -tags nogui ./...`) are the same
  on origin/main f074996d9; both are filed:
  - TestGatewayHeldByAContainer failed 2 of 5 there (#2107).
  - The gateway package runs past go test's default 10m under load:
    1208s on main and 1221s here, both passing with -timeout 40m
    (#2108).
2026-10-14 13:36:03 +08:00
yetone b319657301 gui: a sign-in whose browser didn't open says why and shows its link, and Windows names rundll32 by its path (#2043)
xiaobaifly7: on Windows, signing in to Cloudflare's MCP server (and any
sign-in) opened no browser, and the window said to finish in the
browser until the sign-in timed out. Wails' Browser.OpenURL starts
rundll32 by its bare name, which Go looks up with PATHEXT's extensions;
a PATHEXT without .EXE (WSL passes one on as ".CPL") finds nothing, and
host.OpenURL dropped the error.

- proc.OpenBrowser starts %SystemRoot%\System32\rundll32.exe by its
  path (open / xdg-open / termux-open-url elsewhere) and returns the
  launcher's error. host.OpenURL falls back to it when Wails' fails, and
  the CLI's account sign-in uses it.
- Windows.OpenURL returns the error. /api/signin, the plugin sign-in and
  /api/library/mcp-signin answer it as openError; /api/open fails with
  it, so Open again says why.
- The sign-in card shows "magpie couldn't open your browser: {error}.
  Open the sign-in link below in a browser yourself." where it said
  magpie opened the page, with the link already shown below; the MCP
  editor shows the error with the link and a copy button. Translated
  (zh, zh-TW, ja, de).
- Open again matched its sign-in by identity, so a poll that put a fresh
  copy of the same sign-in in place kept the old error after a
  successful retry; it now matches by id.

Run:
- TestBrowserNamesRundll32ByItsPath, TestMCPSignInSaysTheBrowserDidntOpen
  and the /api/open and /api/signin tests in signin_open_2043_test.go.
  Broken by hand: the bare name back in browserCommand fails the first;
  openError returning "" fails the gui tests.
- On the Windows box (amd64, cmd with PATHEXT=.CPL): exec.Command
  ("rundll32", ...).Start() gave `exec: "rundll32": executable file not
  found in %PATH%`; proc.OpenBrowser started it (nil). The proc test
  binary's TestBrowserNamesRundll32ByItsPath passed there.
- signin-browser-down.test.cjs (new): the card's text, the link, no
  sideways overflow, Open again failing then succeeding, and the MCP
  editor's error and link, in Chromium and WebKit at 1100 and 420 in
  every language: 20/20. The related sign-in GUI tests: 451/452, the
  one a signin-proxy flake also on main (fixed in its own commit).
- gofmt -l clean; go vet darwin, linux -tags nogui, windows; GOOS=linux
  and windows builds; go test -tags nogui ./internal/gui/
  ./internal/proc/ ./internal/cli/.

Not tried: the real Wails app on Windows under PATHEXT=.CPL (the
fallback path is exercised only through proc.OpenBrowser), WSL itself,
the reporter's machine. Not covered: internal/tui/sessions.go and
internal/provider/install.go start `powershell` by its bare name, which
would miss the same way under such a PATHEXT.
2026-10-14 08:36:07 +08:00
yetone 82f3befd29 daemon: magpie daemon install --web serves the browser page from the service, with a link that lasts (HuanCheng Bai)
HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.

- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
  page from one process (stats, StartWeb, Wait). The default page address
  is 127.0.0.1:3430. Any other argument is refused, where before it was
  ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
  `magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
  ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
  and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
  key is made once. A reinstall with no flags keeps the page and its key
  (read back from the file by daemon.Web), so the link survives restarts
  and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
  set, is used and checked like magpie web's. The page defaults to
  loopback; --lan or a non-loopback --addr prints the network links and
  magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
  exits. Beside a gateway another magpie serves, it says so and points to
  `magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
  site's start page (en, zh, ja). The READMEs don't cover the daemon.

Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
  reinstall without the page, the shell's key not carried, no key
  refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
  TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
  with its line reverted by hand: withKey, prevKey in webOptions, and the
  status page line.
- Sandbox HOME with a fake launchctl:
  - `daemon install --web --lan` wrote a 0600 plist with
    `serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
    the link, the network links and the warning.
  - Reinstalling with no flags kept the key. `daemon status` showed the
    link.
  - `daemon web off` / `daemon web` / `daemon web on` behaved as above,
    with a new key after off.
  - `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
  one process. The page answered 200 with the key and 401 without, and the
  page and /v1/models both showed 3 models. `magpie web --no-open` beside
  it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
  windows; go test -tags nogui ./... in a sandbox HOME.

Not tried: a real systemd on Linux. The unit text is checked by test only.
2026-10-14 08:32:41 +08:00
yetone b6cf5e3eab gui: magpie web keeps its sign-in when another service on the same address fills or clears the host's cookies, and a page opened without it says why and takes the key (#2033, 321sssrt-bit)
A browser keeps cookies per host, not per port. magpie web's sign-in
cookie on http://<tailscale-ip>:3430 is the same host's as every other
service on that IP's other ports, so one of them could take it away,
and a bookmark then answered only "open the link magpie web printed
when it started".

Reproduced in Docker (linux/arm64, MAGPIE_WEB_KEY fixed, as the
reporter runs it) with Chromium and WebKit on the old binary:
- docker restart, recreate (an update), browser restart: still signed
  in (the key, its derivation and the 400-day cookie are fine);
- another port on the same host setting ~400 cookies: Chromium evicts
  the sign-in (401), WebKit keeps it;
- another port answering Clear-Site-Data: "cookies" (a sign-out often
  does): 401 in both;
- the page opened by another name for the host (localhost vs IP): 401.

Changed:
- the sign-in cookie is sent with Priority=High, so Chrome evicts it
  last when the host's cookies overflow (with it, the flood case stays
  signed in);
- every magpie_web_<port> cookie the browser sends is tried, so a stale
  one sent first no longer hides the right one;
- an HTML page opened without the sign-in gets a page that says which
  case it is (no cookie sent for this address, or a cookie holding
  another key) and a field to paste MAGPIE_WEB_KEY into; POST
  /_magpie/sign-in sets the cookie and goes back to the page asked for
  (local paths only: no scheme, host, //, /\ or the sign-in itself).
  The key is never in the page. API calls get a plain 401 that now
  says the reason too, and the turn-away is logged with it.
- docs/reference.md's Docker web section says how long a sign-in lasts
  and what can take it away.

Run:
- TestWebGuardBookmark (link cookie has Priority=High; stale cookie
  first still passes; both reasons; API 401; wrong key 401 with no
  cookie; right key 303 to /?view=usage with a 400-day cookie; external,
  //, /\, javascript:, empty, sign-in-path and CRLF back values all go
  to /). Broken by hand: the old guard fails it (link without
  Priority), dropping Priority alone fails it, reading only the first
  cookie fails it (stale cookie: 401).
- The Docker matrix above again on the new binary: the flood keeps the
  sign-in in Chromium; Clear-Site-Data and the other name get the form,
  and the form signs in (wrong key turned away, right key back to
  /?view=usage, 200) in Chromium and WebKit; the log says "turned away
  ... no cookie sent".
- go test -tags nogui ./internal/gui (ok, 173s), gofmt -l, go vet
  darwin, GOOS=linux -tags nogui, GOOS=windows.

Not tried: the reporter's own browser and Tailscale address; which
service on their host takes the cookie is not known.
2026-10-14 05:29:30 +08:00
yetone 7a9490638d gateway, gui, settings: an IP / CIDR allow list for the gateway, from MAGPIE_ALLOW_IPS or Settings beside Share on local network (lc on Discord)
What changed: lc runs magpie in Docker on a public host and asked that
only some addresses reach it at all. With a list set, a request from an
address outside it is refused 403 ("magpie answers only the addresses on
its allow list ..., and this request came from <addr>: add it there to
let it in") before its gateway key is looked at, so a leaked key is no use from
elsewhere. No list is as before.
- internal/settings/allowips.go: SplitAllowIPs, ParseAllowIP (an IP or a
  CIDR range, v4 or v6; zones dropped, v4-mapped unmapped), CleanAllowIPs.
  Settings gets AllowIPs (allowIPs).
- internal/gateway/allow.go: MAGPIE_ALLOW_IPS (comma or space separated)
  sets the list in place of Settings'. The address judged is the
  connection's own (r.RemoteAddr); X-Forwarded-For and the rest are never
  believed (magpie has no trusted-proxy list; MAGPIE_TRUST_PROXY never
  reads forwarding headers, and this follows it). Loopback and this
  computer's own interface addresses always pass. An entry that doesn't
  read lets nothing in and is logged once, so a typo never opens magpie
  to every address. lanGuard checks it after the OAuth callback and
  before the rebound-Host and key checks; `magpie web` is wrapped with
  AllowGuard too.
- GUI: Settings > Network shows an "Allowed addresses" row under Share on local
  network when sharing is on, the page is open from another computer, or
  the env var is set (then read-only, saying so). POST
  /api/settings/allowips saves it, refuses it while the env var is set,
  refuses an entry that doesn't read, and refuses a list that would shut
  out the address the page itself is open from (KeepsOut). The generic
  settings save keeps AllowIPs. Strings in zh, zh-TW, ja and de.
- Docs: the Docker page (en, zh, ja) lists MAGPIE_ALLOW_IPS with a note
  that a request from the host itself arrives from the bridge's gateway
  (172.17.0.1), that Docker Desktop and rootless Docker show clients as
  one address of their own, and that behind a proxy every request comes
  from the proxy; docs/reference.md and docs/subsystems/gateway-routing.md
  describe it. The READMEs list no env vars, so they are unchanged.

Why: a gateway key is the only lock on a public host; an allow list keeps
everyone else from even trying it.

Run (machine load average 75-140 from other sessions, so the large
packages ran in slices of their top-level tests, each slice
-timeout 9m, private GOCACHE, sandbox HOME):
- New tests: internal/gateway/allow_test.go TestAllowList (allowed,
  denied, CIDR v4/v6, IPv6 literal, link-local with zone, v4-mapped,
  loopback and own address always in, on the list still needs a key,
  a typo shuts everything out, env over settings), TestAllowListServed
  (a real connection from loopback passes lanGuard under a list naming
  nothing of this computer's; a request from elsewhere is 403 naming its
  address and MAGPIE_ALLOW_IPS), TestKeepsOut; internal/settings/allowips_test.go
  TestCleanAllowIPs; internal/gui/allowips_settings_test.go
  TestAllowIPsSetting; internal/gui/tests/allow-ips.test.cjs (14/14 in
  chromium and webkit, at a 390px panel, en and zh, keys in zh, zh-TW,
  ja and de). Broken by
  hand: with lanGuard's check removed TestAllowList and TestAllowListServed
  FAIL; with KeepsOut skipped TestAllowIPsSetting FAILs; with renderAllow
  not called the GUI test has 5 failures. Restored, all pass.
- internal/gui/settings_keep_test.go gains AllowIPs:
  TestSettingsSaveKeepsWhatItDoesNotSend failed without it, as it should
  for a new field.
- go test -tags nogui: every package ok. internal/gateway (4 slices),
  internal/agent (4), internal/provider (3), internal/gui (2),
  internal/plugin (2), the rest whole. GUI Network-settings test files
  (11) 98/98 and i18n-keys ok, both engines.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok; linux and windows builds ok.
- Not tried: a real Docker container on a public host (the bridge note
  is from Docker's docs, not checked here); behind a real reverse proxy.
2026-10-12 23:21:41 +08:00
yetone 6f0301f3c3 gui: magpie web gzips its page and JSON, and keeps each versioned script for good (akic404 on Discord)
akic404 runs `magpie web` on a public server over a ~3 Mbps link, and
the page took very long to load. Measured in a sandbox (`magpie web` on
127.0.0.1:3461/3463, sandbox HOME): every load sent the page's ~4.1 MB
of scripts and styles as they are (i18n.js 1.8 MB, app.js 1.1 MB,
app.css 282 KB, routing.js 279 KB, library.js 211 KB) with no
compression, whatever the browser's Accept-Encoding, and the API's JSON
the same way: a day of the Routing page's history
(/api/gateway/history, a real 267-request day copied in) was 1.23 MB,
and the routing trace's JSON grows with every account a request could
go to. Nothing is embedded, vendored or source-mapped in routing.js;
the bytes were the uncompressed transfer.

- webHandler wraps `magpie web`'s guarded handler in gzhttp
  (klauspost/compress, already a dependency): text, scripts, CSS, JSON
  and SVG are gzipped for a client that takes gzip, with Vary:
  Accept-Encoding; PNG/WebP/ICO/JPEG go as they are. A gzipped
  response's ETag ends -gzip, and If-None-Match with it is mapped back
  so a revalidation is still a 304. A 32-byte content-keyed jitter keeps
  the compressed size from being exact (BREACH), since the page carries
  keys. The desktop windows read the page in-process through Wails'
  asset server, with no network, so they stay uncompressed.
- revalidated: a script or stylesheet asked for under its current
  content hash (?v=…, as versionedPage names it) is sent with
  Cache-Control: max-age=31536000, immutable; any other hash, no hash,
  and the page itself stay no-cache + ETag. Applies to both paths.

Numbers (curl, page + its 21 scripts/styles + /api/state):
  before: 4,142,180 bytes, with or without Accept-Encoding: gzip
  after:  1,258,298 bytes with gzip (i18n.js 565 KB, app.js 361 KB,
          routing.js 89 KB), 4,142,180 without
  /api/gateway/history?day=: 1,233,600 -> 72,538 bytes
Real browser (Playwright, ?view=routing, 6s, response body bytes):
  Chromium first load 4,340,720 -> 1,362,922; reload 75,336 -> 10,597
  WebKit   first load 4,340,672 -> 1,362,843; reload 115,667 -> 34,006
  both engines: Routing view shown, no page errors or console errors.

Tests: TestWebCompresses and TestVersionedFilesKept (web_compress_test.go).
Broken by hand, each goes red: webHandler without webCompressed
(Content-Encoding "" for app.js, routing.js, /, /api/state, SVG); no
immutable branch ("/app.js?v=…: Cache-Control no-cache"); no
If-None-Match mapping ("asked again with its gzipped ETag: 200, want
304"); compressing every type ("a PNG: Content-Encoding gzip"); no
ETag suffix ("gzipped keeps the ETag of other bytes").

Run: go test -tags nogui ./internal/gui (ok); full go test -tags nogui
-timeout 40m ./... under a sandbox HOME (all ok); gofmt -l clean; go vet
./internal/gui on darwin, GOOS=linux -tags nogui, GOOS=windows with and
without nogui. GUI: routing-*.test.cjs, panel-routing, web-page,
mobile-web, phone-web, gateway-mode in Chromium and WebKit; results
after rebase are in the report (mobile-web was red on the old base
before ca0b95e3b, which these assets don't touch).
2026-10-11 02:48:13 +08:00
yetone f22218a092 gui: after an update the window shows what changed since the version last run (a Discord user: 点更新后能弹个窗显示更新了啥吗, to see whether their issue was fixed) — magpie updated itself silently, and its release notes were only on GitHub. The version last run is now kept in magpie's own folder (last-version); started on a newer release, the window or magpie web's page asks usemagpie.ai's new /api/notes (beside /api/latest; site/worker.js lists the releases' notes from GitHub, cached as the feed is) for every release since, newest first, without their Install section, and shows them once in a What's new dialog. A fresh install, the same version, a downgrade or a build from source shows nothing; a magpie from before the version was kept, already in use, shows its own version's notes; offline nothing is shown and nothing waits on it; until the site is deployed, the update feed's newest notes stand in when they are the current version's. The tray's panel shows no dialog. The notes are drawn as text, never as HTML: headings, bullets, bold, code, http(s) links, and #NNN links to the issue, opened in the browser as the app's other links are. Settings has a What's new row under Version that opens them again, with a waiting update's notes first, marked Not installed yet; /api/update's notes leave out the Install section too. TestShowNotes, TestStripInstall, TestBetween, TestNotesBetween, TestWhatsNewAfterUpgrade, TestWhatsNewFromBefore, TestUpdateNotesWithoutInstall and whats-new.test.cjs (Chromium, WebKit, en, zh) fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, update-check, update-hide pass. 2026-10-02 03:57:55 +08:00
yetone 171d8dc646 web: behind a reverse proxy, magpie web prints MAGPIE_PUBLIC_URL as the page's link (#372) — with MAGPIE_PUBLIC_URL=https://magpie.example.com the link for other machines was http://magpie.example.com:3430/?k=…, its host on the page's port over plain http, which nothing answers. A public URL with no port of its own is now the link, scheme and path kept; one with a port (a container's published ports, http://<host>:3425) still gives that host on the page's port. TestNetworkLinksPublicURL fails without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass. 2026-10-01 21:49:15 +08:00
yetone b4c6772677 lan: magpie in Docker shares an address other computers can reach (莫 on Discord) — inside a container the LAN address shown was the container's own (172.17…). MAGPIE_PUBLIC_URL now names the share address; without it, a page opened through the host's IP shows that host, and the Gateway page says it runs in a container and how to set the address. lan-docker.test.cjs (Chromium, WebKit, en/zh) and the serve/lan tests fail without the change. 2026-10-01 11:29:05 +08:00
yetone 6927bedfcb windows: the title bar is drawn in the page's own colour, light or dark as magpie's theme is, and a narrow agent row gives up the update pill's words and the version before the agent's name (X, ChengYue: "Mac上是真好看,Windows上的UI简直不要太丑~")
On Windows the system's grey title bar sat over the page as a strip of its own, and stayed light over a page set to dark; at the window's first width Claude Code's name was cut to "C." beside a full "Update to …" pill. Now the main window opens with its title bar and background in the page's --bg for the saved theme (Wails' Windows CustomTheme, #f4f4f6 / #1a1a1e), and the page reports the colour it shows (POST /api/window/titlebar) at start and after every change of theme, system or chosen, for Go to paint the caption with DWM and switch its buttons dark or light; elsewhere nothing changes. The agent row is a size container: under 250px the pill shows only its arrow (its words kept as its aria-label), under 150px the version goes, and the name gives way last.

Tested on Windows 11 (sandboxed HOME): with theme light and dark, the bar and the page are one surface, caption buttons in the matching colour, the name whole. cli-update.test.cjs gains a zh subtest at 980/620/540px (fails on the old CSS). The GUI suite passes in Chromium (settings-groups is flaky on HEAD too), go vet, go build (GOOS=windows, GOOS=linux -tags nogui, dev) and go test -tags nogui ./... pass.
2026-09-30 11:22:21 +08:00
yetone 575e694ed7 settings: a Text size preference (文字大小) that makes everything in magpie's windows larger, 100/110/125/150%, with Ctrl/Cmd + = − 0 in the window and the tray panel, and Windows' text drawn in its own UI faces (Discord, Clarence Dan: the font size is hard on the eyes on Windows) — Settings → Preferences gains a Text size row under Language whose pick posts to a new POST /api/settings/text-size (saved as settings.TextSize, validated against 100/110/125/150, 0 read as 100, and kept by the Settings page's general save, which never sends it). The size is each webview's own zoom, as a browser zooms, not CSS zoom: CSS zoom leaves getBoundingClientRect and clientX in zoomed pixels while offsets, scrollTop and 100vh are not, and app.js, routing.js and library.js place menus, popovers and the panel's height by those measures; under the webview's zoom the page is laid out again in fewer, larger CSS pixels and every measure agrees. Windows and Linux use Wails' SetZoom (WebView2's zoom factor, WebKitGTK's zoom level; the Zoom window option at creation, so the pages open at the saved size), the Mac WKWebView.pageZoom through cgo (macOS 11+), since Wails' Mac zoom is magnification, which lays nothing out again. None is under 100% because Wails clamps SetZoom below 1 on Windows and Linux. The windows grow around the pages: the panel's width and the height its page asks for (still in CSS pixels) are multiplied by the zoom, capped at the screen's work area (textsize.go panelFrame), and resized at once when the size changes; the main window's minimum is 560×420 CSS pixels at the size, capped by the screen, so a window smaller than that grows. Runtime changes wait for the app to be ready (whenReady), and at startup the Mac applies the zoom once its webviews exist. The Mac header, drawn under the native traffic lights which do not grow, divides its height, left padding and the modal's top by --zoom (set from boot.js before the first paint and from the settings), so it stays 50 points tall and level with them. The shortcuts are Cmd on the Mac and Ctrl elsewhere (the other modifier does nothing), step through the sizes and 0 goes back to 100%, with a "Text size 125%" toast; in magpie web the row is hidden and the keys are left to the browser's own zoom. On Windows the page names its faces (html.win): Segoe UI Variable Text, Segoe UI, then Microsoft YaHei UI for Chinese rather than whatever WebView2 falls back to; the Mac and Linux stack is unchanged, and there is no automatic size bump. New strings in en and zh. Tested: internal/gui/tests/text-size.test.cjs in Chromium and WebKit, en and zh, light and dark — a pick posts and does not scroll the settings page, stays picked after a reload with --zoom from boot.js, the Settings page's other saves leave it, the shortcuts step and reset in the window and the panel, and at 150% (the smallest window, 840×630 points, as 560×420 CSS pixels at device scale 1.5) no view, the header or the panel runs off to the side, the Mac header is 50 points; screenshots at 100% and 150% looked at. currency.test.cjs's viewport is 480 tall instead of 420, as settings-groups' is: the new row put the Currency control under the footer, where WebKit's hover could not reach it. Go: TestTextSizeFrames (panel and window sizes) and TestTextSizeSetting (the endpoint saves, zooms the windows, rejects 133, survives the general save, boot.js carries it) in internal/gui, and the settings round trip. go build -tags nogui (and GOOS=linux, GOOS=windows, plain darwin), go test -tags nogui ./... (TestUserPath flaked once under load and passes alone) and the whole GUI suite with --test-concurrency=2 pass. Not tried in the real Wails window on any platform: the Mac pageZoom, WebView2's zoom factor with the resized panel and the Windows font are untested there. 2026-09-30 02:51:17 +08:00
yetone e02d3f28d7 library: a project's agents can have some of the library's skills as the project's own — under Skills, In projects takes a folder (the system's picker in the app, a typed path anywhere) and each of its skills gets agents' chips; magpie links the skill into <project>/.claude/skills for Claude Code and <project>/.agents/skills for Codex, Gemini CLI, OpenCode, Pi, Cursor and Copilot (the agents documented to read it; MiMo Code, Crush, ZCode, DeepSeek Harness and oh-my-pi aren't, so they're left out), or copies it when the project asks for copies, a copy made again whenever the library's skill changes (updates included). What it places is listed between magpie's marks in the project's .gitignore, made if there was none; taking a skill or the project away removes only what magpie placed, its lines and the folders it made once empty, and a folder of the same name that isn't magpie's is left as it is and said so. The home folder, or one whose project skills folder is an agent's own, can't be added 2026-09-28 13:34:17 +08:00
yetone 4e83dc6be8 web: magpie web knows its version and updates itself as the app does — the badge's restart puts the new version in and runs it in the old one's place with the same arguments and key, so the open page reloads onto it (#111); routing: a gateway served by magpie serve, which has no window, is told apart from another magpie's window, and the routing view says to stop it so this magpie serves the gateway and shows routing live (#110) 2026-09-27 19:39:45 +08:00
yetone 8f915a5a23 web: MAGPIE_WEB_KEY takes letters, digits and - . _ ~ only — a cookie can't hold a space, ; or a quote, so the browser was sent a cookie that wasn't the key and turned away with no word why 2026-09-27 17:26:21 +08:00
Arvak 84ef25805e web: MAGPIE_WEB_KEY keeps the page's key across runs — for magpie web kept running as a service, where a new key each start signs every browser out; with it the cookie outlives the browser session too (400 days, Chrome's most). Unset, each run makes its own key in a session cookie, as before. A key under 16 characters is refused. (#109) 2026-09-27 17:26:32 +08:00
yetone 59e4f417b2 web: magpie web serves the app's window to a browser, with the gateway beside it — for a computer that can't show the app: no desktop, WSL, a server over SSH, or the CLI-only build, which now carries the page too (what of the app needs Wails is left out of nogui builds). Every request needs this run's key, which the printed link carries and trades for a cookie other sites' pages can't send with a POST; --lan serves it on the network too, with a warning, --addr elsewhere, --no-open leaves the browser shut. In the browser the page opens links and sign-ins itself, names a folder instead of opening it, and leaves out the tray, the Dock and open at login. Tested from a nogui build in a sandbox: no key, a wrong key and a cookieless POST are refused; with the link Agents, Providers and Settings load and a setting saves. Issue #102 2026-09-27 16:21:26 +08:00