| 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))) |
| + } |
| +} |