Skip to content

Commit 7be6d4e

Browse files
authored
Add os.Create to the readfile rule (#761)
1 parent 75cc7dc commit 7be6d4e

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

cmd/gosec/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func printReport(format string, color bool, rootPaths []string, reportInfo *gose
246246
}
247247

248248
func saveReport(filename, format string, rootPaths []string, reportInfo *gosec.ReportInfo) error {
249-
outfile, err := os.Create(filename)
249+
outfile, err := os.Create(filename) //#nosec G304
250250
if err != nil {
251251
return err
252252
}

rules/readfile.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,6 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
125125
rule.Add("os", "ReadFile")
126126
rule.Add("os", "Open")
127127
rule.Add("os", "OpenFile")
128+
rule.Add("os", "Create")
128129
return rule, []ast.Node{(*ast.CallExpr)(nil)}
129130
}

testutils/source.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,8 @@ func main() {
18911891
}`}, 9, gosec.NewConfig()}}
18921892

18931893
// SampleCodeG304 - potential file inclusion vulnerability
1894-
SampleCodeG304 = []CodeSample{{[]string{`
1894+
SampleCodeG304 = []CodeSample{
1895+
{[]string{`
18951896
package main
18961897
18971898
import (
@@ -2086,7 +2087,38 @@ func main() {
20862087
}
20872088
}
20882089
2089-
`}, 0, gosec.NewConfig()}}
2090+
`}, 0, gosec.NewConfig()}, {[]string{`
2091+
package main
2092+
2093+
import (
2094+
"io"
2095+
"os"
2096+
)
2097+
2098+
func createFile(file string) *os.File {
2099+
f, err := os.Create(file)
2100+
if err != nil {
2101+
panic(err)
2102+
}
2103+
return f
2104+
}
2105+
2106+
func main() {
2107+
s, err := os.Open("src")
2108+
if err != nil {
2109+
panic(err)
2110+
}
2111+
defer s.Close()
2112+
2113+
d := createFile("dst")
2114+
defer d.Close()
2115+
2116+
_, err = io.Copy(d, s)
2117+
if err != nil {
2118+
panic(err)
2119+
}
2120+
}`}, 1, gosec.NewConfig()},
2121+
}
20902122

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

0 commit comments

Comments
 (0)