carlos / platform Public

edge: a backhauled door call is not guarded

The guard skipped a door caller on this box but not one that arrived as a
verified hop carrying the door mark, so a door call to /wp-login.php for
an app homed elsewhere passed here and was blocked there in block mode,
and watched as public traffic by default (final review I2).

The guard now skips hopDoorFrom as the rate limit already does. The mark
is set only beside a verified hop, so a client's own door header, with
or without an unverified hop, is still guarded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 1fe931cb5b5cea7140e613eed1ca8bc0ea2b6998 parent 173d669
2 files changed, +92 −4
  • internal/edge/edge.go +6 −4
  • internal/edge/guard_test.go +86 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 86c5efd59..135782e3d 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -1730,10 +1730,12 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r, out = withOutcome(r)
// The hard and probe checks (guard.go) run for every request this
// box is the first to see. A door caller is the platform itself,
- // not a visitor. A request a tunnel delivered was checked, and is
- // recorded, by the hub that forwarded it, so checking it again
- // here would answer or count it twice.
- if e.guard != guardOff && !isDoor && !fromTunnel(r.Context()) {
+ // not a visitor, whether its door is on this box or on the box that
+ // backhauled it here (hopDoorFrom, set only beside a verified hop,
+ // as the rate limit reads it). A request a tunnel delivered was
+ // checked, and is recorded, by the hub that forwarded it, so
+ // checking it again here would answer or count it twice.
+ if e.guard != guardOff && !isDoor && !hopDoorFrom(r.Context()) && !fromTunnel(r.Context()) {
out.guard = &guardState{mode: e.guard, mint: e.mintRef}
}
defer func() {
diff --git a/internal/edge/guard_test.go b/internal/edge/guard_test.go
index c66572ca4..9ddeb347b 100644
--- a/internal/edge/guard_test.go
+++ b/internal/edge/guard_test.go
@@ -11,6 +11,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/reqrec"
"github.com/carlosframework/platform/internal/tunnelclient"
)
@@ -331,3 +332,88 @@ func TestGuardFragmentKeepsNoToken(t *testing.T) {
}
}
}
+
+// Review I2: a door caller is the platform, not a visitor, on the box its
+// door is on and on the box a backhaul carried it to, where it arrives as a
+// verified hop carrying the door mark. Neither is guarded, even in block
+// mode. A door mark on anything but a verified hop is a header a client made
+// up, and that request is guarded like any other.
+func TestGuardSkipsDoorCalls(t *testing.T) {
+ const probe = "/wp-login.php"
+ served := func(t *testing.T, name string, g *guardRig, w *httptest.ResponseRecorder) {
+ t.Helper()
+ if w.Code != 200 || w.Header().Get("X-Carlos-Ref") != "" || g.upstream.Load() != 1 {
+ t.Fatalf("%s: %d ref %q, upstream %d; want the app's answer", name, w.Code, w.Header().Get("X-Carlos-Ref"), g.upstream.Load())
+ }
+ if len(g.rec.match) != 0 || len(g.rec.resp) != 1 {
+ t.Fatalf("%s: responses %d, matches %d; want 1 and 0", name, len(g.rec.resp), len(g.rec.match))
+ }
+ }
+ blocked := func(t *testing.T, name string, g *guardRig, w *httptest.ResponseRecorder) {
+ t.Helper()
+ if w.Code != 404 || w.Header().Get("X-Carlos-Ref") == "" || g.upstream.Load() != 0 {
+ t.Fatalf("%s: %d ref %q, upstream %d; want the block page", name, w.Code, w.Header().Get("X-Carlos-Ref"), g.upstream.Load())
+ }
+ if len(g.rec.match) != 1 || g.rec.match[0].Kind != reqrec.KindBlock || g.rec.match[0].RuleID != "probe.wordpress.wp-login" {
+ t.Fatalf("%s: matches %+v", name, g.rec.match)
+ }
+ }
+ serve := func(g *guardRig, r *http.Request) *httptest.ResponseRecorder {
+ w := httptest.NewRecorder()
+ g.e.ServeHTTP(w, r)
+ return w
+ }
+
+ // The door on this box: serveDoor's mark on the context.
+ g := guardEdge(t, false, false, http.StatusOK, "app")
+ g.e.guard = guardBlock
+ r := httptest.NewRequest("GET", "http://app.test"+probe, nil)
+ owner := doorCaller{Account: release.RouteAccount(registry.Route{}), App: "shop"}
+ r = r.WithContext(context.WithValue(r.Context(), doorCallerKey{}, owner))
+ served(t, "local door call", g, serve(g, r))
+
+ now := time.Now()
+ hop := func(g *guardRig, auth string, door []string) *http.Request {
+ r := httptest.NewRequest("GET", "http://app.test"+probe, nil)
+ r.Header.Set(hopHeader, "1")
+ if auth != "" {
+ r.Header.Set(hopAuthHeader, auth)
+ }
+ if door != nil {
+ r.Header[hopDoorHeader] = door
+ }
+ return r
+ }
+
+ // Backhauled from the box the door is on: a verified hop with the mark.
+ g = guardEdge(t, false, true, http.StatusOK, "app")
+ g.e.guard = guardBlock
+ served(t, "forwarded door call", g, serve(g, hop(g, signHop(g.key, "app.test", now), []string{signHopDoor(g.key, "app.test", now)})))
+
+ // The same mark on a hop that does not verify, or with no hop at all, is
+ // a client's header. A verified hop without the mark is a visitor sent
+ // on by another box.
+ for _, tc := range []struct {
+ name string
+ req func(g *guardRig) *http.Request
+ }{
+ {"door mark on an unverified hop", func(g *guardRig) *http.Request {
+ return hop(g, "0:deadbeef", []string{signHopDoor(g.key, "app.test", now)})
+ }},
+ {"door mark with no hop", func(g *guardRig) *http.Request {
+ r := httptest.NewRequest("GET", "http://app.test"+probe, nil)
+ r.Header.Set(hopDoorHeader, signHopDoor(g.key, "app.test", now))
+ return r
+ }},
+ {"door mark that does not verify", func(g *guardRig) *http.Request {
+ return hop(g, signHop(g.key, "app.test", now), []string{"0:deadbeef"})
+ }},
+ {"verified hop without a door mark", func(g *guardRig) *http.Request {
+ return hop(g, signHop(g.key, "app.test", now), nil)
+ }},
+ } {
+ g := guardEdge(t, false, true, http.StatusOK, "app")
+ g.e.guard = guardBlock
+ blocked(t, tc.name, g, serve(g, tc.req(g)))
+ }
+}