mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
rand: refactor to use types package
This commit is contained in:
parent
75e0e1aa42
commit
4ff59153ec
1 changed files with 17 additions and 48 deletions
|
@ -16,8 +16,8 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/types"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
|
|
||||||
gas "github.com/GoASTScanner/gas/core"
|
gas "github.com/GoASTScanner/gas/core"
|
||||||
)
|
)
|
||||||
|
@ -28,69 +28,38 @@ type WeakRand struct {
|
||||||
packagePath string
|
packagePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
type pkgFunc struct {
|
func matchFuncCall(n ast.Node, c *gas.Context) (types.Object, *ast.Ident) {
|
||||||
packagePath string
|
|
||||||
funcName string
|
|
||||||
}
|
|
||||||
|
|
||||||
// pkgId takes an import line and returns the identifier used
|
|
||||||
// for that package in the rest of the file
|
|
||||||
func pkgId(i *ast.ImportSpec) string {
|
|
||||||
if i.Name != nil {
|
|
||||||
return i.Name.String()
|
|
||||||
}
|
|
||||||
trim := strings.Trim(i.Path.Value, `"`)
|
|
||||||
a := strings.Split(trim, "/")
|
|
||||||
return a[len(a)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// importIds returns a map of import names to their full paths
|
|
||||||
func importIds(f *ast.File) map[string]string {
|
|
||||||
pkgs := make(map[string]string)
|
|
||||||
for _, v := range f.Imports {
|
|
||||||
pkgs[pkgId(v)] = strings.Trim(v.Path.Value, `"`)
|
|
||||||
}
|
|
||||||
return pkgs
|
|
||||||
}
|
|
||||||
|
|
||||||
// matchPkgFunc will return package level function calls split
|
|
||||||
// by full package path and function name
|
|
||||||
func matchPkgFunc(n ast.Node, c *gas.Context) *pkgFunc {
|
|
||||||
call, ok := n.(*ast.CallExpr)
|
call, ok := n.(*ast.CallExpr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sel, ok := call.Fun.(*ast.SelectorExpr)
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
id, ok := sel.X.(*ast.Ident)
|
id, ok := sel.X.(*ast.Ident)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if id.Obj != nil {
|
return c.Info.ObjectOf(id), sel.Sel
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
i := importIds(c.Root)
|
|
||||||
v, ok := i[id.Name]
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return &pkgFunc{
|
|
||||||
packagePath: v,
|
|
||||||
funcName: sel.Sel.String(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
|
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
|
||||||
call := matchPkgFunc(n, c)
|
o, f := matchFuncCall(n, c)
|
||||||
|
|
||||||
if call != nil && call.packagePath == w.packagePath && w.pattern.MatchString(call.funcName) {
|
if o == nil || f == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
pkg, ok := o.(*types.PkgName)
|
||||||
|
if !ok {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if pkg.Imported().Path() == w.packagePath && w.pattern.MatchString(f.String()) {
|
||||||
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
|
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue