Skip to content

Commit c1970ff

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Handle the ValueSpec when trying to resolve an AST tree node
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent ea9faae commit c1970ff

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

resolve.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
package gosec
1616

17-
import "go/ast"
17+
import (
18+
"go/ast"
19+
)
1820

1921
func resolveIdent(n *ast.Ident, c *Context) bool {
20-
2122
if n.Obj == nil || n.Obj.Kind != ast.Var {
2223
return true
2324
}
@@ -27,7 +28,22 @@ func resolveIdent(n *ast.Ident, c *Context) bool {
2728
return false
2829
}
2930

31+
func resolveValueSpec(n *ast.ValueSpec, c *Context) bool {
32+
if len(n.Values) == 0 {
33+
return false
34+
}
35+
for _, value := range n.Values {
36+
if !TryResolve(value, c) {
37+
return false
38+
}
39+
}
40+
return true
41+
}
42+
3043
func resolveAssign(n *ast.AssignStmt, c *Context) bool {
44+
if len(n.Rhs) == 0 {
45+
return false
46+
}
3147
for _, arg := range n.Rhs {
3248
if !TryResolve(arg, c) {
3349
return false
@@ -37,6 +53,9 @@ func resolveAssign(n *ast.AssignStmt, c *Context) bool {
3753
}
3854

3955
func resolveCompLit(n *ast.CompositeLit, c *Context) bool {
56+
if len(n.Elts) == 0 {
57+
return false
58+
}
4059
for _, arg := range n.Elts {
4160
if !TryResolve(arg, c) {
4261
return false
@@ -61,22 +80,18 @@ func TryResolve(n ast.Node, c *Context) bool {
6180
switch node := n.(type) {
6281
case *ast.BasicLit:
6382
return true
64-
6583
case *ast.CompositeLit:
6684
return resolveCompLit(node, c)
67-
6885
case *ast.Ident:
6986
return resolveIdent(node, c)
70-
87+
case *ast.ValueSpec:
88+
return resolveValueSpec(node, c)
7189
case *ast.AssignStmt:
7290
return resolveAssign(node, c)
73-
7491
case *ast.CallExpr:
7592
return resolveCallExpr(node, c)
76-
7793
case *ast.BinaryExpr:
7894
return resolveBinExpr(node, c)
7995
}
80-
8196
return false
8297
}

resolve_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,26 @@ var _ = Describe("Resolve ast node to concrete value", func() {
9191
Expect(target).ShouldNot(BeNil())
9292
Expect(gosec.TryResolve(target, ctx)).Should(BeTrue())
9393
})
94-
95-
// TODO: It should resolve call expressions
94+
It("should successfully resolve value spec", func() {
95+
var value *ast.ValueSpec
96+
pkg := testutils.NewTestPackage()
97+
defer pkg.Close()
98+
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ var y string = x; println(y) }`)
99+
ctx := pkg.CreateContext("foo.go")
100+
v := testutils.NewMockVisitor()
101+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
102+
if node, ok := n.(*ast.ValueSpec); ok {
103+
if len(node.Names) == 1 && node.Names[0].Name == "y" {
104+
value = node
105+
}
106+
}
107+
return true
108+
}
109+
v.Context = ctx
110+
ast.Walk(v, ctx.Root)
111+
Expect(value).ShouldNot(BeNil())
112+
Expect(gosec.TryResolve(value, ctx)).Should(BeTrue())
113+
})
96114

97115
})
98116

0 commit comments

Comments
 (0)