Files
magpie/internal/gateway/cors.go
T
ggbdpq 8f6dacbf7f gateway: a browser's CORS preflight from another machine is answered, not asked for a key (#4931)
From the report (magpie 0.1.2698, macOS 27.0.1, LAN sharing on, gateway
port 3425): a page at https://learn.trancy.org, listed in Settings ->
Network and sharing -> Web pages, failed to add itself as a custom
engine ("Failed to fetch"). The same OPTIONS preflight for /v1/models,
declaring GET and the authorization header with no key on it, was
answered 204 with the CORS headers at http://127.0.0.1:3425 and refused
401 with no CORS headers at the machine's LAN address and behind an
HTTPS reverse proxy (Nginx Proxy Manager, no CORS handling of its own);
the browser logged net::ERR_FAILED,
corsError: PreflightMissingAllowOriginHeader, and gave up before the
keyed POST. The 401 body was the gateway's own "no API key was sent:
send a magpie gateway key as Authorization: Bearer <key> (or
x-api-key)".

lanGuard sits in front of Handler (Front, oneport.go), and corsGuard is
inside Handler, so a request from another machine met the key check
first: with the gateway shared (or listening on the network),
identifyCaller refused the keyless preflight 401 before corsGuard could
see it. On loopback lanGuard asks no key, which is why the same
preflight was answered there. A preflight is an OPTIONS that declares
the call to come, and per what CORS asks of it carries no key and no
body: the browser cannot send one.

lanGuard now hands a preflight (corsPreflight: an OPTIONS with an
Origin and an Access-Control-Request-Method) to corsGuard instead,
after the allow list and the shared/on-network rules have judged it as
they judge every request. corsGuard answers a listed origin's with its
204 and headers and refuses any other page's as on loopback; the call
the preflight asks after still needs an enabled gateway key, and an
OPTIONS that declares no call is no preflight. `magpie web`'s page
port, which hands client routes to the same Front, is covered by the
same change.

Tests (internal/gateway/cors_test.go), from the report's three-row
table:
- TestCORSPreflightFromLAN: the loopback preflight answered 204 (the
  row that already worked), the LAN-direct one and the reverse-proxy
  one (X-Forwarded-For, loopback peer) answered 204 with Allow-Origin
  and Allow-Headers; an unlisted origin's preflight refused with no
  CORS headers; a keyless GET and a plain OPTIONS from the LAN still
  401; a preflight while magpie isn't shared still refused.

Broken by hand (the preflight handoff taken back out of lanGuard): the
LAN-direct and reverse-proxy rows go red, 401 with the report's own
error body, while the loopback row stays green; restored, ok.

Run (macOS arm64, this tree, -tags nogui):
- go test -run 'TestCORS|TestLAN|TestForeignPages|TestOpaqueOrigin|
  TestLocalhostSubdomain|TestRebound|TestOnePort|TestFront'
  ./internal/gateway: ok.
- the same with -race: ok.
- go vet ./internal/gateway, and with GOOS=linux and GOOS=windows: ok.
- go build . with GOOS=darwin, GOOS=linux and GOOS=windows: ok (the
  darwin cgo enum-conversion warnings in internal/gui are pre-existing).
- gofmt -l on the three touched files: clean.
- go test -count=1 -timeout 50m -tags nogui ./internal/gateway: every
  test ok except TestCodexPoolSaysTheClientsVersion, the known #4807
  (another test's Codex identity reaches it in a full package run); it
  passes run alone at this head.
- go test -tags nogui -count=1 ./internal/access ./internal/settings .:
  ok. ./internal/gui: TestProxyCheckSaysAProxyPassedBy fails, and fails
  the same way run alone on the untouched base a3768cdd1 — this
  machine's environment, not this change (the test drives checkProxy's
  ALL_PROXY walk, nothing of the gateway).

Not tried: a real Trancy page against a real LAN address and a real
Nginx Proxy Manager install; the report's LAN and proxy rows are taken
from its measured curl and browser evidence, and reproduced here at the
lanGuard/corsGuard seam with httptest instead. A preflight arriving
while magpie isn't shared is still refused, so the browser reports the
preflight, not the later call, as the failure — the network's rules
come first, as the report asks.

Review, changed from ggbdpq's patch (security): the patch let every
keyless preflight from another computer past lanGuard's key check.
corsGuard passes some unlisted origins on with no key (a page on this
computer by name or address, *.localhost, the request's own origin,
an app's or extension's scheme), and a route that takes OPTIONS
(Codex's backend relay, the MCP paths) would then serve such a
"preflight" with the user's sign-in. lanGuard now hands on only a
preflight whose origin is listed (corsAllowed); every other one meets
the key check as before, Origin null included. No request other than
an OPTIONS with Origin and Access-Control-Request-Method skips the
key: a POST, GET or DELETE carrying a preflight's headers is 401.

TestCORSPreflightFromLANReachesNothing (added in review) sends a
keyless preflight from the LAN for Codex's relay, an MCP path and
/v1/models from localhost, 127.0.0.1, foo.localhost, the request's own
origin, an extension, app://, null and a foreign site: none reaches
the handler or gets CORS headers. The listed origin's is answered by
corsGuard alone, and other methods with its headers are 401. A
rebound page never gets here: lanGuard's rebound check runs on
loopback first, and from the LAN the key check stands. Broken by
hand at the reviewed head: the corsAllowed condition removed, this
test red (TestCORSPreflightFromLAN stays green); the whole handoff
reverted, both red. Restored, green.

Run at the pushed tree (this stack of nine on origin/main 3c2dceb34),
on mote (macOS arm64, Go 1.26.8):
- go test -count=1 -timeout 45m ./internal/catalog/...
  ./internal/plugin/... ./internal/davsync/... ./internal/gateway/...:
  catalog, plugin and davsync ok; gateway red on one test only,
  TestCodexPoolSaysTheClientsVersion, the open #4807 (another test's
  Codex identity reaches it in a full package run). It passes run
  alone at this tree, 3 of 3.
- go vet ./... (darwin); GOOS=linux and GOOS=windows go vet -tags
  nogui ./...: ok. gofmt -l on every tracked .go file: clean.
- The hand breaks above ran on this stack before its rebase onto
  3c2dceb34. That commit changed member.go only, nothing these tests
  reach.
Not tried: the GUI suite, since no GUI file changed. Windows and Linux
test runs (the Claude bridge tests use a shell fake and skip on
Windows). No real vendor, Claude Code or network was used; every
case runs against fakes.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-03 07:43:18 +08:00

233 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"log"
"net"
"net/http"
"net/url"
"runtime"
"slices"
"strings"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// corsGuard lets the web pages and browser extensions the user listed
// in Settings › Network (settings.CORSOrigins, #1051) call the gateway
// from a browser: their
// preflight is answered, and their calls get the headers that let the page
// read the reply. Such a call carries an enabled gateway key — a page is
// not an agent the user started, and on loopback a call needs no key, so
// without one any script served at that origin (another dev server on
// the same port) could spend the user's subscriptions. A page not listed
// gets no CORS headers, as before, and a request with no Origin (every
// agent's) passes untouched.
//
// Any other web page is refused (403, foreignPage): on loopback a call
// needs no key and the body's Content-Type isn't checked, so a page the
// user merely visits could send a "simple" text/plain POST and spend their
// subscriptions blind. Such a page never read a reply before (no CORS
// headers), so refusing it breaks nothing that worked.
//
// An Origin of "null" is refused too (403) unless the call carries an
// enabled gateway key: any web page can send it from a sandboxed iframe,
// which is that same "simple" POST one frame away (a file:// page and an
// app's webview send it as well). A key, as for a listed origin, still
// lets such a client through; neither gets CORS headers, so a frame or a
// local page still sends blind, as before.
//
// So is a page at a subdomain of localhost (localhostSubdomain, #2256):
// Safari and other WebKit views ask the DNS for such a name, and a
// resolver that answers it serves the page from anywhere.
func corsGuard(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" || !corsAllowed(origin) {
if foreignPage(r, origin) {
log.Printf("refused %s %s from the web page %s: not one listed in Settings", r.Method, r.URL.Path, origin)
writeError(w, provider.Chat, http.StatusForbidden, "magpie doesn't answer the web page "+origin+": list it in Settings → Network and sharing → Web pages to let it call magpie with a gateway key")
return
}
if origin == "null" && !keyedCaller(r) {
log.Printf("refused %s %s from Origin: null: a sandboxed frame or a local page, with no gateway key", r.Method, r.URL.Path)
writeError(w, provider.Chat, http.StatusForbidden, "magpie doesn't answer a sandboxed frame or a local page (Origin: null) without a gateway key: create one in magpie's Gateway page and send it as Authorization: Bearer <key> (or x-api-key)")
return
}
if localhostSubdomain(origin) && !keyedCaller(r) {
log.Printf("refused %s %s from the web page %s: a localhost subdomain, with no gateway key", r.Method, r.URL.Path, origin)
writeError(w, provider.Chat, http.StatusForbidden, "magpie doesn't answer the web page "+origin+" without a gateway key: Safari and other WebKit browsers ask the network's DNS for a localhost subdomain, so the page may not be on this computer. Open it at localhost or 127.0.0.1, or create a key in magpie's Gateway page and send it as Authorization: Bearer <key> (or x-api-key)")
return
}
next.ServeHTTP(w, r)
return
}
h := w.Header()
h.Set("Access-Control-Allow-Origin", origin)
h.Add("Access-Control-Expose-Headers", SessionHeader)
h.Add("Vary", "Origin")
if corsPreflight(r) {
h.Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
if asked := corsHeaders(r.Header.Get("Access-Control-Request-Headers")); asked != "" {
h.Set("Access-Control-Allow-Headers", asked)
}
// Chrome asks before a public page reaches this computer
if r.Header.Get("Access-Control-Request-Private-Network") == "true" {
h.Set("Access-Control-Allow-Private-Network", "true")
}
h.Set("Access-Control-Max-Age", "600")
w.WriteHeader(http.StatusNoContent)
return
}
// lanGuard, in front of Handler on the real server, has already
// taken the key and put magpie's own token in its place
if !keyedCaller(r) {
writeError(w, provider.Chat, http.StatusUnauthorized, "a web page or browser extension calls magpie with a gateway key: create one in magpie's Gateway page and send it as Authorization: Bearer <key> (or x-api-key)")
return
}
next.ServeHTTP(w, r)
})
}
// keyedCaller: the request carries an enabled gateway key — the caller
// lanGuard recorded, or one in its own headers, which is where a handler
// called without lanGuard finds it (the web app's embedded calls, tests).
func keyedCaller(r *http.Request) bool {
return access.Caller(r.Context()).KeyID != "" ||
slices.ContainsFunc(callerKeys(r), func(k string) bool { _, ok := access.Authenticate(k); return ok })
}
// foreignPage: origin is an http(s) web page that is neither on this
// computer (localhost, *.localhost, 127.0.0.0/8, [::1], any port) nor the
// request's own origin (a page served at the address the request names,
// such as magpie's web app on a LAN address), so a browser sent it for a
// site elsewhere. usemagpie.ai may still read the Omarchy theme
// (omarchyTheme).
//
// Only http and https are judged. A scheme only an app or an extension
// can send — app://, file://, tauri://, vscode-webview://,
// chrome-extension:// — comes from desktop apps, editor webviews and
// browser extensions that don't enforce CORS (a webview with web security
// off, an extension holding host permissions), and no ordinary web page
// can put one on a request, so those pass as before. "null" is not one of
// them: any web page sends it from a sandboxed frame and a local file
// sends it too, which corsGuard judges (a gateway key, or 403). A
// subdomain of localhost isn't foreign here either, but corsGuard asks a
// gateway key of it (localhostSubdomain).
func foreignPage(r *http.Request, origin string) bool {
if origin == "" {
return false
}
u, err := url.Parse(origin)
if err != nil {
// a browser's always parses; an unreadable http one is no page of ours
return strings.HasPrefix(strings.ToLower(origin), "http")
}
scheme := strings.ToLower(u.Scheme)
if scheme != "http" && scheme != "https" {
return false
}
host := strings.ToLower(u.Hostname())
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
return false
}
if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() {
return false
}
if hostPort(u.Host, scheme) == hostPort(r.Host, scheme) {
return false
}
if r.URL.Path == "/v1/magpie/omarchy" && siteOrigin(origin) {
return false
}
return true
}
// webviewOS is the system magpie runs on, which localhostSubdomain asks;
// a variable so a test can be each.
var webviewOS = runtime.GOOS
// localhostSubdomain: origin is an http(s) page at a subdomain of
// localhost (#2256, ggbdpq), which may not be on this computer. Chrome and
// Firefox resolve *.localhost to loopback by a rule of their own, but
// Safari and other WebKit views hand the name to the system resolver, and
// before macOS 26 that asks the network's DNS (WebKit bug 160504, closed
// when macOS 26 resolved it below WebKit; WebKitGTK asks whatever the
// system's resolver is). A resolver that answers — a captive portal's, a
// hostile network's — serves the page from anywhere, and it would send the
// "simple" POST Origin "null" was refused for (#2242). The request doesn't
// say which browser resolved the name, so every such page is held to a
// key. tauri.localhost and wails.localhost on Windows are left out: Tauri's
// and Wails' webviews there serve themselves at those names and their HTTP
// plugins send that Origin outside CORS, and no browser on Windows asks the
// DNS for them. On macOS and Linux those apps use tauri:// and wails://, so
// the two names are any page's there.
func localhostSubdomain(origin string) bool {
u, err := url.Parse(origin)
if err != nil {
return false
}
if s := strings.ToLower(u.Scheme); s != "http" && s != "https" {
return false
}
host := strings.TrimSuffix(strings.ToLower(u.Hostname()), ".")
if !strings.HasSuffix(host, ".localhost") {
return false
}
return webviewOS != "windows" || host != "tauri.localhost" && host != "wails.localhost"
}
// hostPort is host[:port] lower-cased with the scheme's port filled in, so
// https://a.example and a Host of a.example:443 compare equal.
func hostPort(hp, scheme string) string {
hp = strings.ToLower(hp)
if hp == "" {
return ""
}
if h, p, err := net.SplitHostPort(hp); err == nil {
return net.JoinHostPort(strings.Trim(h, "[]"), p)
}
port := "80"
if scheme == "https" {
port = "443"
}
return net.JoinHostPort(strings.Trim(hp, "[]"), port)
}
// corsAllowed: origin is one the user listed.
func corsAllowed(origin string) bool {
saved := settings.Load().CORSOrigins
if len(saved) == 0 {
return false
}
// as saved from Settings, or as written into settings.json by hand
list, _ := settings.CleanOrigins(saved)
o, err := settings.CleanOrigins([]string{origin})
return err == nil && len(o) == 1 && slices.Contains(list, o[0])
}
// corsPreflight: r is a browser's CORS preflight, an OPTIONS that
// declares the actual call to come. A browser puts no key on one —
// neither an Origin-headered one on the headers it asks for — which is
// why lanGuard passes one to this file's guard before asking for a key
// (#4931).
func corsPreflight(r *http.Request) bool {
return r.Method == http.MethodOptions && r.Header.Get("Origin") != "" && r.Header.Get("Access-Control-Request-Method") != ""
}
// corsHeaders is a preflight's Access-Control-Request-Headers as the
// answer allows them: the header names asked for, nothing else.
func corsHeaders(asked string) string {
var out []string
for _, f := range strings.Split(asked, ",") {
f = strings.TrimSpace(f)
if f != "" && !strings.ContainsFunc(f, func(c rune) bool {
return !(c == '-' || c == '_' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
}) {
out = append(out, f)
}
}
return strings.Join(out, ", ")
}