| 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 |