@@ -13,7 +13,6 @@ var _ = Describe("Resolve ast node to concrete value", func() {
1313 Context ("when attempting to resolve an ast node" , func () {
1414 It ("should successfully resolve basic literal" , func () {
1515 var basicLiteral * ast.BasicLit
16-
1716 pkg := testutils .NewTestPackage ()
1817 defer pkg .Close ()
1918 pkg .AddFile ("foo.go" , `package main; const foo = "bar"; func main(){}` )
@@ -52,6 +51,47 @@ var _ = Describe("Resolve ast node to concrete value", func() {
5251 Expect (gosec .TryResolve (ident , ctx )).Should (BeTrue ())
5352 })
5453
54+ It ("should successfully resolve variable identifier" , func () {
55+ var ident * ast.Ident
56+ pkg := testutils .NewTestPackage ()
57+ defer pkg .Close ()
58+ pkg .AddFile ("foo.go" , `package main; import "fmt"; func main(){ x := "test"; y := x; fmt.Println(y) }` )
59+ ctx := pkg .CreateContext ("foo.go" )
60+ v := testutils .NewMockVisitor ()
61+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
62+ if node , ok := n .(* ast.Ident ); ok && node .Name == "y" {
63+ ident = node
64+ return false
65+ }
66+ return true
67+ }
68+ v .Context = ctx
69+ ast .Walk (v , ctx .Root )
70+ Expect (ident ).ShouldNot (BeNil ())
71+ Expect (gosec .TryResolve (ident , ctx )).Should (BeTrue ())
72+ })
73+
74+ It ("should successfully not resolve variable identifier with no declaration" , func () {
75+ var ident * ast.Ident
76+ pkg := testutils .NewTestPackage ()
77+ defer pkg .Close ()
78+ pkg .AddFile ("foo.go" , `package main; import "fmt"; func main(){ x := "test"; y := x; fmt.Println(y) }` )
79+ ctx := pkg .CreateContext ("foo.go" )
80+ v := testutils .NewMockVisitor ()
81+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
82+ if node , ok := n .(* ast.Ident ); ok && node .Name == "y" {
83+ ident = node
84+ return false
85+ }
86+ return true
87+ }
88+ v .Context = ctx
89+ ast .Walk (v , ctx .Root )
90+ Expect (ident ).ShouldNot (BeNil ())
91+ ident .Obj .Decl = nil
92+ Expect (gosec .TryResolve (ident , ctx )).Should (BeFalse ())
93+ })
94+
5595 It ("should successfully resolve assign statement" , func () {
5696 var assign * ast.AssignStmt
5797 pkg := testutils .NewTestPackage ()
@@ -73,6 +113,50 @@ var _ = Describe("Resolve ast node to concrete value", func() {
73113 Expect (gosec .TryResolve (assign , ctx )).Should (BeTrue ())
74114 })
75115
116+ It ("should successfully not resolve assign statement without rhs" , func () {
117+ var assign * ast.AssignStmt
118+ pkg := testutils .NewTestPackage ()
119+ defer pkg .Close ()
120+ pkg .AddFile ("foo.go" , `package main; const x = "bar"; func main(){ y := x; println(y) }` )
121+ ctx := pkg .CreateContext ("foo.go" )
122+ v := testutils .NewMockVisitor ()
123+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
124+ if node , ok := n .(* ast.AssignStmt ); ok {
125+ if id , ok := node .Lhs [0 ].(* ast.Ident ); ok && id .Name == "y" {
126+ assign = node
127+ }
128+ }
129+ return true
130+ }
131+ v .Context = ctx
132+ ast .Walk (v , ctx .Root )
133+ Expect (assign ).ShouldNot (BeNil ())
134+ assign .Rhs = []ast.Expr {}
135+ Expect (gosec .TryResolve (assign , ctx )).Should (BeFalse ())
136+ })
137+
138+ It ("should successfully not resolve assign statement with unsolvable rhs" , func () {
139+ var assign * ast.AssignStmt
140+ pkg := testutils .NewTestPackage ()
141+ defer pkg .Close ()
142+ pkg .AddFile ("foo.go" , `package main; const x = "bar"; func main(){ y := x; println(y) }` )
143+ ctx := pkg .CreateContext ("foo.go" )
144+ v := testutils .NewMockVisitor ()
145+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
146+ if node , ok := n .(* ast.AssignStmt ); ok {
147+ if id , ok := node .Lhs [0 ].(* ast.Ident ); ok && id .Name == "y" {
148+ assign = node
149+ }
150+ }
151+ return true
152+ }
153+ v .Context = ctx
154+ ast .Walk (v , ctx .Root )
155+ Expect (assign ).ShouldNot (BeNil ())
156+ assign .Rhs = []ast.Expr {& ast.CallExpr {}}
157+ Expect (gosec .TryResolve (assign , ctx )).Should (BeFalse ())
158+ })
159+
76160 It ("should successfully resolve a binary statement" , func () {
77161 var target * ast.BinaryExpr
78162 pkg := testutils .NewTestPackage ()
@@ -112,6 +196,49 @@ var _ = Describe("Resolve ast node to concrete value", func() {
112196 Expect (value ).ShouldNot (BeNil ())
113197 Expect (gosec .TryResolve (value , ctx )).Should (BeTrue ())
114198 })
199+ It ("should successfully not resolve value spec without values" , func () {
200+ var value * ast.ValueSpec
201+ pkg := testutils .NewTestPackage ()
202+ defer pkg .Close ()
203+ pkg .AddFile ("foo.go" , `package main; const x = "bar"; func main(){ var y string = x; println(y) }` )
204+ ctx := pkg .CreateContext ("foo.go" )
205+ v := testutils .NewMockVisitor ()
206+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
207+ if node , ok := n .(* ast.ValueSpec ); ok {
208+ if len (node .Names ) == 1 && node .Names [0 ].Name == "y" {
209+ value = node
210+ }
211+ }
212+ return true
213+ }
214+ v .Context = ctx
215+ ast .Walk (v , ctx .Root )
216+ Expect (value ).ShouldNot (BeNil ())
217+ value .Values = []ast.Expr {}
218+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
219+ })
220+
221+ It ("should successfully not resolve value spec with unsolvable value" , func () {
222+ var value * ast.ValueSpec
223+ pkg := testutils .NewTestPackage ()
224+ defer pkg .Close ()
225+ pkg .AddFile ("foo.go" , `package main; const x = "bar"; func main(){ var y string = x; println(y) }` )
226+ ctx := pkg .CreateContext ("foo.go" )
227+ v := testutils .NewMockVisitor ()
228+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
229+ if node , ok := n .(* ast.ValueSpec ); ok {
230+ if len (node .Names ) == 1 && node .Names [0 ].Name == "y" {
231+ value = node
232+ }
233+ }
234+ return true
235+ }
236+ v .Context = ctx
237+ ast .Walk (v , ctx .Root )
238+ Expect (value ).ShouldNot (BeNil ())
239+ value .Values = []ast.Expr {& ast.CallExpr {}}
240+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
241+ })
115242
116243 It ("should successfully resolve composite literal" , func () {
117244 var value * ast.CompositeLit
@@ -131,5 +258,84 @@ var _ = Describe("Resolve ast node to concrete value", func() {
131258 Expect (value ).ShouldNot (BeNil ())
132259 Expect (gosec .TryResolve (value , ctx )).Should (BeTrue ())
133260 })
261+
262+ It ("should successfully not resolve composite literal without elst" , func () {
263+ var value * ast.CompositeLit
264+ pkg := testutils .NewTestPackage ()
265+ defer pkg .Close ()
266+ pkg .AddFile ("foo.go" , `package main; func main(){ y := []string{"value1", "value2"}; println(y) }` )
267+ ctx := pkg .CreateContext ("foo.go" )
268+ v := testutils .NewMockVisitor ()
269+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
270+ if node , ok := n .(* ast.CompositeLit ); ok {
271+ value = node
272+ }
273+ return true
274+ }
275+ v .Context = ctx
276+ ast .Walk (v , ctx .Root )
277+ Expect (value ).ShouldNot (BeNil ())
278+ value .Elts = []ast.Expr {}
279+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
280+ })
281+
282+ It ("should successfully not resolve composite literal with unsolvable elst" , func () {
283+ var value * ast.CompositeLit
284+ pkg := testutils .NewTestPackage ()
285+ defer pkg .Close ()
286+ pkg .AddFile ("foo.go" , `package main; func main(){ y := []string{"value1", "value2"}; println(y) }` )
287+ ctx := pkg .CreateContext ("foo.go" )
288+ v := testutils .NewMockVisitor ()
289+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
290+ if node , ok := n .(* ast.CompositeLit ); ok {
291+ value = node
292+ }
293+ return true
294+ }
295+ v .Context = ctx
296+ ast .Walk (v , ctx .Root )
297+ Expect (value ).ShouldNot (BeNil ())
298+ value .Elts = []ast.Expr {& ast.CallExpr {}}
299+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
300+ })
301+
302+ It ("should successfully not resolve call expressions" , func () {
303+ var value * ast.CallExpr
304+ pkg := testutils .NewTestPackage ()
305+ defer pkg .Close ()
306+ pkg .AddFile ("foo.go" , `package main; func main(){ y := []string{"value1", "value2"}; println(y) }` )
307+ ctx := pkg .CreateContext ("foo.go" )
308+ v := testutils .NewMockVisitor ()
309+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
310+ if node , ok := n .(* ast.CallExpr ); ok {
311+ value = node
312+ }
313+ return true
314+ }
315+ v .Context = ctx
316+ ast .Walk (v , ctx .Root )
317+ Expect (value ).ShouldNot (BeNil ())
318+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
319+ })
320+
321+ It ("should successfully not resolve call expressions" , func () {
322+ var value * ast.ImportSpec
323+ pkg := testutils .NewTestPackage ()
324+ defer pkg .Close ()
325+ pkg .AddFile ("foo.go" , `package main; import "fmt"; func main(){ y := []string{"value1", "value2"}; fmt.Println(y) }` )
326+ ctx := pkg .CreateContext ("foo.go" )
327+ v := testutils .NewMockVisitor ()
328+ v .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
329+ if node , ok := n .(* ast.ImportSpec ); ok {
330+ value = node
331+ }
332+ return true
333+ }
334+ v .Context = ctx
335+ ast .Walk (v , ctx .Root )
336+ Expect (value ).ShouldNot (BeNil ())
337+ Expect (gosec .TryResolve (value , ctx )).Should (BeFalse ())
338+ })
339+
134340 })
135341})
0 commit comments