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 C# interpolated sql-injection pattern (#208)

Pranav Arya5f43078eccfa
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fbd1981..e563b49 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- Code: `code.sql-injection` now reports a C# interpolated query with an escaped newline, tab or
+ carriage return before the next keyword (`$"SELECT Id\nFROM Users WHERE Id = {id}"`), as the
+ concatenation form already did.
- Config: `config.remote-script` no longer reports a trailing `#` comment that shows a `| sh`
shortcut after a safe command (`curl ... -o install.sh # or: curl ... | sh`), while still
reporting `#` inside quotes or words (`https://example.com/i.sh#v1`) and comments after the pipe.
diff --git a/src/rules/code.rs b/src/rules/code.rs
index b3816bf..6541bd8 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -374,7 +374,7 @@ pub static RULES: &[Rule] = &[
r#""\s*"#,
kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"]*"\s*\+\s*[A-Za-z_]|\$@?"\s*"#,
- kw!(),
+ kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"]*\{"#
),
None,
@@ -814,5 +814,24 @@ mod tests {
);
}
}
+ // The C# interpolated branch, plain and verbatim.
+ 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 = $@"SELECT Id FROM Users WHERE Id = {id}";"#,
+ "q = $@\"SELECT Id\nFROM Users WHERE Id = {id}\";",
+ ] {
+ assert!(sqli("Users.cs", s), "expected code.sql-injection: {s}");
+ }
+ for s in [
+ r#"msg = $"selected\n{n} files";"#,
+ r#"msg = $"selected\n{n} files from {dir}";"#,
+ r#"msg = $"updated\n{n} rows";"#,
+ ] {
+ assert!(!sqli("Users.cs", s), "unexpected code.sql-injection: {s}");
+ }
}
}
diff --git a/tests/fixtures/code/EXPECTED b/tests/fixtures/code/EXPECTED
index 226f478..51db72a 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -8,6 +8,7 @@ api/Users.cs:1 code.sql-injection
api/Users.cs:2 code.sql-injection
api/Users.cs:3 code.unsafe-deserialization
api/Users.cs:4 code.sql-injection
+api/Users.cs:6 code.sql-injection
api/handler.go:1 code.command-injection
api/handler.go:3 code.sql-injection
api/handler.go:4 code.tls-verify-off
diff --git a/tests/fixtures/code/api/Users.cs b/tests/fixtures/code/api/Users.cs
index 0a5ed7f..1784420 100644
--- a/tests/fixtures/code/api/Users.cs
+++ b/tests/fixtures/code/api/Users.cs
@@ -3,3 +3,5 @@ var cmd2 = new SqlCommand($"SELECT * FROM Users WHERE Id = {id}", conn);
var f = new BinaryFormatter();
var cmd3 = new SqlCommand("SELECT Id\nFROM Users WHERE Id = " + id, conn);
var msg = "selected\n" + n + " files from " + dir;
+var cmd4 = new SqlCommand($"SELECT Id\nFROM Users WHERE Id = {id}", conn);
+var msg2 = $"selected\n{n} files from {dir}";