Skip to content

Commit

Permalink
feat: add os.ReadFile to G304 (#706)
Browse files Browse the repository at this point in the history
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 <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored Oct 14, 2021
1 parent 991dd94 commit 7fd4aef
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -122,6 +122,7 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
rule.clean.Add("path/filepath", "Clean")
rule.clean.Add("path/filepath", "Rel")
rule.Add("io/ioutil", "ReadFile")
rule.Add("os", "ReadFile")
rule.Add("os", "Open")
rule.Add("os", "OpenFile")
return rule, []ast.Node{(*ast.CallExpr)(nil)}
Expand Down
16 changes: 16 additions & 0 deletions testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,22 @@ func main() {
}`}, 1, gosec.NewConfig()}, {[]string{`
package main
import (
"os"
"log"
)
func main() {
f := os.Getenv("tainted_file")
body, err := os.ReadFile(f)
if err != nil {
log.Printf("Error: %v\n", err)
}
log.Print(body)
}`}, 1, gosec.NewConfig()}, {[]string{`
package main
import (
"fmt"
"log"
Expand Down

0 comments on commit 7fd4aef

Please sign in to comment.