Skip to content

Commit 6fbd381

Browse files
committedMay 14, 2024··
Catch os.ModePerm permissions in os.WriteFile
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
1 parent dc5e5a9 commit 6fbd381

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed
 

‎rules/fileperms.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,26 @@ func (r *filePermissions) Match(n ast.Node, c *gosec.Context) (*issue.Issue, err
6161
for _, pkg := range r.pkgs {
6262
if callexpr, matched := gosec.MatchCallByPackage(n, c, pkg, r.calls...); matched {
6363
modeArg := callexpr.Args[len(callexpr.Args)-1]
64-
if mode, err := gosec.GetInt(modeArg); err == nil && !modeIsSubset(mode, r.mode) {
64+
if mode, err := gosec.GetInt(modeArg); err == nil && !modeIsSubset(mode, r.mode) || isOsPerm(modeArg) {
6565
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
6666
}
6767
}
6868
}
6969
return nil, nil
7070
}
7171

72+
// isOsPerm check if the provide ast node contains a os.PermMode symbol
73+
func isOsPerm(n ast.Node) bool {
74+
if node, ok := n.(*ast.SelectorExpr); ok {
75+
if identX, ok := node.X.(*ast.Ident); ok {
76+
if identX.Name == "os" && node.Sel != nil && node.Sel.Name == "ModePerm" {
77+
return true
78+
}
79+
}
80+
}
81+
return false
82+
}
83+
7284
// NewWritePerms creates a rule to detect file Writes with bad permissions.
7385
func NewWritePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
7486
mode := getConfiguredMode(conf, id, 0o600)

0 commit comments

Comments
 (0)
Please sign in to comment.