edge: key gate attempts on a verified hop's client, not the sending edge
A login backhauled from another edge kept that edge's address as its RemoteAddr, so every visitor entering through it shared one limiter bucket: ten wrong guesses locked out every other visitor (hunt H5-M11). acceptHop now marks a verified hop with gate.WithClientAddr, taken from the last entry of the X-Forwarded-For chain the sending edge wrote. An unverified hop is stripped as before; an unparseable chain keeps the old, stricter key. The gate spec notes the change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+172
−8
docs/superpowers/specs/2026-09-07-password-gate-design.md+5 −0internal/edge/homemap.go+16 −1internal/edge/hunt_H5-M11_test.go+79 −0internal/edge/hunt_H5_backhaul_scene_test.go+61 −0internal/gate/gate.go+11 −7
diff --git a/docs/superpowers/specs/2026-09-07-password-gate-design.md b/docs/superpowers/specs/2026-09-07-password-gate-design.md| index 515d391ba..fe7e49df4 100644 |
| --- a/docs/superpowers/specs/2026-09-07-password-gate-design.md |
| +++ b/docs/superpowers/specs/2026-09-07-password-gate-design.md |
| @@ -196,6 +196,11 @@ bucket exists so the CPU is spent on people and not on a script. A |
| backhauled request's remote address is the sibling edge's, which folds every |
| hop from that edge into one bucket; accepted for now, because the fold only |
| ever makes the limit stricter, and named here so it is not rediscovered. |
| +(Changed 2026-11-16, security hunt H5-M11: stricter for the attacker meant |
| +locked out for everyone else, since one visitor's ten wrong guesses spent |
| +every backhauled visitor's attempts. A verified hop is now keyed on the |
| +client the sending edge observed, the last entry of the X-Forwarded-For |
| +chain it signed the hop over; an unparseable chain keeps the old key.) |
| ### 4.3 The cookie |
diff --git a/internal/edge/homemap.go b/internal/edge/homemap.go| index eacdc9893..028b2409a 100644 |
| --- a/internal/edge/homemap.go |
| +++ b/internal/edge/homemap.go |
| @@ -87,6 +87,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/gate" |
| "github.com/carlosframework/platform/internal/lbhealth" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -1500,7 +1501,9 @@ func hopDoorFrom(ctx context.Context) bool { |
| // overwhelming case: a few header lookups on a request |
| // that is not a hop). |
| // verified hop → the mark goes on the context, the inbound |
| -// X-Forwarded-For is believed, and the caller serves |
| +// X-Forwarded-For is believed (by setForwarded, and |
| +// by the gate's attempt limiter, which keys on its |
| +// last entry), and the caller serves |
| // the route locally or refuses (staleHopRefusal). A |
| // door mark that verifies adds hopDoorKey. |
| // unverified hop → every forwarded header is stripped and the sender is |
| @@ -1553,6 +1556,18 @@ func (e *Edge) acceptHop(r *http.Request, host string) (*http.Request, bool) { |
| return r, false |
| } |
| ctx := context.WithValue(r.Context(), hopVerifiedKey{}, true) |
| + // The password gate's attempt limiter keys on the visitor, not on the |
| + // connection: here the connection is the sending edge, shared by every |
| + // visitor it backhauls, so one visitor's wrong guesses spent everyone's |
| + // attempts at this gate (hunt H5-M11). The chain the sending edge wrote |
| + // (setForwarded: the client that dialled it, nothing appended) is |
| + // believed only now that the hop verified, and only when every entry is |
| + // an IP (linkVisitor's rule); its last entry is that client. A chain |
| + // that will not parse leaves the limiter on the sending edge's address, |
| + // the stricter bucket. |
| + if visitor, _, ok := linkVisitor(r.Header); ok { |
| + ctx = gate.WithClientAddr(ctx, visitor) |
| + } |
| // The door mark is read only here, after the hop verified: on any other |
| // request it is a header a client made up. |
| if len(door) == 1 && verifyHopDoorEitherKey(current, next, host, door[0], now) { |
diff --git a/internal/edge/hunt_H5-M11_test.go b/internal/edge/hunt_H5-M11_test.go| new file mode 100644 |
| index 000000000..8ae1eaf23 |
| --- /dev/null |
| +++ b/internal/edge/hunt_H5-M11_test.go |
| @@ -0,0 +1,79 @@ |
| +package edge |
| + |
| +// Hunt H5-M11 (verifier: opus). The gate login limiter is keyed on |
| +// clientKey(r)+host, and clientKey reads r.RemoteAddr (gate.go). Behind |
| +// backhaul, a login that arrives at the home edge over a verified hop keeps |
| +// the sending edge's address as its RemoteAddr (acceptHop does not rewrite |
| +// it to the forwarded client). So every visitor entering through one sending |
| +// edge shares a single limiter bucket: one client's ten wrong passwords |
| +// exhaust the burst, and a DIFFERENT client's correct password is then |
| +// answered 429 before its password is ever checked. |
| +// |
| +// Production wiring: two real edges over a real TLS hop; a gated route homed |
| +// on annie. Two distinct client source addresses stand in for two visitors; |
| +// the secure assertion is that the second visitor's correct password is |
| +// honoured despite the first's wrong guesses. |
| + |
| +import ( |
| + "net" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/gate" |
| +) |
| + |
| +func loginVia(t *testing.T, client *http.Client, host, password string) *http.Response { |
| + t.Helper() |
| + form := url.Values{"password": {password}, "next": {"/"}} |
| + req, err := http.NewRequest(http.MethodPost, "https://"+host+"/.carlos/gate", strings.NewReader(form.Encode())) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Host = host |
| + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + c := *client |
| + c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } |
| + resp, err := c.Do(req) |
| + if err != nil { |
| + t.Fatalf("login POST: %v", err) |
| + } |
| + return resp |
| +} |
| + |
| +func TestHunt_H5_M11_BackhaulCollapsesLoginBucket(t *testing.T) { |
| + s := huntBackhaulScene(t, []string{e2eHomedHost}) |
| + if s.clientIP == "" { |
| + t.Skip("needs a second loopback source address to stand in for a second visitor") |
| + } |
| + h, err := gate.Hash("correct horse") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + gateHostOnAnnie(t, s, e2eHomedHost, h) |
| + |
| + // Victim: a client dialling from a DIFFERENT real source than the scene |
| + // client (the attacker). Both still reach annie over the same hop. |
| + victim := e2eClientFor(s.b, s.ca, &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) |
| + |
| + for i := 0; i < gateBurstForTest(); i++ { |
| + resp := loginVia(t, s.b.client, e2eHomedHost, "wrong") |
| + resp.Body.Close() |
| + if resp.StatusCode == http.StatusTooManyRequests { |
| + t.Fatalf("attacker hit 429 on its own attempt %d before exhausting the burst", i) |
| + } |
| + } |
| + |
| + resp := loginVia(t, victim, e2eHomedHost, "correct horse") |
| + defer resp.Body.Close() |
| + if resp.StatusCode == http.StatusTooManyRequests { |
| + t.Fatalf("H5-M11: a second visitor's CORRECT password was answered 429 — the attacker's ten wrong guesses " + |
| + "through the sending edge exhausted a limiter bucket keyed on the sending edge's address, shared by every visitor") |
| + } |
| + if len(resp.Cookies()) == 0 && resp.StatusCode != http.StatusSeeOther { |
| + t.Fatalf("victim login = %d with no cookie; want a gate cookie", resp.StatusCode) |
| + } |
| +} |
| + |
| +func gateBurstForTest() int { return 10 } |
diff --git a/internal/edge/hunt_H5_backhaul_scene_test.go b/internal/edge/hunt_H5_backhaul_scene_test.go| new file mode 100644 |
| index 000000000..2b59f5a3e |
| --- /dev/null |
| +++ b/internal/edge/hunt_H5_backhaul_scene_test.go |
| @@ -0,0 +1,61 @@ |
| +package edge |
| + |
| +// Shared scene for hunt H5-M5 and H5-M11 (verifier: opus): newBackhaulScene's |
| +// production wiring (a real Edge per box, real cert mirror, real home map, |
| +// real mesh key, real TLS hop) with the home edge ("annie") serving the |
| +// routes the caller names and the sending edge ("betty") serving one local |
| +// route. |
| + |
| +import ( |
| + "context" |
| + "crypto/x509" |
| + "net" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +func huntBackhaulScene(t *testing.T, homed []string) *backhaulScene { |
| + t.Helper() |
| + e2eQuietLogs(t) |
| + bucket := t.TempDir() |
| + s := &backhaulScene{ |
| + seed: release.NewDirStore(bucket), |
| + ca: newE2ECA(t), |
| + leaf: map[string]*x509.Certificate{}, |
| + deadEIP: e2eDeadEIP(t), |
| + } |
| + if err := MintMeshKey(context.Background(), s.seed); err != nil { |
| + t.Fatalf("mint mesh key: %v", err) |
| + } |
| + for _, host := range append(append([]string(nil), homed...), e2eSenderHost) { |
| + s.leaf[host] = s.ca.publish(t, s.seed, host) |
| + } |
| + from := e2eClientAddr(t) |
| + if from != nil { |
| + s.clientIP = from.IP.String() |
| + } |
| + routes := map[string]string{} |
| + for _, h := range homed { |
| + routes[h] = e2eApp(t, e2eHomeMarker).Listener.Addr().String() |
| + } |
| + s.a = newBackhaulEdge(t, bucket, "annie", s.ca, from, routes) |
| + s.b = newBackhaulEdge(t, bucket, "betty", s.ca, from, map[string]string{ |
| + e2eSenderHost: e2eApp(t, e2eSenderMarker).Listener.Addr().String(), |
| + }) |
| + eip, port, err := net.SplitHostPort(s.a.tlsAddr) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + oldPort, oldCAs := backhaulPort, backhaulRootCAs |
| + backhaulPort, backhaulRootCAs = port, s.ca.pool |
| + t.Cleanup(func() { backhaulPort, backhaulRootCAs = oldPort, oldCAs }) |
| + bEIP, _, err := net.SplitHostPort(s.b.tlsAddr) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s.publishEdgesFile(t, "annie", eip, homed...) |
| + s.publishEdgesFile(t, "betty", bEIP, e2eSenderHost) |
| + s.converge(t) |
| + return s |
| +} |
diff --git a/internal/gate/gate.go b/internal/gate/gate.go| index 5d9c9e474..c58ee8cf3 100644 |
| --- a/internal/gate/gate.go |
| +++ b/internal/gate/gate.go |
| @@ -211,9 +211,11 @@ type clientAddrKey struct{} |
| // WithClientAddr marks ctx's request as one from the visitor at addr, |
| // whatever its RemoteAddr says, for the attempt limiter. The edge sets it |
| -// only for a request a linked deployment's hub delivered (linked |
| -// deployments spec §6.2), whose RemoteAddr is the tunnel's, shared by every |
| -// visitor: addr is the client address the hub observed. |
| +// only where RemoteAddr is shared by every visitor and a verified party |
| +// named the real one: a request a linked deployment's hub delivered |
| +// (linked deployments spec §6.2), whose RemoteAddr is the tunnel's and |
| +// addr the client the hub observed; and a verified backhaul hop, whose |
| +// RemoteAddr is the sending edge's and addr the client that edge observed. |
| func WithClientAddr(ctx context.Context, addr string) context.Context { |
| return context.WithValue(ctx, clientAddrKey{}, addr) |
| } |
| @@ -253,10 +255,12 @@ func SafeNext(next string) string { |
| return next |
| } |
| -// clientKey is the remote address without its port. A backhauled request's |
| -// address is the sibling edge's, which folds every hop from that edge into |
| -// one bucket — stricter, never looser, and named in the spec (§4.2). A |
| -// request carrying a visitor address (WithClientAddr) is keyed on that. |
| +// clientKey is the remote address without its port. A request carrying a |
| +// visitor address (WithClientAddr) is keyed on that instead: the edge sets |
| +// one for a verified backhaul hop, whose remote address is the sibling |
| +// edge's (spec §4.2 once folded every hop from that edge into one bucket, |
| +// which let one visitor spend every other visitor's attempts, hunt H5-M11), |
| +// and for a linked deployment's request. |
| func clientKey(r *http.Request) string { |
| if addr, ok := r.Context().Value(clientAddrKey{}).(string); ok && addr != "" { |
| return addr |