Files
magpie/internal/gui/cors_settings_test.go
T
yetone b234507842 gateway, settings, gui: web pages the user lists in Settings › Network may call the gateway from a browser, each request with a gateway key (SkyAerope, #1051)
A page on http://localhost:3000 asking /v1/models got a 404 to its
preflight and no Access-Control-Allow-Origin, so the browser hid every
answer. Settings › Network › Web pages now takes origins (cleaned to
scheme://host[:port]; "*" and anything that isn't an origin are refused,
said in the page's language). For a listed origin corsGuard answers the
preflight (204, the asked header names, Private Network Access) and adds
Access-Control-Allow-Origin; every other request from it needs an enabled
gateway key, even from loopback, or gets a readable 401. Default: none
listed, and a request with any other Origin or none (every agent's) is
unchanged. A save of the rest of the Settings page keeps the list.

Verified: TestCORSOrigins (gateway, loopback requests: preflight, keyed
calls, keyless/managed-token/turned-off key refused, unlisted origins incl.
"null" and port/scheme variants get nothing, agents untouched) fails
without corsGuard in Handler (preflight 404); TestCleanOrigins;
TestCORSOriginsSetting fails without keeping CORSOrigins in POST
/api/settings; TestSettingsSaveKeepsWhatItDoesNotSend. GUI
cors-origins.test.cjs (en/zh, 440px, add by button and Enter, refusals,
remove, no scroll, no left border) in Chromium and WebKit fails without
renderCORS; gui-ja, gui-de, i18n-keys, gateway-port, lan-docker,
redact-rule-row, settings-* pass in both. go test -tags nogui ./... green;
-race -count=5 on the new tests; darwin, linux nogui, windows builds.
click-scroll's two Requests-page subtests fail the same on origin/main.
2026-10-07 03:34:05 +08:00

46 lines
2.0 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The web pages that may call the gateway (#1051) are saved as origins,
// a wildcard or a URL that isn't one is refused with the list left as it
// was, and a save of the rest of the Settings page leaves them alone.
func TestCORSOriginsSetting(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"))
srv := Handler(nil, nil)
post := func(path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
return rec
}
rec := post("/api/settings/cors", `{"origins":["http://LocalHost:3000/","https://app.example.com:443"]}`)
want := []string{"http://localhost:3000", "https://app.example.com"}
if rec.Code != http.StatusOK || !slices.Equal(settings.Load().CORSOrigins, want) || !strings.Contains(rec.Body.String(), `"corsOrigins":["http://localhost:3000","https://app.example.com"]`) {
t.Fatalf("%d %s, saved %v", rec.Code, rec.Body, settings.Load().CORSOrigins)
}
for _, bad := range []string{`["*"]`, `["http://localhost:3000","localhost:4000"]`, `["https://app.example.com/v1"]`} {
if rec := post("/api/settings/cors", `{"origins":`+bad+`}`); rec.Code == http.StatusOK || !slices.Equal(settings.Load().CORSOrigins, want) {
t.Errorf("%s: %d, saved %v", bad, rec.Code, settings.Load().CORSOrigins)
}
}
if rec := post("/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK || !slices.Equal(settings.Load().CORSOrigins, want) {
t.Fatalf("the Settings page's save changed the origins: %d %v", rec.Code, settings.Load().CORSOrigins)
}
if rec := post("/api/settings/cors", `{"origins":[]}`); rec.Code != http.StatusOK || len(settings.Load().CORSOrigins) != 0 {
t.Fatalf("emptied: %d %v", rec.Code, settings.Load().CORSOrigins)
}
}