Steven's ESP32 desk display polls magpie (Docker, on his LAN) every 15s on GET /v1/magpie/quotas and /v1/magpie/usage and never sends a model request. Only a key held to nothing was told the usage summary, so the key kept in the device's flash had to be one that could spend every account. A gateway key can now be read-only (access.Key.ReadOnly): - `magpie gateway-key add <name> --read-only`, `magpie gateway-key access <id> [read-only|full]` (`gateway-key --help` lists it; `list` has an ACCESS column), and "Make read-only" / "Give full access" in the key's menu in Gateway -> Gateway keys, with a "Read only" tag on its row. - readOnlyGuard (internal/gateway/readonly.go), after callerGuard and before withCaller/keyLimited, judges the pattern ServeMux.Handler routes the request to. A read-only key is answered GET /, /v1, the quotas, their history and cards, the usage summary and its requests, its limit and the concurrency. Every other route is refused with 403 gateway_key_read_only: every model route, count_tokens, the Responses WebSocket's upgrade, every MCP server, Codex's and Gemini's backends, /v1/models and the Ollama routes, the catch-all, the trace and route, debug profiles, and every write (usage ingest, quotas refresh). It is default deny, so a route added later is refused until it is classified. - The key is held to nothing else, so heldKey leaves it alone: it reads the usage summary and every account's quotas, as a key held to nothing does. None of those routes holds a key's secret. - The sharing key can't be made read-only, since magpie gives it to its own agents (LANSecret). Update refuses it, and the GUI item is off with why. Older versions: a magpie from before this commit doesn't know `readOnly` and treats such a key as a full one, so downgrading magpie widens the key. Tests: - TestReadOnlyKeyEveryRoute (internal/gateway/readonly_test.go) takes the patterns from the router itself (routes() records each HandleFunc). It fails when a pattern is in neither readOnlyRoutes nor its refused list. It sends every refused pattern, by each method it takes, from 192.168.1.9 and from loopback, plus a WebSocket upgrade and an unclean chat path, and expects 403 gateway_key_read_only with the upstream never reached. It checks that every allowed route answers, that no body holds any key's secret or the upstream key, that the usage summary names the other key's use, and that a full key and a key made full again still reach the model. - TestReadOnlyKey (access) covers add-key and access-key on and off, Authenticate, List, and the sharing key refused. - TestGatewayKeyCLIReadOnly covers --read-only before and after the name and beside --key, access, the list column, bad values, and --help. - gateway-key-readonly.test.cjs runs Chromium and WebKit, 1100 and 420px, in en/zh/zh-TW/ja/de. It covers the tag, both menu items and what they post, the status line, the sharing key's item off with its why, no scroll on click, no sideways scroll, no select, and no left stripe. gateway-card.test.cjs now expects the menu's 4th item. - TestClientPathCoversHandler takes the patterns from routes() instead of parsing Handler's source, which moved into routes(). Broken by hand: - readOnlyGuard passing everything: TestReadOnlyKeyEveryRoute red (103 lines, /v1/models 200 onward). - A new `POST /v1/new-thing` route in routes(): red, "in neither readOnlyRoutes nor readOnlyRefused". - The GUI item not off for the sharing key: gateway-key-readonly red 10 of 10 in Chromium. Run at the pushed head (on origin/main0911a65e7), on mote: TestReadOnly*, TestHeldKey*, TestClientPathCoversHandler, TestGatewayKey*, TestCaller* in access, cli and gateway ok; TestCoreScriptBudget ok; on Linux (wsl-run, both engines) gateway-key-readonly, gateway-card, i18n-keys, gateway-caller-keys and gateway-key-quotas 68/68. Full Go suite on mote (macOS) at the same change on origin/maindca3e2d71(94630237d), -timeout 45m: 78 packages ok; internal/gateway red only on the four TestAntigravityTurned* and TestCodexSpentGrokFailingIsTold (red the same at origin/main, #4362); internal/gui red only on ten AppKit window tests that hit their 30s bar while mote's launchd throttled every app launch (#4125). Run on mote (macOS) on origin/maindca3e2d71: - go test ./internal/access ./internal/cli ./internal/gateway: access ok; cli red only on TestLibraryMCPHub's TempDir cleanup (#4140); gateway red only on the four TestAntigravityTurned* and TestCodexSpentGrokFailingIsTold, red the same at origin/main (#4362). TestReadOnly*, TestHeldKey*, TestClientPathCoversHandler, TestGatewayKeyCLIReadOnly pass. - TestCoreScriptBudget ok (no budget raised). gofmt clean; go vet darwin (access, cli, gateway), GOOS=windows ./... and GOOS=linux -tags nogui ./... clean. - GUI, earlier at807048470on mote: gateway-key-readonly, gateway-key-quotas, i18n-keys, gateway-card, api-key-theme, gateway-connect-keys and gateway-caller-keys 34/34 in both engines. At this tree mote's WebKit hung at launch for every test (#4125), so the 43 gateway-*/key-*/api-key-*/caller-*/menu-*/i18n-* files ran on Linux (wsl-run, both engines): 501 of 514. The reds in api-key-theme, gateway-key-limit, gateway-key-models, key-replace and menu-input-scroll are red the same at origin/maine8d42c02d(#4120, #3523, #4081, #4126, #3402); gateway-keys-empty's WebKit wait was red 1 in ~13 runs here and 0 in 4 at main, filed as #4383 (this change doesn't touch its path). - Full Go suite on Linux (wsl-run, at807048470with this change): reds only where Linux can't build the GUI (no gtk4/webkitgtk) and in internal/agent's install tests (#4033) and the gateway reds above, each red the same at origin/main dca3e2d71; plus TestClientPathCoversHandler, which parsed Handler's source and is now read from routes() (fixed here). Not tried: a real ESP32 or Docker magpie; a remote client against a running gateway (only httptest through lanGuard/Handler); the Windows test runs (vet only); the whole GUI suite (mote's WebKit hung, #4125).
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// gateway-key add --read-only and gateway-key access (Steven on Discord: a
|
|
// desk display's key reads stats and uses no model).
|
|
func TestGatewayKeyCLIReadOnly(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) string {
|
|
t.Helper()
|
|
var out bytes.Buffer
|
|
if err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...)); err != nil {
|
|
t.Fatal(args, err)
|
|
}
|
|
return strings.TrimSpace(out.String())
|
|
}
|
|
display := call("add", "Desk display", "--read-only")
|
|
laptop := call("add", "--read-only", "Laptop", "--key", "cpa-laptop-key-0042")
|
|
if laptop != "cpa-laptop-key-0042" {
|
|
t.Fatal("--read-only beside --key:", laptop)
|
|
}
|
|
for _, s := range []string{display, laptop} {
|
|
if who, ok := access.Authenticate(s); !ok || !who.ReadOnly {
|
|
t.Fatal("added read-only:", who, ok)
|
|
}
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[1].ID
|
|
if got := call("list"); !strings.Contains(got, "ACCESS") || strings.Count(got, "read-only") != 2 {
|
|
t.Fatal("list:", got)
|
|
}
|
|
if got := call("access", id); !strings.Contains(got, "Laptop: read-only") {
|
|
t.Fatal("access:", got)
|
|
}
|
|
if got := call("access", id, "full"); !strings.Contains(got, "Laptop: full access") {
|
|
t.Fatal("access full:", got)
|
|
}
|
|
if who, _ := access.Authenticate(laptop); who.ReadOnly {
|
|
t.Fatal("still read-only:", who)
|
|
}
|
|
if got := call("list"); strings.Count(got, "read-only") != 1 || !strings.Contains(got, "full") {
|
|
t.Fatal("list after:", got)
|
|
}
|
|
call("access", id, "read-only")
|
|
if who, _ := access.Authenticate(laptop); !who.ReadOnly {
|
|
t.Fatal("not read-only:", who)
|
|
}
|
|
for _, args := range [][]string{{"access"}, {"access", id, "write"}, {"access", id, "full", "x"}, {"access", "missing", "full"}} {
|
|
if err := gatewayKeysTo(&bytes.Buffer{}, append([]string{"gateway-key"}, args...)); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
help := helpFor([]string{"gateway-key", "--help"})
|
|
if !strings.Contains(help, "--read-only") || !strings.Contains(help, "gateway-key access <id> [read-only|full]") {
|
|
t.Fatal("help:", help)
|
|
}
|
|
}
|