Skip to content

Commit 5874e63

Browse files
authored
Track back when a file path was sanitized with filepath.Clean (#912)
* Track back when a file path was sanitized with filepath.Clean * Remove unused argument to fix lint warnings
1 parent fd28036 commit 5874e63

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

rules/readfile.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
type readfile struct {
2525
gosec.MetaData
2626
gosec.CallList
27-
pathJoin gosec.CallList
28-
clean gosec.CallList
27+
pathJoin gosec.CallList
28+
clean gosec.CallList
29+
cleanedVar map[any]ast.Node
2930
}
3031

3132
// ID returns the identifier for this rule
@@ -57,24 +58,29 @@ func (r *readfile) isJoinFunc(n ast.Node, c *gosec.Context) bool {
5758
return false
5859
}
5960

60-
// isFilepathClean checks if there is a filepath.Clean before assigning to a variable
61-
func (r *readfile) isFilepathClean(n *ast.Ident, c *gosec.Context) bool {
62-
if n.Obj.Kind != ast.Var {
63-
return false
61+
// isFilepathClean checks if there is a filepath.Clean for given variable
62+
func (r *readfile) isFilepathClean(n *ast.Ident) bool {
63+
if _, ok := r.cleanedVar[n.Obj.Decl]; ok {
64+
return true
6465
}
65-
if node, ok := n.Obj.Decl.(*ast.AssignStmt); ok {
66-
if call, ok := node.Rhs[0].(*ast.CallExpr); ok {
67-
if clean := r.clean.ContainsPkgCallExpr(call, c, false); clean != nil {
68-
return true
69-
}
66+
return false
67+
}
68+
69+
// trackFilepathClean tracks back the declaration of variable from filepath.Clean argument
70+
func (r *readfile) trackFilepathClean(n ast.Node) {
71+
if clean, ok := n.(*ast.CallExpr); ok && len(clean.Args) > 0 {
72+
if ident, ok := clean.Args[0].(*ast.Ident); ok {
73+
r.cleanedVar[ident.Obj.Decl] = n
7074
}
7175
}
72-
return false
7376
}
7477

7578
// Match inspects AST nodes to determine if the match the methods `os.Open` or `ioutil.ReadFile`
7679
func (r *readfile) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
77-
if node := r.ContainsPkgCallExpr(n, c, false); node != nil {
80+
if node := r.clean.ContainsPkgCallExpr(n, c, false); node != nil {
81+
r.trackFilepathClean(n)
82+
return nil, nil
83+
} else if node := r.ContainsPkgCallExpr(n, c, false); node != nil {
7884
for _, arg := range node.Args {
7985
// handles path joining functions in Arg
8086
// eg. os.Open(filepath.Join("/tmp/", file))
@@ -95,7 +101,7 @@ func (r *readfile) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
95101
obj := c.Info.ObjectOf(ident)
96102
if _, ok := obj.(*types.Var); ok &&
97103
!gosec.TryResolve(ident, c) &&
98-
!r.isFilepathClean(ident, c) {
104+
!r.isFilepathClean(ident) {
99105
return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
100106
}
101107
}
@@ -116,6 +122,7 @@ func NewReadFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
116122
Severity: gosec.Medium,
117123
Confidence: gosec.High,
118124
},
125+
cleanedVar: map[any]ast.Node{},
119126
}
120127
rule.pathJoin.Add("path/filepath", "Join")
121128
rule.pathJoin.Add("path", "Join")

testutils/source.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,28 @@ import (
24632463
"path/filepath"
24642464
)
24652465
2466+
func openFile(dir string, filePath string) {
2467+
fp := filepath.Join(dir, filePath)
2468+
fp = filepath.Clean(fp)
2469+
_, err := os.OpenFile(fp, os.O_RDONLY, 0600)
2470+
if err != nil {
2471+
panic(err)
2472+
}
2473+
}
2474+
2475+
func main() {
2476+
repoFile := "path_of_file"
2477+
dir := "path_of_dir"
2478+
openFile(dir, repoFile)
2479+
}
2480+
`}, 0, gosec.NewConfig()}, {[]string{`
2481+
package main
2482+
2483+
import (
2484+
"os"
2485+
"path/filepath"
2486+
)
2487+
24662488
func main() {
24672489
repoFile := "path_of_file"
24682490
relFile, err := filepath.Rel("./", repoFile)

0 commit comments

Comments
 (0)