Follow-up to 7a9490638. AllowIPs read settings.Load().AllowIPs, and Load
returns the defaults when settings.json can't be read or doesn't parse with
no good backup, so for those requests the allow list read as empty and
every address was let in (fail-open). A failed read is unknown, never
empty (LESSONS.md).
- settings: LoadChecked() returns Load's settings and an error when the
file is there but can't be read, or doesn't parse with no last good
generation, the same cases Save already refuses. No file, or an empty
one, is not an error. Load is unchanged and now goes through the same
reader.
- gateway: the Settings list is read with LoadChecked. On an unknown read
the list read last in this process stays in force; with none read yet,
only loopback and this computer's own addresses pass, and the 403 says
settings.json in <dir> couldn't be read. Logged once until a read
succeeds. MAGPIE_ALLOW_IPS is unaffected. AllowIPs returns the list in
force (the last one read on an unknown read).
- gui: POST /api/settings and POST /api/settings/allowips read with
LoadChecked and refuse on an unknown read. settings.Save already
refused to write over such a file, so this didn't write an empty list
before either; the handlers now refuse before building on defaults.
- docs: reference.md and gateway-routing.md say so.
Run (darwin, sandbox HOME/XDG via the tests' own setup, private GOCACHE):
- gofmt -l on the changed packages: empty.
- go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...: clean.
- go test -count=1 ./internal/settings ./internal/gui ./internal/gateway: ok.
- Broken by hand: allowList reading settings.Load() with no error makes
TestAllowListUnreadableSettings fail (garbled after a read: 203.0.113.9
got 200, want 403; never read: remote got 200, want 403 saying
settings.json). LoadChecked never erroring on a parse failure makes
TestLoadChecked fail ("garbled with no backup read as good") and the
gateway test fail at its precondition.
- TestAllowIPsUnreadableSettingsNotSavedOver passes on the old GUI code
too (Save refused already); it guards that path.
Not run: the Windows box, Linux tests, the GUI Playwright suite (no
assets changed), the full go test ./... . Not covered: an empty
settings.json with no .bak still reads as no list, matching Save, so a
file truncated to zero bytes before any second save opens a freshly
started magpie.
102 lines
4.6 KiB
Go
102 lines
4.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The allow list (lc on Discord) is saved cleaned; an entry that isn't an
|
|
// address, a list that would shut out the page saving it, and any list
|
|
// while MAGPIE_ALLOW_IPS sets it are refused with the list left as it
|
|
// was; a save of the rest of the Settings page leaves it alone; the page
|
|
// is told what the variable sets.
|
|
func TestAllowIPsSetting(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 := Handler(nil, nil)
|
|
post := func(path, body, from string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
r.RemoteAddr = from
|
|
srv.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
here := "127.0.0.1:50000"
|
|
rec := post("/api/settings/allowips", `{"ips":[" 203.0.113.7 ","10.1.2.3/8","2001:DB8::/32","203.0.113.7/32"]}`, here)
|
|
want := []string{"203.0.113.7", "10.0.0.0/8", "2001:db8::/32"}
|
|
if rec.Code != http.StatusOK || !slices.Equal(settings.Load().AllowIPs, want) || !strings.Contains(rec.Body.String(), `"allowIPs":["203.0.113.7","10.0.0.0/8","2001:db8::/32"]`) {
|
|
t.Fatalf("%d %s, saved %v", rec.Code, rec.Body, settings.Load().AllowIPs)
|
|
}
|
|
if rec := post("/api/settings/allowips", `{"ips":["my-laptop"]}`, here); rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), "is not an IP address") || !slices.Equal(settings.Load().AllowIPs, want) {
|
|
t.Errorf("not an address: %d %s, saved %v", rec.Code, rec.Body, settings.Load().AllowIPs)
|
|
}
|
|
// magpie web opened from 203.0.113.7: a list without it would shut it out
|
|
if rec := post("/api/settings/allowips", `{"ips":["10.0.0.0/8"]}`, "203.0.113.7:50000"); rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), "shut out 203.0.113.7") || !slices.Equal(settings.Load().AllowIPs, want) {
|
|
t.Errorf("shut out: %d %s, saved %v", rec.Code, rec.Body, settings.Load().AllowIPs)
|
|
}
|
|
if rec := post("/api/settings", `{"theme":"dark","lang":"en"}`, here); rec.Code != http.StatusOK || !slices.Equal(settings.Load().AllowIPs, want) {
|
|
t.Fatalf("the Settings page's save changed the list: %d %v", rec.Code, settings.Load().AllowIPs)
|
|
}
|
|
|
|
t.Setenv(gateway.AllowIPsEnv, "1.2.3.4, 10.0.0.0/8")
|
|
if rec := post("/api/settings/allowips", `{"ips":[]}`, here); rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), "MAGPIE_ALLOW_IPS") || !slices.Equal(settings.Load().AllowIPs, want) {
|
|
t.Errorf("under the variable: %d %s, saved %v", rec.Code, rec.Body, settings.Load().AllowIPs)
|
|
}
|
|
st := settingsState()
|
|
if st.AllowIPsEnv != "1.2.3.4, 10.0.0.0/8" || !slices.Equal(st.AllowIPsFromEnv, []string{"1.2.3.4", "10.0.0.0/8"}) {
|
|
t.Errorf("state: %q %v", st.AllowIPsEnv, st.AllowIPsFromEnv)
|
|
}
|
|
|
|
t.Setenv(gateway.AllowIPsEnv, "")
|
|
if rec := post("/api/settings/allowips", `{"ips":[]}`, here); rec.Code != http.StatusOK || len(settings.Load().AllowIPs) != 0 {
|
|
t.Fatalf("emptied: %d %v", rec.Code, settings.Load().AllowIPs)
|
|
}
|
|
}
|
|
|
|
// settings.json that doesn't parse, with no last good generation, holds an
|
|
// allow list nobody knows: neither the Settings page's save nor the allow
|
|
// list's own writes the defaults (no list, every address let in) over it.
|
|
func TestAllowIPsUnreadableSettingsNotSavedOver(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 := Handler(nil, nil)
|
|
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
garbled := []byte(`{"allowIPs": ["203.0.113.7"`)
|
|
if err := os.WriteFile(settings.Path(), garbled, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, c := range []struct{ path, body string }{
|
|
{"/api/settings", `{"theme":"dark","lang":"en"}`},
|
|
{"/api/settings/allowips", `{"ips":["10.0.0.0/8"]}`},
|
|
} {
|
|
rec := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", c.path, strings.NewReader(c.body))
|
|
r.RemoteAddr = "127.0.0.1:50000"
|
|
srv.ServeHTTP(rec, r)
|
|
if rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), "could not read settings") {
|
|
t.Errorf("%s: %d %s, want refused", c.path, rec.Code, rec.Body)
|
|
}
|
|
if b, _ := os.ReadFile(settings.Path()); string(b) != string(garbled) {
|
|
t.Fatalf("%s wrote over the unreadable settings: %q", c.path, b)
|
|
}
|
|
}
|
|
}
|