Extend the bind rule to handle the case when the net.Listen address in provided from a const

This commit is contained in:
Cosmin Cojocar 2018-12-02 16:28:51 +01:00 committed by Cosmin Cojocar
parent 9b32fcac16
commit 24e3094d2a
3 changed files with 48 additions and 19 deletions

View file

@ -165,7 +165,7 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
return "", "", fmt.Errorf("unable to determine call info") return "", "", fmt.Errorf("unable to determine call info")
} }
// GetCallStringArgsValues returns the values of strings arguments if they can be resolved // GetCallStringArgsValues returns the values of strings arguments if they can be resolved
func GetCallStringArgsValues(n ast.Node, ctx *Context) []string { func GetCallStringArgsValues(n ast.Node, ctx *Context) []string {
values := []string{} values := []string{}
switch node := n.(type) { switch node := n.(type) {
@ -178,28 +178,35 @@ func GetCallStringArgsValues(n ast.Node, ctx *Context) []string {
values = append(values, value) values = append(values, value)
} }
case *ast.Ident: case *ast.Ident:
obj := param.Obj values = append(values, GetIdentStringValues(param)...)
if obj != nil { }
switch decl := obj.Decl.(type) { }
case *ast.ValueSpec: }
for _, v := range decl.Values { return values
value, err := GetString(v) }
if err == nil {
values = append(values, value)
}
}
case *ast.AssignStmt:
for _, v := range decl.Rhs {
value, err := GetString(v)
if err == nil {
values = append(values, value)
}
}
}
// GetIdentStringValues return the string values of an Ident if they can be resolved
func GetIdentStringValues(ident *ast.Ident) []string {
values := []string{}
obj := ident.Obj
if obj != nil {
switch decl := obj.Decl.(type) {
case *ast.ValueSpec:
for _, v := range decl.Values {
value, err := GetString(v)
if err == nil {
values = append(values, value)
}
}
case *ast.AssignStmt:
for _, v := range decl.Rhs {
value, err := GetString(v)
if err == nil {
values = append(values, value)
} }
} }
} }
} }
return values return values
} }

View file

@ -45,6 +45,13 @@ func (r *bindsToAllNetworkInterfaces) Match(n ast.Node, c *gosec.Context) (*gose
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
} }
} }
} else if ident, ok := arg.(*ast.Ident); ok {
values := gosec.GetIdentStringValues(ident)
for _, value := range values {
if r.pattern.MatchString(value) {
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
}
}
} }
} else if len(callExpr.Args) > 0 { } else if len(callExpr.Args) > 0 {
values := gosec.GetCallStringArgsValues(callExpr.Args[0], c) values := gosec.GetCallStringArgsValues(callExpr.Args[0], c)

View file

@ -134,6 +134,21 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
defer l.Close() defer l.Close()
}`}, 1},
{[]string{`
package main
import (
"log"
"net"
)
const addr = "0.0.0.0:2000"
func main() {
l, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(err)
}
defer l.Close()
}`}, 1}, }`}, 1},
} }
// SampleCodeG103 find instances of unsafe blocks for auditing purposes // SampleCodeG103 find instances of unsafe blocks for auditing purposes