gui: the desktop window saving an allow list is this computer, not 192.0.2.1 (Mathresearcher)
Settings › Allowed addresses in the desktop app refused a list with "that allow list would shut out 192.0.2.1, the address this page is open from" (Mathresearcher on Discord). The window's requests reach the API through Wails' asset server, which has no peer address and fills in 192.0.2.1:1234 (TEST-NET-1; assetserver_webview.go in wails v3 beta.24). KeepsOut took that for the page's address. The asset handler handed to Wails is now wrapped in ownWindow, which marks its requests as loopback: only the webview reaches it, never the network. magpie web (web.go, AllowGuard) is unchanged and still judges the real peer. Run: - TestAllowIPsFromTheAppWindow (new): posts ["10.0.0.0/8"] with RemoteAddr 192.0.2.1:1234 through ownWindow(Handler). With ownWindow's rewrite taken out it fails (refused, saved []); with it, passes. - go test -tags nogui -run TestAllowIPs ./internal/gui: ok. - gofmt -l, go vet (darwin), GOOS=linux -tags nogui and GOOS=windows vet of internal/gui and internal/gateway; go build for all three. Not tried: saving the list in a real desktop window (not launched).
This commit is contained in:
@@ -99,3 +99,24 @@ func TestAllowIPsUnreadableSettingsNotSavedOver(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The desktop window's requests reach the API through Wails, which names
|
||||
// their address 192.0.2.1:1234 when it has none. Saving an allow list from
|
||||
// the window doesn't call that address the page's and refuse the list
|
||||
// (Mathresearcher on Discord).
|
||||
func TestAllowIPsFromTheAppWindow(t *testing.T) {
|
||||
h := t.TempDir()
|
||||
t.Setenv("HOME", h)
|
||||
t.Setenv("USERPROFILE", h)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
||||
t.Setenv(gateway.AllowIPsEnv, "")
|
||||
srv := ownWindow(Handler(nil, nil))
|
||||
rec := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/api/settings/allowips", strings.NewReader(`{"ips":["10.0.0.0/8"]}`))
|
||||
r.RemoteAddr = "192.0.2.1:1234" // what Wails' asset server fills in
|
||||
srv.ServeHTTP(rec, r)
|
||||
if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), "192.0.2.1") || !slices.Equal(settings.Load().AllowIPs, []string{"10.0.0.0/8"}) {
|
||||
t.Fatalf("%d %s, saved %v", rec.Code, rec.Body, settings.Load().AllowIPs)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -347,7 +347,7 @@ func Run(version string, showMain bool, link string) error {
|
||||
Name: "magpie",
|
||||
Description: "one place to pick every agent's model",
|
||||
Icon: appIconFor(),
|
||||
Assets: application.AssetOptions{Handler: handler},
|
||||
Assets: application.AssetOptions{Handler: ownWindow(handler)},
|
||||
Mac: application.MacOptions{ActivationPolicy: dockPolicy(settings.Load().Dock)},
|
||||
Windows: windowsOptions(),
|
||||
// the tray stays when lightweight mode has let both windows go
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package gui
|
||||
|
||||
import "net/http"
|
||||
|
||||
// ownWindow serves the app's own windows. Wails hands their requests over
|
||||
// with no RemoteAddr and fills in 192.0.2.1:1234 (TEST-NET-1), so the
|
||||
// allow list took the desktop window for a computer at 192.0.2.1 and
|
||||
// asked for that address to be added (Mathresearcher on Discord). Only
|
||||
// the webview reaches this handler, never the network, so its requests
|
||||
// come from this computer: loopback.
|
||||
func ownWindow(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.RemoteAddr = "127.0.0.1:0"
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user