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.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3378,8 +3378,17 @@ func held(h http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// stateSettings is s without the credentials Settings' own read keeps on
|
||||
// disk (settingsState), for /api/state, which is polled, and the start's
|
||||
// read of the last one (lastKnown), which puts today's settings in it
|
||||
func stateSettings(s settings.Settings) settings.Settings {
|
||||
s.Proxies, s.LANKey, s.BarkKey, s.GitHubToken = nil, "", "", ""
|
||||
return s
|
||||
}
|
||||
|
||||
func state() stateJSON {
|
||||
s := stateJSON{Agents: []agentJSON{}, Profiles: []profileJSON{}, Catalog: catalog.Source(), Settings: settings.Shared()} // only encoded, never changed
|
||||
s.Settings = stateSettings(s.Settings)
|
||||
// state() is asked for after nearly every click, so the rate — a
|
||||
// network fetch once every TTL — is only worth its rare latency when
|
||||
// cny is actually chosen; usd never looks at it
|
||||
|
||||
@@ -130,7 +130,7 @@ func lastKnown() (stateJSON, bool) {
|
||||
if len(b) == 0 || json.Unmarshal(b, &f) != nil || f.Version != Version || f.State.Agents == nil {
|
||||
return stateJSON{}, false
|
||||
}
|
||||
f.State.Settings = settings.Load()
|
||||
f.State.Settings = stateSettings(settings.Load())
|
||||
f.State.Last = true
|
||||
return f.State, true
|
||||
}
|
||||
|
||||
@@ -82,8 +82,11 @@ func TestLastKnownState(t *testing.T) {
|
||||
if !last.Last || !hasCodex(last) {
|
||||
t.Fatalf("last-known state: last=%v codex=%v", last.Last, hasCodex(last))
|
||||
}
|
||||
if last.Settings.Theme != "dark" || last.Settings.GitHubToken != "ghp_secret1519" {
|
||||
t.Errorf("last-known state's settings are not today's: theme %q token %q", last.Settings.Theme, last.Settings.GitHubToken)
|
||||
if last.Settings.Theme != "dark" {
|
||||
t.Errorf("last-known state's settings are not today's: theme %q", last.Settings.Theme)
|
||||
}
|
||||
if last.Settings.GitHubToken != "" {
|
||||
t.Error("the last-known state sends the GitHub token")
|
||||
}
|
||||
|
||||
// another version's is not drawn
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user