|
| 1 | +// Package regexpdynamicpattern implements a Go analysis linter that flags |
| 2 | +// calls to regexp.MustCompile() and regexp.Compile() whose pattern argument |
| 3 | +// is not a compile-time constant string. Dynamically constructed patterns can |
| 4 | +// panic at runtime on malformed input and, when influenced by untrusted |
| 5 | +// input, can enable catastrophic-backtracking (ReDoS) denial-of-service |
| 6 | +// attacks. |
| 7 | +package regexpdynamicpattern |
| 8 | + |
| 9 | +import ( |
| 10 | + "go/ast" |
| 11 | + "go/token" |
| 12 | + "go/types" |
| 13 | + |
| 14 | + "golang.org/x/tools/go/analysis" |
| 15 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 16 | + |
| 17 | + "github.com/github/gh-aw/pkg/linters/internal/astutil" |
| 18 | + "github.com/github/gh-aw/pkg/linters/internal/filecheck" |
| 19 | + "github.com/github/gh-aw/pkg/linters/internal/nolint" |
| 20 | + "github.com/github/gh-aw/pkg/logger" |
| 21 | +) |
| 22 | + |
| 23 | +var pkgLog = logger.New("linters:regexpdynamicpattern") |
| 24 | + |
| 25 | +// Analyzer is the regexp-dynamic-pattern analysis pass. |
| 26 | +var Analyzer = &analysis.Analyzer{ |
| 27 | + Name: "regexpdynamicpattern", |
| 28 | + Doc: "reports regexp.MustCompile and regexp.Compile calls whose pattern is not a compile-time constant string", |
| 29 | + URL: "https://github.com/github/gh-aw/tree/main/pkg/linters/regexpdynamicpattern", |
| 30 | + Requires: []*analysis.Analyzer{inspect.Analyzer, nolint.Analyzer, filecheck.Analyzer}, |
| 31 | + Run: run, |
| 32 | +} |
| 33 | + |
| 34 | +func run(pass *analysis.Pass) (any, error) { |
| 35 | + pkgLog.Printf("analyzing package %s", pass.Pkg.Path()) |
| 36 | + insp, err := astutil.Inspector(pass) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + noLintIndex, err := nolint.Index(pass) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + generatedFiles, err := filecheck.Index(pass) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + for cur := range insp.Root().Preorder((*ast.CallExpr)(nil)) { |
| 50 | + call, ok := cur.Node().(*ast.CallExpr) |
| 51 | + if !ok || !isRegexpCompileCall(pass, call) { |
| 52 | + continue |
| 53 | + } |
| 54 | + if hasConstantStringPattern(pass, call) { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + pos := pass.Fset.PositionFor(call.Pos(), false) |
| 59 | + if filecheck.ShouldSkipFilename(pos.Filename, generatedFiles) { |
| 60 | + continue |
| 61 | + } |
| 62 | + if nolint.HasDirectiveForLinter(pos, noLintIndex, "regexpdynamicpattern") { |
| 63 | + continue |
| 64 | + } |
| 65 | + pkgLog.Printf("flagging dynamic regexp pattern at %s", pos) |
| 66 | + pass.Report(analysis.Diagnostic{ |
| 67 | + Pos: call.Pos(), |
| 68 | + End: call.End(), |
| 69 | + Message: "regexp pattern is not a compile-time constant; dynamic patterns can panic at runtime or enable ReDoS if influenced by untrusted input", |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +// isRegexpCompileCall checks if the call is to regexp.MustCompile or regexp.Compile, |
| 77 | +// resolving the package identity via the type checker to handle aliased imports |
| 78 | +// and avoid false positives from local identifiers named "regexp". |
| 79 | +func isRegexpCompileCall(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 80 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 81 | + if !ok { |
| 82 | + return false |
| 83 | + } |
| 84 | + if sel.Sel.Name != "MustCompile" && sel.Sel.Name != "Compile" { |
| 85 | + return false |
| 86 | + } |
| 87 | + ident, ok := sel.X.(*ast.Ident) |
| 88 | + if !ok || pass.TypesInfo == nil { |
| 89 | + return false |
| 90 | + } |
| 91 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 92 | + if obj == nil { |
| 93 | + return false |
| 94 | + } |
| 95 | + pkgName, ok := obj.(*types.PkgName) |
| 96 | + if !ok || pkgName.Imported() == nil { |
| 97 | + return false |
| 98 | + } |
| 99 | + return pkgName.Imported().Path() == "regexp" |
| 100 | +} |
| 101 | + |
| 102 | +// hasConstantStringPattern checks whether the regexp pattern is a compile-time constant string, |
| 103 | +// such as a string literal, const identifier, or an expression built entirely from constants |
| 104 | +// (e.g. concatenation of string literals/consts). Non-constant expressions such as |
| 105 | +// fmt.Sprintf results, concatenation involving variables, or function parameters return false. |
| 106 | +func hasConstantStringPattern(pass *analysis.Pass, call *ast.CallExpr) bool { |
| 107 | + if len(call.Args) == 0 { |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + patternArg := call.Args[0] |
| 112 | + if lit, ok := patternArg.(*ast.BasicLit); ok && lit.Kind == token.STRING { |
| 113 | + return true |
| 114 | + } |
| 115 | + |
| 116 | + tv, ok := pass.TypesInfo.Types[patternArg] |
| 117 | + if !ok || tv.Value == nil || tv.Type == nil { |
| 118 | + return false |
| 119 | + } |
| 120 | + |
| 121 | + basic, ok := tv.Type.Underlying().(*types.Basic) |
| 122 | + return ok && basic.Kind() == types.String |
| 123 | +} |
0 commit comments