Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40ac530

Browse files
Eshani Parulekarccojocar
authored andcommitted
rules(G202): detect SQL concat in ValueSpec declarations; add test sample\n\n- Handle var query string = 'SELECT ...' + user style declarations\n- Reuse existing binary expr detection on ValueSpec.Values\n- Add postgres sample mirroring issue #1309 report\n- Rules tests: 42 passed
1 parent 4be6b11 commit 40ac530

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

rules/sql.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ func (s *sqlStrConcat) checkQuery(call *ast.CallExpr, ctx *gosec.Context) (*issu
191191
if injection := s.findInjectionInBranch(ctx, decl.Rhs); injection != nil {
192192
return ctx.NewIssue(injection, s.ID(), s.What, s.Severity, s.Confidence), nil
193193
}
194+
case *ast.ValueSpec:
195+
// handle: var query string = "SELECT ...'" + user
196+
if injection := s.findInjectionInBranch(ctx, decl.Values); injection != nil {
197+
return ctx.NewIssue(injection, s.ID(), s.What, s.Severity, s.Confidence), nil
198+
}
194199
}
195200
}
196201

testutils/g202_samples.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,32 @@ func main() {
308308
fmt.Println(result)
309309
}
310310
`}, 0, gosec.NewConfig()},
311+
{[]string{`
312+
package main
313+
314+
import (
315+
"database/sql"
316+
"fmt"
317+
_ "github.com/lib/pq"
318+
)
319+
320+
func main() {
321+
db, err := sql.Open("postgres", "user=postgres password=password dbname=mydb sslmode=disable")
322+
if err!= nil {
323+
panic(err)
324+
}
325+
defer db.Close()
326+
327+
var username string
328+
fmt.Println("请输入用户名:")
329+
fmt.Scanln(&username)
330+
331+
var query string = "SELECT * FROM users WHERE username = '" + username + "'"
332+
rows, err := db.Query(query)
333+
if err!= nil {
334+
panic(err)
335+
}
336+
defer rows.Close()
337+
}
338+
`}, 1, gosec.NewConfig()},
311339
}

0 commit comments

Comments
 (0)