Skip to content

Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.

fix(code): catch Go SQL queries built by concatenation (#92)

Onur Ugur24bf1f9cc365
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4054eed..5bf3383 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,8 @@
- Secrets: new rule `secret.mailgun-key` (critical) for Mailgun private API keys (`key-` followed by 32 lowercase hex characters).
- Secrets: new rule `secret.telegram-bot-token` (high) for Telegram bot tokens (8 to 10 digits, a colon, then 35 letters, digits, `_` or `-`).
- Code: new rule `code.open-redirect` (medium) for redirects built straight from request input (`res.redirect(req.query.next)`, `redirect(request.args.get("next"))`); a redirect to a fixed path stays unflagged.
+- Code: `code.sql-injection` also catches Go queries built by concatenation in `db.Query`, `db.QueryRow`, `db.Exec`
+ and their `Context` variants (`db.Query("SELECT * FROM t WHERE id=" + id)`). A query with a placeholder stays unflagged.
- Code: `code.ssrf` also covers Go (`http.Get`, `http.Head`, `http.Post` with `r.FormValue(...)` or `r.URL.Query().Get(...)`)
and PHP (`file_get_contents`, `curl_init`, `fopen` with `$_GET`, `$_POST` or `$_REQUEST`). A fixed URL stays unflagged.
- Code: `code.open-redirect` now also matches a status code first, so `res.redirect(301, req.query.next)` is reported; a redirect to a fixed path stays unflagged.
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 62a2b1e..0eb7cd8 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -240,6 +240,21 @@ pub static RULES: &[Rule] = &[
SQLI_MSG,
SQLI_FIX
),
+ rule!(
+ "code.sql-injection",
+ High,
+ go,
+ concat!(
+ r#"\.(?:Query|QueryRow|Exec)(?:\(|Context\(\s*[\w.]+(?:\(\))?\s*,)\s*(?:"\s*"#,
+ kw!(),
+ r#"[^"]*"|`\s*"#,
+ kw!(),
+ r#"[^`]*`)\s*\+\s*[A-Za-z_]"#
+ ),
+ None,
+ SQLI_MSG,
+ SQLI_FIX
+ ),
rule!(
"code.sql-injection",
High,
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index ba2b5b1..aa5f3c7 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -10,6 +10,8 @@ api/handler.go:1 code.command-injection
api/handler.go:3 code.sql-injection
api/handler.go:4 code.tls-verify-off
api/handler.go:5 code.weak-hash
+api/handler.go:9 code.sql-injection
+api/handler.go:11 code.sql-injection
api/handler.go:6 code.ssrf
api/index.php:1 code.sql-injection
api/index.php:11 code.sql-injection
diff --git a/tests/fixtures/code/api/handler.go b/tests/fixtures/code/api/handler.go
index e977170..49f811e 100644
--- a/tests/fixtures/code/api/handler.go
+++ b/tests/fixtures/code/api/handler.go
@@ -6,3 +6,7 @@ h := md5.New()
resp, err := http.Get(r.FormValue("u"))
resp, err := http.Get("https://api.example.com")
resp, err := http.Get(url)
+rows, err := db.Query("SELECT * FROM t WHERE id=" + r.URL.Query().Get("id"))
+rows, err := db.Query("SELECT * FROM t WHERE id=?", id)
+row := db.QueryRowContext(r.Context(), "SELECT name FROM users WHERE id = " + id)
+_, err = tx.ExecContext(ctx, "DELETE FROM sessions WHERE user = $1", user)