Skip to content

Commit

Permalink
Add os.Create to the readfile rule (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccojocar committed Jan 12, 2022
1 parent 75cc7dc commit 7be6d4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/gosec/main.go
Expand Up @@ -246,7 +246,7 @@ func printReport(format string, color bool, rootPaths []string, reportInfo *gose
}

func saveReport(filename, format string, rootPaths []string, reportInfo *gosec.ReportInfo) error {
outfile, err := os.Create(filename)
outfile, err := os.Create(filename) //#nosec G304
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions rules/readfile.go
Expand Up @@ -125,5 +125,6 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
rule.Add("os", "ReadFile")
rule.Add("os", "Open")
rule.Add("os", "OpenFile")
rule.Add("os", "Create")
return rule, []ast.Node{(*ast.CallExpr)(nil)}
}
36 changes: 34 additions & 2 deletions testutils/source.go
Expand Up @@ -1891,7 +1891,8 @@ func main() {
}`}, 9, gosec.NewConfig()}}

// SampleCodeG304 - potential file inclusion vulnerability
SampleCodeG304 = []CodeSample{{[]string{`
SampleCodeG304 = []CodeSample{
{[]string{`
package main
import (
Expand Down Expand Up @@ -2086,7 +2087,38 @@ func main() {
}
}
`}, 0, gosec.NewConfig()}}
`}, 0, gosec.NewConfig()}, {[]string{`
package main
import (
"io"
"os"
)
func createFile(file string) *os.File {
f, err := os.Create(file)
if err != nil {
panic(err)
}
return f
}
func main() {
s, err := os.Open("src")
if err != nil {
panic(err)
}
defer s.Close()
d := createFile("dst")
defer d.Close()
_, err = io.Copy(d, s)
if err != nil {
panic(err)
}
}`}, 1, gosec.NewConfig()},
}

// SampleCodeG305 - File path traversal when extracting zip/tar archives
SampleCodeG305 = []CodeSample{{[]string{`
Expand Down

0 comments on commit 7be6d4e

Please sign in to comment.