Files
magpie/internal/gui/pluginpage_window_test.go
T
yetone 1747559d02 plugin pages: name the scheme in a browser's policy, and the service's 'self' (#4198)
In `magpie web`, iOS Safari (18.7) loaded none of a plugin page's
scripts: pagePolicy named the page's folder as a scheme-less host-source
(host/_magpie/plugin/<tok>/), a sandboxed frame's origin is opaque, and
that WebKit took the missing scheme from it, so the bridge never said
hello and the page was broken at 10s (orinsid; YangHonghang on v0.1.2681,
young02036 on Discord).

- pagePolicy: a browser's policy now names the folder under both
  https:// and http:// (ownSources). Behind nginx or Cloudflare TLS ends
  before magpie and nothing magpie trusts says which scheme the page came
  over, so X-Forwarded-Proto is not read; both name the same host, port
  and folder, and an https page can't load http:// files anyway. The
  desktop window keeps its own transport alone (wails://, http:// on
  Windows, #2501).
- The second layer (YangHonghang): a page's local service sends its own
  policy, `connect-src 'self'`, and what it sends runs under magpie's
  `sandbox allow-scripts`. In WebKit, 'self' there didn't match the
  service page's own fetch of api/x. servePageService now names the
  service's root (/_magpie/plugin/<tok>/_magpie/service/) under both
  schemes after each 'self' in a CSP header the service sends, plus ws://
  and wss:// in connect-src and default-src (serviceSelf). 'self' stays,
  so nothing allowed before is taken away. A <meta> policy isn't
  rewritten; the plugin docs (en, zh, ja) say to write whole sources.

Run at f0a723d4c's tree (rebased on c74b932c8), on mote:
- go test ./internal/gui/: every test ok but TestMainWindowCorners, red
  the same at an origin/main e7185daeb copy (#3949, macOS 27). The real
  WKWebView test TestPluginPageLoadsItsFilesInTheWindow (desktop
  wails:// path) passes.
- gofmt -l, go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet:
  clean.
- node --test plugin-page, plugin-panels, plugin-page-idle (chromium and
  webkit): 14/14.
- win-run: TestPluginPage*, TestServiceSelf, TestPluginPanel* ok on
  Windows 11 (the window's http:// scheme).
- A scratch test (not committed) served the real pluginPageRoutes(web)
  and a fake service with "default-src 'self'; connect-src 'self'" over
  http and https (httptest TLS), framed sandbox=allow-scripts, in
  Playwright Chromium 151 and WebKit 26.5. At origin/main WebKit refused
  the service's fetch ("Refused to connect to .../_magpie/service/api/x
  because it does not appear in the connect-src directive", the
  reporter's words); with this change it fetched, and Chromium did both
  times. The page's bridge said hello in both engines both times.

Broken by hand: ownSources back to the scheme-less source and
serviceSelf skipped: TestPluginPageWebPolicyNamesItsScheme,
TestPluginPageServedShutIn, TestPluginPanelsServedShutIn,
TestPluginPageWindowPolicy and TestPluginPageServiceProxied go red.

Not tried: a real iPhone. Playwright's WebKit is not iOS Safari, and it
loads the scheme-less source fine, so the first layer is fixed on the
reporters' evidence and the CSP rule, not seen fail and pass here. Not
tried behind a real nginx or Cloudflare, nor a service WebSocket.
2026-11-02 15:27:15 +08:00

123 lines
4.6 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
)
// Wails strips the URL's scheme before calling the window's handler. The
// policy must still name that scheme: WebKit blocks a scheme-less host
// source under wails://, so the bridge never says hello.
func TestPluginPageWindowPolicy(t *testing.T) {
pagePlugins(t)
page := pluginPages()[0]
// Wails' own base URLs (internal/assetserver/assetserver_<os>.go):
// wails://localhost on macOS and Linux, http://wails.localhost on Windows.
desktop := "wails://localhost"
if runtime.GOOS == "windows" {
desktop = "http://wails.localhost"
}
desktopHost := strings.TrimPrefix(strings.TrimPrefix(desktop, "wails://"), "http://")
for _, c := range []struct {
name string
host string
window bool
source string
}{
{"desktop", desktopHost, true, desktop},
{"web", "127.0.0.1:3437", false, "https://127.0.0.1:3437"},
{"web localhost", "localhost", false, "https://localhost"},
} {
t.Run(c.name, func(t *testing.T) {
var handler http.Handler = pageMux()
if c.window {
handler = ownWindow(handler)
}
for _, file := range []string{"index.html", pluginBridgePath, "app.js", "style.css", "icon.svg"} {
url := pluginPagePrefix + page.token + "/" + file
r := httptest.NewRequest("GET", url, nil)
r.Host = c.host
// A web request cannot opt into the native policy by naming
// the window's host or sending Wails' headers.
r.Header.Set("X-Wails-Window-Id", "1")
w := do(handler, r)
if w.Code != http.StatusOK {
t.Fatalf("%s: HTTP %d", file, w.Code)
}
policy := w.Header().Get("Content-Security-Policy")
source := c.source + pluginPagePrefix + page.token + "/"
for _, directive := range []string{"script-src", "style-src", "img-src", "font-src", "media-src"} {
if !strings.Contains(policy, directive+" "+source) {
t.Errorf("%s: %s cannot load the page's own files: %s", file, directive, policy)
}
}
for _, directive := range []string{"sandbox allow-scripts", "connect-src 'none'", "frame-src 'none'", "form-action 'none'"} {
if !strings.Contains(policy, directive) {
t.Errorf("%s: missing %s", file, directive)
}
}
}
})
}
}
// A browser's page (magpie web) names its folder with a scheme: WebKit
// takes a scheme-less source's scheme from the sandboxed frame's opaque
// origin and matched nothing, so iOS Safari loaded no script (#4198).
// Served over http, or https with TLS ended by a proxy in front, both
// schemes are named, on the Host the browser asked for; what the proxy
// says in its forwarding headers changes nothing.
func TestPluginPageWebPolicyNamesItsScheme(t *testing.T) {
pagePlugins(t)
page := pluginPages()[0]
for _, c := range []struct {
name, host string
hdr map[string]string
}{
{"http", "192.168.1.5:3430", nil},
{"https behind nginx", "magpie.test.xyz", map[string]string{"X-Forwarded-Proto": "https", "X-Forwarded-For": "1.2.3.4"}},
{"https behind Cloudflare", "magpie.test.xyz", map[string]string{"X-Forwarded-Proto": "https", "Cf-Visitor": `{"scheme":"https"}`, "X-Forwarded-Host": "evil.example"}},
} {
t.Run(c.name, func(t *testing.T) {
r := httptest.NewRequest("GET", pluginPagePrefix+page.token+"/index.html", nil)
r.Host = c.host
for k, v := range c.hdr {
r.Header.Set(k, v)
}
w := do(pageMux(), r)
if w.Code != http.StatusOK {
t.Fatalf("HTTP %d", w.Code)
}
policy := w.Header().Get("Content-Security-Policy")
folder := c.host + pluginPagePrefix + page.token + "/"
want := "https://" + folder + " http://" + folder
for _, d := range []string{"script-src " + want + ";", "style-src " + want + " 'unsafe-inline';", "img-src " + want + " data:;", "font-src " + want + ";", "media-src " + want + ";"} {
if !strings.Contains(policy, d) {
t.Errorf("policy has no %q: %s", d, policy)
}
}
// no source without its scheme is left
for _, d := range strings.Split(policy, ";") {
for _, src := range strings.Fields(d)[1:] {
if strings.Contains(src, "/_magpie/") && !strings.HasPrefix(src, "https://") && !strings.HasPrefix(src, "http://") {
t.Errorf("scheme-less source %q in %s", src, policy)
}
}
}
if strings.Contains(policy, "evil.example") {
t.Errorf("a forwarding header named a host: %s", policy)
}
})
}
// the desktop window names its own transport alone
r := httptest.NewRequest("GET", pluginPagePrefix+page.token+"/index.html", nil)
r.Host = "localhost"
w := do(ownWindow(pageMux()), r)
if policy := w.Header().Get("Content-Security-Policy"); strings.Contains(policy, "https://") {
t.Errorf("the window's policy names https: %s", policy)
}
}