carlos / platform Public

edge: key the response cache on the escaped path and Host port

The cache keyed on the decoded path and the port-stripped host while the
proxy sends the backend the escaped path and the Host header as received,
so /a%2Fb and /a/b, or Host app.test:444 and app.test, shared one slot
(hunt H5-M16). The key now carries RawPath and Authority when they differ
from the plain spelling. PurgeURL walks the path group so a purge still
drops every spelling.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1e90a27518c133cb21b00dd22336eb2f6e716258 parent 15b1ee6
4 files changed, +154 −7
  • internal/edge/cache.go +45 −5
  • internal/edge/edge.go +1 −1
  • internal/edge/hunt_H5-M16_test.go +103 −0
  • internal/edge/purge_header.go +5 −1
diff --git a/internal/edge/cache.go b/internal/edge/cache.go
index ebdc1dda9..68e817fac 100644
--- a/internal/edge/cache.go
+++ b/internal/edge/cache.go
@@ -36,14 +36,41 @@ const (
// CacheKey is the full key for a cached response, including Host, Method, Path, RawQuery,
// and VaryValue (concatenation of request-header values named by the response's Vary header).
+//
+// Host and Path are the edge's own names for the request: the host with its
+// port stripped, the DECODED path. Purges name entries by
+// them (purge_header.go), and the per-path admission cap groups by them.
+// But the proxy sends the backend the Host header and the escaped path as
+// the client sent them, and an origin may answer differently for each: one
+// that routes on the escaped path tells /a%2Fb from /a/b, one that builds
+// absolute links from Host tells app.test:444 from app.test. Authority and
+// RawPath carry what Host and Path dropped, so two requests the backend
+// can tell apart never share a slot (hunt H5-M16). Each is set only when it
+// differs from what Host or Path already say, so an ordinary request's key
+// is exactly what it always was.
type CacheKey struct {
- Host string // request Host header, exact match
+ Host string // request Host header with its port stripped (ServeHTTP's host)
Method string // "GET" or "HEAD"
- Path string
+ Path string // decoded (URL.Path)
RawQuery string
+ Authority string // the Host header as sent, when it is not Host itself (it carried a port); "" otherwise
+ RawPath string // the escaped path as sent (URL.EscapedPath), when URL.RawPath is set; "" otherwise
VaryValue string // concatenation of request-header values named by the response's own Vary header; "" if Vary absent
}
+// cacheKeyFor is the request-time key ServeHTTP looks up and stores under:
+// host is ServeHTTP's host, r.Host with its port stripped.
+func cacheKeyFor(r *http.Request, host string) CacheKey {
+ k := CacheKey{Host: host, Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
+ if r.Host != host {
+ k.Authority = r.Host
+ }
+ if r.URL.RawPath != "" {
+ k.RawPath = r.URL.EscapedPath()
+ }
+ return k
+}
+
// CachedResponse holds a cached HTTP response along with TTL and storage time.
type CachedResponse struct {
StatusCode int
@@ -158,6 +185,9 @@ func (c *Cache) purgeBridgeOf() purgeBridge {
type cacheURLKey struct {
Host, Method, Path, RawQuery string
+ // Authority and RawPath: CacheKey's. Not in cachePathKey, so every
+ // spelling of one path shares the path's admission cap and its purge.
+ Authority, RawPath string
}
type cachePathKey struct {
@@ -345,7 +375,7 @@ func (c *Cache) genCurrentLocked(resp *CachedResponse) bool {
}
func (k CacheKey) urlKey() cacheURLKey {
- return cacheURLKey{Host: k.Host, Method: k.Method, Path: k.Path, RawQuery: k.RawQuery}
+ return cacheURLKey{Host: k.Host, Method: k.Method, Path: k.Path, RawQuery: k.RawQuery, Authority: k.Authority, RawPath: k.RawPath}
}
func (k CacheKey) fullKey(vary string) cacheFullKey {
@@ -621,14 +651,24 @@ func (c *Cache) removeLocked(el *list.Element) {
// PurgeURL drops every cached variant (every VaryValue) of (host, path, rawQuery)
// for both GET and HEAD methods and returns how many entries it actually removed.
// Returns 0 if none were cached — not an error.
+//
+// Every spelling of the URL goes with it: each Authority (a Host header with
+// a port) and each RawPath (an escaped spelling of path) stored beside the
+// plain one. A purge names the edge's decoded path and port-less host, as
+// the app knows them, so it walks the path's group (bounded by
+// maxEntriesPerPath) and matches the query, rather than looking up one
+// exact URL key that would find only the plain spelling.
func (c *Cache) PurgeURL(host, path, rawQuery string) int {
c.mu.Lock()
defer c.mu.Unlock()
n := 0
// GET and HEAD are purged together — a write invalidates both.
for _, method := range [...]string{"GET", "HEAD"} {
- uk := cacheURLKey{Host: host, Method: method, Path: path, RawQuery: rawQuery}
- for _, el := range elementsOf(c.byURL[uk]) {
+ pk := cachePathKey{Host: host, Method: method, Path: path}
+ for _, el := range elementsOf(c.byPath[pk]) {
+ if el.Value.(*cacheEntry).key.RawQuery != rawQuery {
+ continue
+ }
c.removeLocked(el)
n++
}
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 6eeba4de1..d023c5633 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -3137,7 +3137,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
!up.route.IsForward() && !markedRequest(r) && !e.pagesOptedIn(up) && !pinned
var key CacheKey
if cacheable {
- key = CacheKey{Host: host, Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
+ key = cacheKeyFor(r, host)
// GetFor, not get: the variant this request belongs to depends on the
// response's Vary header, which doesn't exist yet — GetFor matches r
// against the Vary names each stored variant recorded for itself, so
diff --git a/internal/edge/hunt_H5-M16_test.go b/internal/edge/hunt_H5-M16_test.go
new file mode 100644
index 000000000..33e2ffd36
--- /dev/null
+++ b/internal/edge/hunt_H5-M16_test.go
@@ -0,0 +1,103 @@
+package edge
+
+// Hunt H5-M16 (verifier: opus). The response cache key is
+// {host-without-port, method, URL.Path (decoded), RawQuery} (edge.go:1772,
+// cache.go:35). The proxy forwards the ORIGINAL Host (with its port) and the
+// original escaped path (httputil keeps URL.RawPath), so two requests the
+// backend can tell apart share one cache slot: /a%2Fb vs /a/b, and
+// Host: app.test:444 vs Host: app.test. Whoever asks first chooses what the
+// other URL serves for the response's lifetime.
+//
+// Production wiring: real registry, New, EnableCache, real tcpProxy.
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func huntM16Edge(t *testing.T) (*Edge, *atomic.Int64) {
+ t.Helper()
+ 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")
+ // An origin that builds absolute links from Host, and routes on the
+ // escaped path — both ordinary framework behaviour.
+ w.Write([]byte("uri=" + r.RequestURI + " link=https://" + r.Host + "/next"))
+ }))
+ 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))
+ return e, &hits
+}
+
+func huntM16Get(e *Edge, host, target string) string {
+ r := httptest.NewRequest("GET", "http://app.test"+target, nil)
+ r.Host = host
+ r.RemoteAddr = "198.51.100.7:4444"
+ w := httptest.NewRecorder()
+ e.ServeHTTP(w, r)
+ return w.Body.String()
+}
+
+func TestHunt_H5_M16_EscapedPathSharesSlot(t *testing.T) {
+ e, hits := huntM16Edge(t)
+ poison := huntM16Get(e, "app.test", "/a%2Fb")
+ if poison != "uri=/a%2Fb link=https://app.test/next" {
+ t.Fatalf("test bug: backend saw %q, want the escaped path preserved", poison)
+ }
+ victim := huntM16Get(e, "app.test", "/a/b")
+ if victim != "uri=/a/b link=https://app.test/next" || hits.Load() != 2 {
+ t.Fatalf("H5-M16: GET /a/b was answered %q from the slot primed by GET /a%%2Fb (backend hits=%d)", victim, hits.Load())
+ }
+}
+
+func TestHunt_H5_M16_HostPortSharesSlot(t *testing.T) {
+ e, hits := huntM16Edge(t)
+ poison := huntM16Get(e, "app.test:444", "/")
+ if poison != "uri=/ link=https://app.test:444/next" {
+ t.Fatalf("test bug: backend saw %q, want the Host port preserved", poison)
+ }
+ victim := huntM16Get(e, "app.test", "/")
+ if victim != "uri=/ link=https://app.test/next" || hits.Load() != 2 {
+ t.Fatalf("H5-M16: GET / with Host app.test was answered %q from the slot primed with Host app.test:444 (backend hits=%d)", victim, hits.Load())
+ }
+}
+
+// The key now splits a URL's spellings (an escaped path, a Host with a
+// port) into slots of their own; a purge names the decoded path and the
+// port-less host, so it must still drop every one of them, and leave another
+// query's entry alone.
+func TestPurgeURLDropsEverySpellingOfTheURL(t *testing.T) {
+ e, hits := huntM16Edge(t)
+ huntM16Get(e, "app.test", "/a/b")
+ huntM16Get(e, "app.test", "/a%2Fb")
+ huntM16Get(e, "app.test:444", "/a/b")
+ huntM16Get(e, "app.test", "/a/b?q=1")
+ if hits.Load() != 4 {
+ t.Fatalf("priming: backend hits = %d, want 4 distinct slots", hits.Load())
+ }
+ if n := e.cache.PurgeURL("app.test", "/a/b", ""); n != 3 {
+ t.Fatalf("PurgeURL removed %d entries, want 3 (plain, escaped, with port)", n)
+ }
+ huntM16Get(e, "app.test", "/a/b?q=1")
+ if hits.Load() != 4 {
+ t.Fatalf("the ?q=1 entry was purged too (backend hits = %d)", hits.Load())
+ }
+ huntM16Get(e, "app.test", "/a%2Fb")
+ if hits.Load() != 5 {
+ t.Fatalf("the escaped spelling survived the purge (backend hits = %d)", hits.Load())
+ }
+}
diff --git a/internal/edge/purge_header.go b/internal/edge/purge_header.go
index d6bcfdea0..be043b20c 100644
--- a/internal/edge/purge_header.go
+++ b/internal/edge/purge_header.go
@@ -919,7 +919,11 @@ func (e *Edge) logPurgeIssue(kind, host, msg string) {
// must key identically or it silently purges nothing. edge.go builds every
// cache key as:
//
-// CacheKey{..., Path: r.URL.Path, RawQuery: r.URL.RawQuery}
+// CacheKey{..., Path: r.URL.Path, RawQuery: r.URL.RawQuery} (cacheKeyFor)
+//
+// (cacheKeyFor also records the escaped path and a Host port when the
+// request had them, but those only split slots: PurgeURL and PurgePath
+// match on the decoded path and drop every spelling of it.)
//
// net/url's URL.Path is the DECODED path ("/caf%C3%A9" on the wire is stored
// under "/café"), while URL.RawQuery is the query byte-for-byte as received,