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