prgrmrwy runs magpie on a home server and uses it from work through a remote magpie (and DSH). The remote Codex card said "1 reset", but the reset could only be used from the GUI of the computer that holds the sign-in. What changed: - Gateway: POST /v1/magpie/quotas/codex-reset?user=<email>[&credit=<id>] (Server.codexReset) spends one of that account's resets with provider.SpendCodexReset. That is the Usage page's own code (UseCodexResetCard, then AwaitAllowance), moved out of internal/gui/usage.go so both use it. A reset can't be undone, so the route needs an enabled gateway key with full access, from loopback too. No key gets 401, and that covers every corsGuard exemption that has no key: Origin null, app schemes, *.localhost and a missing Origin. A read-only key gets 403 gateway_key_read_only (readOnlyGuard, which readOnlyRefused now lists, and again in the handler). A held key gets 403 gateway_key_held, even one told quotas. The account must be named (400 otherwise). One ask runs per account at a time (409). When the reset is used, that card is read again. - Remote client: UseRemoteCodexReset (remote_quotas.go) asks the remote with the provider's key. remoteJSON's status mapping is shared through remoteBody. A read-only key there is told as such, and a 404 as a remote too old. With no answer in time it says the reset may have been used, and it never asks again by itself. A remote Codex card now keeps its Resets; a GLM team's (byWindow) and every check-in are still dropped. - GUI: "Use a reset" / "Use one..." show on a remote Codex card (codexResetCard). They open the same in-app confirmation sheet, with a line saying which computer's magpie uses it, and send the card's provider to /api/usage/codex-reset. The remote's errors are put in the user's words. New strings are in all 5 languages. - DSH: no code here. Its provider plugin reads /v1/magpie/quotas/cards and can call the new route with a full key. - resetOutcome (a reset's result in words) moved from app.js into resetask.js, the only place that says it, so the core's scripts stay in TestCoreScriptBudget's 1,533,802 with no raise (1,533,003 after; origin/mainb967b1620is 1,533,756). - Docs: reference.md (remote quotas, gateway keys), gateway-routing.md (read-only list, the route's admission), providers-accounts.md. What was run (rebased on origin/mainb967b1620, then on2e4735f8f): - mote, at the head before the rebase and the resetOutcome move: go test -count=1 -timeout 45m ./internal/provider ./internal/gateway ./internal/gui at load ~300-450. provider ok. gateway: only TestCompactionOverByteCapEveryTime (#4862). gui: TestCoreScriptBudget (this change, 158 bytes over; fixed by the move), TestMainWindowCorners (#3949) and TestGatewaySkewOfAnotherIsSaidNotQuit/own_build (#5053, passes alone here and atb967b1620). - mote, same run: 155 GUI files (reset|quota|usage-|panel-|i18n|budget| remote|gui-|old-webkit|core-script|script-size), 1856 of 1904 pass. - WSL (magpie-desktop), the 9 red files alone, at this change and at origin/mainb967b1620: this change 147 pass / 43 fail, main 146 / 44; every test red here is red on main too: codex-auto-reset webkit de (#4790), remote-magpie (#4398), usage-agents-hide (#3414), usage-fold (#3415, #4790), usage-heatmap (#4718), usage-ledger (#5048). codex-reset-lead (#5054) and panel-routing-providers (#5055) were red only under mote's load and pass alone on both. - mote, at this head: TestCoreScriptBudget and TestGatewaySkewOfAnotherIsSaidNotQuit ok. - WSL, at this head: account-every-reset, codex-resets-used, i18n-keys, providers-reset-use, remote-codex-reset, reset-each-title, reset-pick-card, reset-providers, quota-used-no-reset: 99 of 102 pass. The 3 reds are providers-reset-use's 420px Windows cases ("a click moved the page", #4873); that file alone is red on origin/mainb967b1620too (3 of 40 there, 2 of 40 here). - WSL, rebased on2e4735f8f(main had changed internal/gateway): the new tests and the read-only, CORS, LAN-guard, held-key, remote-magpie and UseCodexResetCard tests in ./internal/gateway ./internal/provider ok; core scripts 1,532,960 under 1,533,802; gofmt clean. - Earlier on mote: TestCodexResetNeedsAFullKey, TestRemoteCodexResetThroughTheGateway, TestRemoteCodexResetAnswers, the read-only, CORS, LAN-guard, held-key and remote-magpie tests ok; remote-codex-reset and reset-pick-card 20 of 20 (chromium and webkit, 5 languages, 900/390/440px). gofmt clean; go vet darwin, windows ok, linux for provider and gateway. Broken by hand: - With the handler's key check turned off, TestCodexResetNeedsAFullKey went red: "this computer, no key: 200 {code: reset}" and a reset was spent. - With codexResetCard put back to provider === "codex", remote-codex-reset.test.cjs went red ("one Use, on the remote Codex card"). On the old code the route doesn't exist, so the full-key case gets 404. Not tried: - A real Codex reset spent between two real magpies: none was spent, to keep the owner's resets. The ChatGPT backend is the fake from codex_autoreset_test. - DSH's plugin calling the route. - GOOS=linux vet of internal/gui: it needs gtk4/webkitgtk, which neither mote nor magpie-desktop has. Its change is usage.go only, and that was vetted on darwin and windows. Thanks to prgrmrwy for the report and the proposal.
79 lines
3.4 KiB
Go
79 lines
3.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A read-only gateway key (access.Key.ReadOnly) reads this magpie's stats
|
|
// and uses nothing (Steven on Discord: an ESP32 desk display polling
|
|
// /v1/magpie/quotas and /v1/magpie/usage every 15s, its key kept in the
|
|
// device's flash, which had to be a key that could spend every account).
|
|
// It is answered the routes below alone, by the pattern the request is
|
|
// routed to, so a route added later is refused it until it is put here:
|
|
// every model route, count_tokens, a WebSocket's upgrade, every MCP
|
|
// server, a plugin's backend and every write are refused, whatever
|
|
// method or path spelling reaches them. TestReadOnlyKeyEveryRoute fails
|
|
// when a route is neither here nor in its list of refused ones.
|
|
//
|
|
// What it reads is what a key held to nothing reads: the usage summary,
|
|
// every account's quotas (it is no one else's to spend, so heldKey
|
|
// leaves it alone unless it is held to a budget, models or accounts as
|
|
// well) and the routing as it happens, redacted (route_events.go,
|
|
// joe21_eee's display, #3387). None of these routes tells a gateway
|
|
// key's secret.
|
|
var readOnlyRoutes = map[string]bool{
|
|
"GET /{$}": true,
|
|
"GET /v1": true,
|
|
"GET /v1/{$}": true,
|
|
"GET /v1/magpie/quotas": true,
|
|
"GET /v1/magpie/quotas/history": true,
|
|
"GET " + provider.RemoteCardsPath: true,
|
|
"GET /v1/magpie/usage": true,
|
|
"GET /v1/magpie/usage/requests": true,
|
|
"GET /v1/magpie/limit": true,
|
|
"GET /v1/magpie/concurrency": true,
|
|
"GET " + routeSnapshotPath: true,
|
|
"GET " + routeEventsPath: true,
|
|
"GET " + OwnMCPPath: true,
|
|
"GET " + KeyPagePath: true,
|
|
"GET " + keyPageData: true,
|
|
}
|
|
|
|
// ReadOnlyCode is the error code a read-only key is refused a route with.
|
|
const ReadOnlyCode = provider.KeyReadOnlyCode
|
|
|
|
// routeMux is a ServeMux that keeps the patterns it was given.
|
|
type routeMux struct {
|
|
*http.ServeMux
|
|
patterns []string
|
|
}
|
|
|
|
func (m *routeMux) HandleFunc(pattern string, h func(http.ResponseWriter, *http.Request)) {
|
|
m.patterns = append(m.patterns, pattern)
|
|
m.ServeMux.HandleFunc(pattern, h)
|
|
}
|
|
|
|
// readOnlyGuard refuses a read-only key every route of mux but
|
|
// readOnlyRoutes, before anything is read, reserved or sent.
|
|
func readOnlyGuard(mux *routeMux, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
who := access.Caller(r.Context())
|
|
if who.ReadOnly {
|
|
// a path that isn't clean is routed to a redirect under the
|
|
// pattern of where it goes, and judged again when it gets there
|
|
if _, pattern := mux.Handler(r); !readOnlyRoutes[pattern] {
|
|
log.Printf("refused %s %s: gateway key %q is read-only", r.Method, r.URL.Path, who.KeyName)
|
|
writeJSON(w, http.StatusForbidden, map[string]any{"error": map[string]any{
|
|
"message": "this gateway key is read-only: it may read magpie's quotas and usage (GET /v1/magpie/quotas, /v1/magpie/quotas/history, /v1/magpie/usage, /v1/magpie/usage/requests, /v1/magpie/limit, /v1/magpie/route-events) and use no model, MCP server or write. Use a key with full access for that (Gateway → Gateway keys, or magpie gateway-key access <id> full)",
|
|
"type": "permission_error", "code": ReadOnlyCode}})
|
|
return
|
|
}
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|