Files
magpie/internal/gateway/cors_test.go
T
yetone 5ee5274bd9 quotas: a remote magpie's Codex reset is used from its card here (#3850)
prgrmrwy runs magpie on a home server and uses it from work through a
remote magpie (and DSH). The remote Codex card said "1 reset", but the
reset could only be used from the GUI of the computer that holds the
sign-in.

What changed:
- Gateway: POST /v1/magpie/quotas/codex-reset?user=<email>[&credit=<id>]
  (Server.codexReset) spends one of that account's resets with
  provider.SpendCodexReset. That is the Usage page's own code
  (UseCodexResetCard, then AwaitAllowance), moved out of
  internal/gui/usage.go so both use it. A reset can't be undone, so the
  route needs an enabled gateway key with full access, from loopback too.
  No key gets 401, and that covers every corsGuard exemption that has no
  key: Origin null, app schemes, *.localhost and a missing Origin. A
  read-only key gets 403 gateway_key_read_only (readOnlyGuard, which
  readOnlyRefused now lists, and again in the handler). A held key gets
  403 gateway_key_held, even one told quotas. The account must be named
  (400 otherwise). One ask runs per account at a time (409). When the
  reset is used, that card is read again.
- Remote client: UseRemoteCodexReset (remote_quotas.go) asks the remote
  with the provider's key. remoteJSON's status mapping is shared through
  remoteBody. A read-only key there is told as such, and a 404 as a
  remote too old. With no answer in time it says the reset may have been
  used, and it never asks again by itself. A remote Codex card now keeps
  its Resets; a GLM team's (byWindow) and every check-in are still
  dropped.
- GUI: "Use a reset" / "Use one..." show on a remote Codex card
  (codexResetCard). They open the same in-app confirmation sheet, with a
  line saying which computer's magpie uses it, and send the card's
  provider to /api/usage/codex-reset. The remote's errors are put in the
  user's words. New strings are in all 5 languages.
- DSH: no code here. Its provider plugin reads /v1/magpie/quotas/cards
  and can call the new route with a full key.
- resetOutcome (a reset's result in words) moved from app.js into
  resetask.js, the only place that says it, so the core's scripts stay
  in TestCoreScriptBudget's 1,533,802 with no raise (1,533,003 after;
  origin/main b967b1620 is 1,533,756).
- Docs: reference.md (remote quotas, gateway keys), gateway-routing.md
  (read-only list, the route's admission), providers-accounts.md.

What was run (rebased on origin/main b967b1620, then on 2e4735f8f):
- mote, at the head before the rebase and the resetOutcome move:
  go test -count=1 -timeout 45m ./internal/provider ./internal/gateway
  ./internal/gui at load ~300-450. provider ok. gateway: only
  TestCompactionOverByteCapEveryTime (#4862). gui: TestCoreScriptBudget
  (this change, 158 bytes over; fixed by the move), TestMainWindowCorners
  (#3949) and TestGatewaySkewOfAnotherIsSaidNotQuit/own_build (#5053,
  passes alone here and at b967b1620).
- mote, same run: 155 GUI files (reset|quota|usage-|panel-|i18n|budget|
  remote|gui-|old-webkit|core-script|script-size), 1856 of 1904 pass.
- WSL (magpie-desktop), the 9 red files alone, at this change and at
  origin/main b967b1620: this change 147 pass / 43 fail, main 146 / 44;
  every test red here is red on main too: codex-auto-reset webkit de
  (#4790), remote-magpie (#4398), usage-agents-hide (#3414), usage-fold
  (#3415, #4790), usage-heatmap (#4718), usage-ledger (#5048).
  codex-reset-lead (#5054) and panel-routing-providers (#5055) were red
  only under mote's load and pass alone on both.
- mote, at this head: TestCoreScriptBudget and
  TestGatewaySkewOfAnotherIsSaidNotQuit ok.
- WSL, at this head: account-every-reset, codex-resets-used, i18n-keys,
  providers-reset-use, remote-codex-reset, reset-each-title,
  reset-pick-card, reset-providers, quota-used-no-reset: 99 of 102 pass.
  The 3 reds are providers-reset-use's 420px Windows cases ("a click
  moved the page", #4873); that file alone is red on origin/main
  b967b1620 too (3 of 40 there, 2 of 40 here).
- WSL, rebased on 2e4735f8f (main had changed internal/gateway): the new
  tests and the read-only, CORS, LAN-guard, held-key, remote-magpie and
  UseCodexResetCard tests in ./internal/gateway ./internal/provider ok;
  core scripts 1,532,960 under 1,533,802; gofmt clean.
- Earlier on mote: TestCodexResetNeedsAFullKey,
  TestRemoteCodexResetThroughTheGateway, TestRemoteCodexResetAnswers, the
  read-only, CORS, LAN-guard, held-key and remote-magpie tests ok;
  remote-codex-reset and reset-pick-card 20 of 20 (chromium and webkit,
  5 languages, 900/390/440px). gofmt clean; go vet darwin, windows ok,
  linux for provider and gateway.

Broken by hand:
- With the handler's key check turned off, TestCodexResetNeedsAFullKey
  went red: "this computer, no key: 200 {code: reset}" and a reset was
  spent.
- With codexResetCard put back to provider === "codex",
  remote-codex-reset.test.cjs went red ("one Use, on the remote Codex
  card"). On the old code the route doesn't exist, so the full-key case
  gets 404.

Not tried:
- A real Codex reset spent between two real magpies: none was spent, to
  keep the owner's resets. The ChatGPT backend is the fake from
  codex_autoreset_test.
- DSH's plugin calling the route.
- GOOS=linux vet of internal/gui: it needs gtk4/webkitgtk, which neither
  mote nor magpie-desktop has. Its change is usage.go only, and that was
  vetted on darwin and windows.

Thanks to prgrmrwy for the report and the proposal.
2026-11-03 17:12:25 +08:00

770 lines
35 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
)
// A web page the user listed in Settings (#1051, SkyAerope: a preflight
// from http://localhost:3000 for /v1/models was answered 404 with no CORS
// headers) has its preflight answered and reads the reply, its calls with
// a gateway key; a page not listed gets no CORS headers, as before, nor
// does any page while none is listed; agents, which send no Origin, are
// untouched.
func TestCORSOrigins(t *testing.T) {
fresh(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Web app"})
if err != nil {
t.Fatal(err)
}
h := New().Handler()
// from this computer, as a browser on it calls 127.0.0.1:3425
do := func(method, path, origin, key string, hdr map[string]string) *httptest.ResponseRecorder {
t.Helper()
var body io.Reader
if method == "POST" {
body = strings.NewReader(chatReq)
}
r := httptest.NewRequest(method, path, body)
r.RemoteAddr = "127.0.0.1:50123"
if origin != "" {
r.Header.Set("Origin", origin)
}
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
for k, v := range hdr {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
preflight := map[string]string{"Access-Control-Request-Method": "GET", "Access-Control-Request-Headers": "authorization, content-type"}
// none listed: the reporter's preflight, as before
if w := do("OPTIONS", "/v1/models", "http://localhost:3000", "", preflight); w.Header().Get("Access-Control-Allow-Origin") != "" || w.Code == http.StatusNoContent {
t.Fatalf("none listed: %d %v", w.Code, w.Header())
}
s := settings.Load()
if s.CORSOrigins, err = settings.CleanOrigins([]string{"http://LOCALHOST:3000/", "https://app.example.com:443"}); err != nil {
t.Fatal(err)
}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
w := do("OPTIONS", "/v1/models", "http://localhost:3000", "", map[string]string{
"Access-Control-Request-Method": "POST", "Access-Control-Request-Headers": "authorization, content-type, x-app, bad name;",
"Access-Control-Request-Private-Network": "true"})
if w.Code != http.StatusNoContent || w.Header().Get("Access-Control-Allow-Origin") != "http://localhost:3000" ||
!strings.Contains(w.Header().Get("Access-Control-Allow-Methods"), "POST") ||
w.Header().Get("Access-Control-Allow-Headers") != "authorization, content-type, x-app" ||
w.Header().Get("Access-Control-Allow-Private-Network") != "true" || w.Header().Get("Vary") != "Origin" {
t.Fatalf("preflight: %d %v", w.Code, w.Header())
}
// the call itself, with a gateway key: answered and readable
if w := do("GET", "/v1/models", "http://localhost:3000", secret, nil); w.Code != 200 || w.Header().Get("Access-Control-Allow-Origin") != "http://localhost:3000" {
t.Fatalf("models: %d %v", w.Code, w.Header())
}
if w := do("POST", "/v1/chat/completions", "https://app.example.com", secret, nil); w.Code != 200 || w.Header().Get("Access-Control-Allow-Origin") != "https://app.example.com" || !strings.Contains(w.Body.String(), "ok") {
t.Fatalf("chat: %d %v %s", w.Code, w.Header(), w.Body)
}
// without one, or with a key that isn't a gateway key, it is refused —
// readably, so the page can say why
for _, key := range []string{"", "magpie", "sk-magpie-not-a-key"} {
w := do("POST", "/v1/chat/completions", "http://localhost:3000", key, nil)
if w.Code != http.StatusUnauthorized || w.Header().Get("Access-Control-Allow-Origin") != "http://localhost:3000" || !strings.Contains(w.Body.String(), "gateway key") {
t.Errorf("key %q: %d %v %s", key, w.Code, w.Header(), w.Body)
}
}
// a key turned off is no key
keys, _ := access.List()
access.Update("off-key", access.Change{Key: keys[len(keys)-1].ID})
if w := do("GET", "/v1/models", "http://localhost:3000", secret, nil); w.Code != http.StatusUnauthorized {
t.Errorf("a key turned off: %d", w.Code)
}
access.Update("on-key", access.Change{Key: keys[len(keys)-1].ID})
// another page: no CORS headers, whatever it sends; a port or scheme
// apart is another origin
for _, o := range []string{"http://localhost:3001", "https://localhost:3000", "http://evil.example", "null"} {
if w := do("OPTIONS", "/v1/models", o, "", preflight); w.Header().Get("Access-Control-Allow-Origin") != "" || w.Code == http.StatusNoContent {
t.Errorf("%s preflight: %d %v", o, w.Code, w.Header())
}
if w := do("GET", "/v1/models", o, secret, nil); w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("%s: %v", o, w.Header())
}
}
// an agent: no Origin, no key, as before
if w := do("POST", "/v1/chat/completions", "", "", nil); w.Code != 200 || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Fatalf("agent: %d %v", w.Code, w.Header())
}
}
// On the real server lanGuard sits in front of Handler and, for a gateway
// key sent from this computer, records who called and puts magpie's own
// token in the key's place; a listed page's keyed call is still answered.
func TestCORSKeyThroughTheServer(t *testing.T) {
fresh(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Web app"})
if err != nil {
t.Fatal(err)
}
s := settings.Load()
s.CORSOrigins = []string{"http://localhost:3000"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if err := sessions.SetGatewayRecording(true, false); err != nil {
t.Fatal(err)
}
srv := lanGuard(New().Handler())
for _, key := range []string{secret, ""} {
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
r.RemoteAddr = "127.0.0.1:50123"
r.Host = "127.0.0.1:3425"
r.Header.Set("Origin", "http://localhost:3000")
r.Header.Set("Content-Type", "application/json")
r.Header.Set(SessionHeader, "browser-chat")
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
want := http.StatusOK
if key == "" {
want = http.StatusUnauthorized
}
if w.Code != want || w.Header().Get("Access-Control-Allow-Origin") != "http://localhost:3000" {
t.Errorf("key %q: %d %v %s", key, w.Code, w.Header(), w.Body)
}
if key != "" && (w.Header().Get(SessionHeader) != "browser-chat" || w.Header().Get("Access-Control-Expose-Headers") != SessionHeader) {
t.Error("browser client cannot read the session its conversation is recorded under")
}
}
}
// Any web page the user visits could make the browser send a "simple"
// text/plain POST to 127.0.0.1:3425, which on loopback needs no key, and
// spend the user's subscriptions without reading the reply. A page whose
// http(s) origin isn't listed, on this computer or the gateway's own
// address is refused on every route before anything reaches a vendor;
// pages on this computer, listed ones, the gateway's own origin, agents
// (no Origin) and apps and extensions (non-http schemes) are answered as
// before. Origin "null" is a page's too (TestOpaqueOriginNeedsAKey): a
// key is asked of it, so it is gone from the rows below.
func TestForeignPagesRefused(t *testing.T) {
fresh(t)
var hits atomic.Int64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Web app"})
if err != nil {
t.Fatal(err)
}
s := settings.Load()
s.CORSOrigins = []string{"https://app.example.com"}
s.LAN = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
h := New().Handler()
type call struct{ method, path, body string }
msg := `{"model":"plan/m1","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`
do := func(c call, origin, host, remote, key string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(c.method, c.path, strings.NewReader(c.body))
r.RemoteAddr = remote
if host != "" {
r.Host = host
}
// what a page's fetch(..., {mode: "no-cors"}) or a form sends
r.Header.Set("Content-Type", "text/plain;charset=UTF-8")
if origin != "" {
r.Header.Set("Origin", origin)
}
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
w := httptest.NewRecorder()
lanGuard(h).ServeHTTP(w, r)
return w
}
chat := call{"POST", "/v1/chat/completions", chatReq}
routes := []call{
chat,
{"POST", "/chat/completions", chatReq},
{"POST", "/v1/responses", `{"model":"plan/m1","input":"hi"}`},
{"POST", "/responses", `{"model":"plan/m1","input":"hi"}`},
{"GET", "/v1/responses", ""},
{"POST", "/v1/messages", msg},
{"POST", "/messages", msg},
{"POST", "/v1/messages/count_tokens", msg},
{"POST", "/v1/systemone", msg},
{"POST", "/v1beta/models/plan/m1:generateContent", `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`},
{"POST", "/v1/embeddings", `{"model":"plan/m1","input":"hi"}`},
{"POST", "/v1/rerank", `{"model":"plan/m1","query":"hi","documents":["a"]}`},
{"POST", "/v1/audio/transcriptions", "--b\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\nplan/m1\r\n--b--\r\n"},
{"POST", "/v1/audio/speech", `{"model":"plan/m1","input":"hi","voice":"coral"}`},
{"POST", "/v1/images/generations", `{"model":"plan/m1","prompt":"hi"}`},
{"POST", "/v1/videos", `{"model":"plan/m1","prompt":"hi"}`},
{"POST", CodexPath + "/responses", `{"model":"plan/m1","input":"hi"}`},
{"POST", "/mcp/notes", `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`},
{"POST", "/_magpie/claude-mcp/tok", `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`},
{"POST", provider.RemoteRefreshPath, ""},
{"POST", provider.RemoteCodexResetPath + "?user=me@example.com", ""},
{"GET", "/v1/magpie/quotas", ""},
{"GET", "/v1/models", ""},
{"OPTIONS", "/v1/chat/completions", ""},
}
const loop = "127.0.0.1:50123"
for _, o := range []string{"https://evil.example", "http://evil.example:3425", "http://localhost.evil.example", "http://192.168.1.7:3000", "HTTPS://Evil.Example", "https://usemagpie.ai"} {
for _, c := range routes {
if w := do(c, o, "127.0.0.1:3425", loop, ""); w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "web page") || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("%s %s from %s: %d %s", c.method, c.path, o, w.Code, w.Body)
}
}
}
// a gateway key doesn't let an unlisted page in either
if w := do(chat, "https://evil.example", "127.0.0.1:3425", loop, secret); w.Code != http.StatusForbidden {
t.Errorf("unlisted page with a key: %d", w.Code)
}
// ... nor does one sandboxed frame, whose Origin "null" any page can
// send: it is refused with the others, and asks for a gateway key
if w := do(chat, "null", "127.0.0.1:3425", loop, ""); w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "gateway key") {
t.Errorf("a sandboxed frame with no key: %d %s", w.Code, w.Body)
}
if n := hits.Load(); n != 0 {
t.Fatalf("a refused page reached the vendor %d times", n)
}
// answered as before
for _, c := range []struct{ origin, host, remote, key string }{
{"", "127.0.0.1:3425", loop, ""}, // every agent
{"http://localhost:5173", "127.0.0.1:3425", loop, ""}, // a page on this computer
{"http://127.0.0.2:8080", "127.0.0.1:3425", loop, ""}, // 127.0.0.0/8
{"http://[::1]:3000", "[::1]:3425", "[::1]:50123", ""}, // IPv6 loopback
{"http://tauri.localhost", "127.0.0.1:3425", loop, secret}, // a localhost subdomain, with a key (keyless on Windows: TestLocalhostSubdomainNeedsAKey)
{"https://app.example.com", "127.0.0.1:3425", loop, secret}, // listed, with its key
{"http://192.168.1.5:3425", "192.168.1.5:3425", "192.168.1.9:50123", secret}, // the gateway's own origin, shared on the LAN
{"https://magpie.example.com", "magpie.example.com", loop, secret}, // its own origin behind a port-443 name, with a key (without one, a rebound page: TestReboundPageRefused)
{"null", "127.0.0.1:3425", loop, secret}, // a sandboxed frame or a file:// page, with its key
{"app://cherry", "127.0.0.1:3425", loop, ""}, // an Electron renderer
{"file://", "127.0.0.1:3425", loop, ""},
{"tauri://localhost", "127.0.0.1:3425", loop, ""},
{"vscode-webview://1abc", "127.0.0.1:3425", loop, ""}, // an editor's webview
{"chrome-extension://abcdefghijklmnop", "127.0.0.1:3425", loop, ""}, // an extension
} {
if w := do(chat, c.origin, c.host, c.remote, c.key); w.Code != 200 || !strings.Contains(w.Body.String(), "ok") {
t.Errorf("%q to %s: %d %s", c.origin, c.host, w.Code, w.Body)
}
}
// usemagpie.ai still reads the Omarchy theme, and only that
if w := do(call{"GET", "/v1/magpie/omarchy", ""}, "https://usemagpie.ai", "127.0.0.1:3425", loop, ""); w.Code == http.StatusForbidden {
t.Errorf("usemagpie.ai's Omarchy theme: %d %s", w.Code, w.Body)
}
}
// Origin "null" is not an app's: a sandboxed frame sends it on any web
// page's behalf (a file:// page and some webviews send it too), so one
// iframe on a page the user visits could send the "simple" text/plain
// POST and spend a subscription with no key, which is the case corsGuard
// exists for. It is held to an enabled gateway key, as a listed page is
// Real browsers proved the request and its null Origin arrive with no
// preflight (2026-10-15): a sandboxed frame on a public https page
// reached loopback in WebKit 27.2 — Chromium 156 and Firefox 157 refuse
// the cross-site request themselves (Local Network Access) — and a
// file:// page reached it in all three. A scheme only an app or an
// extension can send is answered as before.
func TestOpaqueOriginNeedsAKey(t *testing.T) {
fresh(t)
var hits atomic.Int64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Local page"})
if err != nil {
t.Fatal(err)
}
// what a browser sends from a sandboxed frame or an opened file: a
// "simple" POST, so no preflight comes first
do := func(method, path, origin, key string) *httptest.ResponseRecorder {
t.Helper()
var body io.Reader
if method == "POST" {
body = strings.NewReader(chatReq)
}
r := httptest.NewRequest(method, path, body)
r.RemoteAddr = "127.0.0.1:50123"
r.Host = "127.0.0.1:3425"
r.Header.Set("Origin", origin)
r.Header.Set("Sec-Fetch-Site", "cross-site")
r.Header.Set("Sec-Fetch-Mode", "no-cors")
r.Header.Set("Sec-Fetch-Dest", "empty")
r.Header.Set("Content-Type", "text/plain;charset=UTF-8")
r.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/27.2 Safari/605.1.15")
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
w := httptest.NewRecorder()
lanGuard(New().Handler()).ServeHTTP(w, r)
return w
}
for _, p := range []struct{ method, path string }{
{"POST", "/v1/chat/completions"},
{"POST", "/v1/messages"},
{"POST", "/v1/images/generations"},
{"POST", "/v1/embeddings"},
{"GET", "/v1/models"},
} {
if w := do(p.method, p.path, "null", ""); w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "gateway key") || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("%s %s with no key: %d %v %s", p.method, p.path, w.Code, w.Header(), w.Body)
}
}
if hits.Load() != 0 {
t.Fatalf("a sandboxed frame with no key reached the vendor %d times", hits.Load())
}
// with an enabled key it is answered as before, and reads nothing
// either way (no CORS headers: only a listed origin the user named
// gets those)
if w := do("POST", "/v1/chat/completions", "null", secret); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "ok") || w.Header().Get("Access-Control-Allow-Origin") != "" || hits.Load() != 1 {
t.Errorf("with its key: %d %v %s", w.Code, w.Header(), w.Body)
}
// a key turned off is no key
keys, _ := access.List()
access.Update("off-key", access.Change{Key: keys[len(keys)-1].ID})
if w := do("POST", "/v1/chat/completions", "null", secret); w.Code != http.StatusForbidden {
t.Errorf("with a key turned off: %d %s", w.Code, w.Body)
}
access.Update("on-key", access.Change{Key: keys[len(keys)-1].ID})
// a scheme only that app or extension can send is untouched
for _, o := range []string{"app://cherry", "tauri://localhost", "vscode-webview://1abc", "chrome-extension://abcdefghijklmnop"} {
if w := do("POST", "/v1/chat/completions", o, ""); w.Code != http.StatusOK {
t.Errorf("%s as before: %d %s", o, w.Code, w.Body)
}
}
}
// A page at a subdomain of localhost (#2256, ggbdpq) is not this
// computer's in every browser: Chrome and Firefox resolve *.localhost to
// loopback by a rule of their own, but Safari and other WebKit views ask
// the system resolver, which before macOS 26 asks the network's DNS (WebKit
// bug 160504, closed when macOS 26 resolved it below WebKit). A resolver
// that answers such a name — a captive portal's, a hostile network's —
// serves the page from anywhere, and it then reaches the gateway two ways:
// cross-origin from http://evil.localhost with a "simple" text/plain POST,
// and rebound, at http://evil.localhost:3425 itself once the name turns to
// 127.0.0.1, where its GET carries no Origin. Both are held to an enabled
// gateway key, as Origin "null" is (#2242). localhost itself, which the
// system answers from its hosts file, is unchanged, and so are
// tauri.localhost and wails.localhost on Windows, where Tauri's and
// Wails' webviews serve themselves and no browser asks the DNS for them.
func TestLocalhostSubdomainNeedsAKey(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
t.Setenv("MAGPIE_PUBLIC_URL", "")
var hits atomic.Int64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Local page"})
if err != nil {
t.Fatal(err)
}
// what Safari sends: a "simple" POST, so no preflight comes first
do := func(method, path, host, origin, key string) *httptest.ResponseRecorder {
t.Helper()
var body io.Reader
if method == "POST" {
body = strings.NewReader(chatReq)
}
r := httptest.NewRequest(method, path, body)
r.RemoteAddr = "127.0.0.1:50123"
r.Host = host
site := "same-origin"
if origin != "" {
r.Header.Set("Origin", origin)
site = "cross-site"
}
r.Header.Set("Sec-Fetch-Site", site)
r.Header.Set("Sec-Fetch-Mode", "no-cors")
r.Header.Set("Content-Type", "text/plain;charset=UTF-8")
r.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15")
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
w := httptest.NewRecorder()
lanGuard(New().Handler()).ServeHTTP(w, r)
return w
}
routes := []struct{ method, path string }{
{"POST", "/v1/chat/completions"},
{"POST", "/v1/messages"},
{"POST", "/v1/responses"},
{"POST", "/v1/embeddings"},
{"GET", "/v1/models"},
{"GET", "/v1/magpie/quotas"},
}
const gw = "127.0.0.1:3425"
for _, os := range []string{"darwin", "linux", "windows"} {
t.Run(os, func(t *testing.T) {
was := webviewOS
webviewOS = os
t.Cleanup(func() { webviewOS = was })
hits.Store(0)
// cross-origin, from a page at any subdomain; on macOS and
// Linux the two webview names too, which a hostile resolver
// answers as readily
pages := []string{"http://evil.localhost", "http://evil.localhost:8080", "HTTP://Free-Gift.LOCALHOST", "http://a.b.localhost"}
if os != "windows" {
pages = append(pages, "http://tauri.localhost", "http://wails.localhost")
}
for _, o := range pages {
for _, p := range routes {
if w := do(p.method, p.path, gw, o, ""); w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "gateway key") || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("%s %s from %s with no key: %d %s", p.method, p.path, o, w.Code, w.Body)
}
}
}
// rebound: the page's own name turned to 127.0.0.1, its GET
// with no Origin and its POST with its own
for _, p := range routes {
o := ""
if p.method == "POST" {
o = "http://evil.localhost:3425"
}
if w := do(p.method, p.path, "evil.localhost:3425", o, ""); w.Code != http.StatusForbidden {
t.Errorf("%s %s rebound at evil.localhost with no key: %d %s", p.method, p.path, w.Code, w.Body)
}
}
if n := hits.Load(); n != 0 {
t.Fatalf("a localhost subdomain with no key reached the vendor %d times", n)
}
// with an enabled key it is answered, both ways, and reads
// nothing more (no CORS headers)
if w := do("POST", "/v1/chat/completions", gw, "http://evil.localhost", secret); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "ok") || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("cross-origin with its key: %d %s", w.Code, w.Body)
}
if w := do("GET", "/v1/models", "evil.localhost:3425", "", secret); w.Code != http.StatusOK {
t.Errorf("rebound with its key: %d %s", w.Code, w.Body)
}
// answered as before: a page at localhost itself, and the two
// webviews on Windows
keyless := []string{"http://localhost:5173", "http://127.0.0.1:8080"}
if os == "windows" {
keyless = append(keyless, "http://tauri.localhost", "http://wails.localhost")
}
for _, o := range keyless {
if w := do("POST", "/v1/chat/completions", gw, o, ""); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "ok") {
t.Errorf("%s as before: %d %s", o, w.Code, w.Body)
}
}
for _, host := range []string{"localhost:3425", "LOCALHOST.:3425", "docker.for.mac.localhost:3425"} {
if w := do("GET", "/v1/models", host, "", ""); w.Code != http.StatusOK {
t.Errorf("a browser at %s: %d %s", host, w.Code, w.Body)
}
}
})
}
}
// A browser extension the user listed by its origin (#1051, SkyAerope on
// Discord: an extension such as Sider, without host permissions, is held
// to CORS like a page) is treated as a listed web page is, no more: its
// preflight is answered, its keyed call reads the reply, and a call
// without a gateway key is refused 401. An extension not listed is
// answered as before — no CORS headers, so only one that holds host
// permissions (and doesn't enforce CORS) reads anything.
func TestCORSExtensionOrigin(t *testing.T) {
fresh(t)
var hits atomic.Int64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Extension"})
if err != nil {
t.Fatal(err)
}
const ext = "chrome-extension://abcdefghijklmnopabcdefghijklmnop"
const other = "chrome-extension://ponmlkjihgfedcbaponmlkjihgfedcba"
const fox = "moz-extension://e0b5d7c7-c079-484b-8825-44d261383cb6"
srv := lanGuard(New().Handler())
do := func(method, origin, key string) *httptest.ResponseRecorder {
t.Helper()
var body io.Reader
if method == "POST" {
body = strings.NewReader(chatReq)
}
r := httptest.NewRequest(method, "/v1/chat/completions", body)
r.RemoteAddr = "127.0.0.1:50123"
r.Host = "127.0.0.1:3425"
r.Header.Set("Origin", origin)
r.Header.Set("Sec-Fetch-Mode", "cors")
r.Header.Set("User-Agent", "Mozilla/5.0 Chrome/140.0")
if method == "OPTIONS" {
r.Header.Set("Access-Control-Request-Method", "POST")
r.Header.Set("Access-Control-Request-Headers", "authorization, content-type")
} else {
r.Header.Set("Content-Type", "application/json")
}
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
return w
}
// not listed: no CORS headers, as before; a web page listed doesn't list it
s := settings.Load()
s.CORSOrigins = []string{"http://localhost:3000"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if w := do("OPTIONS", ext, ""); w.Code == http.StatusNoContent || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Fatalf("unlisted extension's preflight: %d %v", w.Code, w.Header())
}
if w := do("POST", ext, ""); w.Code != http.StatusOK || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Fatalf("unlisted extension, as before: %d %v %s", w.Code, w.Header(), w.Body)
}
// listed, written as the user might copy it
if s.CORSOrigins, err = settings.CleanOrigins([]string{"http://localhost:3000", "CHROME-EXTENSION://ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP/", fox}); err != nil {
t.Fatal(err)
}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
for _, o := range []string{ext, fox} {
w := do("OPTIONS", o, "")
if w.Code != http.StatusNoContent || w.Header().Get("Access-Control-Allow-Origin") != o ||
w.Header().Get("Access-Control-Allow-Headers") != "authorization, content-type" || !strings.Contains(w.Header().Get("Access-Control-Allow-Methods"), "POST") {
t.Errorf("%s preflight: %d %v", o, w.Code, w.Header())
}
if w := do("POST", o, secret); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "ok") || w.Header().Get("Access-Control-Allow-Origin") != o {
t.Errorf("%s with its key: %d %v %s", o, w.Code, w.Header(), w.Body)
}
}
n := hits.Load()
// a listed extension needs a gateway key, as a listed page does
if w := do("POST", ext, ""); w.Code != http.StatusUnauthorized || w.Header().Get("Access-Control-Allow-Origin") != ext || !strings.Contains(w.Body.String(), "gateway key") {
t.Errorf("listed extension without a key: %d %v %s", w.Code, w.Header(), w.Body)
}
if w := do("POST", ext, "not-a-key"); w.Code != http.StatusUnauthorized {
t.Errorf("listed extension with a wrong key: %d %s", w.Code, w.Body)
}
if hits.Load() != n {
t.Fatal("a keyless call from a listed extension reached the vendor")
}
// another extension is still not listed
if w := do("OPTIONS", other, ""); w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("another extension's preflight: %d %v", w.Code, w.Header())
}
if w := do("POST", other, secret); w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("another extension: %d %v", w.Code, w.Header())
}
}
// A preflight that reaches the gateway from another machine was refused
// 401 for the key a preflight may not carry (#4931: the same preflight
// for a listed origin was answered 204 on loopback and 401, with no CORS
// headers, on the LAN address and behind an HTTPS reverse proxy, whose
// forwarding header makes it remote from loopback too — a browser
// reports it as PreflightMissingAllowOriginHeader and the page's fetch
// fails). lanGuard now hands a preflight to corsGuard, which answers a
// listed origin's and refuses any other page's, while the call it asks
// after still needs the key, and the network's rules (the allow list,
// sharing on the LAN) are judged first, as for every request.
func TestCORSPreflightFromLAN(t *testing.T) {
fresh(t)
s := settings.Load()
s.CORSOrigins = []string{"https://learn.trancy.org"}
s.LAN = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
srv := lanGuard(New().Handler())
do := func(remote, host, method, origin, key string, hdr map[string]string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(method, "/v1/models", nil)
r.RemoteAddr = remote
r.Host = host
if origin != "" {
r.Header.Set("Origin", origin)
}
if key != "" {
r.Header.Set("Authorization", "Bearer "+key)
}
for k, v := range hdr {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
return w
}
pf := map[string]string{"Access-Control-Request-Method": "GET", "Access-Control-Request-Headers": "authorization"}
const trancy = "https://learn.trancy.org"
const lan = "192.168.1.9:50123"
const lanHost = "192.168.1.5:3425"
// the issue's first row: on loopback the preflight was answered
if w := do("127.0.0.1:50123", "127.0.0.1:3425", "OPTIONS", trancy, "", pf); w.Code != http.StatusNoContent ||
w.Header().Get("Access-Control-Allow-Origin") != trancy || w.Header().Get("Access-Control-Allow-Headers") != "authorization" {
t.Errorf("loopback preflight: %d %v", w.Code, w.Header())
}
// direct on the LAN: was 401, with no CORS headers
if w := do(lan, lanHost, "OPTIONS", trancy, "", pf); w.Code != http.StatusNoContent ||
w.Header().Get("Access-Control-Allow-Origin") != trancy || w.Header().Get("Access-Control-Allow-Headers") != "authorization" {
t.Errorf("LAN preflight: %d %v", w.Code, w.Header())
}
// behind a reverse proxy that forwards with a header (nginx): was 401
if w := do("127.0.0.1:50123", "magpie.example.com", "OPTIONS", trancy, "",
map[string]string{"Access-Control-Request-Method": "GET", "Access-Control-Request-Headers": "authorization", "X-Forwarded-For": "203.0.113.7"}); w.Code != http.StatusNoContent ||
w.Header().Get("Access-Control-Allow-Origin") != trancy {
t.Errorf("proxied preflight: %d %v", w.Code, w.Header())
}
// a page not listed is refused, as on loopback: no 204, no headers
if w := do(lan, lanHost, "OPTIONS", "https://evil.example", "", pf); w.Header().Get("Access-Control-Allow-Origin") != "" || w.Code == http.StatusNoContent {
t.Errorf("unlisted origin's preflight: %d %v", w.Code, w.Header())
}
// the call the preflight asks after still needs the key
if w := do(lan, lanHost, "GET", trancy, "", nil); w.Code != http.StatusUnauthorized {
t.Errorf("keyless call from the LAN: %d", w.Code)
}
// an OPTIONS that declares no call is no preflight
if w := do(lan, lanHost, "OPTIONS", trancy, "", nil); w.Code != http.StatusUnauthorized {
t.Errorf("plain OPTIONS from the LAN: %d", w.Code)
}
// and while magpie isn't shared, the network's rules refuse a
// preflight as they refuse everything else from another machine
s.LAN = false
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if w := do(lan, lanHost, "OPTIONS", trancy, "", pf); w.Code == http.StatusNoContent || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("preflight while not shared: %d %v", w.Code, w.Header())
}
}
// A preflight from another machine is let past the key check for a listed
// origin alone, and then corsGuard answers it with nothing behind it
// reached (#4931). corsGuard passes some unlisted origins on unkeyed (a
// page on this computer, the request's own origin, an app's scheme), and
// a route that takes OPTIONS (Codex's relay, the MCP paths) would serve
// such a "preflight" with no key, so those meet the key check as before;
// and no request but a preflight skips it for a preflight's headers.
func TestCORSPreflightFromLANReachesNothing(t *testing.T) {
fresh(t)
s := settings.Load()
s.CORSOrigins = []string{"https://learn.trancy.org"}
s.LAN = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
var reached []string
srv := lanGuard(corsGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reached = append(reached, r.Method+" "+r.URL.Path+" "+r.Header.Get("Origin"))
w.WriteHeader(http.StatusTeapot)
})))
const lan = "192.168.1.9:50123"
const lanHost = "192.168.1.5:3425"
do := func(method, path, origin string, hdr map[string]string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(method, path, nil)
r.RemoteAddr = lan
r.Host = lanHost
if origin != "" {
r.Header.Set("Origin", origin)
}
for k, v := range hdr {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
srv.ServeHTTP(w, r)
return w
}
pf := map[string]string{"Access-Control-Request-Method": "POST", "Access-Control-Request-Headers": "authorization"}
for _, path := range []string{CodexPath + "/responses", "/mcp/github", "/v1/models"} {
for _, origin := range []string{
"http://localhost:5173", // a page on this computer
"http://127.0.0.1:8080", // the same, by address
"http://foo.localhost", // a localhost subdomain
"http://" + lanHost, // the request's own origin
"chrome-extension://abcdef", // an extension
"app://obsidian.md", // an app's scheme
"null", // a sandboxed frame
"https://evil.example", // a site elsewhere
} {
reached = nil
w := do(http.MethodOptions, path, origin, pf)
if len(reached) > 0 || w.Code == http.StatusNoContent || w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Errorf("keyless preflight for %s from %s: %d %v, reached %v", path, origin, w.Code, w.Header(), reached)
}
}
// a listed origin's preflight is answered, by corsGuard alone
reached = nil
if w := do(http.MethodOptions, path, "https://learn.trancy.org", pf); w.Code != http.StatusNoContent || len(reached) > 0 {
t.Errorf("listed preflight for %s: %d, reached %v", path, w.Code, reached)
}
// a preflight's headers on any other method skip nothing
for _, m := range []string{http.MethodPost, http.MethodGet, http.MethodDelete} {
reached = nil
if w := do(m, path, "https://learn.trancy.org", pf); w.Code != http.StatusUnauthorized || len(reached) > 0 {
t.Errorf("keyless %s for %s with a preflight's headers: %d, reached %v", m, path, w.Code, reached)
}
}
}
}