Skip to content

Commit 9b32fca

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Fix the bind rule to handle the case when the arguments of the net.Listen are returned by a function call
1 parent f14f17f commit 9b32fca

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

rules/bind.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,21 @@ func (r *bindsToAllNetworkInterfaces) Match(n ast.Node, c *gosec.Context) (*gose
3737
if callExpr == nil {
3838
return nil, nil
3939
}
40-
if arg, err := gosec.GetString(callExpr.Args[1]); err == nil {
41-
if r.pattern.MatchString(arg) {
42-
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
40+
if len(callExpr.Args) > 1 {
41+
arg := callExpr.Args[1]
42+
if bl, ok := arg.(*ast.BasicLit); ok {
43+
if arg, err := gosec.GetString(bl); err == nil {
44+
if r.pattern.MatchString(arg) {
45+
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
46+
}
47+
}
48+
}
49+
} else if len(callExpr.Args) > 0 {
50+
values := gosec.GetCallStringArgsValues(callExpr.Args[0], c)
51+
for _, value := range values {
52+
if r.pattern.MatchString(value) {
53+
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
54+
}
4355
}
4456
}
4557
return nil, nil

testutils/source.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,42 @@ func main() {
9898
log.Fatal(err)
9999
}
100100
defer l.Close()
101+
}`}, 1},
102+
// Bind to all networks indirectly through a parsing function
103+
{[]string{`
104+
package main
105+
import (
106+
"log"
107+
"net"
108+
)
109+
func parseListenAddr(listenAddr string) (network string, addr string) {
110+
return "", ""
111+
}
112+
func main() {
113+
addr := ":2000"
114+
l, err := net.Listen(parseListenAddr(addr))
115+
if err != nil {
116+
log.Fatal(err)
117+
}
118+
defer l.Close()
119+
}`}, 1},
120+
// Bind to all networks indirectly through a parsing function
121+
{[]string{`
122+
package main
123+
import (
124+
"log"
125+
"net"
126+
)
127+
const addr = ":2000"
128+
func parseListenAddr(listenAddr string) (network string, addr string) {
129+
return "", ""
130+
}
131+
func main() {
132+
l, err := net.Listen(parseListenAddr(addr))
133+
if err != nil {
134+
log.Fatal(err)
135+
}
136+
defer l.Close()
101137
}`}, 1},
102138
}
103139
// SampleCodeG103 find instances of unsafe blocks for auditing purposes

0 commit comments

Comments
 (0)