gui: a plugin page behind a proxy that keeps no Host still loads its scripts (xuanwo on X)
xuanwo's Day card was blank in magpie web behind a reverse proxy. The frame's CSP named the page's folder under r.Host (https:// and http://), and a proxy that keeps no Host (nginx's default proxy_pass) hands magpie its upstream's own address, so the policy named nothing the browser used and every script was blocked. A browser's page now names its own origin in the frame's address (_o=, framed() in pluginpages.js; the frame sends no Referer, its referrerpolicy is no-referrer, so a Referer can't carry it), and ownSources adds it (browserOrigin: http(s) only, no user info, a plain host). The desktop window's scheme is unchanged. Run, on mote: - TestPluginPagePolicyBehindProxyHost: red on the old code (policy names only the upstream host), green now; also checks that a junk _o (javascript:, user info, a path) adds nothing. - Real repro: a sandbox `magpie web` behind a Node proxy that rewrites Host to the upstream's, opened at http://localhost:3495/?view=plugin-day-card in Playwright chromium and webkit. Before: a broken frame (webkit screenshot 52KB, no card). After: the card draws in both, the frame's address carries ?_o=http%3A%2F%2Flocalhost%3A3495. - go vet (darwin), GOOS=windows and GOOS=linux -tags nogui for internal/gui, usageapi; go test internal/plugin, internal/usageapi and internal/gui -run 'PluginPage|Page|Day'; plugin-page*.test.cjs in both engines: 8 of 10 pass. The 2 reds are plugin-page-foreign-error ("own: the frame opened"), red on origin/main too before this change (#6117). Not tried: nginx itself (a Node proxy that rewrites Host stands in), Cloudflare Tunnel, and the desktop window on Windows. The whole GUI suite is started after the tag.
This commit is contained in:
@@ -322,13 +322,20 @@ starts a Bun host.
|
||||
`magpie web` (#4198): a browser's policy names the folder
|
||||
under both `https://` and `http://` (`ownSources`), since a proxy that
|
||||
ends TLS (nginx, Cloudflare) leaves magpie nothing it trusts to say
|
||||
which. A page's service sends its own policy, where `'self'` meant its
|
||||
which. A proxy that keeps no Host (nginx's default `proxy_pass`) leaves
|
||||
`r.Host` the upstream's own address, and the policy named nothing the
|
||||
browser used: every script of the page was blocked (xuanwo on X). A
|
||||
browser's page therefore names its own origin in the frame's address
|
||||
(`_o=`, `framed` in pluginpages.js, since the frame sends no Referer), and
|
||||
`ownSources` adds it (`browserOrigin`: http(s), no user info). It only
|
||||
widens the policy to a name the page's own browser reports; the frame is
|
||||
sandboxed and the token in the path is what grants the page. A page's service sends its own policy, where `'self'` meant its
|
||||
whole 127.0.0.1 origin; sandboxed, WebKit refused the service page's
|
||||
fetches under `connect-src 'self'`. `serviceSelf` names the service's
|
||||
root under both schemes after each `'self'` in a policy header it sends
|
||||
(and `ws://`, `wss://` in `connect-src` and `default-src`); a `<meta>`
|
||||
policy is the plugin's to write with whole sources.
|
||||
`TestPluginPageWindowPolicy`, `TestPluginPageWebPolicyNamesItsScheme`,
|
||||
`TestPluginPageWindowPolicy`, `TestPluginPageWebPolicyNamesItsScheme`, `TestPluginPagePolicyBehindProxyHost`,
|
||||
`TestServiceSelf` and `TestPluginPageServiceProxied` check the policies, and
|
||||
`TestPluginPageLoadsItsFilesInTheWindow` loads a page through
|
||||
`ownWindow` in a real WKWebView; Playwright, on http, can't see it.
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
f.setAttribute("referrerpolicy", "no-referrer");
|
||||
f.setAttribute("allow", "");
|
||||
f.title = titled(p.entry);
|
||||
f.src = p.entry.url;
|
||||
f.src = framed(p.entry.url);
|
||||
p.frame = f;
|
||||
p.hello = false;
|
||||
p.shown = true;
|
||||
@@ -326,6 +326,14 @@
|
||||
console.error(`[magpie] ${p.entry.name}: panel ${p.entry.key}: ${why}`);
|
||||
}
|
||||
|
||||
// The address a frame loads: a browser's page names its own origin in
|
||||
// it (the frame sends no Referer), as the server's policy for the frame
|
||||
// can't know the address a reverse proxy that keeps no Host serves it
|
||||
// at. The desktop window's scheme is the server's own to name.
|
||||
function framed(url) {
|
||||
return /^https?:$/.test(location.protocol) ? url + (url.includes("?") ? "&" : "?") + "_o=" + encodeURIComponent(location.origin) : url;
|
||||
}
|
||||
|
||||
// a panel's frame in body, told context once it says hello
|
||||
function panelFrame(e, context, body) {
|
||||
const f = document.createElement("iframe");
|
||||
@@ -334,7 +342,7 @@
|
||||
f.setAttribute("referrerpolicy", "no-referrer");
|
||||
f.setAttribute("allow", "");
|
||||
f.title = titled(e);
|
||||
f.src = e.url;
|
||||
f.src = framed(e.url);
|
||||
const p = { entry: e, frame: f, context, body, hello: false, broken: false };
|
||||
p.timer = setTimeout(() => { if (!p.hello) panelBroke(p, t("{name}'s page didn't answer", { name: titled(e) })); }, HELLO_MS);
|
||||
panels.set(f, p);
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -266,7 +267,39 @@ func ownSources(r *http.Request, path string) []string {
|
||||
if scheme, _ := r.Context().Value(windowSchemeKey{}).(string); scheme != "" {
|
||||
return []string{scheme + "://" + r.Host + path}
|
||||
}
|
||||
return []string{"https://" + r.Host + path, "http://" + r.Host + path}
|
||||
own := []string{"https://" + r.Host + path, "http://" + r.Host + path}
|
||||
// A reverse proxy that doesn't pass the browser's Host on (nginx's
|
||||
// proxy_pass does not) leaves r.Host as magpie's own address, which
|
||||
// names nothing the browser asked for: every script and style of the
|
||||
// page was refused and it drew as bare HTML (xuanwo on X, Day card).
|
||||
// The window that framed the page is on the address the browser used,
|
||||
// and says so in the frame's address (pageOriginParam): the frame
|
||||
// sends no Referer.
|
||||
if o := browserOrigin(r); o != "" && o != "https://"+r.Host && o != "http://"+r.Host {
|
||||
own = append(own, o+path)
|
||||
}
|
||||
return own
|
||||
}
|
||||
|
||||
// pageOriginParam is the query a window adds to its frame's address: the
|
||||
// origin the window itself is on (pluginpages.js).
|
||||
const pageOriginParam = "_o"
|
||||
|
||||
// browserOrigin is the scheme and host the browser reached this server
|
||||
// at, from the frame's address, or "" when it names none
|
||||
// (or anything but a plain http(s) host and port, which a policy can't be
|
||||
// trusted to carry).
|
||||
func browserOrigin(r *http.Request) string {
|
||||
u, err := url.Parse(r.URL.Query().Get(pageOriginParam))
|
||||
if err != nil || u.Host == "" || u.User != nil || u.Scheme != "http" && u.Scheme != "https" {
|
||||
return ""
|
||||
}
|
||||
for _, c := range u.Host {
|
||||
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsRune(".-:[]", c)) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return u.Scheme + "://" + u.Host
|
||||
}
|
||||
|
||||
// pageHostWait is how long a page's HTML waits for its plugin's host,
|
||||
@@ -542,7 +575,7 @@ func pageRead(read string, params map[string]any) (any, error) {
|
||||
}
|
||||
return out, nil
|
||||
case "usage:day":
|
||||
// one day, by agent and model (pageday.go)
|
||||
// one day, or a span of them, by agent and model (pageday.go)
|
||||
return pageDay(params)
|
||||
case "usage:summary":
|
||||
period := usage.Today
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -373,3 +374,40 @@ func TestPluginPageActions(t *testing.T) {
|
||||
t.Errorf("an action with no title: %+v, want the page's", a[1])
|
||||
}
|
||||
}
|
||||
|
||||
// Behind a reverse proxy that doesn't pass the browser's Host on (nginx's
|
||||
// default proxy_pass), the Host magpie sees is its own address. The page's
|
||||
// policy then named an address the browser never used, so every script and
|
||||
// style was refused and the page drew as bare HTML (xuanwo on X, Day card).
|
||||
// The window names the address the browser used in the frame's address.
|
||||
func TestPluginPagePolicyBehindProxyHost(t *testing.T) {
|
||||
pagePlugins(t)
|
||||
h := pageMux()
|
||||
var pages []pluginPage
|
||||
w := do(h, httptest.NewRequest("GET", "/api/plugin-pages", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &pages); err != nil || len(pages) != 1 {
|
||||
t.Fatalf("pages: %s %v", w.Body, err)
|
||||
}
|
||||
tok := strings.Split(strings.TrimPrefix(pages[0].URL, pluginPagePrefix), "/")[0]
|
||||
csp := func(origin string) string {
|
||||
r := httptest.NewRequest("GET", pages[0].URL+"?"+pageOriginParam+"="+url.QueryEscape(origin), nil)
|
||||
r.Host = "127.0.0.1:3437"
|
||||
return do(h, r).Header().Get("Content-Security-Policy")
|
||||
}
|
||||
named := "https://proxy.example.com/_magpie/plugin/" + tok + "/"
|
||||
if p := csp("https://proxy.example.com"); !strings.Contains(p, "script-src https://127.0.0.1:3437/_magpie/plugin/"+tok+"/ http://127.0.0.1:3437/_magpie/plugin/"+tok+"/ "+named+";") ||
|
||||
!strings.Contains(p, "style-src https://127.0.0.1:3437/_magpie/plugin/"+tok+"/ http://127.0.0.1:3437/_magpie/plugin/"+tok+"/ "+named+" 'unsafe-inline'") {
|
||||
t.Fatalf("the proxy's address isn't named: %s", p)
|
||||
}
|
||||
// nothing but a plain http(s) host is carried into a policy
|
||||
for _, bad := range []string{"", "https://x.example; script-src */", "https://u:p@x.example/", "ftp://x.example/", "https://x.example,y.example/", "javascript:alert(1)"} {
|
||||
p := csp(bad)
|
||||
if strings.Contains(p, "x.example") || strings.Contains(p, "*") || strings.Contains(p, "alert") {
|
||||
t.Fatalf("origin %q reached the policy: %s", bad, p)
|
||||
}
|
||||
}
|
||||
// the Host's own address is named once
|
||||
if p := csp("http://127.0.0.1:3437"); strings.Count(p, "127.0.0.1:3437/_magpie/plugin/"+tok+"/") != 10 {
|
||||
t.Fatalf("the Host's address twice: %s", p)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user