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

@ -178,7 +178,17 @@ 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)...)
}
}
}
return values
}
// 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 { if obj != nil {
switch decl := obj.Decl.(type) { switch decl := obj.Decl.(type) {
case *ast.ValueSpec: case *ast.ValueSpec:
@ -198,9 +208,6 @@ func GetCallStringArgsValues(n ast.Node, ctx *Context) []string {
} }
} }
}
}
}
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