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.
This commit is contained in:
File diff suppressed because one or more lines are too long
+49
-1
@@ -349,7 +349,55 @@ func webServed(port, key string, keep time.Duration, page http.Handler) http.Han
|
||||
// webHandler is what `magpie web` on port serves: the page and its API
|
||||
// behind the key, compressed for the browser.
|
||||
func webHandler(port, key string, keep time.Duration, page http.Handler) http.Handler {
|
||||
return webCompressed(webGuard("magpie_web_"+port, key, keep, page))
|
||||
return webCompressed(webGuard("magpie_web_"+port, key, keep, webOwnPage(page)))
|
||||
}
|
||||
|
||||
// webOwnPage turns away a request for /api that another page sent with
|
||||
// the page's cookie. The cookie is SameSite=Lax, so a browser sends it
|
||||
// with any request one site sends another, and a site is the registrable
|
||||
// domain without the port: a page on another port of 127.0.0.1 or of the
|
||||
// LAN address, or on a sibling name behind the same reverse proxy, could
|
||||
// POST a stdio MCP server to /api/library/servers/save as text/plain in
|
||||
// no-cors mode and start it with /api/library/mcp/check, which is code run
|
||||
// as the user. Only the page itself may write: a write (any method but
|
||||
// GET, HEAD and OPTIONS) is judged by net/http's CrossOriginProtection,
|
||||
// which takes the browser's Sec-Fetch-Site (same-origin, or none for what
|
||||
// the user did) and, from a browser too old to send it, an Origin that is
|
||||
// absent or names the request's own Host or MAGPIE_PUBLIC_URL. A read is
|
||||
// turned away too when the browser says another origin sent it, so that
|
||||
// no GET route that does something (an import read once, a sign-in
|
||||
// started) is any page's to call, whether or not it answers.
|
||||
//
|
||||
// The desktop window's /api is served in the process, over no port, and
|
||||
// has no cookie for another page to borrow, so this is for the browser's
|
||||
// page alone.
|
||||
func webOwnPage(next http.Handler) http.Handler {
|
||||
cop := http.NewCrossOriginProtection()
|
||||
if u, err := url.Parse(gateway.PublicURL()); err == nil && u.Host != "" && (u.Scheme == "http" || u.Scheme == "https") {
|
||||
// behind a proxy that passes on a Host of its own, the page's
|
||||
// address is the one the user named
|
||||
if err := cop.AddTrustedOrigin(u.Scheme + "://" + u.Host); err != nil {
|
||||
log.Printf("magpie web: MAGPIE_PUBLIC_URL %s is no origin: %v", u, err)
|
||||
}
|
||||
}
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if !strings.HasPrefix(r.URL.Path, "/api/") {
|
||||
next.ServeHTTP(rw, r)
|
||||
return
|
||||
}
|
||||
err := cop.Check(r)
|
||||
if err == nil {
|
||||
if site := r.Header.Get("Sec-Fetch-Site"); site != "" && site != "same-origin" && site != "none" {
|
||||
err = errors.New("Sec-Fetch-Site " + site)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("magpie web: turned away %s %s from another page (Origin %q): %v", r.Method, r.URL.Path, r.Header.Get("Origin"), err)
|
||||
http.Error(rw, "magpie's API answers magpie's own page only", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(rw, r)
|
||||
})
|
||||
}
|
||||
|
||||
// webCompressed gzips what `magpie web` sends a browser that takes it: the
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A page on another port of the same host, on another port of the LAN
|
||||
// address, or on a sibling name behind the same proxy is the same site, so
|
||||
// the browser sends it the page's SameSite=Lax cookie: its "simple" POST
|
||||
// (text/plain, no-cors) to /api/library/servers/save, then
|
||||
// /api/library/mcp/check, would start an MCP server of its own as the
|
||||
// user. Through the whole chain `magpie web` serves, with the page's
|
||||
// cookie, every such request is turned away, and the page's own still
|
||||
// reach the API.
|
||||
func TestWebAPIAnswersItsOwnPageOnly(t *testing.T) {
|
||||
onePortHome(t)
|
||||
const lo = "127.0.0.1:50001"
|
||||
noCors := []string{"Sec-Fetch-Mode", "no-cors", "Sec-Fetch-Dest", "empty"}
|
||||
marks := func(site string, hdr ...string) []string {
|
||||
if site != "" {
|
||||
hdr = append(hdr, "Sec-Fetch-Site", site)
|
||||
}
|
||||
return append(hdr, noCors...)
|
||||
}
|
||||
call := func(name string, want bool, method, target, host string, hdr ...string) {
|
||||
t.Helper()
|
||||
var reached bool
|
||||
h := onePortPage(&reached)
|
||||
w := onePortCall(t, h, method, target, lo, host, onePortWebKey, hdr...)
|
||||
if reached != want || !want && w.Code != http.StatusForbidden {
|
||||
t.Errorf("%s: %s %s under Host %s: %d, reached %v, want reached %v: %s", name, method, target, host, w.Code, reached, want, w.Body.String())
|
||||
}
|
||||
}
|
||||
const save, check = "/api/library/servers/save", "/api/library/mcp/check"
|
||||
for _, path := range []string{save, check} {
|
||||
// another port of 127.0.0.1
|
||||
call("same-site Origin on another port", false, "POST", path, "127.0.0.1:3430", marks("same-site", "Origin", "http://127.0.0.1:8080")...)
|
||||
call("cross-port Origin from a browser without Sec-Fetch-Site", false, "POST", path, "127.0.0.1:3430", marks("", "Origin", "http://127.0.0.1:8080")...)
|
||||
call("localhost on another port", false, "POST", path, "localhost:3430", marks("same-site", "Origin", "http://localhost:5173")...)
|
||||
// another port of the LAN address
|
||||
call("the LAN address on another port", false, "POST", path, "192.168.1.20:3430", marks("same-site", "Origin", "http://192.168.1.20:8080")...)
|
||||
call("the LAN address on another port, an old browser", false, "POST", path, "192.168.1.20:3430", marks("", "Origin", "http://192.168.1.20:8080")...)
|
||||
// a sibling name behind the same reverse proxy
|
||||
call("a sibling subdomain", false, "POST", path, "magpie.example.com", marks("same-site", "Origin", "https://blog.example.com")...)
|
||||
call("a sibling subdomain, an old browser", false, "POST", path, "magpie.example.com", marks("", "Origin", "https://blog.example.com")...)
|
||||
call("another site", false, "POST", path, "127.0.0.1:3430", marks("cross-site", "Origin", "https://evil.example")...)
|
||||
// a sandboxed frame or a local file
|
||||
call("Origin null", false, "POST", path, "127.0.0.1:3430", marks("cross-site", "Origin", "null")...)
|
||||
call("Origin null, an old browser", false, "POST", path, "127.0.0.1:3430", marks("", "Origin", "null")...)
|
||||
// what the Origin leaves out the browser still says
|
||||
call("no Origin, Sec-Fetch-Site same-site", false, "POST", path, "127.0.0.1:3430", marks("same-site")...)
|
||||
call("no Origin, Sec-Fetch-Site cross-site", false, "POST", path, "127.0.0.1:3430", marks("cross-site")...)
|
||||
}
|
||||
// a read another page sends: an <img>, a link, a form's GET
|
||||
call("a GET from another port", false, "GET", "/api/import/x", "127.0.0.1:3430", marks("same-site")...)
|
||||
call("a GET from a sibling subdomain", false, "GET", "/api/import/x", "magpie.example.com", "Sec-Fetch-Site", "same-site", "Sec-Fetch-Dest", "image")
|
||||
call("a link from another site", false, "GET", "/api/import/x", "127.0.0.1:3430", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Dest", "document", "Sec-Fetch-Mode", "navigate")
|
||||
call("a preflight from another port", false, "OPTIONS", save, "127.0.0.1:3430", "Origin", "http://127.0.0.1:8080", "Sec-Fetch-Site", "same-site")
|
||||
|
||||
// the page's own
|
||||
own := []string{"Content-Type", "application/json", "Sec-Fetch-Mode", "cors", "Sec-Fetch-Dest", "empty"}
|
||||
call("the page's own origin", true, "POST", save, "127.0.0.1:3430", append([]string{"Origin", "http://127.0.0.1:3430", "Sec-Fetch-Site", "same-origin"}, own...)...)
|
||||
call("the page's own origin, an old browser", true, "POST", save, "127.0.0.1:3430", append([]string{"Origin", "http://127.0.0.1:3430"}, own...)...)
|
||||
call("the page's own origin on the LAN", true, "POST", save, "192.168.1.20:3430", append([]string{"Origin", "http://192.168.1.20:3430", "Sec-Fetch-Site", "same-origin"}, own...)...)
|
||||
call("behind a proxy that keeps the Host", true, "POST", save, "magpie.example.com", append([]string{"Origin", "https://magpie.example.com"}, own...)...)
|
||||
// a proxy that passes on a Host of its own (nginx's default): the
|
||||
// browser's word is enough, and so is MAGPIE_PUBLIC_URL's address
|
||||
call("behind a proxy that rewrites the Host", true, "POST", save, "127.0.0.1:3430", append([]string{"Origin", "https://magpie.example.com", "Sec-Fetch-Site", "same-origin"}, own...)...)
|
||||
call("a page's own read", true, "GET", "/api/state", "127.0.0.1:3430", "Sec-Fetch-Site", "same-origin", "Sec-Fetch-Mode", "cors", "Sec-Fetch-Dest", "empty")
|
||||
call("the address typed in", true, "GET", "/api/state", "127.0.0.1:3430", "Sec-Fetch-Site", "none", "Sec-Fetch-Mode", "navigate", "Sec-Fetch-Dest", "document")
|
||||
call("a client that isn't a browser, with the key's cookie", true, "POST", save, "127.0.0.1:3430", "Content-Type", "application/json")
|
||||
// the page's files aren't the API
|
||||
call("the page itself from a link elsewhere", true, "GET", "/", "127.0.0.1:3430", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "navigate", "Sec-Fetch-Dest", "document")
|
||||
|
||||
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.com/")
|
||||
call("an old browser behind a proxy that rewrites the Host, MAGPIE_PUBLIC_URL set", true, "POST", save, "127.0.0.1:3430", append([]string{"Origin", "https://magpie.example.com"}, own...)...)
|
||||
call("a sibling subdomain, MAGPIE_PUBLIC_URL set", false, "POST", save, "127.0.0.1:3430", marks("same-site", "Origin", "https://blog.example.com")...)
|
||||
}
|
||||
Reference in New Issue
Block a user