mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
rand: use a MatchCallByPackage helper
This commit is contained in:
parent
8a473c7e4b
commit
afb84ff20c
2 changed files with 7 additions and 37 deletions
|
@ -52,11 +52,11 @@ func MatchCall(n ast.Node, r *regexp.Regexp) *ast.CallExpr {
|
||||||
// package and identifier name matches the passed in parameters.
|
// package and identifier name matches the passed in parameters.
|
||||||
//
|
//
|
||||||
// Usage:
|
// Usage:
|
||||||
// node, obj := MatchCall(n, ctx, "math/rand", "Read")
|
// node, obj := MatchCallByPackage(n, ctx, "math/rand", "Read")
|
||||||
//
|
//
|
||||||
func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
|
func MatchCallByPackage(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
|
||||||
call, obj := GetCallObject(n, c)
|
call, obj := GetCallObject(n, c)
|
||||||
if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
|
if obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
|
||||||
return call, obj
|
return call, obj
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
|
@ -16,50 +16,20 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/types"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
gas "github.com/GoASTScanner/gas/core"
|
gas "github.com/GoASTScanner/gas/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WeakRand struct {
|
type WeakRand struct {
|
||||||
gas.MetaData
|
gas.MetaData
|
||||||
pattern *regexp.Regexp
|
funcName string
|
||||||
packagePath string
|
packagePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchFuncCall(n ast.Node, c *gas.Context) (types.Object, *ast.Ident) {
|
|
||||||
call, ok := n.(*ast.CallExpr)
|
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
id, ok := sel.X.(*ast.Ident)
|
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Info.ObjectOf(id), sel.Sel
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
o, f := matchFuncCall(n, c)
|
node, _ := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName)
|
||||||
|
|
||||||
if o == nil || f == nil {
|
if node != 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +38,7 @@ func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
|
||||||
|
|
||||||
func NewWeakRandCheck(conf map[string]interface{}) (r gas.Rule, n ast.Node) {
|
func NewWeakRandCheck(conf map[string]interface{}) (r gas.Rule, n ast.Node) {
|
||||||
r = &WeakRand{
|
r = &WeakRand{
|
||||||
pattern: regexp.MustCompile(`^Read$`),
|
funcName: "Read",
|
||||||
packagePath: "math/rand",
|
packagePath: "math/rand",
|
||||||
MetaData: gas.MetaData{
|
MetaData: gas.MetaData{
|
||||||
Severity: gas.High,
|
Severity: gas.High,
|
||||||
|
|
Loading…
Reference in a new issue