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 Ruby sql-injection pattern (#210)

Pranav Aryab2cfd2c5d177
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55efdea..9308ce1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- 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
return before the next keyword (`"SELECT id\nFROM users WHERE id = $id"`), in the interpolated
and the concatenated form.
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 4d9a4b5..20afc19 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -359,7 +359,7 @@ pub static RULES: &[Rule] = &[
rb,
concat!(
r#"\.(?:where|find_by_sql|order|group|having|joins|select|pluck|exists\?)\(\s*"[^"]*#\{|"\s*"#,
- kw!(),
+ kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"]*#\{"#
),
None,
@@ -868,4 +868,32 @@ mod tests {
assert!(!sqli(s), "unexpected code.sql-injection for: {s}");
}
}
+
+ #[test]
+ fn ruby_sql_injection_matches_escaped_whitespace() {
+ let f = FileInfo::new("db.rb");
+ let sqli = |s| {
+ check(&f, s, false)
+ .iter()
+ .any(|x| x.rule == "code.sql-injection")
+ };
+ for s in [
+ r##"q = "SELECT id\nFROM users WHERE id = #{id}""##,
+ r##"q = "SELECT id\tFROM users WHERE id = #{id}""##,
+ r##"q = "SELECT id\r\nFROM users WHERE id = #{id}""##,
+ r##"q = "UPDATE users\nSET role = '#{role}'""##,
+ r##"q = "DELETE\nFROM sessions WHERE id = #{id}""##,
+ r##"q = "INSERT\nINTO logs VALUES ('#{msg}')""##,
+ ] {
+ assert!(sqli(s), "expected code.sql-injection for: {s}");
+ }
+ for s in [
+ r##"msg = "selected\n#{n} files""##,
+ r##"msg = "selected\n#{n} files from #{dir}""##,
+ r##"msg = "updated\n#{n} rows""##,
+ r##"q = "SELECT id\nFROM users WHERE id = ?""##,
+ ] {
+ assert!(!sqli(s), "unexpected code.sql-injection for: {s}");
+ }
+ }
}
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 6a36289..4facdde 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -31,6 +31,7 @@ api/index.php:7 code.eval
api/index.php:8 code.sql-injection
api/index.php:9 code.command-injection
api/users_controller.rb:1 code.sql-injection
+api/users_controller.rb:11 code.sql-injection
api/users_controller.rb:3 code.command-injection
api/users_controller.rb:4 code.unsafe-deserialization
api/users_controller.rb:5 code.xss-raw-html
diff --git a/tests/fixtures/code/api/users_controller.rb b/tests/fixtures/code/api/users_controller.rb
index b576805..bf7ac86 100644
--- a/tests/fixtures/code/api/users_controller.rb
+++ b/tests/fixtures/code/api/users_controller.rb
@@ -8,3 +8,5 @@ obj = Marshal.restore(data)
obj = YAML.safe_load(data)
a = YAML.unsafe_load(data); b = YAML.safe_load(other)
a = YAML.load(data, permitted_classes: [Date])
+q = "SELECT id\nFROM users WHERE id = #{id}"
+msg = "selected\n#{n} files from #{dir}"