Skip to content

Commit 9d66b0d

Browse files
authored
Fix false negatives for SQL injection in multi-line queries
1 parent 4c1afaa commit 9d66b0d

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

cmd/gosec/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func main() {
364364
if err != nil {
365365
logger.Fatal(err)
366366
}
367-
// get a bug
367+
368368
ruleList := loadRules(includeRules, excludeRules)
369369
if len(ruleList.Rules) == 0 {
370370
logger.Fatal("No rules are configured")

rules/sql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func NewSQLStrFormat(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
282282
noIssueQuoted: gosec.NewCallList(),
283283
sqlStatement: sqlStatement{
284284
patterns: []*regexp.Regexp{
285-
regexp.MustCompile("(?i)(SELECT|DELETE|INSERT|UPDATE|INTO|FROM|WHERE) "),
285+
regexp.MustCompile("(?i)(SELECT|DELETE|INSERT|UPDATE|INTO|FROM|WHERE)( |\n|\r|\t)"),
286286
regexp.MustCompile("%[^bdoxXfFp]"),
287287
},
288288
MetaData: gosec.MetaData{

testutils/source.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,28 @@ import (
11681168
11691169
func main(){
11701170
fmt.Sprintln()
1171-
}`}, 0, gosec.NewConfig()},
1171+
}`}, 0, gosec.NewConfig()}, {[]string{`
1172+
// Format string with \n\r
1173+
package main
1174+
1175+
import (
1176+
"database/sql"
1177+
"fmt"
1178+
"os"
1179+
)
1180+
1181+
func main(){
1182+
db, err := sql.Open("sqlite3", ":memory:")
1183+
if err != nil {
1184+
panic(err)
1185+
}
1186+
q := fmt.Sprintf("SELECT * FROM foo where\n name = '%s'", os.Args[1])
1187+
rows, err := db.Query(q)
1188+
if err != nil {
1189+
panic(err)
1190+
}
1191+
defer rows.Close()
1192+
}`}, 1, gosec.NewConfig()},
11721193
}
11731194

11741195
// SampleCodeG202 - SQL query string building via string concatenation

0 commit comments

Comments
 (0)