Files
magpie/internal/gateway/cors.go
T
yetone 91e7ef56af settings, gateway, gui: a browser extension is listed among the web pages that may call magpie by its origin, chrome-extension://<id> and the like (#1051, SkyAerope on Discord)
What: Settings › Network and sharing › Web pages now takes a browser
extension's origin besides http(s) ones: chrome-extension://<id>
(Chromium, the id 32 letters a-p), moz-extension://<uuid> (Firefox's
Internal UUID) and safari-web-extension://<uuid> (Safari). cleanOrigin
takes only those three schemes, only with an id of that browser's shape,
lower-cased and with a trailing "/" let go; a bare scheme, a wildcard, a
path, a port, user info, a query or another browser's id shape is
refused. The GUI maps the new refusal to the same message, which now
names the extension form, in en/zh/zh-TW/ja/de, and a listed origin
wraps instead of being cut (an extension's runs to 50-60 characters and
was ellipsized at a 440px panel). The 401 a listed caller gets without a
key names extensions too. Docs: gateway-routing.md, the settings field.

Why: an extension without host_permissions (Sider and the like) is held
to CORS; the gateway never answered its preflight, since its origin
couldn't be listed. Nothing else in the access rules changes: corsGuard
treats a listed extension exactly as a listed page (preflight answered,
every call needs an enabled gateway key or gets 401, even on loopback),
an unlisted extension passes with no CORS headers as before (foreignPage
still only judges http/https), and lanGuard's rebound/Host checks, the
allow list and every other route guard are untouched.

The Origin each browser sends was checked, not guessed: real Chromium
(below) sent Origin: chrome-extension://<32 a-p>; Firefox's is
moz-extension://<Internal UUID> (Gotify's and others' CORS docs list
it); Safari's is safari-web-extension://<lowercase uuid>, per Apple's
developer forums (thread 654839), where an Apple engineer notes the uuid
changes on each Safari launch, so a Safari listing lasts until it
restarts (said in the doc). A content script's fetch carries the page's
origin (MDN), so only extension pages, popups and workers are covered.

Run, at 28aac86bc:
- TestCleanExtensionOrigins (good ids, upper case, trailing slash;
  malformed, wildcard, bare scheme, path, port, userinfo, query, swapped
  id shapes) and TestCORSExtensionOrigin (through lanGuard: unlisted
  preflight/request get no CORS headers and the request runs as before;
  listed chrome and moz preflight 204 with the headers; keyed call 200
  with Access-Control-Allow-Origin; keyless or wrong key 401 without
  reaching the vendor; another id still unlisted). Both fail on the old
  origins.go (the extension origin "is not an origin").
- go test ./internal/settings ./internal/gateway ./internal/gui: green.
- gofmt -l clean; go vet ./... darwin, GOOS=linux -tags nogui,
  GOOS=windows: clean.
- GUI: cors-origins (extended: an extension added upper-cased with a
  trailing slash is saved lower-cased and fits the panel, a malformed id
  and chrome-extension://* refused in the page's language), i18n-keys,
  gui-de, gui-ja, gui-zh-tw, allow-ips, lan-docker, chromium and webkit:
  green. cors-origins fails on the old app.js/app.css (the name was cut,
  the extension refusal untranslated).
- Real use: an unpacked MV3 extension with no host_permissions, loaded in
  headless Chromium 1234 (playwright launchPersistentContext,
  --load-extension), fetched a sandbox gateway (lanGuard(New().Handler())
  on 127.0.0.1:3591 under a temp HOME, fake vendor) from its service
  worker and its own page. Not listed: "Failed to fetch" in all four
  cases. Listed (typed upper-case with "/"): with the key, read 200 and
  the vendor's reply; without it, read the 401.

Not tried: Firefox and Safari extensions for real (their origins are from
the sources above), Edge/Brave, and the full magpie serve binary (the
harness ran the gateway's own handler stack, not its background loops).
2026-10-13 01:57:37 +08:00

162 lines
6.2 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"
"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.
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
}
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 r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
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
keyed := access.Caller(r.Context()).KeyID != "" ||
slices.ContainsFunc(callerKeys(r), func(k string) bool { _, ok := access.Authenticate(k); return ok })
if !keyed {
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)
})
}
// 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. "null" (a sandboxed frame, a file://
// page, some Electron renderers) and other schemes — app://, file://,
// tauri://, vscode-webview://, chrome-extension:// — come from desktop
// apps, editor webviews and browser extensions. Such a client could call
// magpie before only because it doesn't enforce CORS (a webview with web
// security off, an extension holding host permissions), and no ordinary
// web page can send those origins, so they pass as before. *.localhost
// counts as this computer: browsers resolve it to loopback, and Tauri's
// and Wails' webviews on Windows are http://tauri.localhost and
// http://wails.localhost, whose HTTP plugins send that Origin outside CORS.
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
}
// 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])
}
// 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, ", ")
}