Skip to content

Commit 90fe5cb

Browse files
committed
Port readfile rule to include ID and metadata
1 parent 58a48c4 commit 90fe5cb

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

rules/readfile.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ import (
2222
)
2323

2424
type readfile struct {
25+
gas.MetaData
2526
gas.CallList
2627
}
2728

29+
// ID returns the identifier for this rule
30+
func (r *readfile) ID() string {
31+
return r.MetaData.ID
32+
}
33+
34+
2835
// Match inspects AST nodes to determine if the match the methods `os.Open` or `ioutil.ReadFile`
2936
func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
3037
if node := r.ContainsCallExpr(n, c); node != nil {
3138
for _, arg := range node.Args {
3239
if ident, ok := arg.(*ast.Ident); ok {
3340
obj := c.Info.ObjectOf(ident)
3441
if _, ok := obj.(*types.Var); ok && !gas.TryResolve(ident, c) {
35-
return gas.NewIssue(c, n, "File inclusion launched with variable", gas.Medium, gas.High), nil
42+
return gas.NewIssue(c, n, r.What, r.Severity, r.Confidence), nil
3643
}
3744
}
3845
}
@@ -41,8 +48,16 @@ func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
4148
}
4249

4350
// NewReadFile detects cases where we read files
44-
func NewReadFile(conf gas.Config) (gas.Rule, []ast.Node) {
45-
rule := &readfile{gas.NewCallList()}
51+
func NewReadFile(id string, conf gas.Config) (gas.Rule, []ast.Node) {
52+
rule := &readfile{
53+
CallList: gas.NewCallList(),
54+
MetaData: gas.MetaData{
55+
ID: id,
56+
What: "Potential file inclusion via variable",
57+
Severity: gas.Medium,
58+
Confidence: gas.High,
59+
},
60+
}
4661
rule.Add("io/ioutil", "ReadFile")
4762
rule.Add("os", "Open")
4863
return rule, []ast.Node{(*ast.CallExpr)(nil)}

0 commit comments

Comments
 (0)