magpie in a LazyCat app (懒猫微服) is opened from the box's app list by someone signed in to the box already, and asks for a key they never saw: it is in the container's log. A package can only hand the key to every browser that reaches the app, or put a proxy in front that adds the key's cookie to what the box signed in. The box's ingress, lzc-ingress, sends a browser that isn't signed in to the box's login page, and puts on every request it passes the app the box account (X-HC-User-ID), its role (X-HC-User-Role, ADMIN or NORMAL) and what sent it (X-HC-Source, client for a browser), in place of what the caller sent. With MAGPIE_WEB_SIGN_IN=lazycat webGuard takes that sign-in besides the key: lazyCatSignIn lets in an ADMIN account sent by a browser, each of the three named once, and tells anyone else the box's reason. The box's sign-in is a cookie of the box's, and the box's apps are one site, so a browser sends it with another app's page's requests too: an earlier build of this change let another app's page on the box include /boot.js and read window.bootPrefs. So it is taken only for what the page itself or the user asked for (boxFetched, the Fetch Metadata: Sec-Fetch-Site same-origin or none, or a GET or HEAD navigation to a document from another page), never for another page's script, frame, fetch or form POST, nor from a browser that sends no Fetch Metadata. It sets no cookie of the page's own, so signing out of the box signs out of the page. The key's link and cookie work as before, /api still answers the page alone (webOwnPage), and the gateway's client routes on the page's port go to the gateway first (onePort), which asks for a gateway key. StartWeb refuses another value, and lazycat where LAZYCAT_APP_DOMAIN, which LazyCat sets in an app's containers, isn't set: all magpie can check, so the setting is the package's word that the box's ingress is the only way to the page's port. magpie web prints the box's address beside the link. Docs: reference.md's new LazyCat section, with a manifest and what must never reach the page's port, and gui-shell.md's Browser mode row. On a box (lzcos 1.6.2+fix.4), a probe app that echoes its request headers: a signed-in browser's request carries X-HC-User-ID, X-HC-User-Role ADMIN and X-HC-Source client, forged ones replaced; not signed in, / goes to the login page and a public path gets none of the three (forged ones taken off, repeated or in lower case too), while X-HC-User-Groups passes on as the caller wrote it, so it is never read; a TCP ingress passes every header as sent. Another app's container reaches the app through the ingress alone (straight to the app's container it times out); the box itself, and a container in its network (network_mode: host, which a manifest may set), reach the port. A test app made from the docs' manifest, running a build of this change on68cb92e8ein the image's place as its user (uid 65532): the box's administrator opens the console with no key, and no cookie is set; /v1/models still asks for a gateway key; from another app's page a navigation opens the console, while a script include of /boot.js and a frame are turned away; in the container, past the ingress, a request with no Fetch Metadata, no X-HC-Source, NORMAL or a header twice gets a 401. Bound to /lzcapp/var/config, which the box makes as root's, magpie couldn't write /config; bound to /lzcapp/var it can, and a gateway key outlived the container. Tests, through webServed with the headers the probe saw: TestLazyCatSignInOpensThePage, TurnsAway, AsksWhereFrom, IsAskedEachTime and IsNoGatewayKey (the client routes, with each sign-in that opens the page), and TestWebBoxSignInChecksItsValue. TestRemoteOpensTheOtherMagpie, new on main, gives webHandler no box sign-in. Run, at this tree, on6c3f113ce: Docker golang:1.26.8 (linux/arm64): gofmt clean; go vet -tags gtk3 ./... (with the GTK and WebKitGTK headers, as CI), -tags nogui and GOOS=windows clean; both builds ok. macOS 27.2 (go1.26.8, not root, a temporary HOME): go vet ./... and the two cross vets clean, the three builds ok; go test -tags nogui ./..., TestScriptFindsClaudeMoved skipped (it runs the real /Applications/Claude.app, #5989): 92 packages ok; red outside this change, each red the same at6c3f113ce: TestBunProxyLoginWithReservedCharacters (#3397), TestGitUpdateInstallsTheBranchsCommitNow and TestGitUpdateKeepsACommitPin (#5988), on this Mac's bun 1.3.11, older than MinBun; TestEveryRouteHasACommandOrIsNamed: POST /api/settings/join, new withe053c337a, has no command. /join, new with it too, is a client route, so the docs' manifest lists it. Broken by hand, each red, all green again put back; a break that lets in what must be turned away was seen to reach the page: TurnsAway with the once-each check out, no sender taken, the role in any case, the role or the sender check out (IsAskedEachTime too), app:… and system alone turned away, or the key's "open the link" kept in the box's 401; AsksWhereFrom with boxFetched out, no Fetch Metadata taken, another page's navigation taken of any method or into a frame, an object or an embed (or anything but an iframe), its request for a document that isn't a navigation, or another app's page's script (same-site, no-cors); OpensThePage with another page's navigation taken by GET alone, or none taken (IsNoGatewayKey too), a cookie set, or webOwnPage out; IsAskedEachTime with the first sign-in kept; ChecksItsValue with lazycat taken outside LazyCat or StartWeb not asking webBoxSignIn's error; IsNoGatewayKey with the client routes behind the box's sign-in, or one the box signs in on, or a HEAD to one, given to the page; TestLazyCatManifestListsTheClientRoutes with /me out of the docs. Not tried: a NORMAL account in a real browser (the box has one account); signing out of the box in a browser; the box's own client apps opening the page; a browser without Fetch Metadata; a header named twice on a signed-in request through the ingress (a browser can't send one); a multi-instance app; another lzcos version; a community plugin's page on the box; a real vendor call through the box. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
6.1 KiB
Go
147 lines
6.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWebGuard(t *testing.T) {
|
|
h := webGuard("c", "key", 0, nil, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusTeapot) }))
|
|
do := func(r *http.Request) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
if c := do(httptest.NewRequest("GET", "/api/state", nil)).Code; c != http.StatusUnauthorized {
|
|
t.Fatalf("no key: %d", c)
|
|
}
|
|
if c := do(httptest.NewRequest("GET", "/?k=nope", nil)).Code; c != http.StatusUnauthorized {
|
|
t.Fatalf("wrong key: %d", c)
|
|
}
|
|
rec := do(httptest.NewRequest("GET", "/?k=key&view=settings", nil))
|
|
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/?view=settings" {
|
|
t.Fatalf("link: %d %q", rec.Code, rec.Header().Get("Location"))
|
|
}
|
|
ck := rec.Result().Cookies()
|
|
if len(ck) != 1 || ck[0].Value != "key" || !ck[0].HttpOnly || ck[0].SameSite != http.SameSiteLaxMode || ck[0].MaxAge != 0 {
|
|
t.Fatalf("cookie: %+v", ck)
|
|
}
|
|
r := httptest.NewRequest("POST", "/api/settings", nil)
|
|
r.AddCookie(ck[0])
|
|
if c := do(r).Code; c != http.StatusTeapot {
|
|
t.Fatalf("with cookie: %d", c)
|
|
}
|
|
r = httptest.NewRequest("POST", "/api/settings", nil)
|
|
r.AddCookie(&http.Cookie{Name: "c", Value: "other"})
|
|
if c := do(r).Code; c != http.StatusUnauthorized {
|
|
t.Fatalf("wrong cookie: %d", c)
|
|
}
|
|
}
|
|
|
|
func TestWebKey(t *testing.T) {
|
|
t.Setenv("MAGPIE_WEB_KEY", "")
|
|
a, fixed, err := webKey()
|
|
if err != nil || fixed || len(a) != 32 {
|
|
t.Fatalf("a run's own key: %q %v %v", a, fixed, err)
|
|
}
|
|
if b, _, _ := webKey(); b == a {
|
|
t.Fatal("two runs got one key")
|
|
}
|
|
t.Setenv("MAGPIE_WEB_KEY", "short")
|
|
if _, _, err := webKey(); err == nil {
|
|
t.Fatal("a short MAGPIE_WEB_KEY was taken")
|
|
}
|
|
t.Setenv("MAGPIE_WEB_KEY", "0123456789abcdef x;y")
|
|
if _, _, err := webKey(); err == nil {
|
|
t.Fatal("a MAGPIE_WEB_KEY a cookie can't hold was taken")
|
|
}
|
|
t.Setenv("MAGPIE_WEB_KEY", "0123456789abcdef-kept")
|
|
if k, fixed, err := webKey(); err != nil || !fixed || k != "0123456789abcdef-kept" {
|
|
t.Fatalf("MAGPIE_WEB_KEY: %q %v %v", k, fixed, err)
|
|
}
|
|
|
|
// a fixed key's cookie outlives the browser
|
|
h := webGuard("c", "0123456789abcdef-kept", webCookieAge, nil, http.NotFoundHandler())
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/?k=0123456789abcdef-kept", nil))
|
|
if ck := rec.Result().Cookies(); len(ck) != 1 || time.Duration(ck[0].MaxAge)*time.Second != webCookieAge {
|
|
t.Fatalf("fixed key's cookie: %+v", ck)
|
|
}
|
|
}
|
|
|
|
// #2033 (321sssrt-bit): the sign-in cookie is the last Chrome drops when
|
|
// other services on the same host fill its cookie limit; a browser that
|
|
// holds two cookies of the page's name, a stale one sent first, still gets
|
|
// in; one turned away is told why and can paste the key, not sent back to
|
|
// the start-up log.
|
|
func TestWebGuardBookmark(t *testing.T) {
|
|
const key = "0123456789abcdef-2033"
|
|
h := webHandler("3430", key, webCookieAge, nil, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusTeapot) }))
|
|
do := func(r *http.Request) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
page := func(cookie string) *http.Request {
|
|
r := httptest.NewRequest("GET", "/?view=usage", nil)
|
|
r.Header.Set("Accept", "text/html,application/xhtml+xml")
|
|
if cookie != "" {
|
|
r.Header.Set("Cookie", cookie)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// the link's cookie, and the form's below, are Chrome's high priority
|
|
link := do(httptest.NewRequest("GET", "/?view=usage&k="+key, nil))
|
|
if sc := link.Header().Get("Set-Cookie"); link.Code != http.StatusSeeOther || !strings.HasSuffix(sc, "; Priority=High") || !strings.HasPrefix(sc, "magpie_web_3430="+key+";") {
|
|
t.Fatalf("link: %d %q", link.Code, sc)
|
|
}
|
|
|
|
// the reporter's case: the stale value first, the right one after
|
|
if c := do(page("magpie_web_3430=0123456789abcdef-old; magpie_web_3430=" + key)).Code; c != http.StatusTeapot {
|
|
t.Fatalf("stale cookie first: %d", c)
|
|
}
|
|
|
|
// no cookie: a form, saying none was sent, and never the key
|
|
rec := do(page(""))
|
|
body := rec.Body.String()
|
|
if rec.Code != http.StatusUnauthorized || !strings.Contains(body, "sent no sign-in cookie") || !strings.Contains(body, `action="`+webSignInPath+`"`) || !strings.Contains(body, `value="/?view=usage"`) || strings.Contains(body, key) {
|
|
t.Fatalf("no cookie: %d %s", rec.Code, body)
|
|
}
|
|
// a cookie with another key: says so
|
|
if body := do(page("magpie_web_3430=0123456789abcdef-old")).Body.String(); !strings.Contains(body, "holds a key that isn") {
|
|
t.Fatalf("other key: %s", body)
|
|
}
|
|
// the page's own fetches get a plain 401 with the reason
|
|
r := httptest.NewRequest("GET", "/api/state", nil)
|
|
if rec := do(r); rec.Code != http.StatusUnauthorized || !strings.Contains(rec.Body.String(), "sent no sign-in cookie") {
|
|
t.Fatalf("api: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
post := func(k, back string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("POST", webSignInPath, strings.NewReader(url.Values{"key": {k}, "back": {back}}.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
return do(r)
|
|
}
|
|
// a wrong key pasted: turned away again, no cookie
|
|
if rec := post("nope-nope-nope-nope", "/?view=usage"); rec.Code != http.StatusUnauthorized || len(rec.Result().Cookies()) != 0 {
|
|
t.Fatalf("wrong pasted key: %d %v", rec.Code, rec.Result().Cookies())
|
|
}
|
|
// the right one: the cookie, back where the bookmark was
|
|
rec = post(key, "/?view=usage")
|
|
ck := rec.Result().Cookies()
|
|
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/?view=usage" || len(ck) != 1 || ck[0].Value != key || time.Duration(ck[0].MaxAge)*time.Second != webCookieAge || !strings.HasSuffix(rec.Header().Get("Set-Cookie"), "; Priority=High") {
|
|
t.Fatalf("pasted key: %d %q %+v", rec.Code, rec.Header().Get("Location"), ck)
|
|
}
|
|
// the return address never leaves this server
|
|
for _, back := range []string{"https://evil.example/", "//evil.example/", `/\evil.example`, "javascript:alert(1)", "", webSignInPath, "/a\r\nSet-Cookie: x=1"} {
|
|
if l := post(key, back).Header().Get("Location"); l != "/" {
|
|
t.Fatalf("back %q went to %q", back, l)
|
|
}
|
|
}
|
|
}
|