Skip to content

Commit

Permalink
Extend the rule G304 with os.OpenFile and add a test to cover it
Browse files Browse the repository at this point in the history
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
  • Loading branch information
ccojocar authored and Cosmin Cojocar committed Jun 17, 2020
1 parent 0c1a71b commit 1d2c951
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
rule.pathJoin.Add("path", "Join")
rule.Add("io/ioutil", "ReadFile")
rule.Add("os", "Open")
rule.Add("os", "OpenFile")
return rule, []ast.Node{(*ast.CallExpr)(nil)}
}
24 changes: 24 additions & 0 deletions testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,30 @@ func main() {
}`}, 1, gosec.NewConfig()}, {[]string{`
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
title := r.URL.Query().Get("title")
f, err := os.OpenFile(title, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
body := make([]byte, 5)
if _, err = f.Read(body); err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Fprintf(w, "%s", body)
})
log.Fatal(http.ListenAndServe(":3000", nil))
}`}, 1, gosec.NewConfig()}, {[]string{`
package main
import (
"log"
"os"
Expand Down

0 comments on commit 1d2c951

Please sign in to comment.