Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add os.Create to the readfile rule #761

Merged
merged 4 commits into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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