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): match escaped whitespace in the Go db.Query concatenation pattern (#211)

Pranav Arya55b50724cae2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9308ce1..721a61c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- Code: `code.sql-injection` now reports a Go query concatenated inside `db.Query`, `QueryRow` or
+ `Exec` with an escaped newline, tab or carriage return before the next keyword
+ (`db.Query("SELECT id\nFROM users WHERE id = " + id)`), as the `fmt.Sprintf` form already did.
- Code: `code.sql-injection` now reports a Ruby query with an escaped newline, tab or carriage
return before the next keyword (`"SELECT id\nFROM users WHERE id = #{id}"`).
- Code: `code.sql-injection` now reports a PHP query with an escaped newline, tab or carriage
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 20afc19..54bb27e 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -316,7 +316,7 @@ pub static RULES: &[Rule] = &[
go,
concat!(
r#"\.(?:Query|QueryRow|Exec)(?:\(|Context\(\s*[\w.]+(?:\(\))?\s*,)\s*(?:"\s*"#,
- kw!(),
+ kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"]*"|`\s*"#,
kw!(),
r#"[^`]*`)\s*\+\s*[A-Za-z_]"#
@@ -686,6 +686,39 @@ mod tests {
}
}
+ #[test]
+ fn go_query_concat_sql_injection_matches_escaped_whitespace() {
+ let f = FileInfo::new("handler.go");
+ let sqli = |s| {
+ check(&f, s, false)
+ .iter()
+ .any(|x| x.rule == "code.sql-injection")
+ };
+ for s in [
+ r#"rows, _ := db.Query("SELECT id\nFROM users WHERE id = " + id)"#,
+ r#"rows, _ := db.Query("SELECT id\tFROM users WHERE id = " + id)"#,
+ r#"rows, _ := db.Query("SELECT id\r\nFROM users WHERE id = " + id)"#,
+ r#"row := db.QueryRow("SELECT name\nFROM users WHERE id = " + id)"#,
+ r#"_, err := db.Exec("UPDATE users\nSET role = " + role)"#,
+ r#"_, err := db.Exec("DELETE\nFROM sessions WHERE id = " + id)"#,
+ r#"row := db.QueryRowContext(ctx, "SELECT name\nFROM users WHERE id = " + id)"#,
+ // A raw string has no escapes; a real line break still counts.
+ "rows, _ := db.Query(`SELECT id\nFROM users WHERE id = ` + id)",
+ ] {
+ assert!(sqli(s), "expected code.sql-injection for: {s}");
+ }
+ for s in [
+ r#"rows, _ := db.Query("selected\n" + n)"#,
+ r#"rows, _ := db.Query("selected\n" + n + " files from " + dir)"#,
+ r#"_, err := db.Exec("updated\n" + n + " rows")"#,
+ r#"rows, _ := db.Query("SELECT id\nFROM users WHERE id = ?", id)"#,
+ // In a raw string `\n` is a backslash and an `n`, not whitespace.
+ r#"rows, _ := db.Query(`SELECT id\nFROM users WHERE id = ` + id)"#,
+ ] {
+ assert!(!sqli(s), "unexpected code.sql-injection for: {s}");
+ }
+ }
+
#[test]
fn js_sql_injection_skips_tagged_templates() {
let sqli = |name: &str, s: &str| {
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 4facdde..2241361 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -18,6 +18,7 @@ api/handler.go:11 code.sql-injection
api/handler.go:13 code.sql-injection
api/handler.go:14 code.sql-injection
api/handler.go:19 code.sql-injection
+api/handler.go:20 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 1fc89b8..855fe93 100644
--- a/tests/fixtures/code/api/handler.go
+++ b/tests/fixtures/code/api/handler.go
@@ -17,3 +17,5 @@ msg2 := fmt.Sprintf("selection is larger than %d bytes", n)
msg3 := fmt.Sprintf("Deletes %s", name)
msg4 := fmt.Sprintf("update check returned %d", code)
q4 := fmt.Sprintf("SELECT id\nFROM users\nWHERE email = '%s'", email)
+rows2, err := db.Query("SELECT id\nFROM users WHERE id = " + id)
+rows3, err := db.Query("selected\n" + n)