Files
magpie/internal/gui/state_secrets_test.go
T
yetone ba3a25421b gui: /api/state sends no saved proxy password, LAN key, Bark key or GitHub token
/api/settings strips these (settingsState), but /api/state, which the
window polls after nearly every click, sent settings.Shared() whole, so
every saved proxy's password and key, the old LAN key, the Bark key and
the GitHub token went with it. /api/state?last=1 (the start's read of
the last state) put settings.Load() in whole too. Both now go through
stateSettings. The page reads none of these from the state (grep of
assets: no .lanKey, .barkKey or .githubToken, and saved proxies come
from /api/settings). The kept state file never held settings
(keptFile empties them), so nothing on disk needs rewriting.

Found while writing the saved-proxy Test: a probe of /api/state carried
a saved proxy's password.

TestLastKnownState used the GitHub token as its mark of today's
settings; the theme is that mark now, and the test asks that the token
is not sent.

Run (at this head's tree):
- mote: go test ./internal/gui -count=1 -timeout 45m: ok (182.9s).
- mote: TestStateSendsNoCredentials and TestLastKnownState -race ok.
- Broken by hand: stateSettings' strip commented out, TestStateSends
  NoCredentials red on all four secrets in /api/state and ?last=1.
- mote: Linux and Windows go vet ./... ok; gofmt -l clean.
2026-11-18 06:42:01 +08:00

52 lines
1.8 KiB
Go

package gui
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// /api/state's settings, polled after nearly every click, carry none of
// the credentials /api/settings keeps on disk: a saved proxy's password,
// the old LAN key, the Bark key and the GitHub token. Neither does the
// last state served at start (?last=1), which has today's settings.
func TestStateSendsNoCredentials(t *testing.T) {
sandboxHome(t)
const pass, lan, bark, gh = "hunter2-proxy-pass", "sk-magpie-statelanstatelanstatelan0123", "bark-state-key-0123", "ghp_statetokenstatetoken0123"
h := Handler(nil, nil)
call := func(method, path, body string) string {
t.Helper()
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
if rec.Code != 200 {
t.Fatalf("%s %d %s", path, rec.Code, rec.Body)
}
return rec.Body.String()
}
call("POST", "/api/proxies/save", `{"name":"Office","url":"http://10.0.0.1:8080","user":"me","pass":"`+pass+`"}`)
s := settings.Load()
if len(s.Proxies) != 1 || s.Proxies[0].Pass != pass {
t.Fatalf("the proxy wasn't saved: %+v", s.Proxies)
}
s.LANKey, s.BarkKey, s.GitHubToken = lan, bark, gh
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
for _, path := range []string{"/api/state", "/api/state?last=1"} {
body := call("GET", path, "")
if path == "/api/state?last=1" && body == "null\n" {
t.Fatal("no state was kept for the next start")
}
for _, secret := range []string{pass, lan, bark, gh} {
if strings.Contains(body, secret) {
t.Errorf("%s carries %q", path, secret)
}
}
}
if s := settings.Load(); s.Proxies[0].Pass != pass || s.LANKey != lan || s.BarkKey != bark || s.GitHubToken != gh {
t.Fatal("the credentials on disk were changed")
}
}