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): require SQL clause keywords in Go fmt.Sprintf sql-injection rule (#173)

Brixton Pham8e2bfca999db
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5578b15..20d8434 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,13 @@
## Unreleased
+- Code: the Go `fmt.Sprintf` pattern in `code.sql-injection` now uses the same SQL clause keyword
+ check (`SELECT ... FROM`, `INSERT INTO`, `UPDATE ... SET`, `DELETE FROM`) as the other SQL
+ injection rules and also matches raw backtick query strings and escaped newlines or tabs
+ (`"SELECT id\nFROM users WHERE id = %s"`), so ordinary messages starting with words like
+ `"updated: %d nodes"`, `"selection is larger than %d bytes"`, `"Deletes %s"` or
+ `"update check returned %d"` are no longer reported. `UPDATE` in every SQL injection rule now
+ also matches schema-qualified tables and aliases (`UPDATE public.users u SET`).
- Secrets: new rule `secret.heroku-key` (critical) for Heroku API keys (`HRKU-AA` followed by 58
letters, digits, `_` or `-`).
- Secrets: new rule `secret.notion-token` (critical) for Notion integration tokens (`ntn_`
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 0eb7cd8..608dff3 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -42,7 +42,68 @@ fn js_py(f: &FileInfo) -> bool {
macro_rules! kw {
() => {
- r"(?i:select\s.+?\sfrom\b|insert\s+into\b|update\s+\w+\s+set\b|delete\s+from\b)"
+ kw!(".", r"\s")
+ };
+ ($ch:literal, $ws:literal) => {
+ concat!(
+ r"(?i:select",
+ $ws,
+ $ch,
+ "+?",
+ $ws,
+ r"from\b|insert",
+ $ws,
+ r"+into\b|update",
+ $ws,
+ r"+\w+(?:\.\w+)*(?:(?:",
+ $ws,
+ r"+as)?",
+ $ws,
+ r"+\w+)?",
+ $ws,
+ r"+set\b|delete",
+ $ws,
+ r"+from\b)"
+ )
+ };
+}
+
+macro_rules! go_verb {
+ () => {
+ r"%[+#\- 0]*(?:\d+|\*)?(?:\.(?:\d+|\*))?[sdvq]"
+ };
+}
+
+macro_rules! go_sqli_body {
+ ($ch:literal, $ws:literal) => {
+ concat!(
+ "(?:",
+ kw!($ch, $ws),
+ $ch,
+ "*",
+ go_verb!(),
+ r"|(?i:select)",
+ $ws,
+ $ch,
+ "*",
+ go_verb!(),
+ $ch,
+ "*?",
+ $ws,
+ r"(?i:from\b)|(?i:update)",
+ $ws,
+ r"+(?:\w+\.)*",
+ go_verb!(),
+ r"(?:\.(?:\w+|",
+ go_verb!(),
+ r"))*(?:(?:",
+ $ws,
+ r"+(?i:as))?",
+ $ws,
+ r"+\w+)?",
+ $ws,
+ r"+(?i:set\b))"
+ )
};
}
@@ -235,7 +296,13 @@ pub static RULES: &[Rule] = &[
"code.sql-injection",
High,
go,
- r#"fmt\.Sprintf\(\s*"(?i:select|insert|update|delete)[^"]*%[sdvq]"#,
+ concat!(
+ r#"fmt\.Sprintf\(\s*(?:"\s*"#,
+ go_sqli_body!(r#"(?:[^"\\]|\\.)"#, r"(?:\s|\\[nrt])"),
+ r"|`\s*",
+ go_sqli_body!("[^`]", r"\s"),
+ ")"
+ ),
None,
SQLI_MSG,
SQLI_FIX
@@ -560,3 +627,55 @@ pub static RULES: &[Rule] = &[
"Create users with passwords passed in at deploy time, not stored in migrations."
),
];
+
+#[cfg(test)]
+mod tests {
+ use crate::rules::{FileInfo, check};
+
+ #[test]
+ fn go_sprintf_sql_injection_requires_sql_clause_keywords() {
+ 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#"q := fmt.Sprintf("SELECT * FROM users WHERE id = %s", id)"#,
+ r#"q := fmt.Sprintf(`SELECT * FROM users WHERE name = '%s'`, name)"#,
+ r#"q := fmt.Sprintf("INSERT INTO logs (msg) VALUES (%q)", msg)"#,
+ r#"q := fmt.Sprintf("UPDATE users SET role = '%s' WHERE id = %d", role, id)"#,
+ r#"q := fmt.Sprintf("UPDATE public.users SET name = '%s'", name)"#,
+ r#"q := fmt.Sprintf("UPDATE users AS u SET name = '%s'", name)"#,
+ r#"q := fmt.Sprintf("UPDATE users u SET name = '%s'", name)"#,
+ r#"q := fmt.Sprintf("UPDATE %s SET active = true", table)"#,
+ r#"q := fmt.Sprintf("UPDATE %s.%s SET active = true", schema, table)"#,
+ r#"q := fmt.Sprintf("UPDATE %s AS u SET active = true", table)"#,
+ r#"q := fmt.Sprintf(`UPDATE %s SET active = true`, table)"#,
+ r#"q := fmt.Sprintf("DELETE FROM sessions WHERE token = %v", tok)"#,
+ r#"q := fmt.Sprintf("SELECT * FROM \"users\" WHERE id = %s", id)"#,
+ r#"q := fmt.Sprintf("SELECT 'active', id FROM users WHERE id = %s", id)"#,
+ r#"q := fmt.Sprintf("SELECT '%s' FROM users", name)"#,
+ r#"q := fmt.Sprintf(`SELECT %s FROM users`, col)"#,
+ r#"q := fmt.Sprintf(`SELECT "id" FROM users WHERE name = '%s'`, name)"#,
+ r#"q := fmt.Sprintf("SELECT id\nFROM users\nWHERE id = %s", id)"#,
+ r#"q := fmt.Sprintf("UPDATE users\n\tSET role = '%s'\nWHERE id = %d", role, id)"#,
+ r#"q := fmt.Sprintf("DELETE\nFROM sessions WHERE token = '%s'", tok)"#,
+ r#"q := fmt.Sprintf("SELECT name FROM users WHERE email = '%s' AND active = 1", email)"#,
+ ] {
+ assert!(sqli(s), "expected code.sql-injection for: {s}");
+ }
+ for s in [
+ r#"msg := fmt.Sprintf("updated: %d nodes", n)"#,
+ r#"msg := fmt.Sprintf("selection is larger than %d bytes", n)"#,
+ r#"msg := fmt.Sprintf("Deletes %s", name)"#,
+ r#"msg := fmt.Sprintf("update check returned %d", code)"#,
+ r#"msg := fmt.Sprintf("update Helm release Secret %q", s)"#,
+ r#"msg := fmt.Sprintf("selected\n%d files from %s", n, dir)"#,
+ r#"msg := fmt.Sprintf("inserted %d rows\ninto %s", n, table)"#,
+ r#"q := fmt.Sprintf("SELECT * FROM users WHERE id = 1") + fmt.Sprintf(" -- %s", note)"#,
+ ] {
+ assert!(!sqli(s), "unexpected code.sql-injection for: {s}");
+ }
+ }
+}
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 335be60..ecf924c 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -12,6 +12,9 @@ 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:13 code.sql-injection
+api/handler.go:14 code.sql-injection
+api/handler.go:19 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 49f811e..1fc89b8 100644
--- a/tests/fixtures/code/api/handler.go
+++ b/tests/fixtures/code/api/handler.go
@@ -10,3 +10,10 @@ 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)
+q2 := fmt.Sprintf(`DELETE FROM users WHERE name = '%s'`, name)
+q3 := fmt.Sprintf("UPDATE users AS u SET name = '%s'", name)
+msg1 := fmt.Sprintf("updated: %d nodes", n)
+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)