Skip to content

Commit 44f484f

Browse files
author
Dmitry Golushko
authored
Additional types for bad defer check (#897)
* Additional types for bad defer check * Ignore new check in tlsconfig.go
1 parent 2fe6c5b commit 44f484f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

cmd/tlsconfig/tlsconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func getTLSConfFromURL(url string) (*ServerSideTLSJson, error) {
6868
if err != nil {
6969
return nil, err
7070
}
71-
defer r.Body.Close()
71+
defer r.Body.Close() //#nosec G307
7272

7373
var sstls ServerSideTLSJson
7474
err = json.NewDecoder(r.Body).Decode(&sstls)

rules/bad_defer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ func NewDeferredClosing(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
5757
typ: "os.File",
5858
methods: []string{"Close"},
5959
},
60+
{
61+
typ: "io.ReadCloser",
62+
methods: []string{"Close"},
63+
},
64+
{
65+
typ: "io.WriteCloser",
66+
methods: []string{"Close"},
67+
},
68+
{
69+
typ: "io.ReadWriteCloser",
70+
methods: []string{"Close"},
71+
},
72+
{
73+
typ: "io.ReadSeekCloser",
74+
methods: []string{"Close"},
75+
},
76+
{
77+
typ: "io.Closer",
78+
methods: []string{"Close"},
79+
},
80+
{
81+
typ: "net.Conn",
82+
methods: []string{"Close"},
83+
},
84+
{
85+
typ: "net.Listener",
86+
methods: []string{"Close"},
87+
},
6088
},
6189
MetaData: gosec.MetaData{
6290
ID: id,

testutils/source.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,23 @@ func main() {
27562756
n4, err := w.WriteString("buffered\n")
27572757
fmt.Printf("wrote %d bytes\n", n4)
27582758
w.Flush()
2759-
}`}, 1, gosec.NewConfig()},
2759+
}`}, 1, gosec.NewConfig()}, {[]string{`
2760+
package main
2761+
2762+
import (
2763+
"net"
2764+
"net/http"
2765+
)
2766+
2767+
func main() {
2768+
response, _ := http.Get("https://127.0.0.1")
2769+
2770+
defer response.Body.Close() // io.ReadCloser
2771+
2772+
conn, _ := net.Dial("tcp", "127.0.0.1:8080")
2773+
defer conn.Close() // net.Conn
2774+
2775+
}`}, 2, gosec.NewConfig()},
27602776
}
27612777

27622778
// SampleCodeG401 - Use of weak crypto MD5

0 commit comments

Comments
 (0)