Skip to content

Commit 19bda8d

Browse files
authored
Find more tempdirs
* Find G303 in string concatenations, with os.TempDir, and in path.Join args * Find G303 with /usr/tmp, too /usr/tmp is commonly found e.g. on Solaris.
1 parent 827fca9 commit 19bda8d

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

rules/tempfiles.go

+35-7
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,41 @@ import (
2323

2424
type badTempFile struct {
2525
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
2830
}
2931

3032
func (t *badTempFile) ID() string {
3133
return t.MetaData.ID
3234
}
3335

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+
3458
func (t *badTempFile) Match(n ast.Node, c *gosec.Context) (gi *gosec.Issue, err error) {
3559
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
3961
}
4062
return nil, nil
4163
}
@@ -45,9 +67,15 @@ func NewBadTempFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
4567
calls := gosec.NewCallList()
4668
calls.Add("io/ioutil", "WriteFile")
4769
calls.AddAll("os", "Create", "WriteFile")
70+
argCalls := gosec.NewCallList()
71+
argCalls.Add("os", "TempDir")
72+
nestedCalls := gosec.NewCallList()
73+
nestedCalls.Add("path", "Join")
4874
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,
5179
MetaData: gosec.MetaData{
5280
ID: id,
5381
Severity: gosec.Medium,

testutils/source.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,7 @@ import (
17581758
"fmt"
17591759
"io/ioutil"
17601760
"os"
1761+
"path"
17611762
)
17621763
17631764
func main() {
@@ -1775,7 +1776,27 @@ func main() {
17751776
if err != nil {
17761777
fmt.Println("Error while writing!")
17771778
}
1778-
}`}, 3, gosec.NewConfig()}}
1779+
err = os.WriteFile("/usr/tmp/demo2", []byte("This is some data"), 0644)
1780+
if err != nil {
1781+
fmt.Println("Error while writing!")
1782+
}
1783+
err = os.WriteFile("/tmp/" + "demo2", []byte("This is some data"), 0644)
1784+
if err != nil {
1785+
fmt.Println("Error while writing!")
1786+
}
1787+
err = os.WriteFile(os.TempDir() + "/demo2", []byte("This is some data"), 0644)
1788+
if err != nil {
1789+
fmt.Println("Error while writing!")
1790+
}
1791+
err = os.WriteFile(path.Join("/var/tmp", "demo2"), []byte("This is some data"), 0644)
1792+
if err != nil {
1793+
fmt.Println("Error while writing!")
1794+
}
1795+
err = os.WriteFile(path.Join(os.TempDir(), "demo2"), []byte("This is some data"), 0644)
1796+
if err != nil {
1797+
fmt.Println("Error while writing!")
1798+
}
1799+
}`}, 8, gosec.NewConfig()}}
17791800

17801801
// SampleCodeG304 - potential file inclusion vulnerability
17811802
SampleCodeG304 = []CodeSample{{[]string{`

0 commit comments

Comments
 (0)