carlos / platform Public

Static sites: a folder requested without its trailing slash (/blog/post) now answers a 301 to the slash form (./post/, relative, query kept), as GitHub Pages and Netlify do, so analytics counts one page once (Paul, 2026-10-29, on paulca.com's /blog/deshittification and /blog/deshittification/) and relative links resolve against the page's own folder. Files are never redirected, a folder with no index.html stays a 404, rewritten requests are left alone, and the site's _redirects come first: a rule naming the path, or any rule that would redirect the slash form (named, splat, placeholder or host-specific, redirectRules.redirectsSlashForm), leaves the path served as asked, so no loop. Docs: docs/site/cli.md. Reviews: Astra R1 NOT READY (1 P1, the loop with splat/placeholder rules, fixed), R2 READY; census census-ruling-static-dir-slash (serveTree, redirectsSlashForm, internal/edge envelope) approved by Astra on #280. CI flake TestHeldDialWebSocketUpgrade filed as #466. Not released.

Squashed from static-dir-slash into main.
Paul Campbell pushed by claude agent, operated by paul@keymail.dev a8b52609cd7a026b950dcc895bd2b475bd711f3f parent 5340da5
12 files changed, +302 −3
  • docs/site/cli.md +6 −0
  • docs/superpowers/notes/census-proposal-static-dir-slash.md +19 −0
  • docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R1.md +30 −0
  • docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R2.md +17 −0
  • docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash-R2.md +50 −0
  • docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash.md +39 −0
  • internal/edge/redirects.go +13 −0
  • internal/edge/redirects_test.go +93 −0
  • internal/edge/static.go +25 −1
  • internal/edge/static_cache_test.go +4 −2
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0
  • internal/provision/testdata/plan14-root-reads-static-dir-slash.tsv +5 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/site/cli.md b/docs/site/cli.md
index 62a213d65..64091d7e1 100644
--- a/docs/site/cli.md
+++ b/docs/site/cli.md
@@ -419,6 +419,12 @@ cannot use, and name the line. Netlify's conditions, such as `Country=`, and
proxying to another host are not supported. A file can have up to 2,000
rules. The edge does not serve the `_redirects` file itself.
+A folder requested without its trailing slash, such as `/blog/post`, is
+redirected with a 301 to `/blog/post/`, where its `index.html` is served.
+Each page then has one address, and relative links in the page resolve
+against its own folder. If the site's `_redirects` rules name that path, or
+redirect its slash form, the folder is served at the path as asked instead.
+
#### Two builds at once
When a new build reaches an awake instance, it starts beside the old one,
diff --git a/docs/superpowers/notes/census-proposal-static-dir-slash.md b/docs/superpowers/notes/census-proposal-static-dir-slash.md
new file mode 100644
index 000000000..cc5938b05
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-static-dir-slash.md
@@ -0,0 +1,19 @@
+# Census approval: static-dir-slash
+
+Ruling `census-ruling-static-dir-slash`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+| unit | change | read sites | what changed |
+|---|---|---|---|
+| `internal/edge/static.go` `staticSite.serveTree` | body | unchanged (2) | A directory requested without its trailing slash now answers a 301 to a relative "./<base>/" (query kept) instead of serving its index.html, unless the request was rewritten, the path ends in "/", a _redirects rule names the path, or the rules would redirect its slash form (redirectsSlashForm, the existing in-memory resolve with exists=true). It opens and stats exactly the files it did before; the new branch only writes a Location header and a status, from the request path and the rules it already parsed. |
+
+Envelopes re-pinned: `internal/edge`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Recorded dispositions, which each change must still satisfy:
+
+- `internal/edge/static.go` `staticSite.serveTree`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J4;basis=root-input;rev3=PROPOSED-static-redirects;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=the same index-aware Stat lookup inside the extracted tree, now run at most twice (the request path, then once for a _redirects rewrite destination cleaned rooted inside the same tree); the Open moves to serveFile; the _redirects file itself is never answered; no root decision;rev3=census-ruling-edge-protection-2;basis=consults the edge protection matcher (internal/edgeguard, pure and in-memory) on the request path (probe tier, only when no file and no _redirects rule names it) and on a rewrite target (hard tier, only when the file is there), and may answer the guard's 404 block page through guardDecide before a redirect or the rewritten file; the _redirects lookup gains an in-memory names check; the same index-aware Stat lookups inside the extracted tree, unchanged; no read added
+
+Seal: `1de834976c615fed28567ca0a9ae7526eaa35341e07898feb112f4e1b0ddee8d` → `fb7cd756f7c3eb2dc019c8fd55fea5475e2df7ee3305fbc693ae669f257f374f`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-static-dir-slash.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R1.md b/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R1.md
new file mode 100644
index 000000000..e28e2afdb
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R1.md
@@ -0,0 +1,30 @@
+Reviewed `origin/main...cba536da6d3c0b25ddaf7d7c6e62aeb14cfbcabe`, round 1.
+
+**P1 — Redirect rules: splat and placeholder rules create a loop.**
+
+With `about/index.html` and `_redirects` containing `/about/* /about 301!`:
+
+- `/about/` redirects to `/about`.
+- `/about` skips the rule’s self-redirect, then the new branch redirects to `./about/`.
+- Following redirects never reaches the page.
+
+Verified with direct `serveTree` calls. The same failure occurs with `/:slug /:slug 301!` and a host-specific splat rule.
+
+Evidence: `internal/edge/static.go:539–555` uses `names` to suppress the automatic redirect, but `internal/edge/redirects.go:429–434` excludes splats and placeholders. The self-redirect skip is at `internal/edge/redirects.go:404–405`.
+
+Concrete fix: add a separate check for slash canonicalization that recognizes matching forced rules, including splats and placeholders, when their self-redirect is skipped. Keep the probe guard’s narrower `names` semantics unchanged. Add regression tests for these cases.
+
+Fault family: **automatic canonicalization must not introduce cycles with existing site redirects.**
+
+No P2 or P3 findings.
+
+The content-type test still checks that identical bytes with different representation types receive different validators. Cache admission excludes 301s; password and protection checks remain before the new response; analytics requires GET/200.
+
+Validation: direct handler checks passed. Existing HTTP tests were blocked by the sandbox refusing a loopback listener. No tracked files were edited; the temporary test file was removed.
+
+Census verdicts:
+
+- **Approve — `staticSite.serveTree`:** unchanged filesystem reads and containment; no added root authority or weakened protection check.
+- **Approve — `internal/edge` envelope:** the changed production function preserves its recorded read boundary. The redirect correctness defect above independently blocks merge.
+
+**NOT READY**
\ No newline at end of file
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R2.md b/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R2.md
new file mode 100644
index 000000000..fdc5156f8
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R2.md
@@ -0,0 +1,17 @@
+Round 1 P1 — **Resolved.** `redirectsSlashForm` checks the slash form through `resolve`, covering forced splat, placeholder and host-specific rules (`internal/edge/redirects.go:450–452`; `internal/edge/static.go:539–541`). Round 1 had no P2 findings.
+
+No new P1, P2 or P3 findings.
+
+Verified that directory redirects require an index file, preserve the query, leave rewritten requests alone, and use a relative escaped Location. Direct-handler checks passed for Unicode, encoded slashes, colon-bearing names, dot paths and double slashes. Prefix routing preserves the request path, so relative redirects retain the prefix.
+
+Protection and password checks remain before the new response. Asset-cache admission requires 200; shared response-cache admission excludes 301; analytics requires GET/200. The changed content-type test still verifies that identical bytes with different representation types receive different validators. The documentation matches the implementation.
+
+Census verdicts:
+
+- **Approve — `staticSite.serveTree`:** filesystem lookups and containment remain unchanged (`internal/edge/static.go:471–490`). The new branch adds only in-memory checks and response writes.
+- **Approve — `redirectRules.redirectsSlashForm`:** delegates to the existing in-memory resolver; adds no filesystem read or authority (`internal/edge/redirects.go:450–452`).
+- **Approve — `internal/edge` envelope:** both production changes preserve the recorded read boundary and protection checks.
+
+Validation: temporary direct-handler tests and targeted redirect-parser tests passed. Existing HTTP tests could not run because the sandbox refused loopback listeners. Temporary tests were removed; tracked files are unchanged and the working tree is clean.
+
+**READY**
\ No newline at end of file
diff --git a/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash-R2.md b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash-R2.md
new file mode 100644
index 000000000..e98f7f4ec
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash-R2.md
@@ -0,0 +1,50 @@
+# Review brief: branch static-dir-slash, round 2 of 3
+
+Repo carlos/platform, branch `static-dir-slash`, commit f7c84fdc6b (full sha: `git rev-parse f7c84fdc6b`).
+Under review: the diff `origin/main...f7c84fdc6b` (internal/edge/static.go serveTree, its tests, docs/site/cli.md, and the census overlay).
+Authorities: the static serving and _redirects code on main (internal/edge/static.go, internal/edge/redirects.go), AGENTS.md (stopping rules; "The census: an approval, not a decision").
+
+You are an independent reviewer. Read the diff, then the code it calls, and check every claim in the comments, the commit message and the docs against that code.
+
+## Fixed: do not re-argue
+
+- Paul's ruling 2026-10-29: a folder requested without its trailing slash redirects to the slash form (301), as GitHub Pages and Netlify do. Report only mechanisms that fail it.
+
+## What to check
+
+1. Correctness of the redirect in serveTree: only directories with an index.html, never files, never "/" or an empty path, never a rewritten request; Location is relative ("./<base>/"), keeps the query, and is safe for escaped, unicode, colon-bearing, dot and double-slash paths; HEAD behaves as GET.
+2. Interaction with _redirects: rules evaluated first are unchanged; the `names` guard really prevents a loop with a forced "/x/ /x 301!" rule, and does not leave other loops open (splat rules, host-specific rules, rules on parent paths, a rewrite onto a folder path without a slash).
+3. Interaction with the edge protection guard (probe/hard tiers), the password gate, the asset cache and the edge response cache: can a 301 now be cached or served where a 200 was expected, or bypass a check it should not? Analytics: confirm a 301 is not counted as a page view.
+4. Path-routed static apps (a prefix on another app's host) and temporary static sites: does the relative Location resolve to the right place?
+5. Tests: sound witnesses, nothing existing weakened (one test in static_cache_test.go now requests /about/ instead of /about; check that its point still holds).
+6. Census: the proposal at docs/superpowers/notes/census-proposal-static-dir-slash.md. Answer its review question for the unit `internal/edge/static.go staticSite.serveTree` and the re-pinned envelope `internal/edge`: approve or reject, with the reason.
+7. Docs text in docs/site/cli.md: true to the code.
+
+## Round 2
+
+Earlier rounds: round 1, docs/superpowers/reviews/CODEX-FINDINGS-static-dir-slash-R1.md
+(1 P1: loops with splat/placeholder/host rules; census approved). Fix in f7c84fdc6b.
+
+First give one line per P1 and P2 from round 1: resolved, partly, or not.
+Carry every unresolved finding forward, then add new findings, looking
+hardest at the fix diff `cba536da6d..f7c84fdc6b`, including whether any
+rule shape can still loop with the edge's redirect, and give the census
+verdict again for the changed serveTree (and redirectsSlashForm).
+
+## Severity
+
+- **P1**: unsafe, would not compile, a test fails, or it would not work. Blocks merge.
+- **P2**: wrong or missing. Fixed without reopening this review.
+- **P3**: clarity or polish. Goes on a follow-up list; never a reason for another round.
+
+A P1 states its failure scenario and the evidence (file:line). Do not raise a P2 to P1 to keep the review open.
+
+## Output
+
+Findings ranked P1, P2, P3. Each gives the area, the problem, the evidence (file:line, verified), the failure scenario, a concrete fix and the invariant or trust boundary it breaks (its fault family). Then the census verdict (approve/reject per unit and envelope). End with exactly one verdict:
+
+- **READY**: no P1 and no P2.
+- **READY AFTER FIXES**: no P1. The listed P2s are fixed without another round.
+- **NOT READY**: one or more P1s, listed.
+
+Do not edit files. Do not spawn Anthropic-model reviewers or subagents of your own. Do not run the full test suites; targeted `go test ./internal/edge/ -run ...` is fine.
diff --git a/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash.md b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash.md
new file mode 100644
index 000000000..2fc256d1e
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-static-dir-slash.md
@@ -0,0 +1,39 @@
+# Review brief: branch static-dir-slash, round 1 of 3
+
+Repo carlos/platform, branch `static-dir-slash`, commit cba536da6d (full sha: `git rev-parse cba536da6d`).
+Under review: the diff `origin/main...cba536da6d` (internal/edge/static.go serveTree, its tests, docs/site/cli.md, and the census overlay).
+Authorities: the static serving and _redirects code on main (internal/edge/static.go, internal/edge/redirects.go), AGENTS.md (stopping rules; "The census: an approval, not a decision").
+
+You are an independent reviewer. Read the diff, then the code it calls, and check every claim in the comments, the commit message and the docs against that code.
+
+## Fixed: do not re-argue
+
+- Paul's ruling 2026-10-29: a folder requested without its trailing slash redirects to the slash form (301), as GitHub Pages and Netlify do. Report only mechanisms that fail it.
+
+## What to check
+
+1. Correctness of the redirect in serveTree: only directories with an index.html, never files, never "/" or an empty path, never a rewritten request; Location is relative ("./<base>/"), keeps the query, and is safe for escaped, unicode, colon-bearing, dot and double-slash paths; HEAD behaves as GET.
+2. Interaction with _redirects: rules evaluated first are unchanged; the `names` guard really prevents a loop with a forced "/x/ /x 301!" rule, and does not leave other loops open (splat rules, host-specific rules, rules on parent paths, a rewrite onto a folder path without a slash).
+3. Interaction with the edge protection guard (probe/hard tiers), the password gate, the asset cache and the edge response cache: can a 301 now be cached or served where a 200 was expected, or bypass a check it should not? Analytics: confirm a 301 is not counted as a page view.
+4. Path-routed static apps (a prefix on another app's host) and temporary static sites: does the relative Location resolve to the right place?
+5. Tests: sound witnesses, nothing existing weakened (one test in static_cache_test.go now requests /about/ instead of /about; check that its point still holds).
+6. Census: the proposal at docs/superpowers/notes/census-proposal-static-dir-slash.md. Answer its review question for the unit `internal/edge/static.go staticSite.serveTree` and the re-pinned envelope `internal/edge`: approve or reject, with the reason.
+7. Docs text in docs/site/cli.md: true to the code.
+
+## Severity
+
+- **P1**: unsafe, would not compile, a test fails, or it would not work. Blocks merge.
+- **P2**: wrong or missing. Fixed without reopening this review.
+- **P3**: clarity or polish. Goes on a follow-up list; never a reason for another round.
+
+A P1 states its failure scenario and the evidence (file:line). Do not raise a P2 to P1 to keep the review open.
+
+## Output
+
+Findings ranked P1, P2, P3. Each gives the area, the problem, the evidence (file:line, verified), the failure scenario, a concrete fix and the invariant or trust boundary it breaks (its fault family). Then the census verdict (approve/reject per unit and envelope). End with exactly one verdict:
+
+- **READY**: no P1 and no P2.
+- **READY AFTER FIXES**: no P1. The listed P2s are fixed without another round.
+- **NOT READY**: one or more P1s, listed.
+
+Do not edit files. Do not spawn Anthropic-model reviewers or subagents of your own. Do not run the full test suites; targeted `go test ./internal/edge/ -run ...` is fine.
diff --git a/internal/edge/redirects.go b/internal/edge/redirects.go
index df918c316..51d5f9b51 100644
--- a/internal/edge/redirects.go
+++ b/internal/edge/redirects.go
@@ -439,6 +439,19 @@ rules:
return false
}
+// redirectsSlashForm reports whether the rules would answer the slash form
+// of rawPath (rawPath + "/", whose cleaned path is p) with a redirect, given
+// that the folder there exists. serveTree asks before sending a folder
+// requested without its slash to the slash form: when the site's own rules
+// redirect that form anywhere, the edge's redirect would at best add a hop
+// and at worst loop (a forced "/about/* /about 301!" answers /about/ with
+// /about, and resolve skips the rule's redirect of /about to itself), so the
+// edge leaves the path alone. A nil set redirects nothing.
+func (rs *redirectRules) redirectsSlashForm(host, p, rawPath, rawQuery string) bool {
+ act, ok := rs.resolve(host, p, rawPath+"/", rawQuery, true)
+ return ok && act.location != ""
+}
+
// hostOf is the host of a "scheme://host[:port]" prefix, normalized as
// requestHost normalizes a request's.
func hostOf(prefix string) string {
diff --git a/internal/edge/redirects_test.go b/internal/edge/redirects_test.go
index 8495a3537..bcf1414bc 100644
--- a/internal/edge/redirects_test.go
+++ b/internal/edge/redirects_test.go
@@ -317,3 +317,96 @@ func TestStaticWithoutRedirectsIsUnchanged(t *testing.T) {
t.Fatalf("GET under a file = %d, want 404", got.code)
}
}
+
+// A directory asked for without its trailing slash is sent to the slash
+// form, as GitHub Pages and Netlify do: one URL per page (analytics counted
+// /blog/post and /blog/post/ as two pages), and the page's relative links
+// resolve against its own directory. The Location is relative, so it
+// survives any prefix the request arrived under, and keeps the query.
+func TestStaticDirectoryWithoutSlashRedirects(t *testing.T) {
+ srv, c := redirectFixture(t, "")
+ const host = "site.example.com"
+ for _, tc := range []struct {
+ path, location string
+ code int
+ body string
+ }{
+ {"/about", "./about/", 301, ""},
+ {"/about?ref=feed", "./about/?ref=feed", 301, ""},
+ {"/about/", "", 200, "about"},
+ {"/docs", "", 404, ""}, // a directory with no index.html stays a 404
+ {"/style.css", "", 200, "body{}"}, // a file is never redirected
+ {"/", "", 200, "home"},
+ } {
+ got := fetch(t, c, "GET", srv.URL, host, tc.path)
+ if got.code != tc.code || got.location != tc.location || (tc.body != "" && got.body != tc.body) {
+ t.Errorf("GET %s = %d %q %q, want %d %q %q", tc.path, got.code, got.location, got.body, tc.code, tc.location, tc.body)
+ }
+ }
+ if got := fetch(t, c, "HEAD", srv.URL, host, "/about"); got.code != 301 || got.location != "./about/" {
+ t.Errorf("HEAD /about = %d %q, want 301 ./about/", got.code, got.location)
+ }
+ // The redirect resolves to the slash form, which serves the page.
+ follow := &http.Client{}
+ req, _ := http.NewRequest("GET", srv.URL+"/about", nil)
+ req.Host = host
+ resp, err := follow.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b, _ := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if resp.StatusCode != 200 || string(b) != "about" || resp.Request.URL.Path != "/about/" {
+ t.Errorf("following /about ended at %s = %d %q, want /about/ 200 \"about\"", resp.Request.URL.Path, resp.StatusCode, b)
+ }
+}
+
+// A site whose _redirects send the slash form back to the bare path would
+// loop with this redirect, whatever shape the rule takes: named outright, a
+// splat, a placeholder, or for this host only. The slash redirect stands
+// aside, and /about serves the page.
+func TestStaticDirectorySlashRedirectYieldsToTheSitesRules(t *testing.T) {
+ const host = "site.example.com"
+ for _, rules := range []string{
+ "/about/ /about 301!\n",
+ "/about/* /about 301!\n",
+ "/:slug /:slug 301!\n",
+ "https://site.example.com/about/* https://site.example.com/about 301!\n",
+ } {
+ srv, c := redirectFixture(t, rules)
+ if got := fetch(t, c, "GET", srv.URL, host, "/about/"); got.code != 301 || !strings.HasSuffix(got.location, "/about") {
+ t.Errorf("rules %q: GET /about/ = %d %q, want the site's 301 to /about", rules, got.code, got.location)
+ }
+ if got := fetch(t, c, "GET", srv.URL, host, "/about"); got.code != 200 || got.body != "about" {
+ t.Errorf("rules %q: GET /about = %d %q %q, want 200 \"about\" (no slash redirect, no loop)", rules, got.code, got.location, got.body)
+ }
+ // Following redirects from either form ends on the page, never in a
+ // loop. (A rule with a host redirects to that host, which the test
+ // client cannot dial; the two answers above already show no loop.)
+ if strings.HasPrefix(rules, "https://") {
+ continue
+ }
+ for _, start := range []string{"/about", "/about/"} {
+ req, _ := http.NewRequest("GET", srv.URL+start, nil)
+ req.Host = host
+ resp, err := (&http.Client{}).Do(req)
+ if err != nil {
+ t.Errorf("rules %q: following %s: %v", rules, start, err)
+ continue
+ }
+ resp.Body.Close()
+ if resp.StatusCode != 200 {
+ t.Errorf("rules %q: following %s ended %d", rules, start, resp.StatusCode)
+ }
+ }
+ }
+}
+
+// A non-forced rule never fires on a folder that exists, so it does not stop
+// the slash redirect: only rules that would redirect the slash form do.
+func TestStaticDirectorySlashRedirectIgnoresRulesThatDoNotFire(t *testing.T) {
+ srv, c := redirectFixture(t, "/about/* /elsewhere 301\n")
+ if got := fetch(t, c, "GET", srv.URL, "site.example.com", "/about"); got.code != 301 || got.location != "./about/" {
+ t.Fatalf("GET /about = %d %q, want 301 ./about/", got.code, got.location)
+ }
+}
diff --git a/internal/edge/static.go b/internal/edge/static.go
index b1f783f89..78e276802 100644
--- a/internal/edge/static.go
+++ b/internal/edge/static.go
@@ -481,7 +481,8 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R
for {
fp := filepath.Join(tree.dir, filepath.FromSlash(p))
fi, err := os.Stat(fp)
- if err == nil && fi.IsDir() {
+ isDir := err == nil && fi.IsDir()
+ if isDir {
fp = filepath.Join(fp, "index.html")
fi, err = os.Stat(fp)
}
@@ -535,6 +536,29 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R
http.NotFound(w, r)
return
}
+ if !rewritten && isDir && r.URL.Path != "" && !strings.HasSuffix(r.URL.Path, "/") &&
+ !tree.redirects.names(requestHost(r.Host), p) &&
+ !tree.redirects.redirectsSlashForm(requestHost(r.Host), p, r.URL.Path, r.URL.RawQuery) {
+ // A directory asked for without its trailing slash goes to the
+ // slash form, as GitHub Pages and Netlify send it: one URL per
+ // page, so analytics does not count /blog/post and /blog/post/
+ // as two, and the page's relative links resolve against its own
+ // directory rather than its parent. Relative, so it survives any
+ // prefix the request arrived under; "./" so a first segment with
+ // a colon is never read as a scheme. The site's _redirects come
+ // first: a rule naming this path says the site has a page here,
+ // and a rule that would redirect the slash form (a forced
+ // "/about/ /about 301!", "/about/* /about 301!" or
+ // "/:slug /:slug 301!") would loop with this one, so in either
+ // case the path is served as it is.
+ loc := "./" + path.Base(r.URL.EscapedPath()) + "/"
+ if q := r.URL.RawQuery; q != "" {
+ loc += "?" + q
+ }
+ w.Header().Set("Location", loc)
+ w.WriteHeader(http.StatusMovedPermanently)
+ return
+ }
if !rewritten && strings.HasSuffix(r.URL.Path, "/index.html") {
// Relative, as ServeFile wrote it, so it survives any prefix the
// request arrived under.
diff --git a/internal/edge/static_cache_test.go b/internal/edge/static_cache_test.go
index 04b23a846..ccdbd09d9 100644
--- a/internal/edge/static_cache_test.go
+++ b/internal/edge/static_cache_test.go
@@ -328,9 +328,11 @@ func TestStaticTagCoversTheContentType(t *testing.T) {
if err := release.Promote(context.Background(), store, priv, release.OpsAccount, "site", "canary/x", "v3"); err != nil {
t.Fatalf("Promote v3: %v", err)
}
- resp, body := reqStatic(t, "GET", srv.URL, "/about", map[string]string{"If-None-Match": plainTag})
+ // Asked for as /about/: /about is now a directory, which redirects to its
+ // slash form (TestStaticDirectoryWithoutSlashRedirects).
+ resp, body := reqStatic(t, "GET", srv.URL, "/about/", map[string]string{"If-None-Match": plainTag})
if resp.StatusCode != http.StatusOK || body != same || !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") {
- t.Fatalf("revalidating /about after it became HTML = %d %q; want a text/html 200", resp.StatusCode, resp.Header.Get("Content-Type"))
+ t.Fatalf("revalidating /about/ after it became HTML = %d %q; want a text/html 200", resp.StatusCode, resp.Header.Get("Content-Type"))
}
}
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index b7a7fc247..840b20d29 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -30,3 +30,4 @@ plan14-root-reads-rootfs-apps-3-workers.tsv 862d7383ad60d522d6b16f8a545c4cfa37de
plan14-root-reads-fresh-box-446.tsv 862d7383ad60d522d6b16f8a545c4cfa37de3d2cc1ba83fec1aff3825a0fe8ab
plan14-root-reads-rootfs-apps-4.tsv e085917e271ee26877974461133f393ccc5d63add91d7aad2afe10b30977914e
plan14-root-reads-regional-domains-447.tsv 1de834976c615fed28567ca0a9ae7526eaa35341e07898feb112f4e1b0ddee8d
+plan14-root-reads-static-dir-slash.tsv fb7cd756f7c3eb2dc019c8fd55fea5475e2df7ee3305fbc693ae669f257f374f
diff --git a/internal/provision/testdata/plan14-root-reads-static-dir-slash.tsv b/internal/provision/testdata/plan14-root-reads-static-dir-slash.tsv
new file mode 100644
index 000000000..76ff3775e
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-static-dir-slash.tsv
@@ -0,0 +1,5 @@
+# Census overlay static-dir-slash, written by census-propose (#369). census-ruling-static-dir-slash, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-static-dir-slash.md.
+# 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@legacy 7d6539646102e6b5f2a6189e382630c5027dfb11790c15ab973ab27dfa9ed2c7 internal/edge/static.go staticSite.serveTree legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=PROPOSED-static-redirects;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=the same index-aware Stat lookup inside the extracted tree, now run at most twice (the request path, then once for a _redirects rewrite destination cleaned rooted inside the same tree); the Open moves to serveFile; the _redirects file itself is never answered; no root decision;rev3=census-ruling-edge-protection-2;basis=consults the edge protection matcher (internal/edgeguard, pure and in-memory) on the request path (probe tier, only when no file and no _redirects rule names it) and on a rewrite target (hard tier, only when the file is there), and may answer the guard's 404 block page through guardDecide before a redirect or the rewritten file; the _redirects lookup gains an in-memory names check; the same index-aware Stat lookups inside the extracted tree, unchanged; no read added;rev3=census-ruling-static-dir-slash;basis=A directory requested without its trailing slash now answers a 301 to a relative "./<base>/" (query kept) instead of serving its index.html, unless the request was rewritten, the path ends in "/", a _redirects rule names the path, or the rules would redirect its slash form (redirectsSlashForm, the existing in-memory resolve with exists=true). It opens and stats exactly the files it did before; the new branch only writes a Location header and a status, from the request path and the rules it already parsed. file:Stat@1081,file:Stat@967 2b330b38cd0a14d890c43711fc8892e968fcbef00d6cfdd10c6e1dccf9df59cb
+internal/edge/static.go staticSite.serveTree legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=PROPOSED-static-redirects;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=the same index-aware Stat lookup inside the extracted tree, now run at most twice (the request path, then once for a _redirects rewrite destination cleaned rooted inside the same tree); the Open moves to serveFile; the _redirects file itself is never answered; no root decision;rev3=census-ruling-edge-protection-2;basis=consults the edge protection matcher (internal/edgeguard, pure and in-memory) on the request path (probe tier, only when no file and no _redirects rule names it) and on a rewrite target (hard tier, only when the file is there), and may answer the guard's 404 block page through guardDecide before a redirect or the rewritten file; the _redirects lookup gains an in-memory names check; the same index-aware Stat lookups inside the extracted tree, unchanged; no read added;rev3=census-ruling-static-dir-slash;basis=A directory requested without its trailing slash now answers a 301 to a relative "./<base>/" (query kept) instead of serving its index.html, unless the request was rewritten, the path ends in "/", a _redirects rule names the path, or the rules would redirect its slash form (redirectsSlashForm, the existing in-memory resolve with exists=true). It opens and stats exactly the files it did before; the new branch only writes a Location header and a status, from the request path and the rules it already parsed. file:Stat@1081,file:Stat@967 2b330b38cd0a14d890c43711fc8892e968fcbef00d6cfdd10c6e1dccf9df59cb
+@coverage internal/edge b11d75a49ff9d2ea83c3c5bd89ea3692997e3fcd9488fda09f6e72410fbb9d3d