carlos / platform Public

edge: refuse a gate login whose page lives on another box (#186)

A login POST re-selected by its next path is refused with 409 and the
approved sentence (copyPathGateAcross) when next matches a prefix whose
target is not a same-box target: on another box, a case 3 target, or
ineligible. The refusal comes before admission, any password hash or
cookie, so the front route's gate never judges it. A pinned (linked)
login has no prefix and is never refused.

refuseLoginAcross and loginLookupPath live in pathhop.go; ServeHTTP
gains the call and loses the next-path parse (384 code lines, record
385).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev db46a8e3003956a8bd68ca8031f9f1f49fd0feb7 parent 1ece9f2
3 files changed, +330 −16
  • internal/edge/edge.go +8 −16
  • internal/edge/path_across_gate_test.go +270 −0
  • internal/edge/pathhop.go +52 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index a43f1c34b..a8f407013 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -34,7 +34,6 @@ import (
"net/http"
"net/http/httputil"
"net/textproto"
- "net/url"
"os"
"os/signal"
"path"
@@ -2554,27 +2553,20 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
- // Resolve on the PATH of `next`, never on the raw string. The 401
- // page's next is SafeNext(r.URL.RequestURI()), so it carries the
- // query the visitor was refused on and it is percent-ENCODED —
- // while the prefix map is keyed on decoded segments, the way
- // r.URL.Path arrives for an ordinary request. Splitting the raw
- // string makes firstSegment("/paulca?tab=1") "paulca?tab=1", which
- // matches nothing and drops the login onto the FRONT route: the
- // wrong hash judges the password and the cookie is signed for the
- // wrong host. Parsing first drops the query and the fragment and
- // decodes the segments, so both shapes land where the refusal did.
+ // Resolve on the PATH of `next`, never on the raw string
+ // (loginLookupPath). A prefix whose target is not a same-box target
+ // is refused first (refuseLoginAcross, spec §7.3): never judged by
+ // the front's gate.
//
// A miss cannot happen — SafeNext yields a path on this host, and
// the worst case "/" resolves to the host's own route, the one
// already in up — but keeping the first up is the honest answer if
// the table changed underneath.
- next := gate.SafeNext(r.PostForm.Get("next"))
- lookupPath := "/"
- if u, err := url.ParseRequestURI(next); err == nil && u.Path != "" {
- lookupPath = u.Path
+ loginSel := e.selectUpstream(r, host, loginLookupPath(r))
+ if refuseLoginAcross(w, loginSel) {
+ return
}
- if target, hit := e.selectUpstream(r, host, lookupPath).sameBox(); hit {
+ if target, hit := loginSel.sameBox(); hit {
up = target
if e.admissionRefuses(w, r, up.route) {
return
diff --git a/internal/edge/path_across_gate_test.go b/internal/edge/path_across_gate_test.go
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)
+}
diff --git a/internal/edge/pathhop.go b/internal/edge/pathhop.go
index ede50ed25..ac26ac1d8 100644
--- a/internal/edge/pathhop.go
+++ b/internal/edge/pathhop.go
@@ -8,10 +8,13 @@ package edge
import (
"context"
"fmt"
+ "io"
"net/http"
+ "net/url"
"strings"
"github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/gate"
"github.com/carlosframework/platform/internal/registry"
)
@@ -263,3 +266,52 @@ func (e *Edge) upgradeSelection(ctx context.Context, host, urlPath string) (upst
}
return sel.Up, true
}
+
+// copyPathGateAcross is the answer to a gate login whose page lives on
+// another box (spec §7.3). Approved copy (Paul, 2026-11-20): change it only
+// through copy review.
+const copyPathGateAcross = "This page\u2019s sign-in isn\u2019t available at this address. Open the page from its own address to sign in."
+
+// loginLookupPath is the path a gate login POST is re-selected by: the PATH
+// of its `next`, never the raw string. The 401 page's next is
+// SafeNext(r.URL.RequestURI()), so it carries the query the visitor was
+// refused on and it is percent-ENCODED, while the prefix map is keyed on
+// decoded segments, the way r.URL.Path arrives for an ordinary request.
+// Splitting the raw string makes firstSegment("/paulca?tab=1")
+// "paulca?tab=1", which matches nothing and drops the login onto the FRONT
+// route: the wrong hash judges the password and the cookie is signed for the
+// wrong host. Parsing first drops the query and the fragment and decodes the
+// segments, so both shapes land where the refusal did. Anything that will
+// not parse is "/", the host's own route.
+func loginLookupPath(r *http.Request) string {
+ next := gate.SafeNext(r.PostForm.Get("next"))
+ if u, err := url.ParseRequestURI(next); err == nil && u.Path != "" {
+ return u.Path
+ }
+ return "/"
+}
+
+// refuseLoginAcross answers a gate login POST whose `next` matched a prefix
+// whose target is not a same-box target (spec §7.3): the target is on
+// another box (Remote, or a verified hop's front fallback), a case 3 target
+// for a front this box does not serve (Up.front set), or ineligible for any
+// reason (no LocalTarget). The password cannot be judged here (the target's
+// box holds its hash, and the body would have to cross first), and the
+// front's gate must never judge it, so the login is refused before any hash
+// is chosen or cookie issued. It reports whether it answered.
+//
+// sel is the login block's selection on loginLookupPath. A pinned (linked)
+// selection has no Prefix and is never refused; neither is a login whose
+// next matched no prefix (the host's own route judges it, as before).
+func refuseLoginAcross(w http.ResponseWriter, sel requestSelection) bool {
+ if sel.Prefix == "" || (sel.LocalTarget && sel.Up.front == nil) {
+ return false
+ }
+ h := w.Header()
+ h.Set("Content-Type", "text/plain; charset=utf-8")
+ h.Set("X-Content-Type-Options", "nosniff")
+ h.Set("Cache-Control", "no-store")
+ w.WriteHeader(http.StatusConflict)
+ io.WriteString(w, copyPathGateAcross)
+ return true
+}