gateway: a sandboxed frame's Origin: null spends the user's subscriptions with no gateway key
What's wrong
corsGuard (internal/gateway/cors.go) refuses a request whose Origin is an http(s) page that isn't listed in Settings › Network › Web pages, because on loopback a call needs no key and the body's Content-Type isn't checked: "a page the user merely visits could send a 'simple' text/plain POST and spend their subscriptions blind" (d5468283, 2026-10-07). null was exempted together with the app schemes (app://, file://, tauri://, vscode-webview://, chrome-extension://), on the reasoning that "no web page can send those origins" (same commit).
That reasoning is false for null: a sandboxed iframe sends Origin: null on any web page's behalf — <iframe sandbox srcdoc="…"> is the whole attack, one element on a page the user visits. The call is a "simple" text/plain POST, so no preflight is sent, and on loopback the gateway asks no key. The call is answered and the user's subscription is spent, unread. A file:// page sends null too.
Why this is exceptionally important
- Who pays: every magpie user whose gateway is running (the default,
127.0.0.1:3425, keyless from this computer). An attacker spends the user's paid Claude / ChatGPT / other subscription quota, blind (the frame cannot read the reply), under the user's own accounts, with nothing in magpie's UI saying anything happened. - Reachable today, in real browsers, by default. Measured 2026-10-15 with Playwright and default settings (no permission granted), from a public
https://…lhr.lifepage whose frames POST to a recording server on loopback:- WebKit 27.2 (Safari's engine) delivered the keyless POST with
Origin: null, no preflight, from a sandboxedsrcdocframe; the server answered 200. That is the attack against the gateway, verbatim. - Chromium 156 and Firefox 157 refuse the cross-site request themselves — Local Network Access, a new browser feature: it is not in Safari, it can be granted by the user, and it is missing from older Chrome/Firefox builds in the installed base.
- A
file://page delivered the same POST in all three engines (an HTML file from Downloads or a mail attachment; every browser, no permission).
- WebKit 27.2 (Safari's engine) delivered the keyless POST with
- Scale: 12,661 stars / 863 forks; the current release (v0.1.1240, 2026-10-14 17:03 UTC) has ~1,000 asset downloads in its first hours; macOS is a primary platform (Homebrew), where Safari is the default browser. The hole is in every build since 2026-10-07 (d5468283, first tag v0.1.1096; 144 tags contain it).
- The maintainer's own rule: the commit that added the refusal says such a page never read a reply (no CORS headers), so refusing it costs nothing that worked; the sibling hole was closed the same way on 2026-10-10 (c431225a: a rebinding page needs an enabled gateway key), whose message records the nightly security review reproducing keyless spend on
POST /v1/chat/completions. This is the residual gap of that same series (#1051 → d5468283 → c431225a). - It stays until fixed: no release changes it, and every model or agent pointed at the loopback gateway makes each blind request cost more.
Minimal reproduction (main at 27a0458d)
magpie running, a provider or a signed-in subscription, nothing listed in Settings › Web pages. In WebKit/Safari, load this page — or open a local copy of it as a file in any browser:
<iframe sandbox="allow-scripts" srcdoc="<script>
fetch('http://127.0.0.1:3425/v1/chat/completions',{method:'POST',mode:'no-cors',
headers:{'Content-Type':'text/plain'},
body:JSON.stringify({model:'<a model magpie serves>',messages:[{role:'user',content:'spend'}]})})
</script>"></iframe>
This is the frame used in the measurement above, pointed at a recording server instead of the gateway.
Expected: 403, as any unlisted page gets. Actual: the gateway answers and the request reaches the vendor; the user's allowance is spent. No preflight, no key.
The recording server's raw side, WebKit 27.2, public https page, sandboxed srcdoc frame (nothing precedes it: no OPTIONS, no preflight):
=== POST /nc-srcdoc
Host: 127.0.0.1:9433
Accept-Encoding: deflate, gzip, br
Content-Type: text/plain
Accept: */*
Origin: null
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/27.2 Safari/605.1.15
Accept-Language: zh-CN
Content-Length: 2
body: b'nc'
A test shows the same at the code level. TestOpaqueOriginNeedsAKey (added by the fix) fails on main — a no-key Origin: null POST is answered 200 and the fake vendor is reached — and passes with the change:
--- FAIL: TestOpaqueOriginNeedsAKey (2.15s)
cors_test.go:357: POST /v1/chat/completions with no key: 200 {"choices": [{"message": {"role": "assistant", "content": "ok"} …
cors_test.go:357: POST /v1/embeddings with no key: 200 …
cors_test.go:357: GET /v1/models with no key: 200 …
cors_test.go:361: a sandboxed frame with no key reached the vendor 5 times
Suggested fix
Hold null to an enabled gateway key, as a listed page is, and answer 403 without one. A scheme only an app or an extension can send (app://, tauri://, vscode-webview://, chrome-extension://) and a request with no Origin (every agent's) stay keyless. A file:// page asks for a key too: it sends null, and nothing in the request tells it apart from a sandboxed frame.
The fix, since this account cannot open a pull request on this repo (GitHub refuses the call: "JasonBuildAI does not have the correct permissions to execute CreatePullRequest"; a REST POST /repos/yetone/magpie/pulls answers 404 — the same as #2229 and #2231): branch fix/null-origin-keyless-spend-a13b21 in the fork JasonBuildAI/magpie, one commit on main at 27a0458d:
git fetch https://github.com/JasonBuildAI/magpie fix/null-origin-keyless-spend-a13b21 && git cherry-pick FETCH_HEAD
internal/gateway/cors.go: thenullrefusal andkeyedCaller, which the listed-origin 401 uses too; doc comments.internal/gateway/cors_test.go:TestOpaqueOriginNeedsAKey(five routes refused with no key; keyed access and app schemes answered as before; a turned-off key is no key) and thenullassertions inTestForeignPagesRefused.internal/gateway/usage_ingest.go,internal/gateway/usage_ingest_test.go: comments that named the exemption.docs/subsystems/gateway-routing.md: the HTTP surface row'snullsentence.
Ran on the branch: the new test fails on main and passes with it; go test -tags nogui ./internal/gateway/ ok; gofmt -l on the changed files prints nothing; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... clean; go build ./..., GOOS=linux go build -tags nogui ./..., GOOS=windows go build ./... ok.
AI disclosure: reported with AI assistance at my request, under my direction and review.
The fix is on branch fix/null-origin-keyless-spend-a13b21 in the fork JasonBuildAI/magpie, head fe11a8ab — one commit on main at 27a0458d, 5 files, +128/−17:
git fetch https://github.com/JasonBuildAI/magpie fix/null-origin-keyless-spend-a13b21 && git cherry-pick FETCH_HEAD
Opening a pull request from it is refused for this account, both ways:
gh pr create --repo yetone/magpie --base main --head JasonBuildAI:fix/null-origin-keyless-spend-a13b21→pull request create failed: GraphQL: JasonBuildAI does not have the correct permissions to execute 'CreatePullRequest' (createPullRequest)- REST
POST /repos/yetone/magpie/pullswith the same head →{"message":"Not Found","status":"404"}
(The same as #2229 and #2231.)
Ran on the head, Windows, go1.26.8, main at 27a0458d:
go test -tags nogui -count=1 -v -run 'TestCORS|TestForeignPagesRefused|TestOpaqueOriginNeedsAKey|TestRebound|TestUsageIngest|TestLAN|TestAllow' ./internal/gateway/→ PASS,ok … 30.066s:TestCORSOrigins,TestCORSKeyThroughTheServer,TestForeignPagesRefused,TestOpaqueOriginNeedsAKey,TestCORSExtensionOrigin,TestReboundPageRefused,TestLANGuardand the usage-ingest tests.go test -tags nogui -race -count=5 -run 'TestOpaqueOriginNeedsAKey|TestForeignPagesRefused|TestCORSOrigins|TestUsageIngestAccess' ./internal/gateway/→ok … 96.690s.gofmt -lon the changed files prints nothing;go vet ./...,GOOS=linux go vet -tags nogui ./...,GOOS=windows go vet ./...clean;go build ./...,GOOS=linux go build -tags nogui ./...,GOOS=windows go build ./...ok.- The whole package (
go test -tags nogui ./internal/gateway/) andgo test -tags nogui ./...hit their package timeouts on this box, which runs several agents' Go suites at once (load 100):FAIL github.com/yetone/magpie/internal/gateway 1800.763swith the stack inTestClaudeDesktopAuxiliaryModel(desktop_turn_test.go), and the 40m full run reddenedagent,cli,davsync,gateway,guiandlibraryby timeout — the same six packages that timed out for an unpatchedmainat 87d83e7a in another session here (#2231). The changed code is not on those stacks.
Thanks. Confirmed and fixed in c20bb76c4, which is your patch, with you credited as co-author.
I reproduced it on main before the fix:
- A
curlPOST withOrigin: nullreached the upstream. - So did a sandboxed iframe in a
file://page, in both WebKit and Chromium.
With the fix, corsGuard, which decides same-origin for every route, answers Origin: null with 403 unless the request carries a working gateway key. App schemes and requests with no Origin are unchanged. The new tests fail on the old code.
The fix lands in the next release after v0.1.1231. I'll leave this open until you've checked it.
Update 2026-10-15: rebased onto main 3d3a834d, branch head affa335c.
The one commit now sits on the current main (it was cut from 27a0458d; the files it touches are untouched upstream between the two, and the rebase was clean). Everything re-run on the rebased tree, Windows, go1.26.8:
- With
cors.goswapped back toupstream/main's (go test -overlay, worktree untouched):--- FAIL: TestOpaqueOriginNeedsAKey—POST /v1/chat/completions with no key: 200,a sandboxed frame with no key reached the vendor 5 times;TestForeignPagesRefusedalso fails itsnullassertion. - With the branch:
ok github.com/yetone/magpie/internal/gateway 3.577s; the CORS, allow-list, LAN and usage-ingest tests beside themok ... 12.760s; both under-race -count=5:ok ... 91.571s. gofmt -lon the changed files prints nothing;go vet ./...,GOOS=linux go vet -tags nogui ./...,GOOS=windows go vet ./...clean;go build ./...,GOOS=linux go build -tags nogui ./...,GOOS=windows go build ./...ok.go test -tags nogui ./...still doesn't finish on this box: under parallel load it reaches the 30m package timeout, andTestClaudeAsRootEnvplus a symlink-privilege test fail on Windows alone (both fail without this change too). None of those touch this path.
Two wording fixes from a review pass are folded into the commit: the 403 now names ?key= as well, since a sandboxed frame would have to preflight for Authorization and that preflight is refused too (measured: OPTIONS with Origin: null and Access-Control-Request-Headers: authorization → 403); and docs/subsystems/gateway-sessions.md, which still said corsGuard lets Origin: null through, now matches gateway-routing.md.
No PR, and not because of the code. The README says "This repository doesn't take pull requests. Only maintainers can open them," and that is what the API answers: gh pr create --repo yetone/magpie --base main --head JasonBuildAI:fix/null-origin-keyless-spend-a13b21 → GraphQL: JasonBuildAI does not have the correct permissions to execute CreatePullRequest; POST /repos/yetone/magpie/pulls → 404. So the fix is offered as this issue plus a branch a maintainer can pick up in one step:
git fetch https://github.com/JasonBuildAI/magpie fix/null-origin-keyless-spend-a13b21
git checkout FETCH_HEAD # affa335c, one commit, +131/-19, tests and docs included
the PR body I would have used
Fixes #2242.
Why this matters, up front. A sandboxed iframe on any web page sends Origin: null with a "simple" text/plain POST, and magpie asks no key on loopback — where its GUI runs the gateway by default (127.0.0.1:3425). One frame on a page the user merely visits therefore spends their paid subscriptions blind and unread, billed to their own accounts. Measured on 2026-10-15 with real browsers at default settings, from a public https page: WebKit 27.2 (Safari's engine) delivered the keyless POST with Origin: null, no preflight, and the server answered 200; Chromium 156 and Firefox 157 refuse that cross-site request themselves (Local Network Access, which Safari lacks), and a file:// page delivered it in all three engines. This is the residual gap of the series that closed the same class for unlisted pages and for rebinding pages (#1051 → d5468283 → c431225a): d5468283 counted null among the origins "no web page can send", which is not so.
What changed
internal/gateway/cors.go:corsGuardrefusesOrigin: nullwith 403 unless the call carries an enabled gateway key — the rule a listed page already has — and logs it.keyedCallerholds that predicate, shared with the listed-origin 401.- App-only schemes (
app://,tauri://,vscode-webview://,chrome-extension://) and no-Origin requests (every agent's) are untouched and stay keyless. Afile://page now needs a key: it sendsnull, and nothing in the request tells it apart from a sandboxed frame. internal/gateway/cors_test.go:TestOpaqueOriginNeedsAKey(five route families refused without a key and the fake vendor untouched; a keyed call answered as before with no CORS headers; a turned-off key is no key; app schemes unchanged) and thenullassertions inTestForeignPagesRefused.internal/gateway/usage_ingest.go,usage_ingest_test.go,docs/subsystems/gateway-routing.md,docs/subsystems/gateway-sessions.md: the comments and the two doc lines that named the old exemption.
Verification (Windows, go1.26.8, one commit on main at 3d3a834d): the before/after, -race -count=5 and vet/build output quoted above; the browser probe in the first comment.
AI disclosure: this change was drafted with AI assistance at my request, under my direction and review.
Checked, and closing. c20bb76c4 is on main (82f48f09), Windows box, Go 1.26.8:
> go test -tags nogui -count=1 -run "TestOpaqueOriginNeedsAKey|TestForeignPagesRefused" ./internal/gateway/
ok github.com/yetone/magpie/internal/gateway 2.867s
Both pass with the merged commit; the before/after quoted above (a no-key Origin: null POST answered 200 and reaching the vendor, TestOpaqueOriginNeedsAKey red, TestForeignPagesRefused's null assertion red) was measured with cors.go swapped back to before the change, and the merged fix is the same rule as the branch's, so the caller with no key is refused while a keyed call and the app schemes are unchanged.
One thing crossed in time: the update above (my rebase onto 3d3a834d, branch head affa335c) went up while your merge was landing, so read it as superseded - fix/null-origin-keyless-spend-a13b21 is a duplicate of c20bb76c4 and stays unmerged. Thanks for the co-author credit.
c20bb76c4 is on main but not in v0.1.1242, so this ships in the next release.
AI disclosure: this verification was produced with AI assistance at my request, under my direction and review.