edge: refuse a gate login only for a real cross-box target (#186)
Task 11 fix round 1 (spec rev 5.2 section 7.3). A login whose next matches a prefix is refused only when the target is served across boxes: a Remote selection, a case 3 target (Up.front), or, when the selection fell back to the front (a verified hop, a front that may not send a prefix across, a map a tick behind), a target route published by another box in the prefix map's account. A target that is not a target route anywhere (missing, another account's, an attached alias) is judged by the front's gate again, as before Task 11. Door callers and door-marked hops skip the refusal. A refusal logs "path-login <host>". requestSelection gains MapAccount (the remote map's account on case 3's refusals). ServeHTTP's code-health record is lowered to 384.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+166
−39
.amadan/code-health.tsv+1 −1internal/edge/edge.go+2 −2internal/edge/path_across_gate_test.go+107 −23internal/edge/pathhop.go+50 −12internal/edge/pathselect.go+6 −1
diff --git a/.amadan/code-health.tsv b/.amadan/code-health.tsv| index d38938f76..d06e58eba 100644 |
| --- a/.amadan/code-health.tsv |
| +++ b/.amadan/code-health.tsv |
| @@ -2186,7 +2186,7 @@ func-lines internal/console (*Store).migrate - 335 |
| func-lines internal/console TestDemoProtectionsSourceToLimitAndTakedown - 412 |
| func-lines internal/console TestDemoSourceToLiveAndUpdate - 383 |
| func-lines internal/console TestHAP2ProductionDoors - 304 |
| -func-lines internal/edge (*Edge).ServeHTTP - 385 |
| +func-lines internal/edge (*Edge).ServeHTTP - 384 |
| func-lines internal/edge TestDemoAPIAliasBoundary - 360 |
| func-lines internal/edge TestRegionalNameNeverLeavesItsRegion - 330 |
| func-lines internal/edge TestTunnelWebSocketEndToEnd - 327 |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index a8f407013..6fe865e5f 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -2554,7 +2554,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| // Resolve on the PATH of `next`, never on the raw string |
| - // (loginLookupPath). A prefix whose target is not a same-box target |
| + // (loginLookupPath). A prefix whose target is served on another box |
| // is refused first (refuseLoginAcross, spec §7.3): never judged by |
| // the front's gate. |
| // |
| @@ -2563,7 +2563,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| // already in up — but keeping the first up is the honest answer if |
| // the table changed underneath. |
| loginSel := e.selectUpstream(r, host, loginLookupPath(r)) |
| - if refuseLoginAcross(w, loginSel) { |
| + if e.refuseLoginAcross(w, r, host, loginSel) { |
| return |
| } |
| if target, hit := loginSel.sameBox(); hit { |
diff --git a/internal/edge/path_across_gate_test.go b/internal/edge/path_across_gate_test.go| index 3bcf497a6..6e374e80b 100644 |
| --- a/internal/edge/path_across_gate_test.go |
| +++ b/internal/edge/path_across_gate_test.go |
| @@ -7,6 +7,7 @@ package edge |
| // cookie issued. It is never judged by the front route's gate. |
| import ( |
| + "context" |
| "io" |
| "net/http" |
| "net/http/httptest" |
| @@ -61,6 +62,40 @@ func (s *pathAcrossScene) login(t *testing.T, be *backhaulEdge, host, password, |
| return acrossResp{resp.StatusCode, resp.Header, string(b)} |
| } |
| +// loginHop is the login form for host posted to be as a verified hop, as a |
| +// sending edge signs one, door-marked when door is set. |
| +func (s *pathAcrossScene) loginHop(t *testing.T, be *backhaulEdge, host, password, next string, door bool) 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") |
| + key := signingKey(be.homes.meshKeys()) |
| + if len(key) == 0 { |
| + t.Fatal("no mesh key mirrored") |
| + } |
| + now := time.Now() |
| + req.Header.Set(hopHeader, "1") |
| + req.Header.Set(hopAuthHeader, signHop(key, host, now)) |
| + if door { |
| + req.Header.Set(hopDoorHeader, signHopDoor(key, host, now)) |
| + } |
| + 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) { |
| @@ -121,38 +156,93 @@ func TestGateLoginRefusedForRemoteTarget(t *testing.T) { |
| // 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 { |
| + req := httptest.NewRequest(http.MethodPost, "https://"+acrossHub+gate.LoginPath, nil) |
| + if !s.tenant.edge.refuseLoginAcross(w, req, acrossHub, 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. |
| +// A prefix whose target is not a target route anywhere keeps today's |
| +// answer: the front serves that page, so the front's gate judges its login. |
| +// A target route on another box is refused even when this request did not |
| +// select it: a verified hop to the front's home (which never takes a remote |
| +// answer), and a selection that fell back to the front. |
| 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")) |
| + h := s.gateAcrossHub(t) |
| - oh, err := gate.Hash(acrossFrontPassword) |
| + // Not a target route anywhere: judged by the front's gate. |
| + oh, err := gate.Hash("other account password") |
| if err != nil { |
| t.Fatal(err) |
| } |
| other := acrossInstance("other.example", acrossOther, "shop", s.hubApp.addr()) |
| other.Gate = oh |
| s.route(t, s.hub, other) |
| + upsertAlias(t, s.hub.reg, "att.example", "other.example") |
| + s.reload(t, s.hub) |
| 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, "att", "att.example", acrossAcct, "hub") |
| s.path(t, s.hub, acrossHub, "ghost", "ghost.example", acrossAcct, "hub") |
| - wantLoginRefused(t, s.login(t, s.hub, acrossHub, acrossFrontPassword, "/ghost")) |
| + for _, next := range []string{"/other/x", "/att/x", "/ghost"} { |
| + res := s.login(t, s.hub, acrossHub, acrossFrontPassword, next) |
| + if res.body == copyPathGateAcross { |
| + t.Fatalf("next=%s refused, want the front's gate", next) |
| + } |
| + wantLoginCookie(t, res.code, res.header, acrossHub, h) |
| + } |
| + // Published on B, but not as a target route: judged by the front's gate. |
| + s.publish(t, s.tenant, acrossTenant, acrossEntry(acrossAcct, "tenant", acrossAcct, "tenant", 6, true, false)) |
| + res := s.login(t, s.hub, acrossHub, acrossFrontPassword, "/acme/x") |
| + wantLoginCookie(t, res.code, res.header, acrossHub, h) |
| + s.publish(t, s.tenant, acrossTenant, acrossRouteEntry(acrossAcct, "tenant", 7)) |
| + |
| + // A target route on B, arriving at A as a verified hop: refused. |
| + wantLoginRefused(t, s.loginHop(t, s.hub, acrossHub, acrossFrontPassword, "/acme/x", false)) |
| + |
| + // The selection fell back to the front (a front that may not send a |
| + // prefix across, or a map a tick behind): refused all the same. |
| + front := s.hub.edge.current()[acrossHub] |
| + fell := requestSelection{Up: front, OK: true, Prefix: acrossPrefix, Target: acrossTenant, Front: front, FrontOK: true} |
| + req := httptest.NewRequest(http.MethodPost, "https://"+acrossHub+gate.LoginPath, nil) |
| + w := httptest.NewRecorder() |
| + if !s.hub.edge.refuseLoginAcross(w, req, acrossHub, fell) || w.Code != http.StatusConflict || w.Body.String() != copyPathGateAcross { |
| + t.Fatalf("fallen-back selection: %d %q, want refused", w.Code, w.Body.String()) |
| + } |
| + // Another account's target route on B is not this map's target. |
| + s.publish(t, s.tenant, acrossTenant, acrossRouteEntry(acrossOther, "tenant", 8)) |
| + if s.hub.edge.refuseLoginAcross(httptest.NewRecorder(), req, acrossHub, fell) { |
| + t.Fatal("another account's target route refused the login, want the front's gate") |
| + } |
| s.hubApp.wantSeen(t) |
| s.tenantApp.wantSeen(t) |
| } |
| +// A door caller never crosses boxes (§7.4): its login is judged as before, |
| +// in process and as a door-marked hop to the front's home. |
| +func TestGateLoginDoorCallerUnchanged(t *testing.T) { |
| + s := newPathAcrossScene(t) |
| + h := s.gateAcrossHub(t) |
| + 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") |
| + req = req.WithContext(context.WithValue(req.Context(), doorCallerKey{}, doorCaller{Account: acrossAcct, App: "hub"})) |
| + w := httptest.NewRecorder() |
| + s.hub.edge.ServeHTTP(w, req) |
| + if w.Body.String() == copyPathGateAcross { |
| + t.Fatal("a door caller's login was refused") |
| + } |
| + wantLoginCookie(t, w.Code, w.Header(), acrossHub, h) |
| + |
| + res := s.loginHop(t, s.hub, acrossHub, acrossFrontPassword, "/acme/x", true) |
| + if res.body == copyPathGateAcross { |
| + t.Fatal("a door-marked hop's login was refused") |
| + } |
| + wantLoginCookie(t, res.code, res.header, acrossHub, h) |
| + 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) |
| @@ -169,22 +259,16 @@ func TestGateLoginRefusedWithEncodedPrefix(t *testing.T) { |
| // 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). |
| +// names a target route on B). |
| func TestGateLoginPinnedNotRefused(t *testing.T) { |
| + s := newPathAcrossScene(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) |
| + // A pin needs a socket backend (pinnedUpstream); the gate answers first. |
| + s.route(t, s.hub, registry.Route{Host: acrossHub, Kind: "instance", Status: "ready", Socket: "/s/hub.sock", Account: acrossAcct, App: "hub", Gate: h}) |
| + e := s.hub.edge |
| 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)) |
diff --git a/internal/edge/pathhop.go b/internal/edge/pathhop.go| index ac26ac1d8..7bc5c26b9 100644 |
| --- a/internal/edge/pathhop.go |
| +++ b/internal/edge/pathhop.go |
| @@ -16,6 +16,7 @@ import ( |
| "github.com/carlosframework/platform/internal/edgesfile" |
| "github.com/carlosframework/platform/internal/gate" |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| // backhaulRouteTo is backhaulTo for a request whose wire Host (signed, SNI, |
| @@ -292,21 +293,20 @@ func loginLookupPath(r *http.Request) string { |
| } |
| // 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. |
| +// whose target is a target route served on another box (loginTargetAcross, |
| +// spec rev 5.2 §7.3). 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) { |
| +// sel is the login block's selection on loginLookupPath; host is the |
| +// request's host, for the log line. |
| +func (e *Edge) refuseLoginAcross(w http.ResponseWriter, r *http.Request, host string, sel requestSelection) bool { |
| + if !e.loginTargetAcross(r, sel) { |
| return false |
| } |
| + e.logThrottled("path-login "+host, fmt.Sprintf( |
| + "gate login on %s refused: its page's prefix %q names %s, served on another box, and the password is judged only there", host, sel.Prefix, sel.Target)) |
| h := w.Header() |
| h.Set("Content-Type", "text/plain; charset=utf-8") |
| h.Set("X-Content-Type-Options", "nosniff") |
| @@ -315,3 +315,41 @@ func refuseLoginAcross(w http.ResponseWriter, sel requestSelection) bool { |
| io.WriteString(w, copyPathGateAcross) |
| return true |
| } |
| + |
| +// loginTargetAcross reports whether a login's selection names a target that |
| +// is not served same-box but is served across boxes: |
| +// |
| +// - no prefix matched (a pinned login has none), or the matched target is |
| +// served here for a front this box serves: false, judged here as before; |
| +// - a door caller, or a door-marked hop: false. Door calls never cross |
| +// boxes (§7.4), and the door's scope is checked after the login block; |
| +// - Remote (case 2, or case 3 with a remote target), or Up.front (case 3, |
| +// target served here for a front this box does not serve): true; |
| +// - otherwise the selection fell back to the front (a verified hop, which |
| +// never takes a remote answer, or a front that may not send a prefix |
| +// across, or a map a tick behind): true only when the home map's exact |
| +// row for the target, published by another box, is a target route of |
| +// the prefix map's account. A target that is not a target route anywhere |
| +// (missing, another account's, an attached alias) is false: the front |
| +// serves that page, so the front's gate judges its login, as before. |
| +func (e *Edge) loginTargetAcross(r *http.Request, sel requestSelection) bool { |
| + if sel.Prefix == "" || (sel.LocalTarget && sel.Up.front == nil) { |
| + return false |
| + } |
| + if _, door := doorCallerFrom(r.Context()); door || hopDoorFrom(r.Context()) { |
| + return false |
| + } |
| + if sel.Remote != nil || sel.Up.front != nil { |
| + return true |
| + } |
| + account := sel.MapAccount |
| + if account == "" { |
| + if !sel.FrontOK { |
| + return false |
| + } |
| + account = release.RouteAccount(sel.Front.route) |
| + } |
| + m := e.homes.Load() |
| + info, ok := m.exactPathRoute(sel.Target) |
| + return ok && info.Target && info.Account == account && info.Box != m.self |
| +} |
diff --git a/internal/edge/pathselect.go b/internal/edge/pathselect.go| index 0689c44da..6bed862d9 100644 |
| --- a/internal/edge/pathselect.go |
| +++ b/internal/edge/pathselect.go |
| @@ -49,6 +49,11 @@ type requestSelection struct { |
| Remote *remoteTarget // non-nil: hop to the target instead; Up/OK then hold nothing |
| Prefix string // the matched prefix, whenever one matched |
| Target string // the matched target host, whenever one matched |
| + // MapAccount: the remote map's account when Prefix came from it on a |
| + // front this box does not serve (case 3's refusals). Empty otherwise: |
| + // a served front's prefix is its route's account (release.RouteAccount |
| + // of Front.route). The login refusal reads it (loginTargetAcross). |
| + MapAccount string |
| // LocalTarget: Up is the matched target's own entry, served here (case 1 |
| // or case 3). The ONLY signal downstream consumers (HA identity, login) |
| // use for "a target was selected"; never compare Target with Up.route.Host, |
| @@ -433,7 +438,7 @@ func (e *Edge) selectForUnservedFront(t *routeTable, host, urlPath string, rm re |
| if !stay.OK { |
| // A shadowing catch-all's answer (stay.OK) is kept whole on a |
| // refusal; an unserved front's refusal reports what matched. |
| - stay.Prefix, stay.Target = prefix, target |
| + stay.Prefix, stay.Target, stay.MapAccount = prefix, target, rm.account |
| } |
| if _, here := t.hosts[target]; here { |
| tu, ok := unservedFrontTarget(t.hosts, target, rm.account) |