Port readfile rule to include ID and metadata

This commit is contained in:
Grant Murphy 2018-03-09 11:27:41 +10:00
parent 58a48c471c
commit 90fe5cb5ab

View file

@ -22,9 +22,16 @@ import (
) )
type readfile struct { type readfile struct {
gas.MetaData
gas.CallList 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` // 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) { func (r *readfile) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
if node := r.ContainsCallExpr(n, c); node != nil { 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 { if ident, ok := arg.(*ast.Ident); ok {
obj := c.Info.ObjectOf(ident) obj := c.Info.ObjectOf(ident)
if _, ok := obj.(*types.Var); ok && !gas.TryResolve(ident, c) { 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 // NewReadFile detects cases where we read files
func NewReadFile(conf gas.Config) (gas.Rule, []ast.Node) { func NewReadFile(id string, conf gas.Config) (gas.Rule, []ast.Node) {
rule := &readfile{gas.NewCallList()} 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("io/ioutil", "ReadFile")
rule.Add("os", "Open") rule.Add("os", "Open")
return rule, []ast.Node{(*ast.CallExpr)(nil)} return rule, []ast.Node{(*ast.CallExpr)(nil)}