A sandboxed iframe, or a page opened from file://, sends `Origin: null`.
foreignPage judges only http and https origins, so corsGuard let "null"
through without a key. A simple text/plain POST from such a page (no
preflight) reached /v1/chat/completions and spent the user's
subscription. corsGuard is where every route's same-origin is decided,
so the fix goes there: a request with Origin: null is answered only
when it carries a working gateway key (keyedCaller, which the listed-
origin 401 path now uses too). It gets 403 and a message that says how
to send a key. App schemes (app://…) and requests with no Origin are
unchanged.
The patch is JasonBuildAI's fe11a8abd (fork branch
fix/null-origin-keyless-spend-a13b21). docs/subsystems/gateway-sessions.md
also says that a null Origin needs a key.
What was run, at this commit's parent 8e8316ed2 vs. this tree:
- Repro with a sandbox HOME, a fake upstream on 127.0.0.1:3672 that logs
each hit, and `magpie serve` on 127.0.0.1:3671. On main, curl POST
with Origin: null got 200 and reached the vendor. A sandboxed srcdoc
iframe in a file:// page, in Playwright WebKit and Chromium, reached
it too (2 hits). Fixed: null POST and GET /v1/models got 403,
app://cherry and no-Origin got 200, and both browsers were refused
with 0 upstream hits.
- With main's cors.go and the new tests, TestForeignPagesRefused and
TestOpaqueOriginNeedsAKey FAIL ("a sandboxed frame with no key reached
the vendor 5 times"). With the fix both pass.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui and GOOS=windows
clean.
- HOME=<scratch> go test -tags nogui ./...: every package ok except
internal/gateway and internal/agent. Those hit the 10m timeout at load
average 76-91: the running tests were 14s and 23s old, not hung. Rerun
with -timeout 40m: gateway ok (724s), agent ok (620s).
Co-authored-by: JasonBuildAI <jasonbuildai@gmail.com>