2018-03-08 23:23:27 +00:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2023-02-15 19:44:13 +00:00
|
|
|
"github.com/securego/gosec/v2/issue"
|
2018-03-08 23:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type readfile struct {
|
2023-02-15 19:44:13 +00:00
|
|
|
issue.MetaData
|
2018-07-19 17:42:25 +01:00
|
|
|
gosec.CallList
|
2023-01-09 15:26:20 +00:00
|
|
|
pathJoin gosec.CallList
|
|
|
|
clean gosec.CallList
|
|
|
|
cleanedVar map[any]ast.Node
|
2018-03-08 23:23:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:27:41 +00:00
|
|
|
// ID returns the identifier for this rule
|
|
|
|
func (r *readfile) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
|
|
|
|
2018-08-28 05:34:07 +01:00
|
|
|
// isJoinFunc checks if there is a filepath.Join or other join function
|
|
|
|
func (r *readfile) isJoinFunc(n ast.Node, c *gosec.Context) bool {
|
2020-01-28 13:11:00 +00:00
|
|
|
if call := r.pathJoin.ContainsPkgCallExpr(n, c, false); call != nil {
|
2018-08-28 05:34:07 +01:00
|
|
|
for _, arg := range call.Args {
|
|
|
|
// edge case: check if one of the args is a BinaryExpr
|
|
|
|
if binExp, ok := arg.(*ast.BinaryExpr); ok {
|
2018-10-11 13:45:31 +01:00
|
|
|
// iterate and resolve all found identities from the BinaryExpr
|
2018-08-28 05:34:07 +01:00
|
|
|
if _, ok := gosec.FindVarIdentities(binExp, c); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 08:40:05 +01:00
|
|
|
// try and resolve identity
|
|
|
|
if ident, ok := arg.(*ast.Ident); ok {
|
|
|
|
obj := c.Info.ObjectOf(ident)
|
|
|
|
if _, ok := obj.(*types.Var); ok && !gosec.TryResolve(ident, c) {
|
|
|
|
return true
|
|
|
|
}
|
2018-08-28 05:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:26:20 +00:00
|
|
|
// isFilepathClean checks if there is a filepath.Clean for given variable
|
2023-01-09 15:49:02 +00:00
|
|
|
func (r *readfile) isFilepathClean(n *ast.Ident, c *gosec.Context) bool {
|
2023-01-09 15:26:20 +00:00
|
|
|
if _, ok := r.cleanedVar[n.Obj.Decl]; ok {
|
|
|
|
return true
|
2020-08-17 20:05:28 +01:00
|
|
|
}
|
2023-01-09 15:49:02 +00:00
|
|
|
if n.Obj.Kind != ast.Var {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if node, ok := n.Obj.Decl.(*ast.AssignStmt); ok {
|
|
|
|
if call, ok := node.Rhs[0].(*ast.CallExpr); ok {
|
|
|
|
if clean := r.clean.ContainsPkgCallExpr(call, c, false); clean != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 15:26:20 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// trackFilepathClean tracks back the declaration of variable from filepath.Clean argument
|
|
|
|
func (r *readfile) trackFilepathClean(n ast.Node) {
|
|
|
|
if clean, ok := n.(*ast.CallExpr); ok && len(clean.Args) > 0 {
|
|
|
|
if ident, ok := clean.Args[0].(*ast.Ident); ok {
|
|
|
|
r.cleanedVar[ident.Obj.Decl] = n
|
2020-08-17 20:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:23:27 +00:00
|
|
|
// Match inspects AST nodes to determine if the match the methods `os.Open` or `ioutil.ReadFile`
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *readfile) Match(n ast.Node, c *gosec.Context) (*issue.Issue, error) {
|
2023-01-09 15:26:20 +00:00
|
|
|
if node := r.clean.ContainsPkgCallExpr(n, c, false); node != nil {
|
|
|
|
r.trackFilepathClean(n)
|
|
|
|
return nil, nil
|
|
|
|
} else if node := r.ContainsPkgCallExpr(n, c, false); node != nil {
|
2018-03-08 23:23:27 +00:00
|
|
|
for _, arg := range node.Args {
|
2018-08-28 05:34:07 +01:00
|
|
|
// handles path joining functions in Arg
|
|
|
|
// eg. os.Open(filepath.Join("/tmp/", file))
|
|
|
|
if callExpr, ok := arg.(*ast.CallExpr); ok {
|
|
|
|
if r.isJoinFunc(callExpr, c) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2018-08-28 05:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// handles binary string concatenation eg. ioutil.Readfile("/tmp/" + file + "/blob")
|
|
|
|
if binExp, ok := arg.(*ast.BinaryExpr); ok {
|
2018-10-11 13:45:31 +01:00
|
|
|
// resolve all found identities from the BinaryExpr
|
2018-08-28 05:34:07 +01:00
|
|
|
if _, ok := gosec.FindVarIdentities(binExp, c); ok {
|
2023-02-15 19:44:13 +00:00
|
|
|
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2018-08-28 05:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:23:27 +00:00
|
|
|
if ident, ok := arg.(*ast.Ident); ok {
|
|
|
|
obj := c.Info.ObjectOf(ident)
|
2020-08-17 20:05:28 +01:00
|
|
|
if _, ok := obj.(*types.Var); ok &&
|
|
|
|
!gosec.TryResolve(ident, c) &&
|
2023-01-09 15:49:02 +00:00
|
|
|
!r.isFilepathClean(ident, c) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2018-03-08 23:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReadFile detects cases where we read files
|
2018-07-19 17:42:25 +01:00
|
|
|
func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
2018-03-09 01:27:41 +00:00
|
|
|
rule := &readfile{
|
2018-08-28 05:34:07 +01:00
|
|
|
pathJoin: gosec.NewCallList(),
|
2020-08-17 20:05:28 +01:00
|
|
|
clean: gosec.NewCallList(),
|
2018-07-19 17:42:25 +01:00
|
|
|
CallList: gosec.NewCallList(),
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2018-03-09 01:27:41 +00:00
|
|
|
ID: id,
|
|
|
|
What: "Potential file inclusion via variable",
|
2023-02-15 19:44:13 +00:00
|
|
|
Severity: issue.Medium,
|
|
|
|
Confidence: issue.High,
|
2018-03-09 02:49:01 +00:00
|
|
|
},
|
2023-01-09 15:26:20 +00:00
|
|
|
cleanedVar: map[any]ast.Node{},
|
2018-03-09 02:49:01 +00:00
|
|
|
}
|
2018-08-28 05:34:07 +01:00
|
|
|
rule.pathJoin.Add("path/filepath", "Join")
|
|
|
|
rule.pathJoin.Add("path", "Join")
|
2020-08-17 20:05:28 +01:00
|
|
|
rule.clean.Add("path/filepath", "Clean")
|
2020-08-19 07:39:55 +01:00
|
|
|
rule.clean.Add("path/filepath", "Rel")
|
2018-03-08 23:23:27 +00:00
|
|
|
rule.Add("io/ioutil", "ReadFile")
|
2021-10-14 08:53:26 +01:00
|
|
|
rule.Add("os", "ReadFile")
|
2018-03-08 23:23:27 +00:00
|
|
|
rule.Add("os", "Open")
|
2020-06-16 12:29:03 +01:00
|
|
|
rule.Add("os", "OpenFile")
|
2022-01-12 18:33:17 +00:00
|
|
|
rule.Add("os", "Create")
|
2018-03-08 23:23:27 +00:00
|
|
|
return rule, []ast.Node{(*ast.CallExpr)(nil)}
|
|
|
|
}
|