package main import ( "fmt" "go/ast" "strings" ) var pluginRouterLibraries = []string{ "github.com/go-chi/chi", "github.com/gorilla/mux", "github.com/julienschmidt/httprouter", "github.com/labstack/echo", "github.com/gin-gonic/gin", } var chiRouteMethods = map[string]bool{ "Get": true, "Post": true, "Put": true, "Patch": true, "Delete": true, "Head": true, "Options": true, "Connect": true, "Trace": true, "Method": true, "MethodFunc": true, "Mount": true, "Route": true, } // checkPluginRoutes enforces that a plugin serves HTTP only through pluginsdk // route, which declares and enforces an access level per route, and that no // public route reads a table holding personal columns. func checkPluginRoutes(src *pluginSource) []pluginRuleFinding { var out []pluginRuleFinding add := func(f *pluginFile, n ast.Node, format string, a ...any) { out = append(out, pluginRuleFinding{file: f.rel, line: src.line(n), msg: fmt.Sprintf(format, a...)}) } skip := src.accessDeclaredMuxCalls() for _, f := range src.files { chiName := "" for name, p := range f.imports { if strings.HasPrefix(p, "github.com/go-chi/chi") { chiName = name } } for _, spec := range f.ast.Imports { p := strings.Trim(spec.Path.Value, `"`) for _, lib := range pluginRouterLibraries { if strings.HasPrefix(p, lib) { add(f, spec, "imports router library %s; register routes with pluginsdk route.New", p) } } } ast.Inspect(f.ast, func(n ast.Node) bool { switch x := n.(type) { case *ast.CallExpr: sel, ok := x.Fun.(*ast.SelectorExpr) if !ok || skip[x] { return true } if p, name, ok := f.pkgSel(sel); ok { switch { case p == "net/http" && name == "NewServeMux": add(f, x, "builds its own http.ServeMux; register routes with pluginsdk route.New") case p == "net/http" && (name == "Handle" || name == "HandleFunc"): add(f, x, "registers a handler with http.%s; register routes with pluginsdk route", name) case strings.HasPrefix(p, "github.com/go-chi/chi") && (name == "NewRouter" || name == "NewMux"): add(f, x, "builds a chi router; register routes with pluginsdk route.New") } return true } name := sel.Sel.Name if (name == "Handle" || name == "HandleFunc") && len(x.Args) == 2 { add(f, x, "registers %s on %s without an access level; register routes with pluginsdk route", name, shortExpr(sel.X)) } if chiName != "" && chiRouteMethods[name] && len(x.Args) == 2 { add(f, x, "registers chi route %s on %s; register routes with pluginsdk route", name, shortExpr(sel.X)) } case *ast.KeyValueExpr: if key, ok := x.Key.(*ast.Ident); ok && key.Name == "HTTPHandler" && !src.servesRouteRouter(f, x.Value) { add(f, x, "HTTPHandler returns a handler that does not come from pluginsdk route.New") } } return true }) for _, reg := range f.routeRegistrations() { if reg.access != "public" { continue } for _, n := range f.pkg.reachable(f.pkg.funcBodies(reg.handler)) { if tables := src.storedPersonalReads(n); len(tables) > 0 { add(f, reg.call, "public route %s reads personal data from table %s; use route.Admin or route.Visitor", shortExpr(reg.path), strings.Join(tables, ", ")) break } } } } return out } // servesRouteRouter reports whether an HTTPHandler value returns a router // built by pluginsdk route.New, directly, through a local helper, a local // variable or a wrapper that takes the router as an argument. func (src *pluginSource) servesRouteRouter(f *pluginFile, v ast.Expr) bool { if id, ok := v.(*ast.Ident); ok && id.Name == "nil" { return true } bodies := f.pkg.funcBodies(v) if len(bodies) == 0 { return false } for _, b := range bodies { if !src.bodyReturnsRouter(f, b, 0) { return false } } return true } func (src *pluginSource) bodyReturnsRouter(f *pluginFile, n ast.Node, depth int) bool { if depth > 4 { return false } var body *ast.BlockStmt switch x := n.(type) { case *ast.FuncDecl: body = x.Body f = src.declFile[x] case *ast.FuncLit: body = x.Body } results := returnExprs(body) if len(results) == 0 { return false } scope := newFuncScope(n) connect, _ := connectMuxes(f, n) for _, r := range results { if len(r) == 0 || !src.isRouteRouter(f, scope, connect, r[0], depth, map[string]bool{}) { return false } } return true } func (src *pluginSource) isRouteRouter(f *pluginFile, scope *funcScope, connect map[string]bool, e ast.Expr, depth int, visiting map[string]bool) bool { switch x := e.(type) { case *ast.ParenExpr: return src.isRouteRouter(f, scope, connect, x.X, depth, visiting) case *ast.Ident: if x.Name == "nil" || connect[x.Name] { return true } values, ok := scope.assigns[x.Name] if !ok || visiting[x.Name] { return false } visiting[x.Name] = true for _, v := range values { if v == nil || !src.isRouteRouter(f, scope, connect, v, depth, visiting) { return false } } return len(values) > 0 case *ast.CallExpr: if p, name, ok := f.pkgFunc(x); ok && ((p == routeImportPath && name == "New") || (p == "net/http" && name == "NotFoundHandler")) { return true } var local []*ast.FuncDecl switch fn := x.Fun.(type) { case *ast.Ident: local = f.pkg.funcs[fn.Name] case *ast.SelectorExpr: if _, _, isPkg := f.pkgSel(fn); !isPkg { local = f.pkg.funcs[fn.Sel.Name] } } if len(local) > 0 { for _, d := range local { if !src.bodyReturnsRouter(f, d, depth+1) { return false } } return true } for _, a := range x.Args { if src.isRouteRouter(f, scope, connect, a, depth, visiting) { return true } } } return false } // accessDeclaredMuxCalls lists ServeMux constructions and registrations that // still declare access: muxes that only mount Connect service bindings (the // host enforces their RBAC method roles) and muxes inside the Assets handler // that read no stored personal data. func (src *pluginSource) accessDeclaredMuxCalls() map[ast.Node]bool { skip := map[ast.Node]bool{} assets := map[*pluginPkg][]ast.Node{} for _, f := range src.files { for _, decl := range f.ast.Decls { if fn, ok := decl.(*ast.FuncDecl); ok { _, calls := connectMuxes(f, fn) for n := range calls { skip[n] = true } } } ast.Inspect(f.ast, func(n ast.Node) bool { if kv, ok := n.(*ast.KeyValueExpr); ok { if key, ok := kv.Key.(*ast.Ident); ok && key.Name == "Assets" { assets[f.pkg] = append(assets[f.pkg], f.pkg.funcBodies(kv.Value)...) } } return true }) } for pkg, roots := range assets { for _, n := range pkg.reachable(roots) { if len(src.storedPersonalReads(n)) > 0 { continue } ast.Inspect(n, func(c ast.Node) bool { if call, ok := c.(*ast.CallExpr); ok { if sel, ok := call.Fun.(*ast.SelectorExpr); ok { switch sel.Sel.Name { case "NewServeMux", "Handle", "HandleFunc": skip[call] = true } } } return true }) } } return skip } func isConnectHandler(f *pluginFile, e ast.Expr) bool { call, ok := e.(*ast.CallExpr) if !ok { return false } p, name, ok := f.pkgFunc(call) return ok && p == "connectrpc.com/connect" && strings.HasPrefix(name, "New") && strings.HasSuffix(name, "Handler") } // connectMuxes returns the ServeMux locals of fn whose every registration is // mux.Handle(path, handler) with both values from a Connect binding's Build() // or a handler from connect.New*Handler, plus the construction and // registration calls of those muxes. func connectMuxes(f *pluginFile, fn ast.Node) (map[string]bool, map[ast.Node]bool) { scope := newFuncScope(fn) fromBuild := func(e ast.Expr) bool { id, ok := e.(*ast.Ident) if !ok || len(scope.assigns[id.Name]) == 0 { return false } for _, v := range scope.assigns[id.Name] { call, ok := v.(*ast.CallExpr) if !ok { return false } sel, ok := call.Fun.(*ast.SelectorExpr) if !ok || sel.Sel.Name != "Build" || len(call.Args) != 0 { return false } } return true } created := map[string][]ast.Node{} for name, values := range scope.assigns { for _, v := range values { if call, ok := v.(*ast.CallExpr); ok { if p, fname, ok := f.pkgFunc(call); ok && p == "net/http" && fname == "NewServeMux" { created[name] = append(created[name], call) } } } } regs := map[string][]*ast.CallExpr{} ast.Inspect(fn, func(n ast.Node) bool { call, ok := n.(*ast.CallExpr) if !ok { return true } if sel, ok := call.Fun.(*ast.SelectorExpr); ok && (sel.Sel.Name == "Handle" || sel.Sel.Name == "HandleFunc") { if id, ok := sel.X.(*ast.Ident); ok && created[id.Name] != nil { regs[id.Name] = append(regs[id.Name], call) } } return true }) names := map[string]bool{} calls := map[ast.Node]bool{} for name, rs := range regs { ok := true for _, r := range rs { if r.Fun.(*ast.SelectorExpr).Sel.Name != "Handle" || len(r.Args) != 2 { ok = false continue } if (!fromBuild(r.Args[0]) || !fromBuild(r.Args[1])) && !isConnectHandler(f, r.Args[1]) { ok = false } } if !ok { continue } names[name] = true for _, r := range rs { calls[r] = true } for _, c := range created[name] { calls[c] = true } } return names, calls }