Files
magpie/internal/gateway/lan_rebind_test.go
T
yetoneandggbdpq d182294be7 gateway: a page at a localhost subdomain spends a subscription only with a gateway key, as Origin null does (#2256, ggbdpq)
What changed
- corsGuard refuses (403) an http(s) Origin at a subdomain of localhost
  unless the call carries an enabled gateway key (localhostSubdomain),
  the rule Origin "null" got in #2242. foreignPage still passes it, so a
  page there that holds a key keeps working; neither gets CORS headers.
- Why: Chrome and Firefox resolve *.localhost to loopback themselves,
  but Safari and other WebKit views hand the name to the system
  resolver, which before macOS 26 asks the network's DNS (WebKit bug
  160504, closed MOVED: "implemented in OS frameworks below WebKit",
  2025-09-19; fails on macOS 15.7). A captive portal's or hostile
  network's resolver serves such a page from anywhere, and its "simple"
  text/plain POST spent subscriptions with no key.
- tauri.localhost and wails.localhost stay keyless on Windows only,
  where Tauri's and Wails' webviews serve themselves at those names. On
  macOS and Linux those apps use tauri:// and wails://, so the two names
  are any page's there, and a hostile resolver answers them as readily
  as any other (the residual the report left open). webviewOS
  (runtime.GOOS) lets the test be each system.
- The same page also reached the gateway rebound: at evil.localhost:3425
  once the name turns to 127.0.0.1, its GET carries no Origin, and
  lanGuard's ownHost counted every *.localhost Host as this computer, so
  GET /v1/models and /v1/magpie/quotas answered 200. ownHost no longer
  counts a subdomain of localhost (localhost itself and
  docker.for.mac.localhost still count); a browser at one needs a key.
- TestForeignPagesRefused's tauri.localhost row now sends a key, and
  TestReboundPageRefused expects wails.localhost refused.
- docs/subsystems/gateway-routing.md says both.

Ran
- TestLocalhostSubdomainNeedsAKey (darwin, linux and windows as
  webviewOS; six routes; cross-origin and rebound; the fake vendor never
  reached): fails on origin/main 00501401a (76 errors, e.g. "POST
  /v1/chat/completions from http://evil.localhost with no key: 200"),
  passes with the change.
- Broken by hand, each red: ownHost counting *.localhost again (the
  rebound GETs 200); localhostSubdomain always false; the Windows-only
  exception dropped (tauri.localhost answered keyless on darwin).
- The cors, rebind, LAN, usage-ingest and allow-list tests: ok.
- gofmt -l clean; go vet ./..., GOOS=windows go vet ./...,
  GOOS=linux go vet -tags nogui ./... clean.
- go test -p 4 ./... (macOS): ok in every package but internal/agent and internal/gateway, which hit the 10m package timeout under load ~30 with no test hung (the running test 1-2s old); rerun with -timeout 40m: gateway ok 551s, agent ok 426s.

Not tried: a real Safari on macOS 15 against a resolver that answers
*.localhost; Tauri or Wails apps on Windows.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-15 05:17:37 +08:00

75 lines
3.1 KiB
Go

package gateway
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// A web page whose site's DNS turns to 127.0.0.1 after it loads (DNS
// rebinding) calls the gateway as its own origin: a GET carries no Origin,
// a POST one equal to the Host, and on loopback no key is asked. Such a
// browser request under a name that isn't this computer's is refused; an
// agent, a browser on localhost or an IP, a container runtime's name for
// the host, and a call with an enabled gateway key go through as before.
func TestReboundPageRefused(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
t.Setenv("MAGPIE_PUBLIC_URL", "")
reached := false
h := lanGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reached = true }))
call := func(method, host string, hdr ...string) int {
reached = false
r := httptest.NewRequest(method, "/v1/models", nil)
r.RemoteAddr = "127.0.0.1:51234"
r.Host = host
for i := 0; i+1 < len(hdr); i += 2 {
r.Header.Set(hdr[i], hdr[i+1])
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code == http.StatusForbidden && !strings.Contains(w.Body.String(), "localhost or 127.0.0.1") {
t.Fatalf("%s %s: %q", method, host, w.Body.String())
}
return w.Code
}
chrome := []string{"User-Agent", "Mozilla/5.0 (Macintosh) Chrome/140", "Sec-Fetch-Site", "same-origin", "Sec-Fetch-Mode", "cors"}
evil := "rebind.attacker.example:3425"
if c := call("GET", evil, chrome...); c != http.StatusForbidden || reached {
t.Fatal("a rebound page's GET:", c)
}
if c := call("POST", evil, append([]string{"Origin", "http://" + evil}, chrome...)...); c != http.StatusForbidden || reached {
t.Fatal("a rebound page's POST:", c)
}
// Safari before Sec-Fetch: the user agent alone
if c := call("GET", evil, "User-Agent", "Mozilla/5.0 (Macintosh) Safari/605"); c != http.StatusForbidden || reached {
t.Fatal("an older browser:", c)
}
for _, host := range []string{"127.0.0.1:3425", "localhost:3425", "[::1]:3425", "host.docker.internal:3425", "LOCALHOST.:3425"} {
if c := call("GET", host, chrome...); c != 200 || !reached {
t.Fatal("a browser at", host, c)
}
}
// a subdomain of localhost is a name Safari asks the DNS for (#2256)
if c := call("GET", "wails.localhost", chrome...); c != http.StatusForbidden || reached {
t.Fatal("a browser at a localhost subdomain:", c)
}
// an agent sends no browser marks, whatever name it was given
if c := call("POST", evil, "Authorization", "Bearer magpie", "User-Agent", "claude-cli/2.1"); c != 200 || !reached {
t.Fatal("an agent:", c)
}
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.net")
if c := call("GET", "magpie.example.net", chrome...); c != 200 || !reached {
t.Fatal("the public URL's host:", c)
}
_, secrets := newCaller(t, "Page")
if c := call("GET", evil, append([]string{"Authorization", "Bearer " + secrets[0]}, chrome...)...); c != 200 || !reached {
t.Fatal("with an enabled gateway key:", c)
}
if c := call("GET", evil, append([]string{"Authorization", "Bearer sk-magpie-wrong"}, chrome...)...); c != http.StatusForbidden || reached {
t.Fatal("with a wrong key:", c)
}
}