carlos / platform Public

edge: a static rewrite onto a hard-tier file outranks the probe hit

serveTree noted the request path's probe verdict before the _redirects
rules ran, and the hard check on a rewrite's target then found a verdict
already there and checked nothing. With "/* /.env 200", /admin served the
secret under block mode and watch mode recorded probe.admin.admin
(final review I1).

The probe hit is now held until the rules have acted. A redirect, a
plain 404 or a page the tree has decides it as before, still ahead of the
redirect or fallback. A rewrite is decided once its target is known: a
hard hit on a file that is there wins, otherwise the probe hit stands. A
verdict ServeHTTP already noted on the request path still wins, and when
the guard has nothing left to decide the _redirects names lookup no longer
runs either.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 173d6697fbeeb0940b1ab49ccf97895486f3bbae parent c89a266
2 files changed, +92 −27
  • internal/edge/static.go +43 −27
  • internal/edge/static_guard_test.go +49 −0
diff --git a/internal/edge/static.go b/internal/edge/static.go
index b12ad4394..b1f783f89 100644
--- a/internal/edge/static.go
+++ b/internal/edge/static.go
@@ -471,6 +471,13 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R
// Clean rooted at "/" so no request path can climb out of the tree.
p := path.Clean("/" + r.URL.Path)
status, rewritten := http.StatusOK, false
+ // guard is whether the guard still has this request to decide: it runs
+ // for r, and ServeHTTP's hard check on the request path noted nothing.
+ // probe is the probe tier's hit on the request path, held rather than
+ // decided until the rules have had their say, because a rewrite onto a
+ // hard-tier file must be the verdict instead (one verdict per request).
+ guard := staticGuardPending(r)
+ var probe edgeguard.Hit
for {
fp := filepath.Join(tree.dir, filepath.FromSlash(p))
fi, err := os.Stat(fp)
@@ -486,23 +493,43 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R
// and only the tree knows. A _redirects rule that names the path
// outright counts as a page there; a catch-all or placeholder rule
// does not, or one "/* /index.html 200" would exempt every probe.
- // This runs before the rules act, so a blocked probe is never
- // redirected or given the fallback page.
- if !found && !tree.redirects.names(requestHost(r.Host), p) &&
- staticGuardHit(w, r, r.URL.Path, r.URL.RawQuery, reqrec.TierProbe) {
- return
+ // It is decided before the rules act, so a blocked probe is never
+ // redirected or given the fallback page; a rewrite is decided
+ // below, once its target is known.
+ if guard && !found && !tree.redirects.names(requestHost(r.Host), p) {
+ probe, _ = edgeguard.CheckTier(r.URL.Path, r.URL.RawQuery, reqrec.TierProbe)
}
- if act, ok := tree.redirects.resolve(requestHost(r.Host), p, r.URL.Path, r.URL.RawQuery, found); ok {
- if act.location != "" {
- http.Redirect(w, r, act.location, act.status)
- return
- }
+ act, ok := tree.redirects.resolve(requestHost(r.Host), p, r.URL.Path, r.URL.RawQuery, found)
+ if ok && act.location == "" {
// A rewrite serves another file of the tree at this URL. It is
// looked up once: rules are not consulted a second time, and a
// destination with no file is a plain 404.
p, status, rewritten = act.file, act.status, true
continue
}
+ if probe.Rule != nil && guardDecide(w, r, probe) {
+ return
+ }
+ if ok {
+ http.Redirect(w, r, act.location, act.status)
+ return
+ }
+ } else if guard {
+ // A rewrite sends a file the request did not name, so the request's
+ // own path passing the hard tier in ServeHTTP says nothing about
+ // it: /download rewritten onto /.env would serve a leak. A hard
+ // hit on a file that is there wins over the request path's probe
+ // hit, which says only what the visitor asked for: "/* /.env 200"
+ // must not serve the secret as a watched /admin.
+ hit := probe
+ if found {
+ if h, ok := edgeguard.CheckTier(p, "", reqrec.TierHard); ok {
+ hit = h
+ }
+ }
+ if hit.Rule != nil && guardDecide(w, r, hit) {
+ return
+ }
}
if !found {
http.NotFound(w, r)
@@ -519,29 +546,18 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R
w.WriteHeader(http.StatusMovedPermanently)
return
}
- // A rewrite sends a file the request did not name, so the request's
- // own path passing the hard tier in ServeHTTP says nothing about it:
- // /download rewritten onto /.env would serve a leak.
- if rewritten && staticGuardHit(w, r, p, "", reqrec.TierHard) {
- return
- }
s.serveFile(fp, p, fi, status, w, r)
return
}
}
-// staticGuardHit runs one tier of the catalogue over urlPath for a request
-// the guard is running for, and hands a match to guardDecide. It reports
-// whether the guard answered the request, in which case the caller must stop.
-// It does nothing, the matcher included, when the guard is off for r or has
-// already decided it, so a request is decided once however many places check.
-func staticGuardHit(w http.ResponseWriter, r *http.Request, urlPath, rawQuery string, tier reqrec.Tier) bool {
+// staticGuardPending reports whether the guard runs for r and has not yet
+// decided it. Otherwise serveTree checks nothing, the matcher and the
+// _redirects lookup included, so a request is decided once however many
+// places could check it.
+func staticGuardPending(r *http.Request) bool {
out := outcomeFrom(r)
- if out == nil || out.guard == nil || out.guard.verdict != nil {
- return false
- }
- hit, ok := edgeguard.CheckTier(urlPath, rawQuery, tier)
- return ok && guardDecide(w, r, hit)
+ return out != nil && out.guard != nil && out.guard.verdict == nil
}
// serveFile sends the tree file at fp, whose tree path is p (for logs). A 200
diff --git a/internal/edge/static_guard_test.go b/internal/edge/static_guard_test.go
index b1db23923..faae416a1 100644
--- a/internal/edge/static_guard_test.go
+++ b/internal/edge/static_guard_test.go
@@ -187,6 +187,55 @@ func TestStaticRewriteToHardFile(t *testing.T) {
}
}
+// Review I1: a request path the probe tier would note, rewritten onto a file
+// the hard tier names. The hard hit on what would be sent is the verdict,
+// not the probe hit on what was asked for: blocking answers with the guard's
+// 404 rather than the secret, and watching records the leak, not /admin.
+func TestStaticRewriteToHardFileOverProbe(t *testing.T) {
+ files := map[string]string{".env": "SECRET=1", "_redirects": "/* /.env 200\n"}
+
+ g := staticGuardEdge(t, guardWatch, files)
+ if w := g.get("/admin"); w.Code != 200 || w.Body.String() != "SECRET=1" {
+ t.Fatalf("watch: %d %q, want the file served", w.Code, w.Body.String())
+ }
+ m := g.onlyMatch(t)
+ if m.Kind != reqrec.KindWatch || m.Tier != reqrec.TierHard || m.RuleID != "hard.secrets.env" || m.Status != 200 || m.Ref != "" {
+ t.Fatalf("watch: match %+v", m)
+ }
+
+ g = staticGuardEdge(t, guardBlock, files)
+ w := g.get("/admin")
+ if w.Code != 404 || w.Header().Get("X-Carlos-Ref") == "" || w.Body.String() == "SECRET=1" {
+ t.Fatalf("block: %d ref %q body %q", w.Code, w.Header().Get("X-Carlos-Ref"), w.Body.String())
+ }
+ m = g.onlyMatch(t)
+ if m.Kind != reqrec.KindBlock || m.Tier != reqrec.TierHard || m.RuleID != "hard.secrets.env" || m.Ref != w.Header().Get("X-Carlos-Ref") {
+ t.Fatalf("block: match %+v", m)
+ }
+}
+
+// The other side of that order: a rewrite onto an ordinary file leaves the
+// request path's probe hit as the verdict, and so does a rewrite onto a
+// hard-tier name the tree does not have, which sends nothing.
+func TestStaticRewriteKeepsProbeVerdict(t *testing.T) {
+ for _, tc := range []struct {
+ name, redirects string
+ code int
+ }{
+ {"ordinary target", "/* /index.html 200\n", 200},
+ {"absent hard target", "/* /.env 200\n", 404},
+ } {
+ g := staticGuardEdge(t, guardWatch, map[string]string{"index.html": "home", "_redirects": tc.redirects})
+ if w := g.get("/wp-login.php"); w.Code != tc.code {
+ t.Fatalf("%s: status %d, want %d", tc.name, w.Code, tc.code)
+ }
+ m := g.onlyMatch(t)
+ if m.Kind != reqrec.KindWatch || m.Tier != reqrec.TierProbe || m.RuleID != "probe.wordpress.wp-login" || m.Status != tc.code {
+ t.Fatalf("%s: match %+v", tc.name, m)
+ }
+ }
+}
+
// A hard-tier path asked for directly is decided by the check in ServeHTTP,
// before the static handler runs. The handler must not file it a second time.
func TestStaticHardAtEdgeOnce(t *testing.T) {