What changed
- lanGuard refuses (403) a loopback request that carries a browser's
marks (Origin, Sec-Fetch-Site/Mode, a Mozilla/ user agent) when its Host
isn't this computer's: an IP, localhost, *.localhost, a container
runtime's name for the host (host.docker.internal and kin),
MAGPIE_PUBLIC_URL's or MAGPIE_ADDR's host, or the machine's own name.
An enabled gateway key still lets it in.
- Why: a page whose DNS answers 127.0.0.1 after it loads is same-origin
with the gateway. Its GET sends no Origin and its POST one equal to the
Host, so corsGuard's foreignPage let it by, and on loopback no key is
asked. The nightly security review reproduced it on origin/main: Host
and Origin rebind.attacker.example:3425 from 127.0.0.1 got 200 on
GET /v1/models, POST /v1/chat/completions (spends subscriptions) and
GET /v1/magpie/quotas (account names, balances). The subsystem doc
listed it as a known gap.
- Agents send none of a browser's marks and pass under any name; browsers
and webviews calling localhost or an IP are unchanged.
- Tests: TestCORSKeyThroughTheServer and the Omarchy test now set a real
Host (httptest's default is example.com). TestForeignPagesRefused's
"own origin behind a port-443 name" row from loopback with no forwarding
header now sends a key: without one it is the rebound page.
- docs/subsystems/gateway-routing.md describes the check in place of the gap.
Ran
- TestReboundPageRefused: fails on origin/main ("a rebound page's GET:
200"), passes with the change.
- go test -count=1 ./internal/gateway/ ./internal/gui/ ./internal/access/
(macOS, go1.27): ok. gofmt -l clean; GOOS=linux and GOOS=windows go vet
./internal/gateway/ clean.
Not tried: a real browser against a real rebinding DNS name.
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// usemagpie.ai on an Omarchy machine asks the magpie there for the theme it
|
|
// draws itself in: only from this machine, only readable by the site.
|
|
func TestOmarchyThemeForTheSite(t *testing.T) {
|
|
fresh(t)
|
|
t.Setenv("MAGPIE_ADDR", "")
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "colors.toml"), []byte("mode = \"light\"\naccent = \"#1e66f5\"\nbackground = \"#eff1f5\"\nforeground = \"#4c4f69\"\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_OMARCHY_THEME", dir)
|
|
t.Setenv("MAGPIE_OMARCHY_FONT", "iA Writer Mono S")
|
|
t.Setenv("HYPRLAND_INSTANCE_SIGNATURE", "")
|
|
was := onOmarchy
|
|
t.Cleanup(func() { onOmarchy = was })
|
|
h := lanGuard(New().Handler())
|
|
call := func(method, from, origin string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, "/v1/magpie/omarchy", nil)
|
|
r.RemoteAddr = from
|
|
r.Host = "127.0.0.1:3425"
|
|
if origin != "" {
|
|
r.Header.Set("Origin", origin)
|
|
}
|
|
if method == "OPTIONS" {
|
|
r.Header.Set("Access-Control-Request-Method", "GET")
|
|
r.Header.Set("Access-Control-Request-Private-Network", "true")
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
onOmarchy = false
|
|
if w := call("GET", "127.0.0.1:5000", "https://usemagpie.ai"); w.Code != http.StatusNotFound {
|
|
t.Fatal("not on Omarchy:", w.Code, w.Body)
|
|
}
|
|
|
|
onOmarchy = true
|
|
w := call("GET", "127.0.0.1:5000", "https://usemagpie.ai")
|
|
if w.Code != 200 || w.Header().Get("Access-Control-Allow-Origin") != "https://usemagpie.ai" {
|
|
t.Fatal("the site, on this machine:", w.Code, w.Header(), w.Body)
|
|
}
|
|
var th struct {
|
|
Mode string
|
|
Vars map[string]string
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &th); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if th.Mode != "light" || th.Vars["--bg"] != "#eff1f5" || th.Vars["--accent"] != "#1e66f5" || th.Vars["--om-radius"] != "0px" {
|
|
t.Fatal("theme:", th.Mode, th.Vars)
|
|
}
|
|
if f := th.Vars["--font"]; len(f) < 18 || f[:18] != `"iA Writer Mono S"` {
|
|
t.Fatal("font:", f)
|
|
}
|
|
|
|
if w := call("GET", "127.0.0.1:5000", "https://evil.example"); w.Header().Get("Access-Control-Allow-Origin") != "" {
|
|
t.Fatal("another site may read it:", w.Header())
|
|
}
|
|
if w := call("GET", "192.168.1.9:5000", ""); w.Code == 200 {
|
|
t.Fatal("another machine got it:", w.Code)
|
|
}
|
|
w = call("OPTIONS", "127.0.0.1:5000", "https://usemagpie.ai")
|
|
if w.Code != http.StatusNoContent || w.Header().Get("Access-Control-Allow-Private-Network") != "true" {
|
|
t.Fatal("preflight:", w.Code, w.Header())
|
|
}
|
|
}
|