Skip to content

Commit 40fa36d

Browse files
authored
G303: catch with os.WriteFile, add os.Create test case (#718)
* Add G303 os.Create test case * Catch G303 with os.WriteFile too
1 parent 873ac24 commit 40fa36d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

rules/tempfiles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (t *badTempFile) Match(n ast.Node, c *gosec.Context) (gi *gosec.Issue, err
4444
func NewBadTempFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
4545
calls := gosec.NewCallList()
4646
calls.Add("io/ioutil", "WriteFile")
47-
calls.Add("os", "Create")
47+
calls.AddAll("os", "Create", "WriteFile")
4848
return &badTempFile{
4949
calls: calls,
5050
args: regexp.MustCompile(`^/tmp/.*$|^/var/tmp/.*$`),

testutils/source.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1757,14 +1757,25 @@ package samples
17571757
import (
17581758
"fmt"
17591759
"io/ioutil"
1760+
"os"
17601761
)
17611762
17621763
func main() {
17631764
err := ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
17641765
if err != nil {
17651766
fmt.Println("Error while writing!")
17661767
}
1767-
}`}, 1, gosec.NewConfig()}}
1768+
f, err := os.Create("/tmp/demo2")
1769+
if err != nil {
1770+
fmt.Println("Error while writing!")
1771+
} else if err = f.Close(); err != nil {
1772+
fmt.Println("Error while closing!")
1773+
}
1774+
err = os.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
1775+
if err != nil {
1776+
fmt.Println("Error while writing!")
1777+
}
1778+
}`}, 3, gosec.NewConfig()}}
17681779

17691780
// SampleCodeG304 - potential file inclusion vulnerability
17701781
SampleCodeG304 = []CodeSample{{[]string{`

0 commit comments

Comments
 (0)