| new file mode 100644 |
| index 000000000..3e9497fe5 |
| --- /dev/null |
| +++ b/internal/edge/hunt_H5-M4_test.go |
| @@ -0,0 +1,134 @@ |
| +package edge |
| + |
| +// Hunt H5-M4 (verifier: opus). The edge rewrites X-Forwarded-For on the way |
| +// out (tcpProxy -> setForwarded -> SetXForwarded: the backend sees the |
| +// client's address), but the response cache computes a variant's Vary value |
| +// from the INBOUND request (maybeStore -> varyValue(rec.request, ...); |
| +// GetFor matches the inbound request too). So a backend that honestly |
| +// declares "Vary: X-Forwarded-For" on a per-client response has its variant |
| +// served to every other client whose INBOUND header matches — including all |
| +// clients that send none. Geo headers are handled correctly because |
| +// ServeHTTP writes them onto r.Header before the cache (edge.go:1511); the |
| +// forwarding headers get no such treatment. |
| +// |
| +// Production wiring: real registry, New, EnableCache, and the route's real |
| +// tcpProxy. Asserts the secure answer: two different clients get their own |
| +// bodies and the backend is asked twice. |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "sync/atomic" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func TestHunt_H5_M4_VaryOnRewrittenForwardedFor(t *testing.T) { |
| + var hits atomic.Int64 |
| + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + hits.Add(1) |
| + w.Header().Set("Cache-Control", "public, max-age=60") |
| + w.Header().Set("Vary", "X-Forwarded-For") |
| + w.Write([]byte("for=" + r.Header.Get("X-Forwarded-For"))) |
| + })) |
| + t.Cleanup(backend.Close) |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: "app.test", Kind: "instance", Addr: backend.Listener.Addr().String(), Status: "ready"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + |
| + get := func(from string) string { |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, geoReq("app.test", from)) |
| + return w.Body.String() |
| + } |
| + first := get("198.51.100.7") // the "attacker", primes the cache |
| + second := get("203.0.113.9") // an unrelated visitor |
| + if first != "for=198.51.100.7" { |
| + t.Fatalf("test bug: backend saw %q, want the first client's address", first) |
| + } |
| + if second != "for=203.0.113.9" || hits.Load() != 2 { |
| + t.Fatalf("H5-M4: second client (203.0.113.9) got %q from the cache (backend hits=%d); the variant keyed on "+ |
| + "Vary: X-Forwarded-For was selected by the INBOUND header (absent for both), not the value the backend received", |
| + second, hits.Load()) |
| + } |
| +} |
| + |
| +// The same client asking twice still gets the cached variant: the view of |
| +// the forwarded headers must be stable for one client, or Vary on them |
| +// would make every response a miss. And a client-supplied X-Forwarded-For, |
| +// which the proxy discards, must not open a slot of its own. |
| +func TestVaryOnForwardedForHitsForTheSameClient(t *testing.T) { |
| + var hits atomic.Int64 |
| + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + hits.Add(1) |
| + w.Header().Set("Cache-Control", "public, max-age=60") |
| + w.Header().Set("Vary", "x-forwarded-for") |
| + w.Write([]byte("for=" + r.Header.Get("X-Forwarded-For"))) |
| + })) |
| + t.Cleanup(backend.Close) |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: "app.test", Kind: "instance", Addr: backend.Listener.Addr().String(), Status: "ready"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + |
| + get := func(spoof string) string { |
| + r := geoReq("app.test", "198.51.100.7") |
| + if spoof != "" { |
| + r.Header.Set("X-Forwarded-For", spoof) |
| + } |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, r) |
| + return w.Body.String() |
| + } |
| + for _, spoof := range []string{"", "", "203.0.113.9"} { |
| + if got := get(spoof); got != "for=198.51.100.7" { |
| + t.Fatalf("X-Forwarded-For %q: got %q, want the connection's address", spoof, got) |
| + } |
| + } |
| + if hits.Load() != 1 { |
| + t.Fatalf("backend hits = %d, want 1: one client, one variant", hits.Load()) |
| + } |
| +} |
| + |
| +// A response that varies on the forwarded proof is never stored: the edge |
| +// signs that header afresh per request, so it is one visitor's every time. |
| +func TestVaryOnForwardedProofIsNotStored(t *testing.T) { |
| + var hits atomic.Int64 |
| + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + hits.Add(1) |
| + w.Header().Set("Cache-Control", "public, max-age=60") |
| + w.Header().Set("Vary", "X-Carlos-Forwarded-Proof") |
| + w.Write([]byte("ok")) |
| + })) |
| + t.Cleanup(backend.Close) |
| + reg := openReg(t) |
| + if err := reg.Upsert(registry.Route{Host: "app.test", Kind: "instance", Addr: backend.Listener.Addr().String(), Status: "ready"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(e.probers.Stop) |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + for i := 0; i < 2; i++ { |
| + e.ServeHTTP(httptest.NewRecorder(), geoReq("app.test", "198.51.100.7")) |
| + } |
| + if hits.Load() != 2 { |
| + t.Fatalf("backend hits = %d, want 2: a Vary on the proof must not be cached", hits.Load()) |
| + } |
| +} |