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.

feat(code): report Kotlin string templates in sql-injection (#212)

Pranav Arya50ffcd5d9780
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 721a61c..435342b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- Code: `code.sql-injection` now reports Kotlin string templates in a query
+ (`"SELECT id FROM users WHERE id = $id"`, `${id}`), in `.kt` and `.kts` files only. A positional
+ placeholder (`$1`) and an escaped `\$` are not templates and stay quiet.
- 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.
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 54bb27e..580273f 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -15,6 +15,9 @@ fn go(f: &FileInfo) -> bool {
fn jvm(f: &FileInfo) -> bool {
f.ext_in(&["java", "kt", "scala", "groovy"])
}
+fn kotlin(f: &FileInfo) -> bool {
+ f.ext_in(&["kt", "kts"])
+}
fn php(f: &FileInfo) -> bool {
f.ext_in(&["php", "phtml"])
}
@@ -338,6 +341,21 @@ pub static RULES: &[Rule] = &[
SQLI_MSG,
SQLI_FIX
),
+ // Kotlin string templates (`$id`, `${id}`). Kotlin files only: in Java a
+ // `$` inside a string is just a character. An escaped `\$` is skipped.
+ rule!(
+ "code.sql-injection",
+ High,
+ kotlin,
+ concat!(
+ r#""\s*"#,
+ kw!(".", r"(?:\s|\\[nrt])"),
+ r#"(?:[^"\\]|\\.)*?\$\{?[A-Za-z_]"#
+ ),
+ None,
+ SQLI_MSG,
+ SQLI_FIX
+ ),
rule!(
"code.sql-injection",
High,
@@ -818,6 +836,56 @@ mod tests {
}
}
+ #[test]
+ fn kotlin_sql_injection_matches_string_templates() {
+ let sqli = |name: &str, s: &str| {
+ check(&FileInfo::new(name), s, false)
+ .iter()
+ .any(|x| x.rule == "code.sql-injection")
+ };
+ let templates = [
+ r#"val q = "SELECT id FROM users WHERE id = $id""#,
+ r#"val q = "SELECT id FROM users WHERE id = ${id}""#,
+ r#"val q = "SELECT id FROM users WHERE name = '${user.name}'""#,
+ r#"val q = "SELECT id\nFROM users WHERE id = $id""#,
+ r#"val q = "UPDATE users SET role = '$role' WHERE id = 1""#,
+ r#"val q = "DELETE FROM sessions WHERE id = $id""#,
+ r#"val q = "INSERT INTO logs VALUES ('$msg')""#,
+ // A positional placeholder first, then a template.
+ r#"val q = "SELECT id FROM users WHERE a = $1 AND b = $name""#,
+ // A raw string.
+ r#"val q = """SELECT id FROM users WHERE id = $id""""#,
+ ];
+ for name in ["Repo.kt", "build.gradle.kts"] {
+ for s in templates {
+ assert!(sqli(name, s), "expected code.sql-injection in {name}: {s}");
+ }
+ for s in [
+ r#"val msg = "Selected $n users from the list""#,
+ r#"val msg = "updated $n rows""#,
+ r#"val q = "SELECT id FROM users WHERE id = ?""#,
+ r#"val q = "SELECT id FROM users WHERE id = $1""#,
+ r#"val q = "SELECT id FROM prices WHERE currency = '\$'""#,
+ r#"val q = "SELECT id FROM users WHERE name = '\$name'""#,
+ r#"val q = "SELECT id FROM users"; val label = "$name""#,
+ ] {
+ assert!(
+ !sqli(name, s),
+ "unexpected code.sql-injection in {name}: {s}"
+ );
+ }
+ }
+ // In Java, Scala and Groovy sources the rule does not run.
+ for name in ["Repo.java", "Repo.scala", "Repo.groovy"] {
+ for s in templates {
+ assert!(
+ !sqli(name, s),
+ "unexpected code.sql-injection in {name}: {s}"
+ );
+ }
+ }
+ }
+
#[test]
fn jvm_and_csharp_sql_injection_match_escaped_whitespace() {
let sqli = |name: &str, s: &str| {
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 2241361..84fbef8 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -1,3 +1,5 @@
+api/Repo.kt:1 code.sql-injection
+api/Repo.kt:2 code.sql-injection
api/UserRepo.java:1 code.sql-injection
api/UserRepo.java:3 code.command-injection
api/UserRepo.java:4 code.unsafe-deserialization
diff --git a/tests/fixtures/code/api/Repo.kt b/tests/fixtures/code/api/Repo.kt
new file mode 100644
index 0000000..d53ac24
--- /dev/null
+++ b/tests/fixtures/code/api/Repo.kt
@@ -0,0 +1,4 @@
+val a = "SELECT id FROM users WHERE id = $id"
+val b = "SELECT id FROM users WHERE id = ${id}"
+val c = "Selected $n users from the list"
+val d = "SELECT id FROM users WHERE id = ?"