Skip to content

Commit

Permalink
G303: catch with os.WriteFile, add os.Create test case (#718)
Browse files Browse the repository at this point in the history
* Add G303 os.Create test case

* Catch G303 with os.WriteFile too
  • Loading branch information
scop authored Nov 9, 2021
1 parent 873ac24 commit 40fa36d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rules/tempfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (t *badTempFile) Match(n ast.Node, c *gosec.Context) (gi *gosec.Issue, err
func NewBadTempFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
calls := gosec.NewCallList()
calls.Add("io/ioutil", "WriteFile")
calls.Add("os", "Create")
calls.AddAll("os", "Create", "WriteFile")
return &badTempFile{
calls: calls,
args: regexp.MustCompile(`^/tmp/.*$|^/var/tmp/.*$`),
Expand Down
13 changes: 12 additions & 1 deletion testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1757,14 +1757,25 @@ package samples
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
err := ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
if err != nil {
fmt.Println("Error while writing!")
}
}`}, 1, gosec.NewConfig()}}
f, err := os.Create("/tmp/demo2")
if err != nil {
fmt.Println("Error while writing!")
} else if err = f.Close(); err != nil {
fmt.Println("Error while closing!")
}
err = os.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
if err != nil {
fmt.Println("Error while writing!")
}
}`}, 3, gosec.NewConfig()}}

// SampleCodeG304 - potential file inclusion vulnerability
SampleCodeG304 = []CodeSample{{[]string{`
Expand Down

0 comments on commit 40fa36d

Please sign in to comment.