XYenon (#2501): on macOS the Plugins page stops answering after 10s. A plugin page is served in a frame with sandbox="allow-scripts", and its CSP named its own folder as a scheme-less source (localhost/p/<tok>/). Under wails://, WebKit doesn't match a scheme-less host source, so the page's bridge.js was refused and the page never said it was ready. ownWindow now records the scheme of the native handler's base URL (wails on macOS and Linux, http on Windows, as Wails v3 beta.24's assetserver_<os>.go sets them) in the request's context, and pagePolicy writes that scheme in front of the page's own source. Web requests have no scheme in the context, so the browser GUI keeps its relative source and still works over HTTP, HTTPS and reverse proxies. The patch and its test are the reporter's. The test now uses Wails' real hosts (wails://localhost, or http://wails.localhost on Windows), not a made-up one. What was run: - A WKWebView harness (Swift, a WKURLSchemeHandler for wails://) loading a page in a sandbox="allow-scripts" frame with the old and new CSP: with localhost/p/T/ the handler is never asked for bridge.js; with wails://localhost/p/T/ it is. At top level, with no frame, both load. - TestPluginPageWindowPolicy fails on the old pagePolicy (bridge.js refused for the desktop case) and passes now. - gofmt -l clean; go vet -tags nogui on darwin, linux and windows ok. - Full suite, set -o pipefail; go test -p 4 -timeout 45m ./... at this pair rebased on63be47717, machine load average 170-330 from other work: 63 packages ok; internal/agent and internal/gateway ran out of 45m (#2544, #2191, #2108); internal/gui TestMainWindowCorners killed at its 40s deadline (#1936). Run alone afterwards, gateway and agent ran out of 45m again, and gateway's TestQuietCodexStreamKeptAliveThroughReplyWriters/a_middleware got its reply unchanged once, the middleware failing open on its time limit (filed #2565; 6/6 alone on63be47717and here). - The same two packages on63be47717alone, side by side with this head under the same load: both ran out of 45m there too, in other tests, so the timeouts are not this change. Not tried: the real desktop app with an installed plugin page, and WebView2 on Windows or WebKitGTK on Linux. The Windows source is an explicit http:// one, which WebView2 matches as before. Co-authored-by: XYenon <20698483+XYenon@users.noreply.github.com>
66 lines
2.2 KiB
Go
66 lines
2.2 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, "127.0.0.1:3437"},
|
|
{"web localhost", "localhost", false, "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)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|