| new file mode 100644 |
| index 000000000..3bcf497a6 |
| --- /dev/null |
| +++ b/internal/edge/path_across_gate_test.go |
| @@ -0,0 +1,270 @@ |
| +package edge |
| + |
| +// Path routes across boxes (#186, spec §7.3): a gate login POST is |
| +// re-resolved by its `next` path. When `next` matches a prefix whose target |
| +// is not a same-box target (on another box, a case 3 target, or ineligible), |
| +// the login is refused with copyPathGateAcross before any hash is chosen or |
| +// cookie issued. It is never judged by the front route's gate. |
| + |
| +import ( |
| + "io" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/gate" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The front's own password: the front's gate would accept it, so a refusal |
| +// proves the front's gate never judged the login. |
| +const acrossFrontPassword = "front horse battery" |
| + |
| +// gateAcrossHub gates hub.example on A with acrossFrontPassword and returns |
| +// the hash. |
| +func (s *pathAcrossScene) gateAcrossHub(t *testing.T) string { |
| + t.Helper() |
| + h, err := gate.Hash(acrossFrontPassword) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + rt := acrossInstance(acrossHub, acrossAcct, "hub", s.hubApp.addr()) |
| + rt.Gate = h |
| + s.route(t, s.hub, rt) |
| + return h |
| +} |
| + |
| +// login is the gate's login form for host posted to be, redirects left |
| +// unfollowed. |
| +func (s *pathAcrossScene) login(t *testing.T, be *backhaulEdge, host, password, next string) acrossResp { |
| + t.Helper() |
| + form := url.Values{"password": {password}, "next": {next}} |
| + req, err := http.NewRequest(http.MethodPost, "https://"+host+gate.LoginPath, strings.NewReader(form.Encode())) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + c := *be.client |
| + c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } |
| + resp, err := c.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer resp.Body.Close() |
| + b, err := io.ReadAll(resp.Body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return acrossResp{resp.StatusCode, resp.Header, string(b)} |
| +} |
| + |
| +// wantLoginRefused fails unless r is the cross-box login refusal: 409, |
| +// no-store, the approved sentence and no cookie. |
| +func wantLoginRefused(t *testing.T, r acrossResp) { |
| + t.Helper() |
| + if r.code != http.StatusConflict || r.body != copyPathGateAcross { |
| + t.Fatalf("login = %d %q, want 409 %q", r.code, r.body, copyPathGateAcross) |
| + } |
| + if cc := r.header.Get("Cache-Control"); cc != "no-store" { |
| + t.Errorf("Cache-Control = %q, want no-store", cc) |
| + } |
| + if sc := r.header.Values("Set-Cookie"); len(sc) != 0 { |
| + t.Errorf("Set-Cookie = %q, want none", sc) |
| + } |
| +} |
| + |
| +// wantLoginCookie fails unless r is the gate's 303 with a cookie verifying |
| +// for host under hash. |
| +func wantLoginCookie(t *testing.T, code int, header http.Header, host, hash string) { |
| + t.Helper() |
| + if code != http.StatusSeeOther { |
| + t.Fatalf("login = %d, want 303", code) |
| + } |
| + resp := http.Response{Header: header} |
| + got := gateCookie(&resp) |
| + if got == "" || !gate.Verify(got, host, hash, time.Now()) { |
| + t.Fatalf("cookie %q does not verify for %s", got, host) |
| + } |
| +} |
| + |
| +// The copy is pinned byte for byte (Paul, 2026-11-20). |
| +func TestGateLoginRefusalCopyBytes(t *testing.T) { |
| + const want = "This page’s sign-in isn’t available at this address. Open the page from its own address to sign in." |
| + if copyPathGateAcross != want { |
| + t.Fatalf("copyPathGateAcross = %q, want %q", copyPathGateAcross, want) |
| + } |
| +} |
| + |
| +// A prefix whose target lives on another box: refused on the front's box |
| +// with the front's own password, and on every other box too (which backhaul |
| +// the login to the front's home). Nothing reaches either app; no cookie. |
| +func TestGateLoginRefusedForRemoteTarget(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + h := s.gateAcrossHub(t) |
| + |
| + // The control: no prefix, so the front's gate judges, and accepts. |
| + ok := s.login(t, s.hub, acrossHub, acrossFrontPassword, "/other") |
| + wantLoginCookie(t, ok.code, ok.header, acrossHub, h) |
| + |
| + for _, be := range s.boxes() { |
| + wantLoginRefused(t, s.login(t, be, acrossHub, acrossFrontPassword, "/acme/x")) |
| + } |
| + s.hubApp.wantSeen(t) |
| + s.tenantApp.wantSeen(t) |
| + if n := s.dialsFrom(s.hub, acrossIPTenant); n != 0 { |
| + t.Fatalf("A dialled B %d times, want 0: the login crossed", n) |
| + } |
| + |
| + // Case 3: a target served here for a front this box does not serve. |
| + w := httptest.NewRecorder() |
| + sel := requestSelection{Up: upstream{front: &frontMark{Host: acrossHub}}, OK: true, Prefix: acrossPrefix, Target: acrossTenant, LocalTarget: true} |
| + if !refuseLoginAcross(w, sel) || w.Code != http.StatusConflict || w.Body.String() != copyPathGateAcross { |
| + t.Fatalf("case 3 selection: %d %q, want refused", w.Code, w.Body.String()) |
| + } |
| +} |
| + |
| +// Every ineligible target is refused, never judged by the front's gate: a |
| +// target on another box that is not a path target, a same-box target of |
| +// another account, and a target on no box at all. |
| +func TestGateLoginRefusedWhenTargetIneligible(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + s.gateAcrossHub(t) |
| + |
| + s.publish(t, s.tenant, acrossTenant, acrossEntry(acrossAcct, "tenant", acrossAcct, "tenant", 6, true, false)) |
| + wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/acme/x")) |
| + |
| + oh, err := gate.Hash(acrossFrontPassword) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + other := acrossInstance("other.example", acrossOther, "shop", s.hubApp.addr()) |
| + other.Gate = oh |
| + s.route(t, s.hub, other) |
| + s.path(t, s.hub, acrossHub, "other", "other.example", acrossAcct, "hub") |
| + wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/other/x")) |
| + |
| + s.path(t, s.hub, acrossHub, "ghost", "ghost.example", acrossAcct, "hub") |
| + wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/ghost")) |
| + |
| + s.hubApp.wantSeen(t) |
| + s.tenantApp.wantSeen(t) |
| +} |
| + |
| +// The 401 page's next keeps the query; the lookup reads the path only. |
| +func TestGateLoginRefusedWithQueryString(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + s.gateAcrossHub(t) |
| + wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/acme?tab=1")) |
| +} |
| + |
| +// next is percent-encoded; the lookup decodes the segment first. |
| +func TestGateLoginRefusedWithEncodedPrefix(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + s.gateAcrossHub(t) |
| + wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/%61cme/x")) |
| +} |
| + |
| +// A linked (pinned) login has no prefix: the pinned app's gate judges it, |
| +// whatever its next names. Unpinned, the same login is refused (its prefix |
| +// names a target on no box). |
| +func TestGateLoginPinnedNotRefused(t *testing.T) { |
| + h, err := gate.Hash(acrossFrontPassword) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: acrossHub, Kind: "instance", Status: "ready", Socket: "/s/hub.sock", Account: acrossAcct, App: "hub", Gate: h}); err != nil { |
| + t.Fatal(err) |
| + } |
| + remotePathRow(t, reg, acrossHub, acrossPrefix, "ghost.example", acrossAcct, "hub") |
| + e, err := New(reg, &fakeActivator{}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + post := func(pin bool) *httptest.ResponseRecorder { |
| + form := url.Values{"password": {acrossFrontPassword}, "next": {"/acme/x"}}.Encode() |
| + req := httptest.NewRequest(http.MethodPost, "https://"+acrossHub+gate.LoginPath, strings.NewReader(form)) |
| + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + w := httptest.NewRecorder() |
| + if pin { |
| + e.ServeLinked(w, req, LinkedTarget{Host: acrossHub, Account: acrossAcct, App: "hub", PublicHost: "shop.pub.example"}) |
| + } else { |
| + e.ServeHTTP(w, req) |
| + } |
| + return w |
| + } |
| + if w := post(false); w.Code != http.StatusConflict || w.Body.String() != copyPathGateAcross { |
| + t.Fatalf("unpinned login = %d %q, want the refusal", w.Code, w.Body.String()) |
| + } |
| + w := post(true) |
| + if w.Code != http.StatusSeeOther || w.Body.String() == copyPathGateAcross { |
| + t.Fatalf("pinned login = %d %q, want the pinned gate's 303", w.Code, w.Body.String()) |
| + } |
| + if gateCookie(w.Result()) == "" { |
| + t.Fatal("pinned login set no gate cookie") |
| + } |
| +} |
| + |
| +// A same-box target keeps today's answer: its own gate judges the login and |
| +// its host signs the cookie. |
| +func TestGateLoginSameBoxUnchanged(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + s.gateAcrossHub(t) |
| + const pw = "local target password" |
| + lh, err := gate.Hash(pw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + local := acrossInstance("local.example", acrossAcct, "shop", s.hubApp.addr()) |
| + local.Gate = lh |
| + s.route(t, s.hub, local) |
| + s.path(t, s.hub, acrossHub, "loc", "local.example", acrossAcct, "hub") |
| + res := s.login(t, s.hub, acrossHub, pw, "/loc/x") |
| + wantLoginCookie(t, res.code, res.header, "local.example", lh) |
| + // The front's password is the wrong one for the target. |
| + if res := s.login(t, s.hub, acrossHub, acrossFrontPassword, "/loc/x"); res.code != http.StatusUnauthorized { |
| + t.Fatalf("front's password on the target = %d, want 401", res.code) |
| + } |
| + s.hubApp.wantSeen(t) |
| +} |
| + |
| +// R3 P1-1: a same-account platform-alias target is its instance, a same-box |
| +// target, so the login is judged by the instance's gate, not refused. |
| +func TestGateLoginSameBoxPlatformAliasTarget(t *testing.T) { |
| + const front, inst, alias = "hub.example", "a.bae.oncarlos.com", "shop.bae.oncarlos.com" |
| + fh, err := gate.Hash(acrossFrontPassword) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + const pw = "shop password here" |
| + ih, err := gate.Hash(pw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openReg(t) |
| + for _, rt := range []registry.Route{ |
| + {Host: front, Kind: "instance", Status: "ready", Addr: "127.0.0.1:1", Account: "bae", App: "hub", Gate: fh}, |
| + {Host: inst, Kind: "instance", Status: "ready", Socket: "/s/a.sock", Account: "bae", App: "shop", Gate: ih}, |
| + {Host: alias, Kind: "instance", Status: "ready", Socket: "/s/a.sock", Account: "bae", App: "shop", Platform: true}, |
| + } { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + remotePathRow(t, reg, front, "shop", alias, "bae", "hub") |
| + e, err := New(reg, &fakeActivator{}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if up := e.current()[alias]; !up.platformAlias { |
| + t.Fatalf("fixture: %s is not a platform alias entry: %+v", alias, up) |
| + } |
| + srv := httptest.NewServer(e) |
| + t.Cleanup(srv.Close) |
| + resp := postLogin(t, srv, front, pw, "/shop/x") |
| + wantLoginCookie(t, resp.StatusCode, resp.Header, inst, ih) |
| +} |