Use explicit packages in call lists

By allowing partial matches of selectors there are chances of collisions
such as those in issue #145, this removes it to expect explicit packages
for each rule.

Closes #145
This commit is contained in:
Grant Murphy 2018-01-05 23:05:53 +10:00
parent b6f85d50da
commit aecbc873ef
7 changed files with 19 additions and 15 deletions

View file

@ -61,14 +61,18 @@ func (c CallList) ContainsCallExpr(n ast.Node, ctx *Context) *ast.CallExpr {
return nil return nil
} }
// Try direct resolution // Use only explicit path to reduce conflicts
if c.Contains(selector, ident) {
return n.(*ast.CallExpr)
}
// Also support explicit path
if path, ok := GetImportPath(selector, ctx); ok && c.Contains(path, ident) { if path, ok := GetImportPath(selector, ctx); ok && c.Contains(path, ident) {
return n.(*ast.CallExpr) return n.(*ast.CallExpr)
} }
/*
// Try direct resolution
if c.Contains(selector, ident) {
log.Printf("c.Contains == true, %s, %s.", selector, ident)
return n.(*ast.CallExpr)
}
*/
return nil return nil
} }

View file

@ -66,7 +66,7 @@ var _ = Describe("call list", func() {
ctx := pkg.CreateContext("md5.go") ctx := pkg.CreateContext("md5.go")
// Search for md5.New() // Search for md5.New()
calls.Add("md5", "New") calls.Add("crypto/md5", "New")
// Stub out visitor and count number of matched call expr // Stub out visitor and count number of matched call expr
matched := 0 matched := 0

View file

@ -46,7 +46,7 @@ func (r *bindsToAllNetworkInterfaces) Match(n ast.Node, c *gas.Context) (*gas.Is
func NewBindsToAllNetworkInterfaces(conf gas.Config) (gas.Rule, []ast.Node) { func NewBindsToAllNetworkInterfaces(conf gas.Config) (gas.Rule, []ast.Node) {
calls := gas.NewCallList() calls := gas.NewCallList()
calls.Add("net", "Listen") calls.Add("net", "Listen")
calls.Add("tls", "Listen") calls.Add("crypto/tls", "Listen")
return &bindsToAllNetworkInterfaces{ return &bindsToAllNetworkInterfaces{
calls: calls, calls: calls,
pattern: regexp.MustCompile(`^(0.0.0.0|:).*$`), pattern: regexp.MustCompile(`^(0.0.0.0|:).*$`),

View file

@ -39,7 +39,7 @@ func (w *weakKeyStrength) Match(n ast.Node, c *gas.Context) (*gas.Issue, error)
// NewWeakKeyStrength builds a rule that detects RSA keys < 2048 bits // NewWeakKeyStrength builds a rule that detects RSA keys < 2048 bits
func NewWeakKeyStrength(conf gas.Config) (gas.Rule, []ast.Node) { func NewWeakKeyStrength(conf gas.Config) (gas.Rule, []ast.Node) {
calls := gas.NewCallList() calls := gas.NewCallList()
calls.Add("rsa", "GenerateKey") calls.Add("crypto/rsa", "GenerateKey")
bits := 2048 bits := 2048
return &weakKeyStrength{ return &weakKeyStrength{
calls: calls, calls: calls,

View file

@ -52,7 +52,7 @@ func (r *subprocess) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
// NewSubproc detects cases where we are forking out to an external process // NewSubproc detects cases where we are forking out to an external process
func NewSubproc(conf gas.Config) (gas.Rule, []ast.Node) { func NewSubproc(conf gas.Config) (gas.Rule, []ast.Node) {
rule := &subprocess{gas.NewCallList()} rule := &subprocess{gas.NewCallList()}
rule.Add("exec", "Command") rule.Add("os/exec", "Command")
rule.Add("syscall", "Exec") rule.Add("syscall", "Exec")
return rule, []ast.Node{(*ast.CallExpr)(nil)} return rule, []ast.Node{(*ast.CallExpr)(nil)}
} }

View file

@ -39,7 +39,7 @@ func (t *badTempFile) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err erro
// NewBadTempFile detects direct writes to predictable path in temporary directory // NewBadTempFile detects direct writes to predictable path in temporary directory
func NewBadTempFile(conf gas.Config) (gas.Rule, []ast.Node) { func NewBadTempFile(conf gas.Config) (gas.Rule, []ast.Node) {
calls := gas.NewCallList() calls := gas.NewCallList()
calls.Add("ioutil", "WriteFile") calls.Add("io/ioutil", "WriteFile")
calls.Add("os", "Create") calls.Add("os", "Create")
return &badTempFile{ return &badTempFile{
calls: calls, calls: calls,

View file

@ -41,10 +41,10 @@ func (t *templateCheck) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
func NewTemplateCheck(conf gas.Config) (gas.Rule, []ast.Node) { func NewTemplateCheck(conf gas.Config) (gas.Rule, []ast.Node) {
calls := gas.NewCallList() calls := gas.NewCallList()
calls.Add("template", "HTML") calls.Add("html/template", "HTML")
calls.Add("template", "HTMLAttr") calls.Add("html/template", "HTMLAttr")
calls.Add("template", "JS") calls.Add("html/template", "JS")
calls.Add("template", "URL") calls.Add("html/template", "URL")
return &templateCheck{ return &templateCheck{
calls: calls, calls: calls,
MetaData: gas.MetaData{ MetaData: gas.MetaData{