carlos / platform Public

console: registrar guard covers construction and handler

Task 2 review R3, parked under stopping rule 2 with a stated scope. The
registrar scan now also requires the mux field to be exactly
http.NewServeMux() in NewServer's literal, the middleware wrap to be the
single s.handler assignment in NewServer through the imported perf
package (no identifier perf may be declared in the package), and Server
to embed no ServeMux or Handler. Its doc comment, and a paragraph in the
spec's review findings, say what it covers (routes added by ordinary
means) and what it leaves to code review (deliberate evasion inside the
package). New fixtures for each; mutations 13 and 14 in the notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1299721bbf7ca192d4792b6cbca3ba5d46bbbe0e parent 924969c
4 files changed, +209 −36
  • docs/superpowers/notes/2026-11-09-parity-guard-mutations.md +11 −0
  • docs/superpowers/specs/2026-11-09-parity-guard-design.md +12 −0
  • internal/console/claim_routes_test.go +26 −1
  • internal/console/svcroutes_test.go +160 −35
diff --git a/docs/superpowers/notes/2026-11-09-parity-guard-mutations.md b/docs/superpowers/notes/2026-11-09-parity-guard-mutations.md
index 011d55668..531d70f69 100644
--- a/docs/superpowers/notes/2026-11-09-parity-guard-mutations.md
+++ b/docs/superpowers/notes/2026-11-09-parity-guard-mutations.md
@@ -20,6 +20,8 @@ in the committed tree.
| 10 | `gateDemo.holds` returns false | TestRouteDeclsMatchRegistrations | `GET /api/box/demo-builds: no configuration registers it (gate "demo" never holds)` (one per demo declaration) and `gate "demo" is never on in the configuration matrix` |
| 11 | `mux := s.mux; mux.HandleFunc("GET /api/cli/unlisted", s.handleHome)` added in NewServer (Task 2 review R1 P1) | TestClaimConsoleMuxHasOneRegistrar; every scanner test (checked with TestAuthCLIFamilyResolvesOnlyPersons) | `mux registration outside registerRoute: server.go:1946:2`. Before the fix this passed: the scan only recognised a receiver field named `mux`. |
| 12 | `instanceupdates.Mount(s.mux, "unlisted", nil, nil)` added in NewServer (Task 2 review R2) | TestClaimConsoleMuxHasOneRegistrar; every scanner test (checked with TestAuthCLIFamilyResolvesOnlyPersons) | `mux referenced at server.go:1946:24: the console's mux is reached only through registerRoute`. Before the fix this passed: Mount calls HandleFunc in another package. |
+| 13 | In NewServer, `m := http.NewServeMux()`, `instanceupdates.Mount(m, "probe", ...)`, and the Server literal's `mux: m` (Task 2 review R3) | TestClaimConsoleMuxHasOneRegistrar; every scanner test (checked with TestAuthCLIFamilyResolvesOnlyPersons) | `mux set at server.go:1862:3 to something other than a fresh http.NewServeMux(): the console's mux is reached only through registerRoute` |
+| 14 | After registerDecls in NewServer, `next := s.handler` and a second `s.handler = http.HandlerFunc(...)` wrapping it (Task 2 review R3) | TestClaimConsoleMuxHasOneRegistrar; every scanner test (checked with TestAuthCLIFamilyResolvesOnlyPersons) | `the served handler is assigned 2 times; it is assigned exactly once, in NewServer` |
## What the mutations found
@@ -45,3 +47,12 @@ field is set only in NewServer's literal. TestClaimConsoleMuxHasOneRegistrar
also checks Server's only *http.ServeMux field is the unexported mux.
TestRegistrarViolationsRefusesAliases adds an external Mount, an alias, a
reassignment, a function argument, a getter and a literal outside NewServer.
+
+Mutations 13 and 14 come from the Task 2 review (round 3). The scan now
+also requires the mux field to be set to exactly `http.NewServeMux()`, the
+middleware wrap to be the one assignment of s.handler in NewServer through
+the imported perf package (no identifier perf is declared in the
+package), s.handler to be assigned exactly once, and Server to embed no
+routing field. Its stated scope: routes added by ordinary means; code
+written inside the package to evade it (shadowing it does not model,
+reflection, unsafe) is left to code review.
diff --git a/docs/superpowers/specs/2026-11-09-parity-guard-design.md b/docs/superpowers/specs/2026-11-09-parity-guard-design.md
index 1c28eae13..4e25c3fdf 100644
--- a/docs/superpowers/specs/2026-11-09-parity-guard-design.md
+++ b/docs/superpowers/specs/2026-11-09-parity-guard-design.md
@@ -238,6 +238,18 @@ stays as it is: main.go's switch and each command's own dispatcher.
- **R3-5 (console security scanners):** applied (B5).
- **R2 census/docsite note:** no root-binary change in this slice;
`docsite`'s scanner is reused unchanged (C5).
+- **Build review, Task 2 (rounds T2, T2-R2, T2-R3):** the registrar
+ guard (`registrarViolations`, `TestClaimConsoleMuxHasOneRegistrar`) was
+ widened each round: any Handle or HandleFunc outside `registerRoute`,
+ any reference to the console's mux outside `registerRoute` and the
+ middleware wrap, a mux at construction that is exactly
+ `http.NewServeMux()`, one assignment of the served handler, the wrap
+ through the imported `perf` package, and no embedded routing field. Its
+ scope is stated in its doc comment: it catches a route added outside
+ `routeDecls` by any ordinary means; it does not defend against code
+ inside the package written to evade it (shadowing, reflection, unsafe),
+ which code review covers. R3's two P1s are parked with this ruling
+ (stopping rule 2).
## Left to the builder
diff --git a/internal/console/claim_routes_test.go b/internal/console/claim_routes_test.go
index e8c57a407..ab7a686e5 100644
--- a/internal/console/claim_routes_test.go
+++ b/internal/console/claim_routes_test.go
@@ -56,6 +56,13 @@ func TestClaimConsoleRouteCensus(t *testing.T) {
// only by registerDecls (the loop over routeDecls) and by the cli helpers
// forwarding to registerRoute. Anything else is a route registered outside
// the declaration list, and fails with its position.
+//
+// Its scope is registrarViolations': it catches a route added outside
+// routeDecls by any ordinary means (Handle or HandleFunc anywhere, any
+// reference to the mux outside the boundary, a mux at construction that is
+// not fresh, a second assignment of the served handler). It does not
+// defend against code inside the package written to evade it (shadowing,
+// reflection, unsafe); code review covers that.
func TestClaimConsoleMuxHasOneRegistrar(t *testing.T) {
fset, files := parseConsoleSource(t)
violations, muxSites := registrarViolations(fset, files)
@@ -103,7 +110,11 @@ func TestClaimConsoleMuxHasOneRegistrar(t *testing.T) {
func TestRegistrarViolationsRefusesAliases(t *testing.T) {
const clean = `package console
-import "net/http"
+import (
+ "net/http"
+
+ "github.com/carlosframework/platform/internal/perf"
+)
func (s *Server) registerRoute(d routeDecl, h http.HandlerFunc) { s.mux.HandleFunc(d.Pattern, h) }
@@ -125,6 +136,10 @@ func NewServer() *Server {
if v, n := scan(t, clean); len(v) != 0 || n != 1 {
t.Fatalf("clean source: violations %q, mux sites %d (want none and 1)", v, n)
}
+ renamed := strings.Replace(clean, `"github.com/carlosframework/platform/internal/perf"`, `perf "example.com/notperf"`, 1)
+ if v, _ := scan(t, renamed); len(v) == 0 {
+ t.Error("not reported: perf imported from another package")
+ }
for _, c := range []struct{ name, src string }{
{"local alias of the mux", "func NewServer(s *Server) { mux := s.mux; mux.HandleFunc(\"GET /api/cli/unlisted\", s.handleHome) }"},
{"method value of HandleFunc", "func NewServer(s *Server) { h := s.mux.HandleFunc; h(\"GET /x\", s.handleHome) }"},
@@ -138,6 +153,16 @@ func NewServer() *Server {
{"mux passed to a function", "func NewServer(s *Server) { f(s.mux) }"},
{"mux getter", "func (s *Server) Mux() *http.ServeMux { return s.mux }"},
{"mux set outside NewServer", "func other() *Server { return &Server{mux: http.NewServeMux()} }"},
+ {"mux built elsewhere and mounted first", "func NewServer() *Server { m := http.NewServeMux(); instanceupdates.Mount(m, \"probe\", nil, nil); return &Server{mux: m} }"},
+ {"mux from another call", "func NewServer() *Server { return &Server{mux: newMux()} }"},
+ {"second handler assignment", "func NewServer(s *Server, m *http.ServeMux) { next := s.handler; s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) }) }"},
+ {"handler assigned in another method", "func (s *Server) other() { s.handler = nil }"},
+ {"handler set in a literal", "func other() *Server { return &Server{handler: nil} }"},
+ {"perf shadowed in NewServer", "func NewServer(s *Server) { if true { perf := fake{}; _ = perf } }"},
+ {"perf as a parameter", "func helper(perf fake) {}"},
+ {"perf as a package var", "var perf = fake{}"},
+ {"embedded mux in Server", "type Server struct { *http.ServeMux }"},
+ {"embedded handler in Server", "type Server struct { http.Handler }"},
} {
t.Run(c.name, func(t *testing.T) {
if v, _ := scan(t, clean+"\n"+c.src+"\n"); len(v) == 0 {
diff --git a/internal/console/svcroutes_test.go b/internal/console/svcroutes_test.go
index ddf506288..4202f02e3 100644
--- a/internal/console/svcroutes_test.go
+++ b/internal/console/svcroutes_test.go
@@ -1150,24 +1150,35 @@ var registrarSites = map[string]map[string]bool{
"cliScopedRoute": {"registerRoute": true},
}
-// registrarViolations reports, with its position, every mention of a
-// registration helper outside registrarSites (a call, a method value or a
-// method expression alike) and every selector named Handle or HandleFunc
-// outside registerRoute, whatever its receiver: s.mux, a local alias of
-// it, http's default mux, or a method value taken to call later. Nothing
-// else in the package has a reason to name either. muxSites counts the
-// ones inside registerRoute, so a caller can tell an empty walk from a
-// clean one.
+// registrarViolations is the registration-completeness guard: it reports,
+// with its position, every way it knows of to put a route on what the
+// console serves without passing registerRoute (and so routeDecls, the
+// scanners and claimEntries).
//
-// It also holds the boundary those checks rest on: the console's mux is
-// reached only through registerRoute. Any selector named mux is refused
-// except inside registerRoute and the one perf.Middleware(...)(s.mux) wrap
-// in NewServer, and the field is set (mux: in a literal) only in
-// NewServer. So the mux cannot be passed to another package's Mount,
-// aliased, reassigned or returned by a getter, any of which would
-// register routes no scan or claimEntries sees.
+// Scope. It catches a route added outside routeDecls by any ordinary
+// means: a registration helper named outside registrarSites (a call, a
+// method value or a method expression alike); a selector named Handle or
+// HandleFunc anywhere outside registerRoute, whatever its receiver (s.mux,
+// an alias, http's default mux, a method value); any reference to the mux
+// outside the boundary below; a mux at construction that is not a fresh
+// http.NewServeMux(); a middleware wrap that is not the imported perf
+// package; a second assignment of the served handler; and an embedded
+// routing field in Server. It does not defend against code inside the
+// package written to evade it (shadowing tricks it does not model,
+// reflection, unsafe); code review covers that.
+//
+// The boundary: the console's mux is reached only through registerRoute.
+// A selector named mux is allowed inside registerRoute and as the argument
+// of the one perf.Middleware(...)(s.mux) that NewServer assigns to
+// s.handler; the field is set only by NewServer's literal, to exactly
+// http.NewServeMux(); s.handler is assigned exactly once, there; and no
+// identifier perf is declared in the package, so perf can only be the
+// import. muxSites counts registerRoute's Handle/HandleFunc selectors, so
+// a caller can tell an empty walk from a clean one.
func registrarViolations(fset *token.FileSet, files []*ast.File) (violations []string, muxSites int) {
const muxBoundary = "the console's mux is reached only through registerRoute"
+ report := func(format string, args ...any) { violations = append(violations, fmt.Sprintf(format, args...)) }
+ handlerAssigns := 0
for _, f := range files {
var enclosing *ast.FuncDecl
wrapped := map[*ast.SelectorExpr]bool{}
@@ -1176,8 +1187,15 @@ func registrarViolations(fset *token.FileSet, files []*ast.File) (violations []s
if !ok || fd.Recv != nil || fd.Name.Name != "NewServer" || fd.Body == nil {
continue
}
+ if !importsPerf(f) {
+ report("%s: NewServer's file does not import internal/perf as perf, so the middleware wrap is not the reviewed one", fset.Position(f.Package))
+ }
ast.Inspect(fd.Body, func(n ast.Node) bool {
- if sel, ok := perfWrappedMux(n); ok {
+ as, ok := n.(*ast.AssignStmt)
+ if !ok || len(as.Lhs) != 1 || len(as.Rhs) != 1 || !isSelectorNamed(as.Lhs[0], "handler") {
+ return true
+ }
+ if sel, ok := perfWrappedMux(as.Rhs[0]); ok {
wrapped[sel] = true
}
return true
@@ -1186,31 +1204,89 @@ func registrarViolations(fset *token.FileSet, files []*ast.File) (violations []s
var visit func(n ast.Node) bool
visit = func(n ast.Node) bool {
site, onServer := "", false
+ inNewServer := enclosing != nil && enclosing.Recv == nil && enclosing.Name.Name == "NewServer"
if enclosing != nil {
site, onServer = enclosing.Name.Name, receiverIsServer(enclosing)
}
- if kv, ok := n.(*ast.KeyValueExpr); ok {
- if id, ok := kv.Key.(*ast.Ident); ok && id.Name == "mux" && !(enclosing != nil && enclosing.Recv == nil && site == "NewServer") {
- violations = append(violations, fmt.Sprintf("mux set outside NewServer at %s: %s", fset.Position(kv.Pos()), muxBoundary))
+ switch n := n.(type) {
+ case *ast.KeyValueExpr:
+ id, ok := n.Key.(*ast.Ident)
+ if !ok {
+ return true
+ }
+ switch {
+ case id.Name == "mux" && !inNewServer:
+ report("mux set outside NewServer at %s: %s", fset.Position(n.Pos()), muxBoundary)
+ case id.Name == "mux" && !isFreshServeMux(n.Value):
+ report("mux set at %s to something other than a fresh http.NewServeMux(): %s", fset.Position(n.Pos()), muxBoundary)
+ case id.Name == "handler":
+ report("handler set in a literal at %s; the served handler is assigned once, in NewServer", fset.Position(n.Pos()))
}
return true
- }
- sel, ok := n.(*ast.SelectorExpr)
- if !ok {
+ case *ast.AssignStmt:
+ for _, lhs := range n.Lhs {
+ if isSelectorNamed(lhs, "handler") {
+ handlerAssigns++
+ if !inNewServer {
+ report("the served handler assigned at %s; it is assigned once, in NewServer", fset.Position(n.Pos()))
+ }
+ }
+ }
+ if n.Tok == token.DEFINE {
+ for _, lhs := range n.Lhs {
+ if id, ok := lhs.(*ast.Ident); ok && id.Name == "perf" {
+ report("perf declared at %s; perf names only the imported package", fset.Position(id.Pos()))
+ }
+ }
+ }
return true
- }
- name := sel.Sel.Name
- if name == "mux" && !(onServer && site == "registerRoute") && !wrapped[sel] {
- violations = append(violations, fmt.Sprintf("mux referenced at %s: %s", fset.Position(sel.Pos()), muxBoundary))
- }
- if registrationHelpers[name] && !(onServer && registrarSites[site][name]) {
- violations = append(violations, fmt.Sprintf("%s outside registerDecls and the helpers forwarding to registerRoute: %s", name, fset.Position(sel.Pos())))
- }
- if name == "Handle" || name == "HandleFunc" {
- if onServer && site == "registerRoute" {
- muxSites++
- } else {
- violations = append(violations, fmt.Sprintf("mux registration outside registerRoute: %s", fset.Position(sel.Pos())))
+ case *ast.ValueSpec:
+ for _, id := range n.Names {
+ if id.Name == "perf" {
+ report("perf declared at %s; perf names only the imported package", fset.Position(id.Pos()))
+ }
+ }
+ return true
+ case *ast.FuncType:
+ for _, fl := range []*ast.FieldList{n.Params, n.Results} {
+ if fl == nil {
+ continue
+ }
+ for _, fld := range fl.List {
+ for _, id := range fld.Names {
+ if id.Name == "perf" {
+ report("perf declared at %s; perf names only the imported package", fset.Position(id.Pos()))
+ }
+ }
+ }
+ }
+ return true
+ case *ast.TypeSpec:
+ if n.Name.Name == "perf" {
+ report("perf declared at %s; perf names only the imported package", fset.Position(n.Name.Pos()))
+ }
+ if st, ok := n.Type.(*ast.StructType); ok && n.Name.Name == "Server" {
+ for _, fld := range st.Fields.List {
+ if len(fld.Names) == 0 && isRoutingType(fld.Type) {
+ report("Server embeds a routing field at %s: %s", fset.Position(fld.Pos()), muxBoundary)
+ }
+ }
+ }
+ return true
+ case *ast.SelectorExpr:
+ name := n.Sel.Name
+ if name == "mux" && !(onServer && site == "registerRoute") && !wrapped[n] {
+ report("mux referenced at %s: %s", fset.Position(n.Pos()), muxBoundary)
+ }
+ if registrationHelpers[name] && !(onServer && registrarSites[site][name]) {
+ report("%s outside registerDecls and the helpers forwarding to registerRoute: %s", name, fset.Position(n.Pos()))
+ }
+ if name == "Handle" || name == "HandleFunc" {
+ if onServer && site == "registerRoute" {
+ muxSites++
+ } else {
+ report("mux registration outside registerRoute: %s", fset.Position(n.Pos()))
+ }
}
}
return true
@@ -1219,6 +1295,10 @@ func registrarViolations(fset *token.FileSet, files []*ast.File) (violations []s
enclosing = nil
if fd, ok := d.(*ast.FuncDecl); ok {
enclosing = fd
+ if fd.Name.Name == "perf" {
+ report("perf declared at %s; perf names only the imported package", fset.Position(fd.Name.Pos()))
+ }
+ ast.Inspect(fd.Type, visit)
if fd.Body != nil {
ast.Inspect(fd.Body, visit)
}
@@ -1227,9 +1307,54 @@ func registrarViolations(fset *token.FileSet, files []*ast.File) (violations []s
ast.Inspect(d, visit)
}
}
+ if handlerAssigns != 1 {
+ report("the served handler is assigned %d times; it is assigned exactly once, in NewServer", handlerAssigns)
+ }
return violations, muxSites
}
+// isSelectorNamed reports whether e is a selector x.name.
+func isSelectorNamed(e ast.Expr, name string) bool {
+ sel, ok := e.(*ast.SelectorExpr)
+ return ok && sel.Sel.Name == name
+}
+
+// isFreshServeMux reports whether e is exactly http.NewServeMux().
+func isFreshServeMux(e ast.Expr) bool {
+ call, ok := e.(*ast.CallExpr)
+ if !ok || len(call.Args) != 0 {
+ return false
+ }
+ sel, ok := call.Fun.(*ast.SelectorExpr)
+ if !ok || sel.Sel.Name != "NewServeMux" {
+ return false
+ }
+ pkg, ok := sel.X.(*ast.Ident)
+ return ok && pkg.Name == "http"
+}
+
+// isRoutingType reports whether t is http.ServeMux, *http.ServeMux or
+// http.Handler.
+func isRoutingType(t ast.Expr) bool {
+ if star, ok := t.(*ast.StarExpr); ok {
+ t = star.X
+ }
+ sel, ok := t.(*ast.SelectorExpr)
+ return ok && (sel.Sel.Name == "ServeMux" || sel.Sel.Name == "Handler")
+}
+
+// importsPerf reports whether f imports internal/perf under the name perf.
+func importsPerf(f *ast.File) bool {
+ for _, imp := range f.Imports {
+ path, err := strconv.Unquote(imp.Path.Value)
+ if err != nil || path != "github.com/carlosframework/platform/internal/perf" {
+ continue
+ }
+ return imp.Name == nil || imp.Name.Name == "perf"
+ }
+ return false
+}
+
// perfWrappedMux matches perf.Middleware(...)(x.mux) and returns x.mux.
func perfWrappedMux(n ast.Node) (*ast.SelectorExpr, bool) {
call, ok := n.(*ast.CallExpr)