diff --git a/internal/gui/allowips_settings_test.go b/internal/gui/allowips_settings_test.go index 3467d39d8..52e2402eb 100644 --- a/internal/gui/allowips_settings_test.go +++ b/internal/gui/allowips_settings_test.go @@ -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) + } +} diff --git a/internal/gui/app.go b/internal/gui/app.go index 2e7102fd8..7d3e82e48 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -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 diff --git a/internal/gui/window_peer.go b/internal/gui/window_peer.go new file mode 100644 index 000000000..8ed30cd8e --- /dev/null +++ b/internal/gui/window_peer.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) + }) +}