@@ -23,19 +23,41 @@ import (
23
23
24
24
type badTempFile struct {
25
25
gosec.MetaData
26
- calls gosec.CallList
27
- args * regexp.Regexp
26
+ calls gosec.CallList
27
+ args * regexp.Regexp
28
+ argCalls gosec.CallList
29
+ nestedCalls gosec.CallList
28
30
}
29
31
30
32
func (t * badTempFile ) ID () string {
31
33
return t .MetaData .ID
32
34
}
33
35
36
+ func (t * badTempFile ) findTempDirArgs (n ast.Node , c * gosec.Context , suspect ast.Node ) * gosec.Issue {
37
+ if s , e := gosec .GetString (suspect ); e == nil {
38
+ if t .args .MatchString (s ) {
39
+ return gosec .NewIssue (c , n , t .ID (), t .What , t .Severity , t .Confidence )
40
+ }
41
+ return nil
42
+ }
43
+ if ce := t .argCalls .ContainsPkgCallExpr (suspect , c , false ); ce != nil {
44
+ return gosec .NewIssue (c , n , t .ID (), t .What , t .Severity , t .Confidence )
45
+ }
46
+ if be , ok := suspect .(* ast.BinaryExpr ); ok {
47
+ if ops := gosec .GetBinaryExprOperands (be ); len (ops ) != 0 {
48
+ return t .findTempDirArgs (n , c , ops [0 ])
49
+ }
50
+ return nil
51
+ }
52
+ if ce := t .nestedCalls .ContainsPkgCallExpr (suspect , c , false ); ce != nil {
53
+ return t .findTempDirArgs (n , c , ce .Args [0 ])
54
+ }
55
+ return nil
56
+ }
57
+
34
58
func (t * badTempFile ) Match (n ast.Node , c * gosec.Context ) (gi * gosec.Issue , err error ) {
35
59
if node := t .calls .ContainsPkgCallExpr (n , c , false ); node != nil {
36
- if arg , e := gosec .GetString (node .Args [0 ]); t .args .MatchString (arg ) && e == nil {
37
- return gosec .NewIssue (c , n , t .ID (), t .What , t .Severity , t .Confidence ), nil
38
- }
60
+ return t .findTempDirArgs (n , c , node .Args [0 ]), nil
39
61
}
40
62
return nil , nil
41
63
}
@@ -45,9 +67,15 @@ func NewBadTempFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
45
67
calls := gosec .NewCallList ()
46
68
calls .Add ("io/ioutil" , "WriteFile" )
47
69
calls .AddAll ("os" , "Create" , "WriteFile" )
70
+ argCalls := gosec .NewCallList ()
71
+ argCalls .Add ("os" , "TempDir" )
72
+ nestedCalls := gosec .NewCallList ()
73
+ nestedCalls .Add ("path" , "Join" )
48
74
return & badTempFile {
49
- calls : calls ,
50
- args : regexp .MustCompile (`^/tmp/.*$|^/var/tmp/.*$` ),
75
+ calls : calls ,
76
+ args : regexp .MustCompile (`^(/(usr|var))?/tmp(/.*)?$` ),
77
+ argCalls : argCalls ,
78
+ nestedCalls : nestedCalls ,
51
79
MetaData : gosec.MetaData {
52
80
ID : id ,
53
81
Severity : gosec .Medium ,
0 commit comments