carlos / platform Public

edge: read and store pages only for the Host the app is sent

A page was keyed by the port-stripped host while the app was sent the
Host header as written, so a page rendered for app.test:444 answered
app.test (O4 R1, H5-M16). A request whose Host names a port now
bypasses the page cache both ways; page keys and purges are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev f05ea5070dce4029633075d62a7c85b9d259af4f parent fdc4e27
2 files changed, +45 −4
  • internal/edge/hunt_H5-M16_test.go +32 −0
  • internal/edge/pagecache.go +13 −4
diff --git a/internal/edge/hunt_H5-M16_test.go b/internal/edge/hunt_H5-M16_test.go
index 33e2ffd36..e9d08a5b0 100644
--- a/internal/edge/hunt_H5-M16_test.go
+++ b/internal/edge/hunt_H5-M16_test.go
@@ -101,3 +101,35 @@ func TestPurgeURLDropsEverySpellingOfTheURL(t *testing.T) {
t.Fatalf("the escaped spelling survived the purge (backend hits = %d)", hits.Load())
}
}
+
+// The page cache's half of hunt H5-M16 (O4 round 1): a page is keyed by
+// the port-stripped host, but the app is sent the Host header as the
+// client wrote it, and may build its links from it. A page rendered for
+// one authority (any port, the default one included) must never answer
+// another: a page is read and stored only when the Host the app is sent is
+// the host it is keyed by. Through the production wiring: an armed box (newPCScene)
+// serving a marked page from a real backend through ServeHTTP.
+func TestPageCacheKeepsTheAuthorityTheAppSaw(t *testing.T) {
+ s := newPCScene(t, func(w http.ResponseWriter, r *http.Request) {
+ pageOK("rendered for "+r.Host)(w, r)
+ })
+ for _, authority := range []string{pcHost + ":444", pcHost + ":443"} {
+ t.Run(authority, func(t *testing.T) {
+ before := s.b.n.Load()
+ odd := s.do(http.MethodGet, "http://"+authority+"/p-"+authority)
+ wantBody(t, odd, 200, "rendered for "+authority)
+ plain := s.get("/p-" + authority)
+ wantBody(t, plain, 200, "rendered for "+pcHost)
+ if n := s.b.n.Load() - before; n != 2 {
+ t.Fatalf("the app saw %d requests, want 2: the plain request was answered from the other authority's page", n)
+ }
+ // And the other way round: a page stored for the plain host
+ // does not answer the odd authority.
+ wantBody(t, s.do(http.MethodGet, "http://"+authority+"/p-"+authority), 200, "rendered for "+authority)
+ })
+ }
+ t.Run("the plain host still hits", func(t *testing.T) {
+ wantCache(t, s.get("/q"), "miss")
+ wantCache(t, s.get("/q"), "hit")
+ })
+}
diff --git a/internal/edge/pagecache.go b/internal/edge/pagecache.go
index 7526d9e2f..43b925c14 100644
--- a/internal/edge/pagecache.go
+++ b/internal/edge/pagecache.go
@@ -434,9 +434,9 @@ type pageServe struct {
// wraps the response in (wrap) and settles once the proxy has returned
// (end); nil for a route that is not an instance.
//
-// A hit needs an eligible route and a readable request, and a page rendered
-// from the route's current inputs whose ignore list covers every cookie the
-// request carries. A hit whose body cannot be opened (the entry was evicted
+// A hit needs an eligible route and a readable request whose Host names no
+// port, and a page rendered from the route's current inputs whose ignore
+// list covers every cookie the request carries. A hit whose body cannot be opened (the entry was evicted
// since the lookup) goes on down the miss path (P1-8). A miss is recorded
// when the request is storable and the process that will answer it was
// started from the current inputs.
@@ -447,9 +447,18 @@ func (e *Edge) servePage(w http.ResponseWriter, r *http.Request, host string, up
}
x := ps.x
inputs, ok := e.pageRouteEligible(host, r.URL.Path, up)
- if !ok {
+ if !ok || r.Host != host {
return ps, false
}
+ // The key is what the app is sent: the proxy passes the Host header on
+ // as the client wrote it, and host is that header with any port
+ // stripped, so a request that named a port (r.Host != host, above) is
+ // neither read nor stored. Keyed by host, a page rendered for
+ // app.test:444, whose links the app may build from that Host, answered
+ // app.test (hunt H5-M16, O4 round 1). The path is canonical
+ // (pageReadable), so it and the query are as sent too. The LRU keeps
+ // such requests apart instead (CacheKey.Authority); a page key does not
+ // grow an authority, so purges and the index stay as they are.
key := pageindex.Key{Host: host, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
if cookies, ok := pageReadable(r); ok {
if ent, miss := x.Lookup(ps.scope, key, inputs, cookies); miss == pageindex.MissNone {