2018-07-18 13:31:07 +01:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
"github.com/securego/gosec"
|
2018-07-18 13:31:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type archive struct {
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.MetaData
|
|
|
|
calls gosec.CallList
|
2018-07-18 13:31:07 +01:00
|
|
|
argType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *archive) ID() string {
|
|
|
|
return a.MetaData.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match inspects AST nodes to determine if the filepath.Joins uses any argument derived from type zip.File
|
2018-07-19 17:42:25 +01:00
|
|
|
func (a *archive) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
|
2020-01-28 13:11:00 +00:00
|
|
|
if node := a.calls.ContainsPkgCallExpr(n, c, false); node != nil {
|
2018-07-18 13:31:07 +01:00
|
|
|
for _, arg := range node.Args {
|
|
|
|
var argType types.Type
|
|
|
|
if selector, ok := arg.(*ast.SelectorExpr); ok {
|
|
|
|
argType = c.Info.TypeOf(selector.X)
|
|
|
|
} else if ident, ok := arg.(*ast.Ident); ok {
|
|
|
|
if ident.Obj != nil && ident.Obj.Kind == ast.Var {
|
|
|
|
decl := ident.Obj.Decl
|
|
|
|
if assign, ok := decl.(*ast.AssignStmt); ok {
|
|
|
|
if selector, ok := assign.Rhs[0].(*ast.SelectorExpr); ok {
|
|
|
|
argType = c.Info.TypeOf(selector.X)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argType != nil && argType.String() == a.argType {
|
2018-07-19 17:42:25 +01:00
|
|
|
return gosec.NewIssue(c, n, a.ID(), a.What, a.Severity, a.Confidence), nil
|
2018-07-18 13:31:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewArchive creates a new rule which detects the file traversal when extracting zip archives
|
2018-07-19 17:42:25 +01:00
|
|
|
func NewArchive(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
|
|
|
calls := gosec.NewCallList()
|
2018-07-18 13:31:07 +01:00
|
|
|
calls.Add("path/filepath", "Join")
|
|
|
|
return &archive{
|
|
|
|
calls: calls,
|
|
|
|
argType: "*archive/zip.File",
|
2018-07-19 17:42:25 +01:00
|
|
|
MetaData: gosec.MetaData{
|
2018-07-18 13:31:07 +01:00
|
|
|
ID: id,
|
2018-07-19 17:42:25 +01:00
|
|
|
Severity: gosec.Medium,
|
|
|
|
Confidence: gosec.High,
|
2018-07-18 13:31:07 +01:00
|
|
|
What: "File traversal when extracting zip archive",
|
|
|
|
},
|
|
|
|
}, []ast.Node{(*ast.CallExpr)(nil)}
|
|
|
|
}
|