Skip to content

Commit 7fd4aef

Browse files
authored
feat: add os.ReadFile to G304 (#706)
In Go 1.16 or higher, the `io/ioutil` has been deprecated and the `ioutil.ReadFile` function now calls `os.ReadFile`. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 991dd94 commit 7fd4aef

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

rules/readfile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
122122
rule.clean.Add("path/filepath", "Clean")
123123
rule.clean.Add("path/filepath", "Rel")
124124
rule.Add("io/ioutil", "ReadFile")
125+
rule.Add("os", "ReadFile")
125126
rule.Add("os", "Open")
126127
rule.Add("os", "OpenFile")
127128
return rule, []ast.Node{(*ast.CallExpr)(nil)}

testutils/source.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,22 @@ func main() {
17871787
}`}, 1, gosec.NewConfig()}, {[]string{`
17881788
package main
17891789
1790+
import (
1791+
"os"
1792+
"log"
1793+
)
1794+
1795+
func main() {
1796+
f := os.Getenv("tainted_file")
1797+
body, err := os.ReadFile(f)
1798+
if err != nil {
1799+
log.Printf("Error: %v\n", err)
1800+
}
1801+
log.Print(body)
1802+
1803+
}`}, 1, gosec.NewConfig()}, {[]string{`
1804+
package main
1805+
17901806
import (
17911807
"fmt"
17921808
"log"

0 commit comments

Comments
 (0)