From 90fe5cb5ab5f7ca5c9198137b2a25f8f0570f219 Mon Sep 17 00:00:00 2001 From: Grant Murphy Date: Fri, 9 Mar 2018 11:27:41 +1000 Subject: [PATCH] Port readfile rule to include ID and metadata --- rules/readfile.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/rules/readfile.go b/rules/readfile.go index 7c7117f..83b11f2 100644 --- a/rules/readfile.go +++ b/rules/readfile.go @@ -22,9 +22,16 @@ import ( ) type readfile struct { + gas.MetaData gas.CallList } +// ID returns the identifier for this rule +func (r *readfile) ID() string { + return r.MetaData.ID +} + + // Match inspects AST nodes to determine if the match the methods `os.Open` or `ioutil.ReadFile` func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) { if node := r.ContainsCallExpr(n, c); node != nil { @@ -32,7 +39,7 @@ func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) { if ident, ok := arg.(*ast.Ident); ok { obj := c.Info.ObjectOf(ident) if _, ok := obj.(*types.Var); ok && !gas.TryResolve(ident, c) { - return gas.NewIssue(c, n, "File inclusion launched with variable", gas.Medium, gas.High), nil + return gas.NewIssue(c, n, r.What, r.Severity, r.Confidence), nil } } } @@ -41,8 +48,16 @@ func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) { } // NewReadFile detects cases where we read files -func NewReadFile(conf gas.Config) (gas.Rule, []ast.Node) { - rule := &readfile{gas.NewCallList()} +func NewReadFile(id string, conf gas.Config) (gas.Rule, []ast.Node) { + rule := &readfile{ + CallList: gas.NewCallList(), + MetaData: gas.MetaData{ + ID: id, + What: "Potential file inclusion via variable", + Severity: gas.Medium, + Confidence: gas.High, + }, + } rule.Add("io/ioutil", "ReadFile") rule.Add("os", "Open") return rule, []ast.Node{(*ast.CallExpr)(nil)}