Files
magpie/internal/settings/loadchecked_test.go
T
yetone 7d9382db23 gateway, settings: an unreadable settings.json keeps the allow list read last, or lets in only this computer, never every address
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.
2026-10-12 23:38:50 +08:00

91 lines
2.6 KiB
Go

package settings
import (
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
"github.com/yetone/magpie/internal/lastgood"
)
// LoadChecked tells settings that aren't there (no error) from settings
// nobody knows (an error): a file that can't be read, or doesn't parse
// with no last good generation. Load still reads the defaults for those,
// and Save refuses the same files it reports.
func TestLoadChecked(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
lastgood.Reset()
t.Cleanup(lastgood.Reset)
if s, err := LoadChecked(); err != nil || len(s.AllowIPs) != 0 {
t.Fatalf("no file: %v %v", s.AllowIPs, err)
}
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), []byte(" \n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := LoadChecked(); err != nil {
t.Fatalf("an empty file: %v", err)
}
os.Remove(Path())
for _, list := range [][]string{{"10.0.0.0/8"}, {"198.51.100.7"}} {
if err := Save(Settings{AllowIPs: list}); err != nil {
t.Fatal(err)
}
}
if s, err := LoadChecked(); err != nil || len(s.AllowIPs) != 1 || s.AllowIPs[0] != "198.51.100.7" {
t.Fatalf("a good file: %v %v", s.AllowIPs, err)
}
// garbled, with its last good generation: that is read, no error
garbled := []byte(`{"allowIPs": ["198.51.100.7"`)
if err := os.WriteFile(Path(), garbled, 0o600); err != nil {
t.Fatal(err)
}
if s, err := LoadChecked(); err != nil || len(s.AllowIPs) != 1 || s.AllowIPs[0] != "10.0.0.0/8" {
t.Fatalf("garbled with a backup: %v %v", s.AllowIPs, err)
}
// and without one: unknown, and never saved over
if err := os.Remove(lastgood.Bak(Path())); err != nil {
t.Fatal(err)
}
if s, err := LoadChecked(); err == nil {
t.Fatalf("garbled with no backup read as good: %v", s.AllowIPs)
}
if s := Load(); !reflect.DeepEqual(s, Settings{}.normal()) {
t.Fatalf("Load of a garbled file changed: %+v", s)
}
if err := Save(Load()); err == nil {
t.Fatal("Save wrote the defaults over a garbled settings.json")
}
if b, _ := os.ReadFile(Path()); string(b) != string(garbled) {
t.Fatalf("the garbled file was written over: %q", b)
}
// one that can't be read at all
if runtime.GOOS == "windows" {
return
}
if err := os.WriteFile(Path(), []byte(`{"allowIPs":["10.0.0.0/8"]}`), 0o200); err != nil {
t.Fatal(err)
}
if err := os.Chmod(Path(), 0o200); err != nil {
t.Fatal(err)
}
if _, err := os.ReadFile(Path()); err == nil {
t.Skip("this process can read a write-only file")
}
if _, err := LoadChecked(); err == nil {
t.Fatal("an unreadable file read as good")
}
}