Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rules/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type sqlStatement struct {
}

var sqlCallIdents = map[string]map[string]int{
"*database/sql.Conn": {
"ExecContext": 1,
"QueryContext": 1,
"QueryRowContext": 1,
"PrepareContext": 1,
},
"*database/sql.DB": {
"Exec": 0,
"ExecContext": 1,
Expand Down
30 changes: 30 additions & 0 deletions testutils/g201_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ func main(){
panic(err)
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
// Format string without proper quoting with connection
package main
import (
"context"
"database/sql"
"fmt"
"os"
)

func main(){
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
conn, err := db.Conn(context.Background())
if err != nil {
panic(err)
}
q := fmt.Sprintf("select * from foo where name = '%s'", os.Args[1])
rows, err := conn.QueryContext(context.Background(), q)
if err != nil {
panic(err)
}
defer rows.Close()
if err := conn.Close(); err != nil {
panic(err)
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
// Format string false positive, safe string spec.
Expand Down
29 changes: 29 additions & 0 deletions testutils/g202_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ func main(){
panic(err)
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
// DB connection check
package main

import (
"context"
"database/sql"
"os"
)

func main(){
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
conn, err := db.Conn(context.Background())
if err != nil {
panic(err)
}
rows, err := conn.QueryContext(context.Background(), "select * from foo where name = " + os.Args[1])
if err != nil {
panic(err)
}
defer rows.Close()
if err := conn.Close(); err != nil {
panic(err)
}
}
`}, 1, gosec.NewConfig()},
{[]string{`
// multiple string concatenation
Expand Down
Loading