Skip to content

Commit 1fce461

Browse files
authored
fix: WriteParams rule to work also with golang 1.16 (#577)
In go 1.16 the `ioutil` package was deprecated and the functions should be replaced by their equivalents in either `io` or `os` packages. This means, that `ioutil.WriteFile` should be replaced by `os.WriteFile` instead. To account for this change and to detect incorrect permissions also for `os.WriteFile` I changed `filePermissions` rule slightly to allows specifying multiple packages that can contain given function and that we should check. This workaround can be removed after a sufficient time has passed and after it is decided that checking `os.WriteFile` is enough. Fixes: #576
1 parent dcbcc4d commit 1fce461

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

rules/fileperms.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
type filePermissions struct {
2626
gosec.MetaData
2727
mode int64
28-
pkg string
28+
pkgs []string
2929
calls []string
3030
}
3131

@@ -51,10 +51,12 @@ func getConfiguredMode(conf map[string]interface{}, configKey string, defaultMod
5151
}
5252

5353
func (r *filePermissions) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
54-
if callexpr, matched := gosec.MatchCallByPackage(n, c, r.pkg, r.calls...); matched {
55-
modeArg := callexpr.Args[len(callexpr.Args)-1]
56-
if mode, err := gosec.GetInt(modeArg); err == nil && mode > r.mode {
57-
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
54+
for _, pkg := range r.pkgs {
55+
if callexpr, matched := gosec.MatchCallByPackage(n, c, pkg, r.calls...); matched {
56+
modeArg := callexpr.Args[len(callexpr.Args)-1]
57+
if mode, err := gosec.GetInt(modeArg); err == nil && mode > r.mode {
58+
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
59+
}
5860
}
5961
}
6062
return nil, nil
@@ -65,7 +67,7 @@ func NewWritePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
6567
mode := getConfiguredMode(conf, "G306", 0600)
6668
return &filePermissions{
6769
mode: mode,
68-
pkg: "io/ioutil",
70+
pkgs: []string{"io/ioutil", "os"},
6971
calls: []string{"WriteFile"},
7072
MetaData: gosec.MetaData{
7173
ID: id,
@@ -82,7 +84,7 @@ func NewFilePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
8284
mode := getConfiguredMode(conf, "G302", 0600)
8385
return &filePermissions{
8486
mode: mode,
85-
pkg: "os",
87+
pkgs: []string{"os"},
8688
calls: []string{"OpenFile", "Chmod"},
8789
MetaData: gosec.MetaData{
8890
ID: id,
@@ -99,7 +101,7 @@ func NewMkdirPerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
99101
mode := getConfiguredMode(conf, "G301", 0750)
100102
return &filePermissions{
101103
mode: mode,
102-
pkg: "os",
104+
pkgs: []string{"os"},
103105
calls: []string{"Mkdir", "MkdirAll"},
104106
MetaData: gosec.MetaData{
105107
ID: id,

0 commit comments

Comments
 (0)