| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package httpmux |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/constant" |
| 10 | "go/types" |
| 11 | "regexp" |
| 12 | "slices" |
| 13 | "strings" |
| 14 | |
| 15 | "golang.org/x/mod/semver" |
| 16 | "golang.org/x/tools/go/analysis" |
| 17 | "golang.org/x/tools/go/analysis/passes/inspect" |
| 18 | "golang.org/x/tools/go/ast/inspector" |
| 19 | "golang.org/x/tools/go/types/typeutil" |
| 20 | "golang.org/x/tools/internal/typesinternal" |
| 21 | ) |
| 22 | |
| 23 | const Doc = `report using Go 1.22 enhanced ServeMux patterns in older Go versions |
| 24 | |
| 25 | The httpmux analysis is active for Go modules configured to run with Go 1.21 or |
| 26 | earlier versions. It reports calls to net/http.ServeMux.Handle and HandleFunc |
| 27 | methods whose patterns use features added in Go 1.22, like HTTP methods (such as |
| 28 | "GET") and wildcards. (See https://pkg.go.dev/net/http#ServeMux for details.) |
| 29 | Such patterns can be registered in older versions of Go, but will not behave as expected.` |
| 30 | |
| 31 | var Analyzer = &analysis.Analyzer{ |
| 32 | Name: "httpmux", |
| 33 | Doc: Doc, |
| 34 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/httpmux", |
| 35 | Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 36 | Run: run, |
| 37 | } |
| 38 | |
| 39 | var inTest bool // So Go version checks can be skipped during testing. |
| 40 | |
| 41 | func run(pass *analysis.Pass) (any, error) { |
| 42 | if !inTest { |
| 43 | // Check that Go version is 1.21 or earlier. |
| 44 | if goVersionAfter121(goVersion(pass.Pkg)) { |
| 45 | return nil, nil |
| 46 | } |
| 47 | } |
| 48 | if !typesinternal.Imports(pass.Pkg, "net/http") { |
| 49 | return nil, nil |
| 50 | } |
| 51 | // Look for calls to ServeMux.Handle or ServeMux.HandleFunc. |
| 52 | inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 53 | |
| 54 | nodeFilter := []ast.Node{ |
| 55 | (*ast.CallExpr)(nil), |
| 56 | } |
| 57 | |
| 58 | inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 59 | call := n.(*ast.CallExpr) |
| 60 | if isServeMuxRegisterCall(pass, call) { |
| 61 | pat, ok := stringConstantExpr(pass, call.Args[0]) |
| 62 | if ok && likelyEnhancedPattern(pat) { |
| 63 | pass.ReportRangef(call.Args[0], "possible enhanced ServeMux pattern used with Go version before 1.22 (update go.mod file?)") |
| 64 | } |
| 65 | } |
| 66 | }) |
| 67 | return nil, nil |
| 68 | } |
| 69 | |
| 70 | // isServeMuxRegisterCall reports whether call is a static call to one of: |
| 71 | // - net/http.Handle |
| 72 | // - net/http.HandleFunc |
| 73 | // - net/http.ServeMux.Handle |
| 74 | // - net/http.ServeMux.HandleFunc |
| 75 | // TODO(jba): consider expanding this to accommodate wrappers around these functions. |
| 76 | func isServeMuxRegisterCall(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 77 | fn := typeutil.StaticCallee(pass.TypesInfo, call) |
| 78 | if fn == nil { |
| 79 | return false |
| 80 | } |
| 81 | if typesinternal.IsFunctionNamed(fn, "net/http", "Handle", "HandleFunc") { |
| 82 | return true |
| 83 | } |
| 84 | if !isMethodNamed(fn, "net/http", "Handle", "HandleFunc") { |
| 85 | return false |
| 86 | } |
| 87 | recv := fn.Signature().Recv() // isMethodNamed() -> non-nil |
| 88 | isPtr, named := typesinternal.ReceiverNamed(recv) |
| 89 | return isPtr && typesinternal.IsTypeNamed(named, "net/http", "ServeMux") |
| 90 | } |
| 91 | |
| 92 | // isMethodNamed reports when a function f is a method, |
| 93 | // in a package with the path pkgPath and the name of f is in names. |
| 94 | // |
| 95 | // (Unlike [analysis.IsMethodNamed], it ignores the receiver type name.) |
| 96 | func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool { |
| 97 | if f == nil { |
| 98 | return false |
| 99 | } |
| 100 | if f.Pkg() == nil || f.Pkg().Path() != pkgPath { |
| 101 | return false // not at pkgPath |
| 102 | } |
| 103 | if f.Signature().Recv() == nil { |
| 104 | return false // not a method |
| 105 | } |
| 106 | return slices.Contains(names, f.Name()) |
| 107 | } |
| 108 | |
| 109 | // stringConstantExpr returns expression's string constant value. |
| 110 | // |
| 111 | // ("", false) is returned if expression isn't a string |
| 112 | // constant. |
| 113 | func stringConstantExpr(pass *analysis.Pass, expr ast.Expr) (string, bool) { |
| 114 | lit := pass.TypesInfo.Types[expr].Value |
| 115 | if lit != nil && lit.Kind() == constant.String { |
| 116 | return constant.StringVal(lit), true |
| 117 | } |
| 118 | return "", false |
| 119 | } |
| 120 | |
| 121 | // A valid wildcard must start a segment, and its name must be valid Go |
| 122 | // identifier. |
| 123 | var wildcardRegexp = regexp.MustCompile(`/\{[_\pL][_\pL\p{Nd}]*(\.\.\.)?\}`) |
| 124 | |
| 125 | // likelyEnhancedPattern reports whether the ServeMux pattern pat probably |
| 126 | // contains either an HTTP method name or a wildcard, extensions added in Go 1.22. |
| 127 | func likelyEnhancedPattern(pat string) bool { |
| 128 | if strings.Contains(pat, " ") { |
| 129 | // A space in the pattern suggests that it begins with an HTTP method. |
| 130 | return true |
| 131 | } |
| 132 | return wildcardRegexp.MatchString(pat) |
| 133 | } |
| 134 | |
| 135 | func goVersionAfter121(goVersion string) bool { |
| 136 | if goVersion == "" { // Maybe the stdlib? |
| 137 | return true |
| 138 | } |
| 139 | version := versionFromGoVersion(goVersion) |
| 140 | return semver.Compare(version, "v1.21") > 0 |
| 141 | } |
| 142 | |
| 143 | func goVersion(pkg *types.Package) string { |
| 144 | // types.Package.GoVersion did not exist before Go 1.21. |
| 145 | if p, ok := any(pkg).(interface{ GoVersion() string }); ok { |
| 146 | return p.GoVersion() |
| 147 | } |
| 148 | return "" |
| 149 | } |
| 150 | |
| 151 | var ( |
| 152 | // Regexp for matching go tags. The groups are: |
| 153 | // 1 the major.minor version |
| 154 | // 2 the patch version, or empty if none |
| 155 | // 3 the entire prerelease, if present |
| 156 | // 4 the prerelease type ("beta" or "rc") |
| 157 | // 5 the prerelease number |
| 158 | tagRegexp = regexp.MustCompile(`^go(\d+\.\d+)(\.\d+|)((beta|rc)(\d+))?$`) |
| 159 | ) |
| 160 | |
| 161 | // Copied from pkgsite/internal/stdlib.VersionForTag. |
| 162 | func versionFromGoVersion(goVersion string) string { |
| 163 | // Special cases for go1. |
| 164 | if goVersion == "go1" { |
| 165 | return "v1.0.0" |
| 166 | } |
| 167 | if goVersion == "go1.0" { |
| 168 | return "" |
| 169 | } |
| 170 | m := tagRegexp.FindStringSubmatch(goVersion) |
| 171 | if m == nil { |
| 172 | return "" |
| 173 | } |
| 174 | version := "v" + m[1] |
| 175 | if m[2] != "" { |
| 176 | version += m[2] |
| 177 | } else { |
| 178 | version += ".0" |
| 179 | } |
| 180 | if m[3] != "" { |
| 181 | version += "-" + m[4] + "." + m[5] |
| 182 | } |
| 183 | return version |
| 184 | } |