Skip to content

Commit

Permalink
Fix false negatives for SQL injection in multi-line queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiili committed Jan 5, 2022
1 parent 4c1afaa commit 9d66b0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/gosec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func main() {
if err != nil {
logger.Fatal(err)
}
// get a bug

ruleList := loadRules(includeRules, excludeRules)
if len(ruleList.Rules) == 0 {
logger.Fatal("No rules are configured")
Expand Down
2 changes: 1 addition & 1 deletion rules/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func NewSQLStrFormat(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
noIssueQuoted: gosec.NewCallList(),
sqlStatement: sqlStatement{
patterns: []*regexp.Regexp{
regexp.MustCompile("(?i)(SELECT|DELETE|INSERT|UPDATE|INTO|FROM|WHERE) "),
regexp.MustCompile("(?i)(SELECT|DELETE|INSERT|UPDATE|INTO|FROM|WHERE)( |\n|\r|\t)"),
regexp.MustCompile("%[^bdoxXfFp]"),
},
MetaData: gosec.MetaData{
Expand Down
23 changes: 22 additions & 1 deletion testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,28 @@ import (
func main(){
fmt.Sprintln()
}`}, 0, gosec.NewConfig()},
}`}, 0, gosec.NewConfig()}, {[]string{`
// Format string with \n\r
package main
import (
"database/sql"
"fmt"
"os"
)
func main(){
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
q := fmt.Sprintf("SELECT * FROM foo where\n name = '%s'", os.Args[1])
rows, err := db.Query(q)
if err != nil {
panic(err)
}
defer rows.Close()
}`}, 1, gosec.NewConfig()},
}

// SampleCodeG202 - SQL query string building via string concatenation
Expand Down

0 comments on commit 9d66b0d

Please sign in to comment.