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 PHP sql-injection patterns (#209)

Pranav Arya28e21a310ea9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e563b49..55efdea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- 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.
- 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.
diff --git a/src/rules/code.rs b/src/rules/code.rs
index 6541bd8..4d9a4b5 100644
--- a/src/rules/code.rs
+++ b/src/rules/code.rs
@@ -344,9 +344,9 @@ pub static RULES: &[Rule] = &[
php,
concat!(
r#"["']\s*"#,
- kw!(),
+ kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"']*["']\s*\.\s*\$|"\s*"#,
- kw!(),
+ kw!(".", r"(?:\s|\\[nrt])"),
r#"[^"]*\$[A-Za-z_{]"#
),
None,
@@ -834,4 +834,38 @@ mod tests {
assert!(!sqli("Users.cs", s), "unexpected code.sql-injection: {s}");
}
}
+
+ #[test]
+ fn php_sql_injection_matches_escaped_whitespace() {
+ let f = FileInfo::new("db.php");
+ let sqli = |s| {
+ check(&f, s, false)
+ .iter()
+ .any(|x| x.rule == "code.sql-injection")
+ };
+ for s in [
+ // Interpolation.
+ 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')";"#,
+ // Concatenation.
+ r#"$q = "SELECT id\nFROM users WHERE id = " . $id;"#,
+ r#"$q = "UPDATE users\nSET role = " . $role;"#,
+ r#"$q = "DELETE\nFROM sessions WHERE id = " . $id;"#,
+ ] {
+ 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 = "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 51db72a..6a36289 100644
--- a/tests/fixtures/code/EXPECTED
+++ b/tests/fixtures/code/EXPECTED
@@ -22,6 +22,7 @@ api/handler.go:6 code.ssrf
api/index.php:1 code.sql-injection
api/index.php:11 code.sql-injection
api/index.php:12 code.ssrf
+api/index.php:14 code.sql-injection
api/index.php:2 code.command-injection
api/index.php:3 code.file-inclusion
api/index.php:4 code.unsafe-deserialization
diff --git a/tests/fixtures/code/api/index.php b/tests/fixtures/code/api/index.php
index 89ca6bf..b85a087 100644
--- a/tests/fixtures/code/api/index.php
+++ b/tests/fixtures/code/api/index.php
@@ -11,3 +11,5 @@ $q = "SELECT * FROM t WHERE id = ?";
$insert = "UPDATE users SET password = '$pass_new' WHERE user = 1";
$page = file_get_contents($_GET['url']);
$page = file_get_contents("https://api.example.com/status");
+$q2 = "SELECT id\nFROM users WHERE id = $id";
+$msg = "selected\n$n files from $dir";