Phase 10, part 5: access enforced on every page, form and fragment

One table maps every address to an area (a module, Settings, anything
signed in, or public); each route is checked against it, and the app won't
start with a route missing from it. Changing needs edit on the module. For
members who may only view, forms that would change something are shown
disabled and htmx buttons hidden. The menu, the Overview, Search and the
SEO emails follow each person's modules; Settings is for admins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 09:13:16 -07:00
co-authored by Claude Opus 5.5
parent 47a504b7b2
commit 2b9d04297f
13 changed files with 578 additions and 41 deletions
+6 -24
View File
@@ -79,18 +79,6 @@ const (
csrfHeader = "X-CSRF-Token" // htmx sends it (app.js)
)
// publicPaths are reached without signing in.
func publicPath(p string) bool {
switch {
case p == "/healthz", p == "/favicon.ico", p == "/mcp",
strings.HasPrefix(p, "/static/"),
p == "/signin", p == "/signin/code", p == "/signout", p == "/forgot",
strings.HasPrefix(p, "/reset/"), strings.HasPrefix(p, "/setup/"), strings.HasPrefix(p, "/invite/"):
return true
}
return false
}
// sessions finds who each request comes from. On the server edition,
// anyone not signed in is sent to sign in.
func (s *Server) sessions(next http.Handler) http.Handler {
@@ -293,17 +281,7 @@ func addCSRF(page []byte, token string) []byte {
}
field := []byte(`<input type="hidden" name="` + csrfField + `" value="` + html.EscapeString(token) + `">`)
return formTag.ReplaceAllFunc(page, func(tag []byte) []byte {
var method, action, enctype string
for _, m := range attrRe.FindAllSubmatch(tag, -1) {
switch strings.ToLower(string(m[1])) {
case "method":
method = strings.ToLower(string(m[2]))
case "action":
action = html.UnescapeString(string(m[2]))
case "enctype":
enctype = strings.ToLower(string(m[2]))
}
}
method, action, enctype := formAttrs(tag)
if method != "post" || !ownAddress(action) {
return tag
}
@@ -335,5 +313,9 @@ func withCSRF(r *http.Request, exec func(*bytes.Buffer) error) ([]byte, error) {
if err := exec(&buf); err != nil {
return nil, err
}
return addCSRF(buf.Bytes(), csrfToken(r)), nil
var v *viewer
if vv := viewerOf(r); !vv.Member.Admin() {
v = vv // forms they can't send are disabled
}
return rewriteForms(buf.Bytes(), csrfToken(r), r.URL.Path, v), nil
}