Skip to content

Commit 017d1d6

Browse files
imirkinccojocar
authored andcommitted
G201/G202: add checks for injection into sql.Conn methods
We check sql.DB and sql.Tx, but sql.Conn appears to have been missed. It carries the same issues as DB/Tx in terms of injection.
1 parent 67f63d4 commit 017d1d6

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

rules/sql.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type sqlStatement struct {
3232
}
3333

3434
var sqlCallIdents = map[string]map[string]int{
35+
"*database/sql.Conn": {
36+
"ExecContext": 1,
37+
"QueryContext": 1,
38+
"QueryRowContext": 1,
39+
"PrepareContext": 1,
40+
},
3541
"*database/sql.DB": {
3642
"Exec": 0,
3743
"ExecContext": 1,

testutils/g201_samples.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ func main(){
103103
panic(err)
104104
}
105105
}
106+
`}, 1, gosec.NewConfig()},
107+
{[]string{`
108+
// Format string without proper quoting with connection
109+
package main
110+
import (
111+
"context"
112+
"database/sql"
113+
"fmt"
114+
"os"
115+
)
116+
117+
func main(){
118+
db, err := sql.Open("sqlite3", ":memory:")
119+
if err != nil {
120+
panic(err)
121+
}
122+
conn, err := db.Conn(context.Background())
123+
if err != nil {
124+
panic(err)
125+
}
126+
q := fmt.Sprintf("select * from foo where name = '%s'", os.Args[1])
127+
rows, err := conn.QueryContext(context.Background(), q)
128+
if err != nil {
129+
panic(err)
130+
}
131+
defer rows.Close()
132+
if err := conn.Close(); err != nil {
133+
panic(err)
134+
}
135+
}
106136
`}, 1, gosec.NewConfig()},
107137
{[]string{`
108138
// Format string false positive, safe string spec.

testutils/g202_samples.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ func main(){
119119
panic(err)
120120
}
121121
}
122+
`}, 1, gosec.NewConfig()},
123+
{[]string{`
124+
// DB connection check
125+
package main
126+
127+
import (
128+
"context"
129+
"database/sql"
130+
"os"
131+
)
132+
133+
func main(){
134+
db, err := sql.Open("sqlite3", ":memory:")
135+
if err != nil {
136+
panic(err)
137+
}
138+
conn, err := db.Conn(context.Background())
139+
if err != nil {
140+
panic(err)
141+
}
142+
rows, err := conn.QueryContext(context.Background(), "select * from foo where name = " + os.Args[1])
143+
if err != nil {
144+
panic(err)
145+
}
146+
defer rows.Close()
147+
if err := conn.Close(); err != nil {
148+
panic(err)
149+
}
150+
}
122151
`}, 1, gosec.NewConfig()},
123152
{[]string{`
124153
// multiple string concatenation

0 commit comments

Comments
 (0)